-
Notifications
You must be signed in to change notification settings - Fork 0
/
device.ts
67 lines (49 loc) · 1.57 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
import { isFirstGen, isFirstPilot, isGlow } from './utils.js'
import type { Attrs, Device } from '../types.js'
import type { IDeviceModel } from './interfaces.js'
export class DeviceModel implements IDeviceModel {
static #instances = new Map<string, DeviceModel>()
public readonly id: string
public readonly name: string
public readonly productKey: string
public readonly productName: string
public isFirstGen: boolean
public isFirstPilot: boolean
public isGlow: boolean
#data: Attrs
private constructor(device: Device, data: Attrs) {
;({
dev_alias: this.name,
did: this.id,
product_key: this.productKey,
product_name: this.productName,
} = device)
this.isFirstGen = isFirstGen(this.productKey)
this.isFirstPilot = isFirstPilot(this.productName)
this.isGlow = isGlow(this.productKey)
this.#data = data
}
public get data(): Attrs {
return this.#data
}
public static getAll(): DeviceModel[] {
return [...this.#instances.values()]
}
public static getById(id: string): DeviceModel | undefined {
return this.#instances.get(id)
}
public static getByName(name: string): DeviceModel | undefined {
return this.getAll().find(({ name: instanceName }) => instanceName === name)
}
public static sync(
devices: readonly Device[],
data: Record<string, Attrs>,
): void {
this.#instances = new Map(
devices.map((device) => [device.did, new this(device, data[device.did])]),
)
}
public update(data: Partial<Attrs>): void {
this.#data = { ...this.#data, ...data }
}
}