This repository was archived by the owner on Oct 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHSPI.cs
703 lines (590 loc) · 24.7 KB
/
HSPI.cs
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;
using System.Timers;
using System.Web;
using System.Web.Script.Serialization;
using HomeSeerAPI;
using HSPI_LiftMasterMyQ.DataContainers;
using HSPI_LiftMasterMyQ.Enums;
using Scheduler;
using Scheduler.Classes;
namespace HSPI_LiftMasterMyQ
{
// ReSharper disable once InconsistentNaming
public class HSPI : HspiBase
{
public const string PLUGIN_NAME = "LiftMaster MyQ";
private MyQClient myqClient;
private Timer pollTimer;
private readonly Dictionary<string, int> serialToRef;
private readonly Dictionary<int, int> refToMyqId;
private readonly Dictionary<int, bool> refLastOnlineStatus;
private bool currentlySyncingDevices = false;
private const string DEFAULT_POLL_INTERVAL = "10000";
public HSPI() {
Name = "LiftMaster MyQ";
PluginIsFree = true;
PluginActionCount = 1;
serialToRef = new Dictionary<string, int>();
refToMyqId = new Dictionary<int, int>();
refLastOnlineStatus = new Dictionary<int, bool>();
}
public override string InitIO(string port) {
Program.WriteLog(LogType.Verbose, "InitIO");
hs.RegisterPage("LiftMasterMyQSettings", Name, InstanceFriendlyName());
var configLink = new HomeSeerAPI.WebPageDesc {
plugInName = Name,
link = "LiftMasterMyQSettings",
linktext = "Settings",
order = 1,
page_title = "LiftMaster MyQ Settings",
plugInInstance = InstanceFriendlyName()
};
callbacks.RegisterConfigLink(configLink);
callbacks.RegisterLink(configLink);
myqClient = new MyQClient(hs.GetINISetting("Options", "myq_use_craftsman", "0", IniFilename) == "1" ? MyQMake.Craftsman : MyQMake.LiftMaster);
var myqUsername = hs.GetINISetting("Authentication", "myq_username", "", IniFilename);
var myqPassword = getMyQPassword(false);
if (myqUsername.Length > 0 && myqPassword.Length > 0) {
myqClient.Login(myqUsername, myqPassword).ContinueWith(t => {
if (t.Result == "") {
// no error occurred
syncDevices();
}
});
}
pollTimer = new Timer(double.Parse(hs.GetINISetting("Options", "myq_poll_frequency", DEFAULT_POLL_INTERVAL, IniFilename)));
pollTimer.Elapsed += (Object source, ElapsedEventArgs e) => { syncDevices(); };
pollTimer.AutoReset = false;
// don't enable just yet
Timer sanityCheck = new Timer(60000) {AutoReset = true};
sanityCheck.Elapsed += (object src, ElapsedEventArgs a) => {
long lastUpdatedTime = Helpers.GetUnixTimeSeconds() - myqClient.DevicesLastUpdated;
if (lastUpdatedTime > 60) {
// Devices last updated 60 seconds ago, so something broke.
Program.WriteLog(LogType.Warn, "MyQ devices last updated " + lastUpdatedTime + " seconds ago; running poll now");
syncDevices();
}
};
sanityCheck.Start();
return "";
}
public override void SetIOMulti(List<CAPI.CAPIControl> colSend) {
foreach (var upd in colSend) {
Program.WriteLog(LogType.Debug, "Ref " + upd.Ref + " set to " + upd.ControlValue);
int myqId;
if (!refToMyqId.TryGetValue(upd.Ref, out myqId)) {
Program.WriteLog(LogType.Error, "No MyQ ID for ref " + upd.Ref + "!!");
continue;
}
myqClient.moveDoor(myqId, (MyQDoorState) upd.ControlValue).ContinueWith(t => {
Program.WriteLog(LogType.Debug, "Move door command completed" + (t.Result.Length > 0 ? " with error: " + t.Result : ""));
Timer timer = new Timer(1000);
timer.AutoReset = false;
timer.Elapsed += (Object source, ElapsedEventArgs e) => { syncDevices(); };
timer.Start();
});
}
}
public override string get_ActionName(int actionNumber) {
switch (actionNumber) {
case 1:
return Name + " Actions";
default:
return "UNKNOWN ACTION";
}
}
public override string ActionBuildUI(string unique, IPlugInAPI.strTrigActInfo actInfo) {
if (actInfo.TANumber != 1) {
return "Bad action number " + actInfo.TANumber + "," + actInfo.SubTANumber;
}
StringBuilder builder = new StringBuilder();
SubAction selectedSubAction = (SubAction) actInfo.SubTANumber;
// Sub-action dropdown
clsJQuery.jqDropList actionSelector = new clsJQuery.jqDropList("SubAction" + unique, "events", true);
foreach (SubAction subAction in Enum.GetValues(typeof(SubAction))) {
string actName = Helpers.AddSpacesToCamelCase(Enum.GetName(typeof(SubAction), subAction));
if (actName == "Invalid") {
actName = "(Choose A " + Name + " Action)";
}
actionSelector.AddItem(actName, ((int) subAction).ToString(), selectedSubAction == subAction);
}
builder.Append(actionSelector.Build());
if (selectedSubAction == SubAction.SetPollingInterval) {
SetPollTimeData eventData = SetPollTimeData.Unserialize(actInfo.DataIn);
if (ActionAdvancedMode) {
clsJQuery.jqTextBox textBox = new clsJQuery.jqTextBox(
"PollingInterval_txt" + unique,
"number",
eventData.PollIntervalMilliseconds == 0 ? "" : eventData.PollIntervalMilliseconds.ToString(),
"events",
30,
true
);
textBox.dialogCaption = "Polling Interval";
textBox.promptText = "Enter the new polling interval in milliseconds.";
builder.Append("<br />Polling Interval (ms): ");
builder.Append(textBox.Build());
} else {
clsJQuery.jqTimeSpanPicker timePicker =
new clsJQuery.jqTimeSpanPicker("PollingInterval" + unique, "Polling Interval:", "events", true);
timePicker.showDays = false;
timePicker.defaultTimeSpan = TimeSpan.FromMilliseconds(eventData.PollIntervalMilliseconds);
builder.Append(timePicker.Build());
}
}
return builder.ToString();
}
public override IPlugInAPI.strMultiReturn ActionProcessPostUI(NameValueCollection postData,
IPlugInAPI.strTrigActInfo actInfo) {
if (actInfo.TANumber != 1) {
throw new Exception("Unknown action number " + actInfo.TANumber);
}
IPlugInAPI.strMultiReturn output = new IPlugInAPI.strMultiReturn();
output.TrigActInfo.TANumber = actInfo.TANumber;
output.TrigActInfo.SubTANumber = actInfo.SubTANumber;
output.DataOut = actInfo.DataIn;
SubAction selectedSubAction = (SubAction) actInfo.SubTANumber;
foreach (string key in postData.AllKeys) {
string[] parts = key.Split('_');
if (parts.Length > 1) {
postData.Add(parts[0], postData.Get(key));
}
}
// Are we changing the sub action?
SubAction newSubAction = (SubAction) int.Parse(postData.Get("SubAction"));
if (newSubAction != selectedSubAction) {
output.TrigActInfo.SubTANumber = (int) newSubAction;
// We don't need to clear the action data since we only have one action data class right now.
// If we add another one, then we might need to.
return output;
}
switch ((SubAction) actInfo.SubTANumber) {
case SubAction.SetPollingInterval:
SetPollTimeData actData = SetPollTimeData.Unserialize(actInfo.DataIn);
string temp;
if ((temp = postData.Get("PollingInterval")) != null) {
uint? newPollInterval = Helpers.DecodeTimeSpanToMilliseconds(temp);
if (newPollInterval != null && newPollInterval >= 1000) {
actData.PollIntervalMilliseconds = (uint) newPollInterval;
}
}
output.DataOut = actData.Serialize();
break;
}
return output;
}
public override bool ActionConfigured(IPlugInAPI.strTrigActInfo actInfo) {
switch ((SubAction) actInfo.SubTANumber) {
case SubAction.PollNow:
case SubAction.ResetPollingIntervalToConfiguredValue:
return true;
case SubAction.SetPollingInterval:
SetPollTimeData actData = SetPollTimeData.Unserialize(actInfo.DataIn);
return actData.PollIntervalMilliseconds >= 1000;
default:
return false;
}
}
public override string ActionFormatUI(IPlugInAPI.strTrigActInfo actInfo) {
if (actInfo.TANumber != 1) {
return "Unknown action number " + actInfo.TANumber;
}
StringBuilder builder = new StringBuilder();
switch ((SubAction) actInfo.SubTANumber) {
case SubAction.PollNow:
builder.Append("<span class=\"event_Txt_Selection\">Poll MyQ device status</span> immediately.");
break;
case SubAction.ResetPollingIntervalToConfiguredValue:
builder.Append("<span class=\"event_Txt_Selection\">Reset MyQ poll interval</span> to the value configured on the plugin settings page (");
builder.Append("<span class=\"event_Txt_Option\">");
builder.Append(double.Parse(hs.GetINISetting("Options", "myq_poll_frequency", DEFAULT_POLL_INTERVAL, IniFilename)) / 1000.0);
builder.Append(" seconds</span>).");
break;
case SubAction.SetPollingInterval:
SetPollTimeData actData = SetPollTimeData.Unserialize(actInfo.DataIn);
builder.Append(
"<span class=\"event_Txt_Selection\">Set MyQ poll interval</span> to <span class=\"event_Txt_Option\">");
builder.Append(actData.PollIntervalMilliseconds / 1000.0);
builder.Append(" seconds</span>.");
break;
default:
return "Unknown sub-action " + actInfo.SubTANumber;
}
return builder.ToString();
}
public override bool HandleAction(IPlugInAPI.strTrigActInfo actInfo) {
if (actInfo.TANumber != 1) {
Program.WriteLog(LogType.Error,
"Bad action number " + actInfo.TANumber + " for event " + actInfo.evRef);
return false;
}
switch ((SubAction) actInfo.SubTANumber) {
case SubAction.PollNow:
syncDevices();
return true;
case SubAction.ResetPollingIntervalToConfiguredValue:
pollTimer.Interval = double.Parse(hs.GetINISetting("Options", "myq_poll_frequency", DEFAULT_POLL_INTERVAL, IniFilename));
Program.WriteLog(LogType.Info, "Resetting poll interval to " + pollTimer.Interval);
return true;
case SubAction.SetPollingInterval:
SetPollTimeData actData = SetPollTimeData.Unserialize(actInfo.DataIn);
Program.WriteLog(LogType.Info, "Setting poll interval to " + actData.PollIntervalMilliseconds);
pollTimer.Interval = actData.PollIntervalMilliseconds;
return true;
default:
Program.WriteLog(LogType.Error, "Bad sub-action number " + actInfo.SubTANumber + " for event " + actInfo.evRef);
return false;
}
}
public override string GetPagePlugin(string pageName, string user, int userRights, string queryString) {
Program.WriteLog(LogType.Verbose, "Requested page name " + pageName + " by user " + user + " with rights " + userRights);
switch (pageName) {
case "LiftMasterMyQSettings":
return buildSettingsPage(user, userRights, queryString);
}
return "";
}
private string buildSettingsPage(string user, int userRights, string queryString,
string messageBox = null, string messageBoxClass = null) {
var sb = new StringBuilder();
sb.Append(PageBuilderAndMenu.clsPageBuilder.DivStart("myq_settings", ""));
if (messageBox != null) {
sb.Append("<div" + (messageBoxClass != null ? " class=\"" + messageBoxClass + "\"" : "") + ">");
sb.Append(messageBox);
sb.Append("</div>");
}
sb.Append(PageBuilderAndMenu.clsPageBuilder.FormStart("myq_settings_form",
"myq_settings_form", "post"));
sb.Append(@"
<style>
#myq_settings_form label {
font-weight: bold;
text-align: right;
width: 150px;
margin-right: 10px;
display: inline-block;
}
#myq_settings_form button {
margin-left: 163px;
}
.myq_message_box {
padding: 10px;
border-radius: 10px;
display: inline-block;
margin-bottom: 10px;
}
.myq_success_message {
background-color: rgba(50, 255, 50, 0.8);
}
.myq_error_message {
color: white;
background-color: rgba(255, 50, 50, 0.8);
}
</style>
<div>
<label for=""myq_username"">MyQ Email</label>
<input type=""email"" name=""myq_username"" id=""myq_username"" />
</div>
<div>
<label for=""myq_password"">MyQ Password</label>
<input type=""password"" name=""myq_password"" id=""myq_password"" />
</div>
<div>
<label for=""myq_poll_frequency"">Poll Frequency (ms)</label>
<input type=""number"" name=""myq_poll_frequency"" id=""myq_poll_frequency"" step=""1"" min=""5000"" />
</div>
<div>
<label for=""myq_use_craftsman"">Brand</label>
<select name=""myq_use_craftsman"" id=""myq_use_craftsman"">
<option value=""0"">LiftMaster / Chamberlain</option>
<option value=""1"">Craftsman</option>
</select>
</div>
<button type=""submit"">Submit</button>
");
sb.Append(PageBuilderAndMenu.clsPageBuilder.FormEnd());
var savedSettings = new Dictionary<string, string> {
{"myq_username", hs.GetINISetting("Authentication", "myq_username", "", IniFilename)},
{"myq_password", getMyQPassword(true)},
{"myq_poll_frequency", hs.GetINISetting("Options", "myq_poll_frequency", DEFAULT_POLL_INTERVAL, IniFilename)},
{"myq_use_craftsman", hs.GetINISetting("Options", "myq_use_craftsman", "0", IniFilename)},
};
sb.Append("<script>var myqSavedSettings = ");
sb.Append(new JavaScriptSerializer().Serialize(savedSettings));
sb.Append(";");
sb.Append(@"
for (var i in myqSavedSettings) {
if (myqSavedSettings.hasOwnProperty(i)) {
document.getElementById(i).value = myqSavedSettings[i];
}
}
</script>");
var builder = new PageBuilderAndMenu.clsPageBuilder("LiftMasterMyQSettings");
builder.reset();
builder.AddHeader(hs.GetPageHeader("LiftMasterMyQSettings", "LiftMaster MyQ Settings", "", "", false, true));
builder.AddBody(sb.ToString());
builder.AddFooter(hs.GetPageFooter());
builder.suppressDefaultFooter = true;
return builder.BuildPage();
}
public override string PostBackProc(string pageName, string data, string user, int userRights) {
Program.WriteLog(LogType.Verbose, "PostBackProc for page " + pageName + " with data " + data + " by user " + user + " with rights " + userRights);
switch (pageName) {
case "LiftMasterMyQSettings":
if ((userRights & 2) != 2) {
// User is not an admin
return buildSettingsPage(user, userRights, "",
"Access denied: You are not an administrative user", "myq_message_box myq_error_message");
}
var authData = new string[] {
"myq_username",
"myq_password"
};
var qs = HttpUtility.ParseQueryString(data);
var authCredsChanged = false;
foreach (var key in authData) {
var oldValue = hs.GetINISetting("Authentication", key, "", IniFilename);
var newValue = qs.Get(key);
if (key == "myq_username") {
newValue = newValue.Trim();
}
if (newValue != "*****" && oldValue != newValue) {
authCredsChanged = true;
}
}
if (authCredsChanged) {
var username = qs.Get("myq_username").Trim();
var password = qs.Get("myq_password");
hs.SaveINISetting("Authentication", "myq_username", username.Trim(), IniFilename);
if (password != "*****") {
// This doesn't provide any actual security, but at least the password isn't in
// plaintext on the disk. Base64 is juuuuust barely not plaintext, but what're ya
// gonna do?
var encoded = System.Convert.ToBase64String(Encoding.UTF8.GetBytes((string) password));
hs.SaveINISetting("Authentication", "myq_password", encoded, IniFilename);
}
}
var pollFrequency = qs.Get("myq_poll_frequency");
int n;
if (pollFrequency != null && int.TryParse(pollFrequency, out n) && n >= 5000) {
hs.SaveINISetting("Options", "myq_poll_frequency", pollFrequency, IniFilename);
pollTimer.Interval = n;
}
var prevUseCraftsman = hs.GetINISetting("Options", "myq_use_craftsman", "0", IniFilename) == "1";
var newUseCraftsman = prevUseCraftsman;
string useCraftsman;
if ((useCraftsman = qs.Get("myq_use_craftsman")) != null) {
newUseCraftsman = useCraftsman == "1";
hs.SaveINISetting("Options", "myq_use_craftsman", useCraftsman, IniFilename);
if (newUseCraftsman != prevUseCraftsman) {
myqClient = new MyQClient(useCraftsman == "1" ? MyQMake.Craftsman : MyQMake.LiftMaster);
}
}
if (authCredsChanged || newUseCraftsman != prevUseCraftsman) {
var authTask = myqClient.Login(hs.GetINISetting("Authentication", "myq_username", "", IniFilename),
getMyQPassword(false), true);
authTask.Wait();
if (authTask.Result.Length > 0) {
return buildSettingsPage(user, userRights, "", authTask.Result,
"myq_message_box myq_error_message");
} else {
syncDevices();
return buildSettingsPage(user, userRights, "",
"Settings have been saved successfully. Authentication success.",
"myq_message_box myq_success_message");
}
} else {
return buildSettingsPage(user, userRights, "", "Settings have been saved successfully.",
"myq_message_box myq_success_message");
}
}
return "";
}
public override IPlugInAPI.strInterfaceStatus InterfaceStatus() {
// Do we have a password set?
if (hs.GetINISetting("Authentication", "myq_username", "", IniFilename).Length == 0 ||
getMyQPassword().Length == 0) {
return new IPlugInAPI.strInterfaceStatus {
intStatus = IPlugInAPI.enumInterfaceStatus.CRITICAL,
sStatus = "Missing MyQ credentials"
};
}
switch (myqClient.ClientStatus) {
case MyQClient.STATUS_OK:
return new IPlugInAPI.strInterfaceStatus {
intStatus = IPlugInAPI.enumInterfaceStatus.OK
};
case MyQClient.STATUS_MYQ_DOWN:
return new IPlugInAPI.strInterfaceStatus {
intStatus = IPlugInAPI.enumInterfaceStatus.WARNING,
sStatus = myqClient.ClientStatusString.Length == 0
? "MyQ appears to be down"
: myqClient.ClientStatusString
};
case MyQClient.STATUS_UNAUTHORIZED:
return new IPlugInAPI.strInterfaceStatus {
intStatus = IPlugInAPI.enumInterfaceStatus.CRITICAL,
sStatus = myqClient.ClientStatusString.Length == 0
? "MyQ credentials are incorrect"
: myqClient.ClientStatusString
};
case MyQClient.STATUS_THROTTLED:
return new IPlugInAPI.strInterfaceStatus {
intStatus = IPlugInAPI.enumInterfaceStatus.CRITICAL,
sStatus = myqClient.ClientStatusString.Length == 0
? "Login attempts throttled"
: myqClient.ClientStatusString
};
default:
return new IPlugInAPI.strInterfaceStatus {
intStatus = IPlugInAPI.enumInterfaceStatus.INFO,
sStatus = "Unknown status " + myqClient.ClientStatus
};
}
}
private async void syncDevices() {
if (currentlySyncingDevices) {
Program.WriteLog(LogType.Debug, "Suppressing MyQ sync because we're currently syncing devices");
}
currentlySyncingDevices = true;
Program.WriteLog(LogType.Verbose, "Syncing MyQ devices");
var errorMsg = await myqClient.getDevices();
currentlySyncingDevices = false;
pollTimer.Stop();
pollTimer.Start(); // enqueue the next poll
if (errorMsg != "") {
// Something went wrong!
Program.WriteLog(LogType.Error, "Cannot retrieve device list from MyQ: " + errorMsg);
return;
}
Program.WriteLog(LogType.Verbose, "Got list of " + myqClient.Devices.Count + " devices");
foreach (MyQDevice dev in myqClient.Devices) {
int devRef = 0;
if (!serialToRef.TryGetValue(dev.DeviceSerialNumber, out devRef)) {
// We need to look it up in HS3, and maybe create the device
devRef = hs.DeviceExistsAddress(dev.DeviceSerialNumber, false);
if (devRef == -1) {
devRef = 0;
} else {
Program.WriteLog(LogType.Debug, "Found existing device for GDO " + dev.DeviceSerialNumber + " with ref " + devRef);
serialToRef.Add(dev.DeviceSerialNumber, devRef);
}
if (devRef == 0) {
Program.WriteLog(LogType.Debug, "Creating new device in HS3 for GDO serial " + dev.DeviceSerialNumber);
// Didn't find an existing device; create one
devRef = hs.NewDeviceRef(dev.DeviceTypeName);
var hsDev = (DeviceClass) hs.GetDeviceByRef(devRef);
hsDev.set_Address(hs, dev.DeviceSerialNumber);
hsDev.set_Interface(hs, Name);
hsDev.set_InterfaceInstance(hs, InstanceFriendlyName());
hsDev.set_Device_Type_String(hs, "LiftMaster MyQ Garage Door Opener");
hsDev.set_DeviceType_Set(hs, new DeviceTypeInfo_m.DeviceTypeInfo {
Device_API = DeviceTypeInfo_m.DeviceTypeInfo.eDeviceAPI.Plug_In
});
// Create buttons
VSVGPairs.VSPair openBtn = new VSVGPairs.VSPair(ePairStatusControl.Both);
openBtn.PairType = VSVGPairs.VSVGPairType.SingleValue;
openBtn.Render = HomeSeerAPI.Enums.CAPIControlType.Button;
openBtn.Status = "Open";
openBtn.Value = (int) MyQDoorState.Open;
openBtn.ControlUse = ePairControlUse._DoorUnLock;
VSVGPairs.VSPair closeBtn = new VSVGPairs.VSPair(ePairStatusControl.Control);
closeBtn.PairType = VSVGPairs.VSVGPairType.SingleValue;
closeBtn.Render = HomeSeerAPI.Enums.CAPIControlType.Button;
closeBtn.Status = "Close";
closeBtn.Value = (int) MyQDoorState.Closed;
closeBtn.ControlUse = ePairControlUse._DoorLock;
VSVGPairs.VSPair closedStatus = new VSVGPairs.VSPair(ePairStatusControl.Status);
closedStatus.PairType = VSVGPairs.VSVGPairType.SingleValue;
closedStatus.Status = "Closed";
closedStatus.Value = (int) MyQDoorState.Closed;
VSVGPairs.VSPair openingStatus = new VSVGPairs.VSPair(ePairStatusControl.Status);
openingStatus.PairType = VSVGPairs.VSVGPairType.SingleValue;
openingStatus.Status = "Opening";
openingStatus.Value = (int) MyQDoorState.GoingUp;
VSVGPairs.VSPair closingStatus = new VSVGPairs.VSPair(ePairStatusControl.Status);
closingStatus.PairType = VSVGPairs.VSVGPairType.SingleValue;
closingStatus.Status = "Closing";
closingStatus.Value = (int) MyQDoorState.GoingDown;
VSVGPairs.VSPair stoppedStatus = new VSVGPairs.VSPair(ePairStatusControl.Status);
stoppedStatus.PairType = VSVGPairs.VSVGPairType.SingleValue;
stoppedStatus.Status = "Stopped";
stoppedStatus.Value = (int) MyQDoorState.Stopped;
VSVGPairs.VSPair notClosedStatus = new VSVGPairs.VSPair(ePairStatusControl.Status);
notClosedStatus.PairType = VSVGPairs.VSVGPairType.SingleValue;
notClosedStatus.Status = "Not Closed";
notClosedStatus.Value = (int) MyQDoorState.NotClosed;
hs.DeviceVSP_AddPair(devRef, openBtn);
hs.DeviceVSP_AddPair(devRef, closeBtn);
hs.DeviceVSP_AddPair(devRef, closedStatus);
hs.DeviceVSP_AddPair(devRef, openingStatus);
hs.DeviceVSP_AddPair(devRef, closingStatus);
hs.DeviceVSP_AddPair(devRef, stoppedStatus);
hs.DeviceVSP_AddPair(devRef, notClosedStatus);
// Status images
foreach (var state in (MyQDoorState[]) Enum.GetValues(typeof(MyQDoorState))) {
hs.DeviceVGP_AddPair(devRef, new VSVGPairs.VGPair {
PairType = VSVGPairs.VSVGPairType.SingleValue,
Set_Value = (int) state,
Graphic = MyQDevice.GetDeviceStatusImage(state)
});
}
hsDev.MISC_Set(hs, HomeSeerAPI.Enums.dvMISC.SHOW_VALUES);
hsDev.MISC_Set(hs, HomeSeerAPI.Enums.dvMISC.AUTO_VOICE_COMMAND);
}
}
if (devRef == 0) {
Program.WriteLog(LogType.Warn, "Somehow we still ended up with devRef == 0 for door " + dev.DeviceSerialNumber);
continue;
}
int devIdTry;
if (!refToMyqId.TryGetValue(devRef, out devIdTry) || devIdTry != dev.DeviceId) {
if (devIdTry != dev.DeviceId) {
refToMyqId.Remove(devRef);
}
refToMyqId.Add(devRef, dev.DeviceId);
}
if (hs.DeviceValue(devRef) != (int) dev.DoorState) {
hs.SetDeviceValueByRef(devRef, (int) dev.DoorState, true);
}
bool deviceLastSeenOnline;
if (!refLastOnlineStatus.TryGetValue(devRef, out deviceLastSeenOnline)) {
// No last known online status, so set the "last known status" to the inverse of what it is now
// That way we update HS3
deviceLastSeenOnline = !dev.IsOnline;
}
if (!dev.IsOnline && deviceLastSeenOnline) {
var hsDevice = (DeviceClass) hs.GetDeviceByRef(devRef);
Program.WriteLog(LogType.Warn, "Device ref " + devRef + " (MyQ ID " + dev.DeviceId + ") is offline");
hsDevice.set_Attention(hs, "The device is offline. Please check the power and network connections.");
}
else if (dev.IsOnline && !deviceLastSeenOnline) {
// It's online
var hsDevice = (DeviceClass) hs.GetDeviceByRef(devRef);
Program.WriteLog(LogType.Info, "Device ref " + devRef + " (MyQ ID " + dev.DeviceId + ") is now online");
hsDevice.set_Attention(hs, null);
}
refLastOnlineStatus[devRef] = dev.IsOnline;
}
}
/// <summary>
/// Get the saved MyQ password from INI
/// </summary>
/// <param name="censor">If true, only return "*****" if a password is saved.</param>
/// <returns>string</returns>
private string getMyQPassword(bool censor = true) {
var password = hs.GetINISetting("Authentication", "myq_password", "", IniFilename);
if (password.Length == 0) {
return password;
} else if (censor) {
return "*****";
} else {
var decoded = Encoding.UTF8.GetString(System.Convert.FromBase64String(password));
return decoded;
}
}
}
}