-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathJ_SmartSwitch1.js
199 lines (153 loc) · 5.23 KB
/
J_SmartSwitch1.js
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
var SMART_SWITCH_SID = 'urn:hugheaves-com:serviceId:SmartSwitch1';
var SMART_SWITCH_CONTROLLER_SID = 'urn:hugheaves-com:serviceId:SmartSwitchController1';
var SECURITY_SENSOR_SID = 'urn:micasaverde-com:serviceId:SecuritySensor1';
var SWITCH_SID = 'urn:upnp-org:serviceId:SwitchPower1';
var ss_settingSid;
var ss_settingVar;
var ss_deviceId = 0;
function ss_showSettings(deviceId) {
var panelHtml = '';
ss_deviceId = deviceId;
ss_settingSid = SMART_SWITCH_SID;
ss_settingVar = 'SwitchIds';
panelHtml += '<p>To control switches using this plug-in, you must '
+ 'first add the switches to the list below. '
+ 'When you save your settings, the plug-in will then '
+ 'create a new "smart switch" device for each switch in the '
+ 'list. After the new smart switches have been created, '
+ 'go to the settings page for each '
+ 'smart switch to add the sensors that control '
+ 'that switch.</p>';
panelHtml += '<p>To add a switch, select a device from the drop-down '
+ 'below and and click the "Add" button. To remove an existing '
+ 'switch, click the "Remove" button next to the device. ';
panelHtml += 'To save your changes (and allow the plug-in to create '
+ 'or remove the smart switches), remember to click the "Save" button '
+ 'at the top of the screen after closing this dialog.</p>';
panelHtml += ss_deviceSelectDropdown(SWITCH_SID, SWITCH_SID);
panelHtml += ss_devicesTable();
set_panel_html(panelHtml);
}
/*
* Generic (Shared) functions
*/
function ss_findDevices(sid1, sid2) {
var foundDevices = [];
var devices = jsonp.ud.devices;
for ( var i = 0; i < devices.length; i++) {
var device = devices[i];
var found = false;
for ( var j = 0; j < device.states.length; j++) {
var state = device.states[j];
if (state.service == sid1 || state.service == sid2) {
found = true;
}
}
if (found) {
foundDevices.push(device);
}
}
return foundDevices;
}
function ss_removeUsedDevices(devices) {
var deviceIds = ss_getDeviceIdsSetting();
var newDevices = [];
for ( var i = 0; i < devices.length; ++i) {
var device = devices[i];
var index = deviceIds.indexOf(device.id);
if (index == -1) {
newDevices.push(device);
}
}
return newDevices;
}
function ss_getDeviceIdsSetting() {
var deviceIdsJSON = get_device_state(ss_deviceId, ss_settingSid,
ss_settingVar, 0);
deviceIdsJSON = deviceIdsJSON.replace(/'/g, '\"');
if (deviceIdsJSON == "") {
return ([]);
} else {
return (JSON.parse(deviceIdsJSON));
}
}
function ss_setDeviceIdsSetting(deviceIds) {
var deviceIdsJSON = JSON.stringify(deviceIds);
deviceIdsJSON = deviceIdsJSON.replace(/"/g, '\'');
set_device_state(ss_deviceId, ss_settingSid, ss_settingVar, deviceIdsJSON, 0);
}
function ss_deviceSelectDropdown(sid1, sid2) {
var panelHtml = '';
var devices = ss_findDevices(sid1, sid2);
devices = ss_removeUsedDevices(devices);
panelHtml += '<select id="deviceSelect">'
panelHtml += '<option value="0">-- Select a device --</option>'
for ( var i = 0; i < devices.length; ++i) {
var device = devices[i];
panelHtml += '<option value="' + device.id + '">' + device.name + ' (#'
+ device.id + ')</option>';
}
panelHtml += '</select>';
panelHtml += '<input type="button" value="Add" onclick="ss_addSelectedDevice()" />';
panelHtml += '<p/>';
return panelHtml;
}
function ss_devicesTable() {
var panelHtml = '';
panelHtml += ss_tableHeader();
var deviceIds = ss_getDeviceIdsSetting();
for ( var i = 0; i < deviceIds.length; ++i) {
panelHtml += ss_tableRow(jsonp.get_device_by_id(deviceIds[i]));
}
panelHtml += ss_tableFooter();
return panelHtml;
}
function ss_tableHeader() {
var panelHtml = '';
panelHtml += '<table style="border-collapse:collapse">';
panelHtml += '<tr>';
panelHtml += '<th style="width: 20%">Device Id</th>';
panelHtml += '<th style="width: 20%">Room</th>';
panelHtml += '<th style="width: 50%">Name</th>';
panelHtml += '<th style="width: 20%">Action</th>';
panelHtml += '</tr>';
return panelHtml;
}
function ss_tableRow(device) {
var panelHtml = '';
var roomName = 'none';
if (device.room > 0) {
roomName = jsonp.get_room_by_id(device.room).name;
}
panelHtml += '<tr style="border: 1px solid black">';
panelHtml += '<td style="border: 1px solid black">' + device.id + '</td>';
panelHtml += '<td style="border: 1px solid black">' + roomName + '</td>';
panelHtml += '<td style="border: 1px solid black">' + device.name + '</td>';
panelHtml += '<td style="border: 1px solid black"><input type="button" value="Remove" '
+ 'onclick="ss_removeDevice(\'' + device.id + '\')" /></td>';
panelHtml += '</tr>';
return panelHtml;
}
function ss_tableFooter() {
return '</table>';
}
function ss_removeDevice(deviceId) {
var deviceIds = ss_getDeviceIdsSetting();
var index = deviceIds.indexOf(deviceId);
if (index > -1) {
deviceIds.splice(index, 1);
ss_setDeviceIdsSetting(deviceIds);
ss_showSettings(ss_deviceId);
}
}
function ss_addSelectedDevice() {
var element = document.getElementById("deviceSelect");
var deviceId = element.options[element.selectedIndex].value;
var deviceIds = ss_getDeviceIdsSetting();
var index = deviceIds.indexOf(deviceId);
if (index == -1 && deviceId > 0) {
deviceIds.push(deviceId);
ss_setDeviceIdsSetting(deviceIds);
ss_showSettings(ss_deviceId);
}
}