-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathindex.js
241 lines (211 loc) · 8.72 KB
/
index.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
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
/*
See README.md
*/
var Service, Characteristic, types;
var request = require('request');
// Handle registration with homebridge
module.exports = function(homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
types = homebridge.hapLegacyTypes;
homebridge.registerPlatform("homebridge-wireless-sensor-tag", "wireless-sensor-tag", WirelessTagPlatform);
}
// Platform object for the wireless tags. Represents the wireless tag manager
function WirelessTagPlatform(log,config) {
this.username = config.username;
this.password = config.password;
this.queryFrequency = config.queryFrequency;
this.log = log;
this.tagMap = {};
this.tagList = [];
this.ignoreList = (config.ignoreList == undefined) ? [] : config.ignoreList;
}
WirelessTagPlatform.prototype.shouldIgnore = function(deviceData) {
if(deviceData != undefined && deviceData.name != undefined) {
if(this.ignoreList.indexOf(deviceData.name) > -1) {
return true;
}
}
return false;
}
// Forms a valid request to authenticate against the wireless tag manager. Calls handleResult with a boolean indicating
// success or failure.
WirelessTagPlatform.prototype.sendAuthentication = function(handleResult) {
request({
method: 'POST',
uri: 'http://www.mytaglist.com/ethAccount.asmx/Signin',
json: true,
jar: true,
gzip: true,
body: { email: this.username, password: this.password }
}, function (error, response, body) {
if(error) {
handleResult(true);
} else {
handleResult(response.statusCode!=200);
}
});
}
// Forms a valid request to get the latest tag list. Calls handleResult with a boolean indicating success or failure.
WirelessTagPlatform.prototype.getTagList = function(handleResult) {
request({
method: 'POST',
uri: 'http://www.mytaglist.com/ethClient.asmx/GetTagList2',
json: true,
jar: true,
gzip: true,
body: {}
}, function (error, response, body) {
if(error) {
handleResult(true,body);
} else {
handleResult(response.statusCode!=200,body);
}
});
}
// Does a request to the server to refresh the tag data. Authenticate every time as not sure how often the
// authentication requires refreshing. New tags we see get added to the tag map and the tag list. New tags
// added AFTER startup will be tracked internally but not exposed as a device as frankly I don't know how
// to do that dynamically.
WirelessTagPlatform.prototype.refreshTagData = function(complete) {
var that = this;
// Auth
this.sendAuthentication(function(failed) {
if(!failed) {
// Gets the tag list
that.getTagList(function(innerFailure, body) {
if(!innerFailure) {
for(var index = 0; index < body.d.length; index++) {
var uuid = body.d[index].uuid;
// We haven't seen the tag yet, add it
if(that.tagMap[uuid] == null) {
if(!that.shouldIgnore(body.d[index])) {
var newTag = new WirelessTagAccessory(that.log, body.d[index]);
that.tagMap[uuid] = newTag;
that.tagList.push(newTag);
}
// We have, just update it.
} else {
that.tagMap[uuid].handleUpdate(body.d[index]);
}
}
complete(true);
} else {
that.log("Failed getting tag list");
complete(false);
}
});
} else {
that.log("Failed authenticating to tag list");
complete(false);
}
});
}
// Handles the periodic timer. Reports success or failure and sets the next timer period.
WirelessTagPlatform.prototype.handleTimer = function() {
var that = this;
this.log("Starting update of wireless tags from tag manager");
this.refreshTagData(function(success) {
if(success) {
that.log("Updated wireless tag data");
} else {
that.log("Failed to update wireless tags");
}
// Hack as I was seeing issues with an undefined queryFrequency. Believe I have fixed it but keeping the
// minimum in to keep us from spamming the service. Also prevents values below 5000ms.
if(that.queryFrequency == undefined || that.queryFrequency < 5000) {
that.log("Error ... invalid query frequency, setting to 20000ms default");
that.queryFrequency = 20000;
} else {
setTimeout(that.handleTimer.bind(that), that.queryFrequency);
}
});
}
// Responds to the homebridge platform through calling callback once list of accessories is identified.
WirelessTagPlatform.prototype.accessories = function(callback) {
var that = this;
that.refreshTagData(function(success) {
that.log("Completed getting devices. Result="+success);
if(success) {
that.log("Authenticated and got tag list. Enumerating tags as accessories");
that.handleTimer();
callback(that.tagList);
} else {
that.log("Could not get tags, no accessories exposed");
callback([]);
}
});
}
// Represents a single tag
function WirelessTagAccessory(log,deviceData) {
this.log = log;
this.handleUpdate(deviceData);
}
// Refreshes tag data from the device update data
WirelessTagAccessory.prototype.handleUpdate = function(deviceData) {
this.isOutOfRange = deviceData.OutOfRange;
this.isAlive = deviceData.alive;
this.batteryRemaining = deviceData.batteryRemaining;
this.uuid = deviceData.uuid;
this.tagType = deviceData.tagType;
this.temperature = deviceData.temperature;
this.name = deviceData.name;
if(deviceData.cap != undefined) {
this.humidity = Math.round(deviceData.cap);
} else {
this.humidity = 0.0;
}
this.uuid_base = this.uuid;
// Protections to ensure we don't set the current characteristic value whens services are not yet registered.
if(this.tempService != null && this.tempService != undefined) {
this.tempService
.setCharacteristic(Characteristic.CurrentTemperature, this.temperature);
}
if(this.humidityService != null && this.humidityService != undefined) {
this.humidityService
.setCharacteristic(Characteristic.CurrentRelativeHumidity, this.humidity);
}
}
// Handles getting the temperature in format needed by homebridge. Luckily this is just the raw value in degrees C
WirelessTagAccessory.prototype.getTemperature = function(callback) {
callback(null,this.temperature);
}
// Handles getting the humidity in format needed by homebridge.
WirelessTagAccessory.prototype.getHumidity = function(callback) {
callback(null,this.humidity);
}
// Translates tag state into an occupancy value. If tag is inactive or out of range occupancy is false
WirelessTagAccessory.prototype.getOccupancy = function(callback) {
var isAway = false;
if(this.isOutOfRange == undefined) {
isAway = true;
} else if(this.isOutOfRange) {
isAway = true;
} else if(!this.isAlive) {
isAway = true;
}
callback(null,isAway ? Characteristic.OccupancyDetected.OCCUPANCY_NOT_DETECTED : Characteristic.OccupancyDetected.OCCUPANCY_DETECTED);
}
// Sets up the information, temperature and occupancy services.
WirelessTagAccessory.prototype.getServices = function() {
this.informationService = new Service.AccessoryInformation();
this.informationService
.setCharacteristic(Characteristic.Manufacturer, "SmartHome")
.setCharacteristic(Characteristic.Model, "Wireless Sensor Tag Type="+this.tagType)
.setCharacteristic(Characteristic.SerialNumber, this.uuid);
this.tempService = new Service.TemperatureSensor();
this.tempService
.getCharacteristic(Characteristic.CurrentTemperature)
.on('get', this.getTemperature.bind(this));
this.humidityService = new Service.HumiditySensor();
this.humidityService
.getCharacteristic(Characteristic.CurrentRelativeHumidity)
.on('get', this.getHumidity.bind(this));
this.occupancyService = new Service.OccupancySensor();
this.occupancyService
.getCharacteristic(Characteristic.OccupancyDetected)
.on('get', this.getOccupancy.bind(this));
return [this.informationService, this.tempService, this.occupancyService, this.humidityService];
}
module.exports.platform = WirelessTagPlatform;
module.exports.accessory = WirelessTagAccessory;