/
Device.ts
220 lines (176 loc) · 5.66 KB
/
Device.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
import {
Fields,
Item,
Model,
} from '@vuex-orm/core'
import {
DeviceModel,
DeviceControlAction,
FirmwareManufacturer,
HardwareManufacturer,
DeviceConnectionState,
DevicePropertyName,
} from '@fastybird/modules-metadata'
import capitalize from 'lodash/capitalize'
import {
DeviceCreateInterface,
DeviceEntityTypes,
DeviceInterface,
DeviceUpdateInterface,
} from '@/lib/models/devices/types'
import DeviceProperty from '@/lib/models/device-properties/DeviceProperty'
import { DevicePropertyInterface } from '@/lib/models/device-properties/types'
import DeviceConfiguration from '@/lib/models/device-configuration/DeviceConfiguration'
import { DeviceConfigurationInterface } from '@/lib/models/device-configuration/types'
import Channel from '@/lib/models/channels/Channel'
import { ChannelInterface } from '@/lib/models/channels/types'
import DeviceConnector from '@/lib/models/device-connector/DeviceConnector'
// ENTITY MODEL
// ============
export default class Device extends Model implements DeviceInterface {
static get entity(): string {
return 'devices_device'
}
static fields(): Fields {
return {
id: this.string(''),
type: this.string(DeviceEntityTypes.DEVICE),
draft: this.boolean(false),
parentId: this.string(null).nullable(),
key: this.string(''),
identifier: this.string(''),
name: this.string(null).nullable(),
comment: this.string(null).nullable(),
enabled: this.boolean(false),
hardwareModel: this.string(DeviceModel.CUSTOM),
hardwareManufacturer: this.string(HardwareManufacturer.GENERIC),
hardwareVersion: this.string(null).nullable(),
macAddress: this.string(null).nullable(),
firmwareManufacturer: this.string(FirmwareManufacturer.GENERIC),
firmwareVersion: this.string(null).nullable(),
control: this.attr([]),
owner: this.string(null).nullable(),
// Relations
relationshipNames: this.attr([]),
children: this.hasMany(Device, 'parentId'),
channels: this.hasMany(Channel, 'deviceId'),
properties: this.hasMany(DeviceProperty, 'deviceId'),
configuration: this.hasMany(DeviceConfiguration, 'deviceId'),
connector: this.hasOne(DeviceConnector, 'deviceId'),
}
}
id!: string
type!: DeviceEntityTypes
draft!: boolean
parentId!: string | null
key!: string
identifier!: string
name!: string | null
comment!: string | null
enabled!: boolean
hardwareModel!: DeviceModel
hardwareManufacturer!: HardwareManufacturer
hardwareVersion!: string | null
macAddress!: string | null
firmwareManufacturer!: FirmwareManufacturer
firmwareVersion!: string | null
control!: string[]
owner!: string | null
relationshipNames!: string[]
children!: DeviceInterface[]
channels!: ChannelInterface[]
properties!: DevicePropertyInterface[]
configuration!: DeviceConfigurationInterface[]
connector!: DeviceConnector
get isEnabled(): boolean {
return this.enabled
}
get isReady(): boolean {
const property = DeviceProperty
.query()
.where('identifier', DevicePropertyName.STATE)
.where('deviceId', this.id)
.first()
return property !== null && (property.actualValue === DeviceConnectionState.READY || property.actualValue === DeviceConnectionState.RUNNING)
}
get icon(): string {
if (this.hardwareManufacturer === HardwareManufacturer.ITEAD) {
switch (this.hardwareModel) {
case DeviceModel.SONOFF_SC:
return 'thermometer-half'
case DeviceModel.SONOFF_POW:
case DeviceModel.SONOFF_POW_R2:
return 'calculator'
}
}
return 'plug'
}
get title(): string {
if (this.name !== null) {
return this.name
}
const storeInstance = Device.store()
if (
Object.prototype.hasOwnProperty.call(storeInstance, '$i18n')
) {
if (this.isCustomModel) {
return capitalize(this.identifier)
}
// @ts-ignore
if (!storeInstance.$i18n.t(`devicesModule.vendors.${this.hardwareManufacturer}.devices.${this.hardwareModel}.title`).toString().includes('devicesModule.vendors.')) {
// @ts-ignore
return storeInstance.$i18n.t(`devicesModule.vendors.${this.hardwareManufacturer}.devices.${this.hardwareModel}.title`).toString()
}
}
return capitalize(this.identifier)
}
get hasComment(): boolean {
return this.comment !== null && this.comment !== ''
}
get isCustomModel(): boolean {
return this.hardwareModel === DeviceModel.CUSTOM
}
static async get(id: string, includeChannels: boolean): Promise<boolean> {
return await Device.dispatch('get', {
id,
includeChannels,
})
}
static async fetch(includeChannels: boolean): Promise<boolean> {
return await Device.dispatch('fetch', {
includeChannels,
})
}
static async add(data: DeviceCreateInterface, id?: string | null, draft = true): Promise<Item<Device>> {
return await Device.dispatch('add', {
id,
draft,
data,
})
}
static async edit(device: DeviceInterface, data: DeviceUpdateInterface): Promise<Item<Device>> {
return await Device.dispatch('edit', {
device,
data,
})
}
static async save(device: DeviceInterface): Promise<Item<Device>> {
return await Device.dispatch('save', {
device,
})
}
static async remove(device: DeviceInterface): Promise<boolean> {
return await Device.dispatch('remove', {
device,
})
}
static transmitCommand(device: DeviceInterface, command: DeviceControlAction): Promise<boolean> {
return Device.dispatch('transmitCommand', {
device,
command,
})
}
static reset(): void {
Device.dispatch('reset')
}
}