-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathUPS_event.hgx
270 lines (225 loc) · 7.75 KB
/
UPS_event.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
<?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>Program.Setup(()=>
{
Program.AddOption("UPS_Event.Path", @"/usr/local/bin/homegenie/log/upsevent.log", "Path to log file","text");
Program.AddOption("UPS_Event.recipients", "", "Email notification recipient(s)","text");
});
return true;</ScriptCondition>
<ScriptSource>string ActivityLogPath = Program.Option("UPS_Event.Path").Value;
string recipients = Program.Option("UPS_Event.recipients").Value;
string messagetext = "";
bool sendemail = false;
Action<string>
Log = (string logtext) => {
string text = DateTime.Now.ToLocalTime().ToString("yyyyMMdd HH:mm:ss.fffffff") + " ; " + logtext + "\n";
System.IO.File.AppendAllText(ActivityLogPath, text);
};
Action<string, string>
WriteToSensor = (string dataType, string dataVal) => {
// dataType = parameter name
// dataVal = parameter value
var module = Modules.InDomain("UPS").WithAddress("UPS").Get();
if (module.Instance == null)
{
Program.AddVirtualModule("UPS", "UPS", "Sensor", "homegenie/generic/sensor");
module = Modules.InDomain("UPS").WithAddress("UPS").Get();
}
if(module.Parameter(dataType).Value != dataVal){
module.Parameter(dataType).Value = dataVal;
Program.RaiseEvent(module, dataType, dataVal, "");
}
};
/*
apcupsd events
killpower - turning off ups
commfailure - comm lost to ups
commok - comm restored to ups
powerout - ups detected power outage
onbattery - ups running on battery
offbattery - ups detected power restored
mainsback - attempts to cancel shutdown
failing - ups battery power exhausted
timeout - ups battery runtime limit reached
loadlimit - ups battery discharge limit reached
runlimit - ups battery percent limit reached
doreboot - system reboot
doshutdown - system shutdown
annoyme - informs user to log off
emergency - emergency shutdown
changeme - ups battery failure (info)
remotedown - remote shutdown call detected
startselftest - ups self test detected
endselftest - ups self test ended
battdetach - ups battery has been disconnected
battattach - ups battery has been reconnected
*/
When.WebServiceCallReceived( "ups.event", (args) =>
{
string[] reqs = ((string)args).Split('/');
string upsevent = reqs[1];
sendemail = false;
Log("UPS event ; " + upsevent);
Program.Notify("UPS event",upsevent);
messagetext = "";
switch (upsevent)
{
case "killpower":
messagetext="killpower - turning off ups";
sendemail = true;
break;
case "commfailure":
messagetext="commfailure - comm lost to ups";
break;
case "commok":
messagetext="commok - comm restored to ups";
break;
case "powerout":
messagetext="powerout - ups detected power outage";
break;
case "onbattery":
messagetext="onbattery - ups running on battery";
sendemail = true;
break;
case "offbattery":
messagetext="offbattery - ups detected power restored";
break;
case "mainsback":
messagetext="mainsback - attempts to cancel shutdown";
break;
case "failing":
messagetext="failing - ups battery power exhausted";
break;
case "timeout":
messagetext="timeout - ups battery runtime limit reached";
break;
case "loadlimit":
messagetext="loadlimit - ups battery discharge limit reached";
break;
case "runlimit":
messagetext="runlimit - ups battery percent limit reached";
break;
case "doreboot":
messagetext="doreboot - system reboot";
break;
case "doshutdown":
messagetext="doshutdown - system shutdown";
break;
case "annoyme":
messagetext="annoyme - informs user to log off";
break;
case "emergency":
messagetext="emergency - emergency shutdown";
break;
case "changeme":
messagetext="changeme - ups battery failure (info)";
sendemail = true;
break;
case "remotedown":
messagetext="remotedown - remote shutdown call detected";
break;
case "startselftest":
messagetext="startselftest - ups self test detected";
break;
case "endselftest":
messagetext="endselftest - ups self test ended";
break;
case "battdetach":
messagetext="battdetach - ups battery has been disconnected";
break;
case "battattach":
messagetext="battattach - ups battery has been reconnected";
break;
default:
messagetext="unknown event";
break;
}
if (recipients != "" && sendemail)
{
Net.SendMessage( recipients, "HG UPS Event (" + upsevent + ")", "A UPS event has been detected at " + DateTime.Now.ToString("HH:mm:ss") + ". Details of the error are as follows:\n\n" + messagetext);
}
return null;
});
while (Program.IsEnabled)
{
var proc = new System.Diagnostics.Process
{
StartInfo = new System.Diagnostics.ProcessStartInfo
{
FileName = "apcaccess",
Arguments = "status",
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
}
};
proc.Start();
string status = "";
while (!proc.StandardOutput.EndOfStream) {
string line = proc.StandardOutput.ReadLine();
string[] output = ((string)line).Split(':');
switch (output[0].Trim()) {
case "STATUS":
status = output[1].Trim();
//Program.Notify("USP Status.Level", status);
/*
if (output[1].Trim() == "ONLINE") {
WriteToSensor("Status.Level", 1);
} else {
WriteToSensor("Status.Level", 0);
}
*/
//Program.RaiseEvent("UPS", "Sensor.Status", output[1].Trim(), "update UPS status");
WriteToSensor("Sensor.Status", output[1].Trim());
break;
case "BCHARGE":
//Status.Battery
string b_tmp = output[1].Trim();
string[] b_tmp1 = b_tmp.Split(' ');
var bcharge = Convert.ToDouble(b_tmp1[0]);
//Program.RaiseEvent("UPS", "Status.Battery", bcharge, "update UPS battery level");
WriteToSensor("Status.Battery", bcharge.ToString());
break;
case "TIMELEFT":
//Program.RaiseEvent("UPS", "Sensor.TimeLeft", output[1].Trim(), "update UPS time left");
WriteToSensor("Sensor.TimeLeft", output[1].Trim());
/*
string t_tmp = output[1].Trim();
string[] t_tmp1 = t_tmp.Split(' ');
var timeleft = Convert.ToDouble(t_tmp1[0]);
WriteToSensor("Sensor.TimeLeft", timeleft);
*/
break;
}
}
//Program.Notify(status,"BCHARGE=" + bcharge.ToString() + "\nTIMELEFT=" + timeleft.ToString() + "\nMBATTCHG=" + mbattchg.ToString());
Program.RaiseEvent("Program.UiRefresh", "Data updated", "");
Pause(30);
}
When.SystemStarted( () =>
{
Net.SendMessage( recipients, "HomeGenie started", "notification");
return false;
});
When.SystemStopping( () =>
{
Net.SendMessage( recipients, "HomeGenie shutting down", "notification");
// returning true will route this event to other listeners
return true;
});
Program.GoBackground();</ScriptSource>
<ScriptErrors />
<Domain>HomeAutomation.HomeGenie.Automation</Domain>
<Address>1017</Address>
<Name>UPS event</Name>
<Description>apcupsd event handler</Description>
<Group>NEW</Group>
<Features />
<ActivationTime>2016-12-29T20:10:31.139263Z</ActivationTime>
<TriggerTime>2016-12-29T20:10:33.238656Z</TriggerTime>
<Type>CSharp</Type>
<IsEnabled>true</IsEnabled>
</ProgramBlock>