-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmqtt.js
145 lines (132 loc) · 2.63 KB
/
mqtt.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
import { validateStates, parseDefault, detectFunction, getRoom } from '../adapters';
import _rfdc from 'rfdc/default';
export default 'MQTT';
export const namespace = 'mqtt';
export const deviceObjectType = 'folder';
export const devicePattern = '((?!info).)*';
/*
* State Mapping
*/
const STATE_MAPPING = {
// lights
"light": {
"dimmer": {
"state": ".Dimmer",
"action": ".Dimmer"
},
"ct": {
"state": ".CT",
"action": ".CT",
"properties": {
"min": 153,
"max": 500
}
},
"hue": {
"state": ".Hue",
"action": ".Hue"
},
"sat": {
"state": ".Saturation",
"action": ".Saturation"
}
},
// other
"other": {
"version": [ ".Version", ".INFO.Version" ],
"reachability": ".alive",
"ip": ".INFO.IPAddress",
"signal": ".Wifi_Signal",
"dataReceived": ".RfReceived_Data",
"power": {
"state": ".POWER",
"action": ".POWER"
},
"powerCurrent": {
"state": ".ENERGY_Current",
"unit": " A"
},
"powerMeter": {
"state": ".ENERGY_Power",
"unit": " W"
},
"powerConsumption": {
"state": ".ENERGY_Total",
"unit": " kWh"
},
"powerConsumptionToday": {
"state": ".ENERGY_Today",
"unit": " kWh"
},
"powerConsumptionYesterday": {
"state": ".ENERGY_Yesterday",
"unit": " kWh"
},
"power1": {
"state": ".POWER1",
"action": ".POWER1"
},
"power2": {
"state": ".POWER2",
"action": ".POWER2"
},
"power3": {
"state": ".POWER3",
"action": ".POWER3"
},
"power4": {
"state": ".POWER4",
"action": ".POWER4"
},
"power5": {
"state": ".POWER5",
"action": ".POWER5"
},
"power6": {
"state": ".POWER6",
"action": ".POWER6"
},
"power7": {
"state": ".POWER7",
"action": ".POWER7"
},
"power8": {
"state": ".POWER8",
"action": ".POWER8"
},
"power9": {
"state": ".POWER9",
"action": ".POWER9"
}
}
}
/**
*
*
*/
export function parse(deviceStructure, options) {
return new Promise(resolve => {
const obj = deviceStructure.objects[deviceStructure.root] || {};
let device = {
'name': (obj.common && obj.common.name) || obj._id || 'Unknown MQTT Device Name',
'function': 'other',
'room': getRoom(deviceStructure),
'states': {
..._rfdc(STATE_MAPPING.other),
..._rfdc(STATE_MAPPING.light)
}
}
// validate states
device.states = validateStates(device.states, deviceStructure);
// nothing left, so read it all
if (device.states.power === undefined && device.states.dimmer === undefined) {
parseDefault(deviceStructure, options, device).then(resolve);
}
else {
// detect function
device = detectFunction(device);
// resolve
resolve(device);
}
});
}