summaryrefslogtreecommitdiff
path: root/packages/airmx/src/airmx.ts
blob: aa9fe301f423d022821eca0e33eb003a6df35957 (plain)
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
import crypto from 'crypto'
import { MqttClient } from 'mqtt'
import { EagleStatus } from './eagle.js'
import { SnowStatus } from './snow.js'
import type { Config, SnowListener, EagleListener } from './types.js'
import { Command } from './types.js'

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: EagleListener[],
    snow: SnowListener[]
  } = {
    eagle: [],
    snow: []
  }

  #client: MqttClient

  constructor(
    private readonly config: Config
  ) {
    this.#client = this.config.mqtt
    this.#client.on('connect', this.#handleConnect.bind(this))
    this.#client.on('message', this.#handleMessage.bind(this))
  }

  onSnowUpdate(callback: SnowListener) {
    this.#listeners.snow.push(callback)
    return this
  }

  onEagleUpdate(callback: EagleListener) {
    this.#listeners.eagle.push(callback)
    return this
  }

  #handleConnect() {
    this.#client.subscribe('airmx/01/+/+/1/1/+')
  }

  #handleMessage(topic: string, message: Buffer): void {
    let t: Topic

    try {
      t = Topic.parse(topic)
    } catch (e) {
      return
    }

    const str = message.toString()
    const data = JSON.parse(str)
    this.#validateMessage(t.deviceId, str, data.sig)

    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))
  }

  #validateMessage(deviceId: number, message: string, sig: string) {
    const device = this.config.devices.find((device) => device.id === deviceId)
    if (device === undefined) {
      throw new Error(`Could not find the device with ID ${deviceId}.`)
    }
    const plainText = message.slice(1, message.lastIndexOf('"sig"'))
    const calculated = crypto.createHash('md5')
      .update(plainText)
      .update(device.key)
      .digest('hex')
    if (calculated !== sig) {
      throw new Error('Failed to validate the message.')
    }
  }
}