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
|
import { MqttClient } from 'mqtt'
import { EagleStatus } from './eagle'
import { SnowStatus } from './snow'
type TSnowListener = (status: SnowStatus) => void
type TEagleListener = (status: EagleStatus) => void
export interface TConfig {
mqtt: MqttClient
}
export enum Command {
SnowStatus = 200,
EagleStatus = 210
}
export enum MessageSource {
Snow = 1,
Eagle = 2,
App_iOS = 3,
App_Android = 4
}
export interface TMessage<TMessageData> {
cmdId: number
name: string
time: number
from: MessageSource
data: TMessageData
sig: string
}
export const TOPIC_STATUS = 'airmx/01/+/+/1/1/+'
export class Topic {
constructor(
public readonly unknown1: boolean,
public readonly unknown2: boolean,
public readonly unknown3: boolean,
public readonly unknown4: boolean,
public readonly deviceId: number
) {
//
}
static parse(topic: string) {
const components = topic.split('/')
if (components.length !== 7) {
throw new Error('The topic format is expected to be airmx/+/+/+/+/+/+.')
}
if (components[0] !== 'airmx') {
throw new Error('The 1st part of the topic must be "airmx".')
}
if (components[1] !== '01') {
throw new Error('The 2nd part of the topic must be "01".')
}
for (let i = 2; i < 6; i++) {
if (components[i] !== '0' && components[i] !== '1') {
const ordinal = `${i + 1}${(i + 1) === 3 ? 'rd' : 'th'}`
throw new Error(`The ${ordinal} part of the topic must be either "1" or "0".`)
}
}
const deviceId = components[6]
if (deviceId === '' || ! /^\d+$/.test(deviceId)) {
throw new Error('The 7th part of the topic must be a device ID.')
}
return new this(
components[2] === '1',
components[3] === '1',
components[4] === '1',
components[5] === '1',
+deviceId
)
}
}
export class Airmx {
#listeners: {
eagle: TEagleListener[],
snow: TSnowListener[]
} = {
eagle: [],
snow: []
}
#client: MqttClient
constructor(
private readonly config: TConfig
) {
this.#client = this.config.mqtt
this.#client.on('connect', this.#handleConnect.bind(this))
this.#client.on('message', this.#handleMessage.bind(this))
}
onSnowUpdate(callback: TSnowListener) {
this.#listeners.snow.push(callback)
return this
}
onEagleUpdate(callback: TEagleListener) {
this.#listeners.eagle.push(callback)
return this
}
#handleConnect() {
this.#client.subscribe(TOPIC_STATUS)
}
#handleMessage(topic: string, message: Buffer): void {
let t: Topic
try {
t = Topic.parse(topic)
} catch (Error) {
return
}
const data = JSON.parse(message.toString())
switch (data.cmdId) {
case Command.SnowStatus:
this.#notifySnow(SnowStatus.from(t.deviceId, data))
break
case Command.EagleStatus:
this.#notifyEagle(EagleStatus.from(t.deviceId, data))
break
}
}
#notifySnow(status: SnowStatus) {
this.#listeners.snow.forEach((listener) => listener(status))
}
#notifyEagle(status: EagleStatus) {
this.#listeners.eagle.forEach((listener) => listener(status))
}
}
|