This repository was archived by the owner on Apr 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathesp-adapter.js
171 lines (147 loc) · 5.07 KB
/
esp-adapter.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
/**
* esp-adapter.js - Web server adapter implemented as a plugin.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.*
*/
'use strict';
const fetch = require('node-fetch');
let Adapter, Device, Property;
try {
Adapter = require('../adapter');
Device = require('../device');
Property = require('../property');
} catch (e) {
if (e.code !== 'MODULE_NOT_FOUND') {
throw e;
}
const gwa = require('gateway-addon');
Adapter = gwa.Adapter;
Device = gwa.Device;
Property = gwa.Property;
}
class ESPProperty extends Property {
constructor(device, name, propertyDescription) {
super(device, name, propertyDescription);
this.unit = propertyDescription.unit;
this.description = propertyDescription.description;
this.href = propertyDescription.href;
this.device = device;
this.setCachedValue(propertyDescription.value);
this.device.notifyPropertyChanged(this);
let url = this.device.url + this.href;
console.log('New ESPProperty, url='+url);
fetch(url)
.then((resp) => resp.json())
.then((resp) => {
let keys = Object.keys(resp);
let values = Object.values(resp);
for (var i=0; i<keys.length; i++) {
let obj = this.device.findProperty(keys[i]);
obj.setCachedValue(values[i]);
this.device.notifyPropertyChanged(obj);
}
});
}
/**
* @method setValue
* @returns a promise which resolves to the updated value.
*
* @note it is possible that the updated value doesn't match
* the value passed in.
*/
setValue(value) {
return new Promise((resolve, reject) => {
// set value but allow override in response.
this.setCachedValue(value);
resolve(value);
this.device.notifyPropertyChanged(this);
let url = this.device.url + this.href+'?'+this.name+'='+value;
console.log('Getting '+url);
fetch(url)
.then((resp) => resp.json())
.then((resp) => {
let keys = Object.keys(resp);
let values = Object.values(resp);
for (var i=0; i<keys.length; i++) {
console.log('Setting value for '+keys[i]+' to '+values[i]);
let obj = this.device.findProperty(keys[i]);
obj.setCachedValue(values[i]);
this.device.notifyPropertyChanged(obj);
}
});
});
}
}
class ESPDevice extends Device {
constructor(adapter, id, name, type, description, url, properties) {
super(adapter, id);
this.url = url;
this.name = name;
this.type = type;
this.description = description;
console.log("Adding device at "+url);
// properties are set by a json response from the actual device
let keys = Object.keys(properties);
let values = Object.values(properties);
for (var i=0; i<keys.length; i++) {
this.properties.set(keys[i], new ESPProperty(this, keys[i], values[i]));
}
}
}
class ESPAdapter extends Adapter {
constructor(addonManager, packageName, manifest) {
super(addonManager, 'ESPAdapter', packageName);
addonManager.addAdapter(this);
this.manifest = manifest;
}
async tryDevice(url, i) {
console.log("Trying "+url);
try {
let response = await fetch(url);
if (!response.ok) // or check for response.status
throw new Error(response.statusText);
let thingResponse = await response.json();
let keys = Object.keys(thingResponse);
let values = Object.values(thingResponse);
for( var n=0; n<keys.length; n++ ) {
console.log('Adding thing->'+keys[n]);
let thingObj = values[n];
let name = thingObj['name'];
let id = this.name + "-" + i + ':' + n;
let description = '';
if( thingObj['description'] )
description = thingObj['description'];
let type = thingObj['type'];
this.handleDeviceAdded(new ESPDevice(this, id, name, type, description, url, thingObj['properties']));
}
} catch(err) {
//console.log('tryDevice err:+'+err);
}
}
startPairing(timeoutSeconds) {
console.log(this.name, 'id', this.id, 'pairing started');
var ipStart = this.manifest.moziot.config.ipStart;
var ipStartSplit = ipStart.split(".");
var ipEnd = this.manifest.moziot.config.ipEnd;
var ipEndSplit = ipEnd.split(".");
var url="";
var thingUser = this.manifest.moziot.config.thingUser;
var thingPwd = this.manifest.moziot.config.thingPwd;
console.log("Pairing "+ipStartSplit[3]+" to "+ipEndSplit[3]);
for(var i=Number(ipStartSplit[3]); i<=Number(ipEndSplit[3]); i++) {
if( thingUser ) {
url = "http://"+thingUser+":"+thingPwd+"@"+ipStartSplit[0]+"."+ipStartSplit[1]+"."+ipStartSplit[2]+"."+i+"/things/esp";
}
else {
url = "http://"+ipStartSplit[0]+"."+ipStartSplit[1]+"."+ipStartSplit[2]+"."+i+"/things/esp";
}
this.tryDevice(url, i);
}
}
}
function loadESPAdapter(addonManager, manifest, _errorCallback) {
let adapter = new ESPAdapter(addonManager, manifest.name, manifest);
}
module.exports = loadESPAdapter;