-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCheckIp.hgx
92 lines (77 loc) · 2.92 KB
/
CheckIp.hgx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?xml version="1.0" encoding="utf-16"?>
<ProgramBlock xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<ConditionType>OnTrue</ConditionType>
<Conditions />
<Commands />
<ScriptCondition>// this Setup delegate will be executed once, when program become active
Program.Setup(()=>
{
// Program.AddControlWidget("homegenie/generic/program");
Program.AddInputField("checkip.Path", @"/usr/local/bin/homegenie/log/ip.log", "Path to log file");
Program.AddInputField("checkip.recipients", "", "Email notification recipient(s)");
});
return true;
</ScriptCondition>
<ScriptSource>string ActivityLogPath = Program.InputField("checkip.Path").Value;
string recipients = Program.InputField("checkip.recipients").Value;
string IP = "checkip.IP";
Program.Parameter(IP).Value = "-1";
Action<string>
Log = (string logtext) => {
string text = DateTime.Now.ToLocalTime().ToString("yyyyMMdd HH:mm:ss.fffffff") + " ; " + logtext + "\n";
System.IO.File.AppendAllText(ActivityLogPath, text);
};
while (Program.IsEnabled)
{
try
{
// string webserviceurl = "http://www.trackip.net/ip";
string webserviceurl = "http://checkip.dyndns.com";
var ipweb = Net.WebService(webserviceurl).GetData();
int first = ipweb.IndexOf("Address: ") + 9;
int last = ipweb.LastIndexOf("</body>");
var ipdata = ipweb.Substring(first, last - first);
Program.Notify("IP address", ipdata);
if (ipdata != Program.Parameter(IP).Value && ipdata.Length>6)
{
Log("Old IP " + Program.Parameter(IP).Value.ToString());
Log("New IP " + ipdata);
var subject = "Homegenie ip address is changed";
var messagetext = "The external ip address is changed from '" +
Program.Parameter(IP).Value +
"' to '" + ipdata + "'.";
try
{
Net.SendMessage( recipients, subject, messagetext );
Program.Notify("Ip Information", "Mail send");
Log("email sent");
}
catch
{
Program.Notify("Ip Error!", "Mail error");
Log("email failed");
}
Program.Parameter(IP).Value = ipdata;
}
}
catch //(Exception e)
{
Program.Notify("Ip ERROR!", "Unable to get data from service.");
Log("Unable to get data from service.");
Program.Parameter(IP).Value = "-1" ;
}
Pause(3600); // pause 1 hour before next check
}
Program.GoBackground();</ScriptSource>
<ScriptErrors />
<Domain>HomeAutomation.HomeGenie.Automation</Domain>
<Address>1006</Address>
<Name>CheckIp</Name>
<Description>Get the external Ip address.</Description>
<Group>NEW</Group>
<Features />
<ActivationTime xsi:nil="true" />
<TriggerTime>2016-12-29T01:39:37.353057Z</TriggerTime>
<Type>CSharp</Type>
<IsEnabled>true</IsEnabled>
</ProgramBlock>