-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
86 lines (82 loc) · 3.58 KB
/
app.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
const Aqara = require('lumi-aqara');
const config = require('./config.js');
const smartWirelessSwitch = require('./lib/smartWirelessSwitch.js');
const humanMotionSensor = require('./lib/humanMotionSensor.js');
const windowDoorSensor = require('./lib/windowDoorSensor.js');
const temperatureHumiditySensor = require('./lib/temperatureHumiditySensor.js');
const smartHomeGateway = require('./lib/smartHomeGateway.js');
const aqara = new Aqara()
aqara.on('gateway', (gateway) => {
console.log('Gateway discovered')
// console.log(gateway);
gateway.on('ready', () => {
console.log('Gateway is ready');
gateway.setPassword(config.gatewayDevPass)
})
gateway.on('offline', () => {
gateway = null
console.log('Gateway is offline')
})
gateway.on('subdevice', (device) => {
// console.log(device);
switch (device.getType()) {
case 'magnet':
device.on('open', () => {
windowDoorSensor(device.getSid(), device.getBatteryPercentage(), '0');
})
device.on('close', () => {
windowDoorSensor(device.getSid(), device.getBatteryPercentage(), '1');
})
break
case 'switch':
device.on('click', () => {
smartWirelessSwitch(device.getSid(), device.getBatteryPercentage(), '1');
})
device.on('doubleClick', () => {
smartWirelessSwitch(device.getSid(), device.getBatteryPercentage(), '2');
})
device.on('longClickPress', () => {
smartWirelessSwitch(device.getSid(), device.getBatteryPercentage(), '3');
})
device.on('longClickRelease', () => {
smartWirelessSwitch(device.getSid(), device.getBatteryPercentage(), '4');
})
break
case 'motion':
device.on('motion', () => {
humanMotionSensor(device.getSid(), device.getBatteryPercentage(), device.getLux(), '0', '1');
})
device.on('noMotion', () => {
humanMotionSensor(device.getSid(), device.getBatteryPercentage(), device.getLux(), device.getSecondsSinceMotion(), '0');
})
break
case 'sensor':
device.on('update', () => {
temperatureHumiditySensor(device.getSid(), device.getBatteryPercentage(), device.getTemperature(), device.getHumidity(), device.getPressure());
})
break
case 'leak':
console.log(` Leak sensor`)
device.on('update', () => {
console.log(`${device.getSid()}${device.isLeaking() ? '' : ' not'} leaking`)
})
break
case 'cube':
console.log(` Cube`)
device.on('update', () => {
console.log(`${device.getSid()} ${device.getStatus()}${device.getRotateDegrees() !== null ? ' ' + device.getRotateDegrees() : ''}`)
})
break
case 'smoke':
console.log(` Smoke`)
device.on('update', () => {
console.log(`${device.getSid()} (${device.hasAlarm() ? 'SMOKE DETECTED' : 'no smoke detected'} density: ${device.getDensity()})`)
})
break
}
})
gateway.on('lightState', (state) => {
console.log(state);
smartHomeGateway(gateway.sid, state.intensity, '000');
})
})