Initial commit

This commit is contained in:
2019-01-16 11:42:54 +08:00
commit 44ca0897d7
82 changed files with 4809 additions and 0 deletions
+81
View File
@@ -0,0 +1,81 @@
'use strict';
const texts = {
0: 'Release alarm',
1: 'Gas alarm',
2: 'Analog alarm',
64: 'Sensitivity fault alarm',
32768: 'I2C communication failure'
};
function Alarm(sid, ip, hub, model) {
this.type = model;
this.sid = sid;
this.hub = hub;
this.ip = ip;
this.state = null;
this.description = null;
this.voltage = null;
this.percent = null;
}
Alarm.prototype.getData = function (data, isHeartbeat) {
let newData = false;
let obj = {};
if (data.voltage !== undefined) {
data.voltage = parseInt(data.voltage, 10);
this.voltage = data.voltage / 1000;
this.percent = ((data.voltage - 2200) / 10);
if (this.percent > 100) {
this.percent = 100;
}
if (this.percent < 0) {
this.percent = 0;
}
obj.voltage = this.voltage;
obj.percent = this.percent;
newData = true;
}
if (data.alarm !== undefined && !isHeartbeat) {
if (data.alarm !== true && data.alarm !== false) {
data.alarm = parseInt(data.alarm, 10) || 0;
if (data.alarm === 1) {
if (this.type === 'smoke') {
data.description = 'Smoke alarm';
} else {
data.description = 'Gas alarm';
}
} else {
data.description = texts[data.alarm] || '';
}
data.alarm = (data.alarm === 1 || data.alarm === 2);
}
this.state = data.alarm;
obj.state = data.alarm;
obj.description = data.description;
newData = true;
}
return newData ? obj : null;
};
Alarm.prototype.heartBeat = function (token, data) {
if (data) {
const obj = this.getData(data, true);
if (obj) {
this.hub.emit('data', this.sid, this.type, obj);
}
}
};
Alarm.prototype.onMessage = function (message) {
if (message.data) {
const obj = this.getData(message.data);
if (obj) {
this.hub.emit('data', this.sid, this.type, obj);
}
}
};
module.exports = Alarm;
+71
View File
@@ -0,0 +1,71 @@
'use strict';
function Button(sid, ip, hub) {
this.type = 'switch';
this.sid = sid;
this.hub = hub;
this.voltage = null;
this.percent = null;
this.ip = ip;
return this;
}
Button.prototype.getData = function (data) {
let newData = false;
let obj = {};
if (data.voltage) {
data.voltage = parseInt(data.voltage, 10);
this.voltage = data.voltage / 1000;
this.percent = ((data.voltage - 2200) / 10);
if (this.percent > 100) {
this.percent = 100;
}
if (this.percent < 0) {
this.percent = 0;
}
obj.voltage = this.voltage;
obj.percent = this.percent;
newData = true;
}
if (data.status) {
if (data.status === 'click') {
obj.click = true;
setTimeout(() => this.hub.emit('data', this.sid, this.type, {click: false}), 300);
}
if (data.status === 'double_click') {
obj.double = true;
setTimeout(() => this.hub.emit('data', this.sid, this.type, {double: false}), 300);
}
if (data.status === 'long_click_press') {
obj.long = true;
}
if (data.status === 'long_click_release') {
obj.long = false;
}
newData = true;
}
return newData ? obj : null;
};
Button.prototype.heartBeat = function (token, data) {
if (data) {
const obj = this.getData(data);
if (obj) {
this.hub.emit('data', this.sid, this.type, obj);
}
}
};
Button.prototype.onMessage = function (message) {
if (message.data) {
const obj = this.getData(message.data);
if (obj) {
this.hub.emit('data', this.sid, this.type, obj);
}
}
};
module.exports = Button;
+97
View File
@@ -0,0 +1,97 @@
'use strict';
function Cube(sid, ip, hub, rotatePosition) {
this.type = 'cube';
this.sid = sid;
this.ip = ip;
this.hub = hub;
this.voltage = null;
this.percent = null;
this.rotate_position = parseFloat(rotatePosition || 0) || 0;
return this;
}
Cube.prototype.trigger = function (obj, name) {
obj[name] = true;
setTimeout(() => {
const _obj = {};
_obj[name] = false;
this.hub.emit('data', this.sid, this.type, _obj);
}, 300);
};
Cube.prototype.getData = function (data) {
let newData = false;
let obj = {};
if (data.voltage) {
data.voltage = parseInt(data.voltage, 10);
this.voltage = data.voltage / 1000;
this.percent = ((data.voltage - 2200) / 10);
if (this.percent > 100) {
this.percent = 100;
}
if (this.percent < 0) {
this.percent = 0;
}
obj.voltage = this.voltage;
obj.percent = this.percent;
newData = true;
}
if (data.status) {
// flip90, flip180, move, tap_twice, shake_air, swing, alert, free_fall, rotate_left, rotate_right
this.trigger(obj, data.status);
newData = true;
}
if (data.rotate) {
// rotate
obj.rotate = parseFloat(data.rotate.replace(',', '.')) || 0;
if (obj.rotate >= 0) {
this.trigger(obj, 'rotate_right');
} else if (obj.rotate < 0) {
this.trigger(obj, 'rotate_left');
}
this.rotate_position += obj.rotate;
if (this.rotate_position < 0) this.rotate_position = 0;
if (this.rotate_position > 100) this.rotate_position = 100;
obj.rotate_position = this.rotate_position;
newData = true;
}
return newData ? obj : null;
};
Cube.prototype.Control = function (attr, val) {
if (attr === 'rotate_position') {
val = parseFloat(val);
if (val < 0) val = 0;
if (val > 100) val = 100;
if (this.rotate_position !== val) {
this.hub.emit('data', this.sid, this.type, {rotate_position: this.rotate_position});
}
}
};
Cube.prototype.heartBeat = function (token, data) {
if (data) {
const obj = this.getData(data);
if (obj) {
this.hub.emit('data', this.sid, this.type, obj);
}
}
};
Cube.prototype.onMessage = function (message) {
if (message.data) {
const obj = this.getData(message.data);
if (obj) {
this.hub.emit('data', this.sid, this.type, obj);
}
}
};
module.exports = Cube;
+90
View File
@@ -0,0 +1,90 @@
'use strict';
function Curtain(sid, ip, hub) {
this.type = 'curtain';
this.sid = sid;
this.hub = hub;
this.ip = ip;
this.curtain_level = null;
}
Curtain.prototype.getData = function (data) {
let newData = false;
let obj = {};
if (data.curtain_level) {
this.curtain_level = parseFloat(data.curtain_level);
obj.curtain_level = this.curtain_level;
newData = true;
}
if (data.status) {
if (this.status === 'open') {
obj.open = true;
newData = true;
} else
if (this.status === 'close') {
obj.close = true;
newData = true;
} else
if (this.status === 'stop') {
obj.stop = true;
newData = true;
} else {
this.hub.emit('warning', 'Unknown status "' + this.status + '"');
}
}
return newData ? obj : null;
};
Curtain.prototype.heartBeat = function (token, data) {
if (data) {
const obj = this.getData(data);
if (obj) {
this.hub.emit('data', this.sid, this.type, obj);
}
}
};
Curtain.prototype.Control = function (attr, value) {
let message;
if (attr === 'stop' || attr === 'open' || attr === 'close') {
message = {
cmd: 'write',
model: this.type,
sid: this.sid,
short_id: 0,
data: {
status: attr,
key: this.hub.getKey(this.ip)
}
};
} else if (attr === 'curtain_level') {
message = {
cmd: 'write',
model: this.type,
sid: this.sid,
short_id: 0,
data: {
curtain_level: value.toString(), //Strange thing, working only if string.
key: this.hub.getKey(this.ip)
}
};
} else {
this.hub.emit('warning', 'Unknown control attribute "' + attr + '"');
return;
}
this.hub.sendMessage(message, this.ip);
};
Curtain.prototype.onMessage = function (message) {
if (message.data) {
if (message.data.status) {
this.on = message.data.status === 'on';
this.hub.emit('data', this.sid, this.type, {
state: this.on
});
}
}
};
module.exports = Curtain;
+54
View File
@@ -0,0 +1,54 @@
'use strict';
function DoorSensor(sid, ip, hub) {
this.type = 'magnet';
this.sid = sid;
this.hub = hub;
this.ip = ip;
this.opened = null;
this.voltage = null;
this.percent = null;
}
DoorSensor.prototype.getData = function (data, isHeartbeat) {
let newData = false;
let obj = {};
if (data.voltage !== undefined) {
data.voltage = parseInt(data.voltage, 10);
this.voltage = data.voltage / 1000;
this.percent = ((data.voltage - 2200) / 10);
if (this.percent > 100) {
this.percent = 100;
}
if (this.percent < 0) {
this.percent = 0;
}
obj.voltage = this.voltage;
obj.percent = this.percent;
newData = true;
}
if (data.status && data.status !== 'unknown' && !isHeartbeat) {
this.opened = data.status !== 'close';
obj.state = this.opened;
newData = true;
}
return newData ? obj : null;
};
DoorSensor.prototype.heartBeat = function (token, data) {
if (data) {
const obj = this.getData(data, true);
if (obj) {
this.hub.emit('data', this.sid, this.type, obj);
}
}
};
DoorSensor.prototype.onMessage = function (message) {
if (message.data) {
const obj = this.getData(message.data);
if (obj) {
this.hub.emit('data', this.sid, this.type, obj);
}
}
};
module.exports = DoorSensor;
+242
View File
@@ -0,0 +1,242 @@
'use strict';
function Gateway(sid, ip, hub) {
this.type = 'gateway';
this.sid = sid;
this.ip = ip;
this.hub = hub;
this.token = null;
this.illumination = null;
this.connected = false;
this.lastValues = {
rgb: '#FFFFFF',
dimmer: 100
};
this.rgb = null;
this.dimmer = null;
this.on = null;
this.hub.sendMessage({cmd: 'get_id_list', sid: sid}, this.ip);
this.timeout = null;
return this;
}
Gateway.prototype.getData = function (data) {
let newData = false;
let obj = {};
if (data.illumination !== undefined) {
this.illumination = parseFloat(data.illumination);
obj.illumination = this.illumination;
newData = true;
}
if (!this.connected) {
this.connected = true;
obj.connected = this.connected;
newData = true;
}
// Start timeout to detect dicsonnect
this.timeout && clearTimeout(this.timeout);
this.timeout = setTimeout(() => {
this.timeout = null;
this.hub.emit('data', this.sid, this.type, {connected: false});
}, 20000);
if (data.rgb !== undefined) {
let rgb = parseInt(data.rgb, 10);
if (!rgb) {
this.rgb = '#000000';
this.dimmer = 0;
this.on = false;
} else {
rgb = rgb.toString(16);
if (rgb.length === 7) {
rgb = '0' + rgb;
} else if (rgb.length === 6) {
rgb = '00' + rgb;
} else if (rgb.length === 5) {
rgb = '000' + rgb;
} else if (rgb.length === 4) {
rgb = '0000' + rgb;
} else if (rgb.length === 3) {
rgb = '00000' + rgb;
} else if (rgb.length === 2) {
rgb = '000000' + rgb;
} else if (rgb.length === 1) {
rgb = '0000000' + rgb;
}
this.dimmer = parseInt(rgb.substring(0, 2), 16);
this.rgb = '#' + rgb.substring(2).toUpperCase();
this.on = true;
}
obj.on = this.on;
obj.dimmer = this.dimmer;
obj.rgb = this.rgb;
// remember last non null values
if (obj.dimmer) {
this.lastValues.dimmer = obj.dimmer;
}
if (parseInt(obj.rgb.replace('#', ''), 16)) {
this.lastValues.rgb = obj.rgb;
}
newData = true;
}
return newData ? obj : null;
};
Gateway.prototype.heartBeat = function (token, data) {
if (token) {
this.token = token;
const obj = this.getData(data);
if (obj) {
this.hub.emit('data', this.sid, this.type, obj);
}
}
};
Gateway.prototype.onMessage = function (message) {
if (message.cmd === 'get_id_list_ack') {
this.initSensors(message.data);
} else if (message.cmd === 'write_ack') {
if (message.data.error) {
this.hub.emit('error', message.data.error);
}
const obj_ = this.getData(message.data);
if (obj_) {
this.hub.emit('data', this.sid, this.type, obj_);
}
} else if (message.data) {
const obj = this.getData(message.data);
if (obj) {
this.hub.emit('data', this.sid, this.type, obj);
}
}
};
Gateway.prototype.Control = function (attr, value) {
if (attr === 'on' || attr === 'dimmer' || attr === 'rgb') {
if (this.dimmer === null) {
this.dimmer = this.lastValues.dimmer;
}
if (this.rgb === null) {
this.rgb = this.lastValues.rgb;
}
if (this.on === null) {
this.on = true;
}
if (attr === 'on') {
this.on = !!value;
if (this.on) {
if (!parseInt(this.rgb.replace('#', ''), 16)) {
this.rgb = this.lastValues.rgb;
}
if (!this.dimmer) {
this.dimmer = this.lastValues.dimmer;
}
}
}
if (attr === 'dimmer') {
this.dimmer = value;
if (this.dimmer < 0) this.dimmer = 0;
if (this.dimmer > 100) this.dimmer = 100;
if (this.dimmer) {
this.on = true;
if (!parseInt(this.rgb.replace('#', ''), 16)) {
this.rgb = this.lastValues.rgb;
}
}
}
if (attr === 'rgb') {
this.rgb = value;
if (parseInt(this.rgb.replace('#', ''), 16)) {
this.on = true;
if (!this.dimmer) {
this.dimmer = this.lastValues.dimmer;
}
}
}
if (this.timer) clearTimeout(this.timer);
// collect data before send
this.timer = setTimeout(() => {
this.timer = null;
let value;
if (!this.on || !this.dimmer || this.rgb === '000000' || this.rgb === '#000000') {
value = 0;
} else {
value = (this.dimmer << 24) | parseInt(this.rgb.replace('#', ''), 16);
}
const message = {
cmd: 'write',
model: this.type,
sid: this.sid,
short_id: 0,
data: {
rgb: value,
key: this.hub.getKey(this.ip)
}
};
this.hub.sendMessage(message, this.ip);
}, 200);
} else if (attr === 'volume') {
if (value < 0) value = 0;
if (value > 100) value = 100;
const message = {
cmd: 'write',
model: this.type,
sid: this.sid,
short_id: 0,
data: {
mid: this.mid || 999,
vol: value,
key: this.hub.getKey(this.ip)
}
};
this.hub.sendMessage(message, this.ip);
} else if (attr === 'mid') {
this.mid = value;
const message = {
cmd: 'write',
model: this.type,
sid: this.sid,
short_id: 0,
data: {
mid: value,
key: this.hub.getKey(this.ip)
}
};
this.hub.sendMessage(message, this.ip);
} else {
this.hub.emit('warning', 'Unknown attribute ' + attr);
}
};
Gateway.prototype.initSensors = function (sids) {
this.hub.sendMessage({cmd: 'read', sid: this.sid}, this.ip);
for (let i = 0; i < sids.length; i++) {
this.hub.sendMessage({cmd: 'read', sid: sids[i]}, this.ip);
}
};
module.exports = Gateway;
+78
View File
@@ -0,0 +1,78 @@
'use strict';
function Lock(sid, ip, hub) {
this.type = 'lock_aq1';
this.sid = sid;
this.ip = ip;
this.hub = hub;
this.fing_verified = null;
this.psw_verified = null;
this.card_verified = null;
this.verified_wrong = null;
}
// {'cmd': 'read_ack', 'model': 'lock.aq1', 'sid': '158d000222****', 'short_id': 55***, 'data': '{"voltage":3387}'}
// {'cmd': 'read_ack', 'model': 'lock.aq1', 'sid': '158d000222****', 'short_id': 55***, 'data': '{"fing_verified": 65536}'}
// {'cmd': 'read_ack', 'model': 'lock.aq1', 'sid': '158d000222****', 'short_id': 55***, 'data': '{"fing_verified": 65537}'}
// {'cmd': 'read_ack', 'model': 'lock.aq1', 'sid': '158d000222****', 'short_id': 55***, 'data': '{"psw_verified": 131074}'}
// {'cmd': 'read_ack', 'model': 'lock.aq1', 'sid': '158d000222****', 'short_id': 55***, 'data': '{"card_verified": 196608}'}
// {'cmd': 'report', 'model': 'lock.aq1', 'sid': '158d000222****', 'short_id': 55***, 'data': '{"verified_wrong":"3"}'}
Lock.prototype.getData = function (data) {
let newData = false;
let obj = {};
if (data.voltage !== undefined) {
data.voltage = parseInt(data.voltage, 10);
this.voltage = data.voltage / 1000;
this.percent = ((data.voltage - 2200) / 10);
if (this.percent > 100) {
this.percent = 100;
}
if (this.percent < 0) {
this.percent = 0;
}
obj.voltage = this.voltage;
obj.percent = this.percent;
newData = true;
}
if (data.fing_verified) {
this.fing_verified = parseInt(data.fing_verified);
obj.fing_verified = this.fing_verified;
newData = true;
}
if (data.psw_verified) {
this.psw_verified = parseInt(data.psw_verified);
obj.psw_verified = this.psw_verified;
newData = true;
}
if (data.card_verified) {
this.card_verified = parseInt(data.card_verified);
obj.card_verified = this.card_verified;
newData = true;
}
if (data.verified_wrong) {
this.verified_wrong = parseInt(data.verified_wrong);
obj.verified_wrong = this.verified_wrong;
newData = true;
}
return newData ? obj : null;
};
Lock.prototype.heartBeat = function (token, data) {
if (data) {
const obj = this.getData(data);
if (obj) {
this.hub.emit('data', this.sid, this.type, obj);
}
}
};
Lock.prototype.onMessage = function (message) {
if (message.data) {
const obj = this.getData(message.data);
if (obj) {
this.hub.emit('data', this.sid, this.type, obj);
}
}
};
module.exports = Lock;
+70
View File
@@ -0,0 +1,70 @@
'use strict';
function MotionSensor(sid, ip, hub, model) {
this.type = model;
this.sid = sid;
this.ip = ip;
this.hub = hub;
this.motion = null;
this.voltage = null;
this.percent = null;
}
MotionSensor.prototype.getData = function (data, isHeartbeat) {
let newData = false;
let obj = {};
if (data.voltage !== undefined) {
data.voltage = parseInt(data.voltage, 10);
this.voltage = data.voltage / 1000;
this.percent = ((data.voltage - 2200) / 10);
if (this.percent > 100) {
this.percent = 100;
}
if (this.percent < 0) {
this.percent = 0;
}
obj.voltage = this.voltage;
obj.percent = this.percent;
newData = true;
}
if (data.status && !isHeartbeat) {
this.motion = data.status === 'motion';
obj.state = this.motion;
if (this.motion) {
this.no_motion = 0;
obj.no_motion = 0;
}
newData = true;
}
if (data.no_motion !== undefined && !isHeartbeat) {
this.no_motion = parseInt(data.no_motion, 10);
obj.no_motion = this.no_motion;
obj.state = !this.no_motion;
newData = true;
}
if (data.lux !== undefined) {
this.lux = parseInt(data.lux, 10);
obj.lux = this.lux;
newData = true;
}
return newData ? obj : null;
};
MotionSensor.prototype.heartBeat = function (token, data) {
if (data) {
const obj = this.getData(data, true);
if (obj) {
this.hub.emit('data', this.sid, this.type, obj);
}
}
};
MotionSensor.prototype.onMessage = function (message) {
if (message.data) {
const obj = this.getData(message.data);
if (obj) {
this.hub.emit('data', this.sid, this.type, obj);
}
}
};
module.exports = MotionSensor;
+83
View File
@@ -0,0 +1,83 @@
'use strict';
function Plug (sid, ip, hub) {
this.type = 'plug';
this.sid = sid;
this.ip = ip;
this.hub = hub;
this.on = null;
this.load_power = null;
this.power_consumed = null;
this.state = null;
this.inuse = null;
}
// {"cmd":"report","model":"plug","sid":"dfdfd","short_id":52239,"data":{"status":"on"}}
// {"cmd":"report","model":"plug","sid":"dfdfdf","short_id":52239,"data":{"status":"off"}}
// {"cmd":"heartbeat","model":"plug","sid":"fdfdfd","short_id":52239,"data":{"voltage":3600,"status":"on","inuse":"0","power_consumed":"26326","load_power":"0.00"}}
Plug.prototype.getData = function (data) {
let newData = false;
let obj = {};
if (data.load_power) {
this.load_power = parseFloat(data.load_power);
obj.load_power = this.load_power;
newData = true;
}
if (data.power_consumed) {
this.power_consumed = parseFloat(data.power_consumed);
obj.power_consumed = this.power_consumed;
newData = true;
}
if (data.inuse) {
this.inuse = !!parseInt(data.inuse, 10);
obj.inuse = this.inuse;
newData = true;
}
if (data.status) {
this.state = data.status === 'on';
obj.state = this.state;
newData = true;
}
return newData ? obj : null;
};
Plug.prototype.heartBeat = function (token, data) {
if (data) {
const obj = this.getData(data);
if (obj) {
this.hub.emit('data', this.sid, this.type, obj);
}
}
};
Plug.prototype.Control = function (attr, value) {
if (attr !== 'channel_0' && attr !== 'state') {
this.hub.emit('warning', 'Unknown attribute ' + attr);
return;
}
const message = {
cmd: 'write',
model: this.type,
sid: this.sid,
short_id: 0,
data: {
channel_0: value ? 'on' : 'off',
key: this.hub.getKey(this.ip)
}
};
this.hub.sendMessage(message, this.ip);
};
Plug.prototype.onMessage = function (message) {
if (message.data) {
if (message.data.status) {
this.on = message.data.status === 'on';
this.hub.emit('data', this.sid, this.type, {
state: this.on
});
}
}
};
module.exports = Plug;
+89
View File
@@ -0,0 +1,89 @@
'use strict';
function THSensor(sid, ip, hub, model, options) {
this.type = model;
this.sid = sid;
this.hub = hub;
this.ip = ip;
this.interval = parseInt((options && options.interval) || 5000, 10) || 0;
this.temperature = null;
this.humidity = null;
this.voltage = null;
this.percent = null;
this.lastData = null;
}
THSensor.prototype.getData = function (data) {
let newData = false;
let obj = {};
const ts = Date.now();
if (this.interval && this.lastData) {
const diff = ts - this.lastData;
if (diff > 200 && diff < this.interval) {
obj.doublePress = true;
// deactivate after 300 ms
setTimeout(() => this.hub.emit('data', this.sid, this.type, {doublePress: false}), 300);
this.lastData = null;
} else {
this.lastData = ts;
}
} else {
this.lastData = ts;
}
if (data.voltage !== undefined) {
data.voltage = parseInt(data.voltage, 10);
this.voltage = data.voltage / 1000;
this.percent = ((data.voltage - 2200) / 10);
if (this.percent > 100) {
this.percent = 100;
}
if (this.percent < 0) {
this.percent = 0;
}
obj.voltage = this.voltage;
obj.percent = this.percent;
newData = true;
}
if (data.temperature === '10000' || data.temperature === 10000) {
// ignore all values if 10000 as temperature.
return null;
}
if (data.temperature !== undefined) {
this.temperature = parseInt(data.temperature) / 100;
obj.temperature = this.temperature;
newData = true;
}
if (data.humidity !== undefined) {
this.humidity = parseInt(data.humidity) / 100;
obj.humidity = this.humidity;
newData = true;
}
if (data.pressure !== undefined) {
this.pressure = parseInt(data.pressure) / 100;
obj.pressure = this.pressure;
newData = true;
}
return newData ? obj : null;
};
THSensor.prototype.heartBeat = function (token, data) {
if (data) {
const obj = this.getData(data);
if (obj) {
this.hub.emit('data', this.sid, this.type, obj);
}
}
};
THSensor.prototype.onMessage = function (message) {
if (message.data) {
const obj = this.getData(message.data);
if (obj) {
this.hub.emit('data', this.sid, this.type, obj);
}
}
};
module.exports = THSensor;
+84
View File
@@ -0,0 +1,84 @@
'use strict';
function VibrationSensor(sid, ip, hub, model) {
this.type = model;
this.sid = sid;
this.ip = ip;
this.hub = hub;
this.vibration = null;
this.orientationX = null;
this.orientationY = null;
this.orientationZ = null;
this.bed_activity = null;
this.tilt_angle = null;
this.voltage = null;
this.percent = null;
}
VibrationSensor.prototype.getData = function (data, isHeartbeat) {
let newData = false;
let obj = {};
if (data.voltage !== undefined) {
data.voltage = parseInt(data.voltage, 10);
this.voltage = data.voltage / 1000;
this.percent = ((data.voltage - 2200) / 10);
if (this.percent > 100) {
this.percent = 100;
}
if (this.percent < 0) {
this.percent = 0;
}
obj.voltage = this.voltage;
obj.percent = this.percent;
newData = true;
}
if (data.status && !isHeartbeat) {
this.vibration = data.status === 'vibration' || data.status === 'vibrate';
if (this.vibration) {
setTimeout(() => this.hub.emit('data', this.sid, this.type, {state: false}), 300);
}
obj.state = this.vibration;
newData = true;
}
if (data.final_tilt_angle !== undefined) {
this.tilt_angle = parseInt(data.final_tilt_angle, 10);
obj.tilt_angle = this.tilt_angle;
newData = true;
}
if (data.coordination !== undefined) {
const parts = data.coordination.split(',').map(num => parseInt(num.trim()));
this.orientationX = parts[0];
this.orientationY = parts[1];
this.orientationZ = parts[2];
obj.orientationX = this.orientationX;
obj.orientationY = this.orientationY;
obj.orientationZ = this.orientationZ;
newData = true;
}
if (data.bed_activity !== undefined) {
this.bed_activity = parseInt(data.bed_activity, 10);
obj.bed_activity = this.bed_activity;
newData = true;
}
return newData ? obj : null;
};
VibrationSensor.prototype.heartBeat = function (token, data) {
if (data) {
const obj = this.getData(data, true);
if (obj) {
this.hub.emit('data', this.sid, this.type, obj);
}
}
};
VibrationSensor.prototype.onMessage = function (message) {
if (message.data) {
const obj = this.getData(message.data);
if (obj) {
this.hub.emit('data', this.sid, this.type, obj);
}
}
};
module.exports = VibrationSensor;
+89
View File
@@ -0,0 +1,89 @@
'use strict';
function WallButtons(sid, ip, hub, model) {
this.type = model;
this.sid = sid;
this.hub = hub;
this.ip = ip;
this.channel_0 = null;
this.channel_1 = null;
this.dual_channel = null;
this.voltage = null;
this.percent = null;
return this;
}
WallButtons.prototype.getData = function (data) {
let newData = false;
let obj = {};
if (data.voltage !== undefined) {
data.voltage = parseInt(data.voltage, 10);
this.voltage = data.voltage / 1000;
this.percent = ((data.voltage - 2200) / 10);
if (this.percent > 100) {
this.percent = 100;
}
if (this.percent < 0) {
this.percent = 0;
}
obj.voltage = this.voltage;
obj.percent = this.percent;
newData = true;
}
if (data.channel_0) {
obj.channel_0_double = data.channel_0 === 'double_click';
this.channel_0 = data.channel_0 === 'click';
obj.channel_0 = this.channel_0;
newData = true;
if (obj.channel_0) {
setTimeout(() => this.hub.emit('data', this.sid, this.type, {channel_0: false}), 300);
}
if (obj.channel_0_double) {
setTimeout(() => this.hub.emit('data', this.sid, this.type, {channel_0_double: false}), 300);
}
}
if (data.channel_1) {
obj.channel_1_double = data.channel_1 === 'double_click';
this.channel_1 = data.channel_1 === 'click';
obj.channel_1 = this.channel_1;
newData = true;
if (obj.channel_1) {
setTimeout(() => this.hub.emit('data', this.sid, this.type, {channel_1: false}), 300);
}
if (obj.channel_1_double) {
setTimeout(() => this.hub.emit('data', this.sid, this.type, {channel_1_double: false}), 300);
}
}
if (data.dual_channel) {
this.dual_channel = data.dual_channel === 'both_click';
obj.dual_channel = this.dual_channel;
newData = true;
if (obj.dual_channel) {
setTimeout(() => this.hub.emit('data', this.sid, this.type, {dual_channel: false}), 300);
}
}
return newData ? obj : null;
};
WallButtons.prototype.heartBeat = function (token, data) {
if (data) {
const obj = this.getData(data);
if (obj) {
this.hub.emit('data', this.sid, this.type, obj);
}
}
};
WallButtons.prototype.onMessage = function (message) {
if (message.data) {
const obj = this.getData(message.data);
if (obj) {
this.hub.emit('data', this.sid, this.type, obj);
}
}
};
module.exports = WallButtons;
+70
View File
@@ -0,0 +1,70 @@
'use strict';
function WallWiredSwitch(sid, ip, hub, model) {
this.type = model;
this.sid = sid;
this.hub = hub;
this.ip = ip;
this.channel_0 = null;
this.channel_1 = null;
return this;
}
WallWiredSwitch.prototype.getData = function (data) {
let newData = false;
let obj = {};
if (data.channel_0) {
this.channel_0 = data.channel_0 === 'on';
obj.channel_0 = this.channel_0;
newData = true;
}
if (data.channel_1) {
this.channel_1 = data.channel_1 === 'on';
obj.channel_1 = this.channel_1;
newData = true;
}
return newData ? obj : null;
};
WallWiredSwitch.prototype.Control = function (attr, value) {
if (attr !== 'channel_0' && attr !== 'channel_1') {
this.hub.emit('warning', 'Unknown attribute ' + attr);
return;
}
const message = {
cmd: 'write',
model: this.type,
sid: this.sid,
short_id: 0,
data: {
key: this.hub.getKey(this.ip)
}
};
message.data[attr] = value ? 'on' : 'off';
this.hub.sendMessage(message, this.ip);
};
WallWiredSwitch.prototype.heartBeat = function (token, data) {
if (data) {
const obj = this.getData(data);
if (obj) {
this.hub.emit('data', this.sid, this.type, obj);
}
}
};
WallWiredSwitch.prototype.onMessage = function (message) {
if (message.data) {
const obj = this.getData(message.data);
if (obj) {
this.hub.emit('data', this.sid, this.type, obj);
}
}
};
module.exports = WallWiredSwitch;
+57
View File
@@ -0,0 +1,57 @@
'use strict';
function WaterSensor(sid, ip, hub) {
this.type = 'sensor_wleak.aq1';
this.sid = sid;
this.hub = hub;
this.ip = ip;
this.motion = null;
this.voltage = null;
this.percent = null;
}
WaterSensor.prototype.getData = function (data, isHeartbeat) {
let newData = false;
let obj = {};
if (data.voltage !== undefined) {
data.voltage = parseInt(data.voltage, 10);
this.voltage = data.voltage / 1000;
this.percent = ((data.voltage - 2200) / 10);
if (this.percent > 100) {
this.percent = 100;
}
if (this.percent < 0) {
this.percent = 0;
}
obj.voltage = this.voltage;
obj.percent = this.percent;
newData = true;
}
// {“cmd”:“report”,“model”:“sensor_wleak.aq1”,“sid”:“aaa000xxxxxxx”,“short_id”:12345,“data”:"{“status”:“leak”}"}
// {“cmd”:“report”,“model”:“sensor_wleak.aq1”,“sid”:“aaa000xxxxxxx”,“short_id”:12345,“data”:"{“status”:“no_leak”}"}
if (data.status && !isHeartbeat) {
this.leak = data.status === 'leak';
obj.state = this.leak;
newData = true;
}
return newData ? obj : null;
};
WaterSensor.prototype.heartBeat = function (token, data) {
if (data) {
const obj = this.getData(data, true);
if (obj) {
this.hub.emit('data', this.sid, this.type, obj);
}
}
};
WaterSensor.prototype.onMessage = function (message) {
if (message.data) {
const obj = this.getData(message.data);
if (obj) {
this.hub.emit('data', this.sid, this.type, obj);
}
}
};
module.exports = WaterSensor;