summaryrefslogtreecommitdiff
path: root/src/messages.ts
blob: 66c63d1320830a9a388193657179fae87870fb85 (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
import type { EagleControlData, InstantPushData } from './types.js'
import { MessageSource } from './types.js'

export class CommandMessage<T> {
  constructor(
    readonly commandId: number,
    readonly commandName: string,
    readonly data: T,
    readonly time: number,
    readonly from: number,
  ) {}

  payload() {
    return {
      cmdId: this.commandId,
      name: this.commandName,
      time: this.time,
      from: this.from,
      data: this.data,
    }
  }
}

const current = () => {
  return Math.floor(new Date().getTime() / 1000)
}

export class InstantPushMessage extends CommandMessage<InstantPushData> {
  static commandId() {
    return 40
  }

  /**
   * @param frequency - Report frequency in seconds.
   * @param duration - Report duration in seconds.
   */
  static make(frequency: number, duration: number) {
    const data = {
      frequencyTime: frequency,
      durationTime: duration,
    }
    return new InstantPushMessage(
      this.commandId(),
      'instantPush',
      data,
      current(),
      MessageSource.App_Android,
    )
  }
}

export class EagleControlMesasge extends CommandMessage<EagleControlData> {
  static commandId() {
    return 100
  }

  static make(data: EagleControlData) {
    const timestamp = Math.floor(new Date().getTime() / 1000)
    return new EagleControlMesasge(
      this.commandId(),
      'control',
      data,
      timestamp,
      MessageSource.App_Android,
    )
  }
}