-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtr-064.js
196 lines (178 loc) · 4.48 KB
/
tr-064.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
import { v5 as uuid } from 'uuid';
import _rfdc from 'rfdc/default';
import { validateStates, getRoom } from '../adapters';
export default 'tr-064';
export const namespace = 'tr-064';
export const deviceObjectType = 'state';
/*
* State Mapping
*/
const STATE_MAPPING = {
// calllists
"calllists": {
"allCount": {
"state": ".all.count"
},
"allHTML": {
"state": ".all.html"
},
"allJson": {
"state": ".all.json"
},
"inboundCount": {
"state": ".inbound.count"
},
"inboundHTML": {
"state": ".inbound.html"
},
"inboundJson": {
"state": ".inbound.json"
},
"missedCount": {
"state": ".missed.count"
},
"missedHTML": {
"state": ".missed.html"
},
"missedJson": {
"state": ".missed.json"
},
"outboundCount": {
"state": ".outbound.count"
},
"outboundHTML": {
"state": ".outbound.html"
},
"outboundJson": {
"state": ".outbound.json"
}
},
// phonebook
"phonebook": {
"image": {
"state": ".image"
},
"name": {
"state": ".name"
},
"number": {
"state": ".number"
}
},
// devices
/*
"devices": {
"active": {
"state": ".active"
},
"lastActive": {
"state": ".lastActive"
},
"lastActive-ts": {
"state": ".lastActive-ts"
},
"lastIP": {
"state": ".lastIP"
},
"lastInactive": {
"state": ".lastInactive"
},
"lastInactive": {
"state": ".lastInactive"
},
"lastMAC-address": {
"state": ".lastMAC-address"
}
},
*/
// states
"states": {
"ab": {
"state": ".ab"
},
"ip": {
"state": ".externalIP"
},
"ipv6": {
"state": ".externalIPv6"
},
"reboot": {
"action": ".reboot",
"actionElement": "IconButtonAction"
},
"reconnect": {
"action": ".reconnectInternet",
"actionElement": "IconButtonAction"
},
"wlan24": {
"state": ".wlan24"
},
"wlan50": {
"state": ".wlan50"
}
}
}
/**
*
*
*/
export function root(objects, options) {
return new Promise(resolve => {
const stateList = Object.keys(objects);
const devices = {};
for (let instance = 0; instance < 99; instance++) {
// verify that instance exists
if (stateList.indexOf(namespace + '.' + instance + '.states.externalIP') === -1) {
break;
}
// loop channels
for (const key in STATE_MAPPING) {
devices[key + '.' + instance] = devices[key + '.' + instance] || {
'id': (namespace + '.' + instance + '-' + key).toLowerCase().replace(/ /g, '') + '_' + uuid(namespace + '.' + instance, '4eaf6392-6a70-4802-b343-5ff1a1673f39').substr(0, 5),
'name': namespace + '.' + instance + ' ' + key,
'function': 'other',
'room': '',
'states': {}
}
// loop assigned state keys
for (const stateKey in STATE_MAPPING[key]) {
const s = _rfdc(STATE_MAPPING[key][stateKey]);
// add general states to device
if (stateList.indexOf(namespace + '.' + instance + '.' + key + s.state) > -1 || stateList.indexOf(namespace + '.' + instance + '.' + key + s.action) > -1) {
s.state = s.state && ('.' + key + s.state);
s.action = s.action && ('.' + key + s.action);
devices[key + '.' + instance].states[stateKey] = s;
devices[key + '.' + instance].room = getRoom(namespace + '.' + instance + '.' + key + s.state);
}
}
}
// add devices tree as dedicated device
for (const state of stateList) {
if (state.indexOf(namespace + '.' + instance + '.devices.') > -1) {
let deviceName = state.replace(namespace + '.' + instance + '.devices.', '')
deviceName = deviceName.substr(0, deviceName.indexOf('.'));
const deviceId = deviceName.toLowerCase().replace(/ /g, '');
const stateKey = state.substr(state.lastIndexOf('.') + 1);
if (deviceName && stateKey !== 'lastActive-ts' && stateKey !== 'lastInactive-ts') {
devices[deviceId + '.' + instance] = devices[deviceId + '.' + instance] || {
'id': deviceId + '_' + uuid(namespace + '.' + instance, '4eaf6392-6a70-4802-b343-5ff1a1673f39').substr(0, 5),
'name': deviceName,
'function': 'other',
'states': {}
}
devices[deviceId + '.' + instance].states = {
...devices[deviceId + '.' + instance].states,
[stateKey]: state.replace(namespace + '.' + instance, '')
}
}
}
}
}
// validate states
for (const key in devices) {
const [ , instance ] = key.split('.');
devices[key].states = validateStates(devices[key].states, { 'objects': objects, 'list': stateList, 'root': namespace + '.' + instance });
}
resolve(Object.values(devices));
});
}