/
index.ts
413 lines (353 loc) · 11.8 KB
/
index.ts
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
import { RpCallResponse } from '@fastybird/vue-wamp-v1'
import * as exchangeEntitySchema
from '@fastybird/modules-metadata/resources/schemas/devices-module/entity.device.control.json'
import {
ModuleOrigin,
DeviceControlEntity as ExchangeEntity,
DevicesModule as RoutingKeys, DataType,
} from '@fastybird/modules-metadata'
import {
ActionTree,
MutationTree,
} from 'vuex'
import Jsona from 'jsona'
import Ajv from 'ajv'
import { AxiosResponse } from 'axios'
import get from 'lodash/get'
import uniq from 'lodash/uniq'
import Device from '@/lib/models/devices/Device'
import { DeviceInterface } from '@/lib/models/devices/types'
import DeviceControl from '@/lib/models/device-controls/DeviceControl'
import {
DeviceControlEntityTypes,
DeviceControlInterface,
DeviceControlResponseInterface,
DeviceControlsResponseInterface,
} from '@/lib/models/device-controls/types'
import {
ApiError,
OrmError,
} from '@/lib/errors'
import {
JsonApiModelPropertiesMapper,
JsonApiJsonPropertiesMapper,
} from '@/lib/jsonapi'
import { DeviceControlJsonModelInterface, ModuleApiPrefix, SemaphoreTypes } from '@/lib/types'
interface SemaphoreFetchingState {
items: string[]
item: string[]
}
interface SemaphoreState {
fetching: SemaphoreFetchingState
creating: string[]
updating: string[]
deleting: string[]
}
interface DeviceControlState {
semaphore: SemaphoreState
}
interface SemaphoreAction {
type: SemaphoreTypes
id: string
}
const jsonApiFormatter = new Jsona({
modelPropertiesMapper: new JsonApiModelPropertiesMapper(),
jsonPropertiesMapper: new JsonApiJsonPropertiesMapper(),
})
const apiOptions = {
dataTransformer: (result: AxiosResponse<DeviceControlResponseInterface> | AxiosResponse<DeviceControlsResponseInterface>): DeviceControlJsonModelInterface | DeviceControlJsonModelInterface[] => jsonApiFormatter.deserialize(result.data) as DeviceControlJsonModelInterface | DeviceControlJsonModelInterface[],
}
const jsonSchemaValidator = new Ajv()
const moduleState: DeviceControlState = {
semaphore: {
fetching: {
items: [],
item: [],
},
creating: [],
updating: [],
deleting: [],
},
}
const moduleActions: ActionTree<DeviceControlState, unknown> = {
async get({ state, commit }, payload: { device: DeviceInterface, id: string }): Promise<boolean> {
if (state.semaphore.fetching.item.includes(payload.id)) {
return false
}
commit('SET_SEMAPHORE', {
type: SemaphoreTypes.GETTING,
id: payload.id,
})
try {
await DeviceControl.api().get(
`${ModuleApiPrefix}/v1/devices/${payload.device.id}/controls/${payload.id}`,
apiOptions,
)
return true
} catch (e: any) {
throw new ApiError(
'devices-module.device-controls.fetch.failed',
e,
'Fetching device control failed.',
)
} finally {
commit('CLEAR_SEMAPHORE', {
type: SemaphoreTypes.GETTING,
id: payload.id,
})
}
},
async fetch({ state, commit }, payload: { device: DeviceInterface }): Promise<boolean> {
if (state.semaphore.fetching.items.includes(payload.device.id)) {
return false
}
commit('SET_SEMAPHORE', {
type: SemaphoreTypes.FETCHING,
id: payload.device.id,
})
try {
await DeviceControl.api().get(
`${ModuleApiPrefix}/v1/devices/${payload.device.id}/controls`,
apiOptions,
)
return true
} catch (e: any) {
throw new ApiError(
'devices-module.device-controls.fetch.failed',
e,
'Fetching device controls failed.',
)
} finally {
commit('CLEAR_SEMAPHORE', {
type: SemaphoreTypes.FETCHING,
id: payload.device.id,
})
}
},
async transmitCommand(_store, payload: { control: DeviceControlInterface, value?: string | number | boolean | null }): Promise<boolean> {
if (!DeviceControl.query().where('id', payload.control.id).exists()) {
throw new Error('devices-module.device-controls.transmit.failed')
}
const device = Device.find(payload.control.deviceId)
if (device === null) {
throw new Error('devices-module.device-controls.transmit.failed')
}
return new Promise((resolve, reject) => {
DeviceControl.wamp().call<{ data: string }>({
routing_key: RoutingKeys.DEVICES_CONTROL_DATA,
origin: DeviceControl.$devicesModuleOrigin,
data: {
device: device.id,
control: payload.control.id,
expected_value: payload.value,
},
})
.then((response: RpCallResponse<{ data: string }>): void => {
if (get(response.data, 'response') === 'accepted') {
resolve(true)
} else {
reject(new Error('devices-module.device-controls.transmit.failed'))
}
})
.catch((): void => {
reject(new Error('devices-module.device-controls.transmit.failed'))
})
})
},
async socketData({ state, commit }, payload: { origin: string, routingKey: string, data: string }): Promise<boolean> {
if (payload.origin !== ModuleOrigin.MODULE_DEVICES) {
return false
}
if (
![
RoutingKeys.DEVICES_CONTROL_ENTITY_CREATED,
RoutingKeys.DEVICES_CONTROL_ENTITY_UPDATED,
RoutingKeys.DEVICES_CONTROL_ENTITY_DELETED,
].includes(payload.routingKey as RoutingKeys)
) {
return false
}
const body: ExchangeEntity = JSON.parse(payload.data)
const validate = jsonSchemaValidator.compile<ExchangeEntity>(exchangeEntitySchema)
if (validate(body)) {
if (
!DeviceControl.query().where('id', body.id).exists() &&
payload.routingKey === RoutingKeys.DEVICES_CONTROL_ENTITY_DELETED
) {
return true
}
if (payload.routingKey === RoutingKeys.DEVICES_CONTROL_ENTITY_DELETED) {
commit('SET_SEMAPHORE', {
type: SemaphoreTypes.DELETING,
id: body.id,
})
try {
await DeviceControl.delete(body.id)
} catch (e: any) {
throw new OrmError(
'devices-module.device-controls.delete.failed',
e,
'Delete device control failed.',
)
} finally {
commit('CLEAR_SEMAPHORE', {
type: SemaphoreTypes.DELETING,
id: body.id,
})
}
} else {
if (payload.routingKey === RoutingKeys.DEVICES_CONTROL_ENTITY_UPDATED && state.semaphore.updating.includes(body.id)) {
return true
}
commit('SET_SEMAPHORE', {
type: payload.routingKey === RoutingKeys.DEVICES_CONTROL_ENTITY_UPDATED ? SemaphoreTypes.UPDATING : SemaphoreTypes.CREATING,
id: body.id,
})
const entityData: { [index: string]: string | boolean | number | string[] | number[] | DataType | null | undefined } = {
type: DeviceControlEntityTypes.CONTROL,
}
const camelRegex = new RegExp('_([a-z0-9])', 'g')
Object.keys(body)
.forEach((attrName) => {
const camelName = attrName.replace(camelRegex, g => g[1].toUpperCase())
if (camelName === 'device') {
const device = Device.query().where('id', body[attrName]).first()
if (device !== null) {
entityData.deviceId = device.id
}
} else {
entityData[camelName] = body[attrName]
}
})
try {
await DeviceControl.insertOrUpdate({
data: entityData,
})
} catch (e: any) {
const failedEntity = DeviceControl.query().with('device').where('id', body.id).first()
if (failedEntity !== null && failedEntity.device !== null) {
// Updating entity on api failed, we need to refresh entity
await DeviceControl.get(
failedEntity.device,
body.id,
)
}
throw new OrmError(
'devices-module.device-controls.update.failed',
e,
'Edit device control failed.',
)
} finally {
commit('CLEAR_SEMAPHORE', {
type: payload.routingKey === RoutingKeys.DEVICES_CONTROL_ENTITY_UPDATED ? SemaphoreTypes.UPDATING : SemaphoreTypes.CREATING,
id: body.id,
})
}
}
return true
} else {
return false
}
},
reset({ commit }): void {
commit('RESET_STATE')
},
}
const moduleMutations: MutationTree<DeviceControlState> = {
['SET_SEMAPHORE'](state: DeviceControlState, action: SemaphoreAction): void {
switch (action.type) {
case SemaphoreTypes.FETCHING:
state.semaphore.fetching.items.push(action.id)
// Make all keys uniq
state.semaphore.fetching.items = uniq(state.semaphore.fetching.items)
break
case SemaphoreTypes.GETTING:
state.semaphore.fetching.item.push(action.id)
// Make all keys uniq
state.semaphore.fetching.item = uniq(state.semaphore.fetching.item)
break
case SemaphoreTypes.CREATING:
state.semaphore.creating.push(action.id)
// Make all keys uniq
state.semaphore.creating = uniq(state.semaphore.creating)
break
case SemaphoreTypes.UPDATING:
state.semaphore.updating.push(action.id)
// Make all keys uniq
state.semaphore.updating = uniq(state.semaphore.updating)
break
case SemaphoreTypes.DELETING:
state.semaphore.deleting.push(action.id)
// Make all keys uniq
state.semaphore.deleting = uniq(state.semaphore.deleting)
break
}
},
['CLEAR_SEMAPHORE'](state: DeviceControlState, action: SemaphoreAction): void {
switch (action.type) {
case SemaphoreTypes.FETCHING:
// Process all semaphore items
state.semaphore.fetching.items
.forEach((item: string, index: number): void => {
// Find created item in reading one item semaphore...
if (item === action.id) {
// ...and remove it
state.semaphore.fetching.items.splice(index, 1)
}
})
break
case SemaphoreTypes.GETTING:
// Process all semaphore items
state.semaphore.fetching.item
.forEach((item: string, index: number): void => {
// Find created item in reading one item semaphore...
if (item === action.id) {
// ...and remove it
state.semaphore.fetching.item.splice(index, 1)
}
})
break
case SemaphoreTypes.CREATING:
// Process all semaphore items
state.semaphore.creating
.forEach((item: string, index: number): void => {
// Find created item in creating semaphore...
if (item === action.id) {
// ...and remove it
state.semaphore.creating.splice(index, 1)
}
})
break
case SemaphoreTypes.UPDATING:
// Process all semaphore items
state.semaphore.updating
.forEach((item: string, index: number): void => {
// Find created item in updating semaphore...
if (item === action.id) {
// ...and remove it
state.semaphore.updating.splice(index, 1)
}
})
break
case SemaphoreTypes.DELETING:
// Process all semaphore items
state.semaphore.deleting
.forEach((item: string, index: number): void => {
// Find removed item in removing semaphore...
if (item === action.id) {
// ...and remove it
state.semaphore.deleting.splice(index, 1)
}
})
break
}
},
['RESET_STATE'](state: DeviceControlState): void {
Object.assign(state, moduleState)
},
}
export default {
state: (): DeviceControlState => (moduleState),
actions: moduleActions,
mutations: moduleMutations,
}