-
Notifications
You must be signed in to change notification settings - Fork 17
/
vehicle.ts
193 lines (174 loc) · 5.28 KB
/
vehicle.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
import { v4 as uuid4 } from 'uuid'
import * as FunnyName from '@/libs/funny-name/funny-name'
import { Signal } from '@/libs/signal'
import type {
Altitude,
Attitude,
Battery,
CommandAck,
Coordinates,
PageDescription,
Parameter,
PowerSupply,
StatusGPS,
StatusText,
Velocity,
} from '@/libs/vehicle/types'
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type Abstract = AbstractVehicle<any>
/**
* Define possible firmwares used by the vehicle
*/
export enum Firmware {
ArduPilot = 'ardupilot',
PX4 = 'px4',
None = 'none',
}
/**
* Define vehicle type
*/
export enum Type {
Antenna = 'antenna',
Blimp = 'blimp',
Copter = 'copter',
Plane = 'plane',
Rover = 'rover',
Sub = 'sub',
}
/**
* Create funny name based on the vehicle type
* @param {Type} type
* @returns {FunnyName.Type}
*/
function toFunnyNameType(type: Type): FunnyName.Type {
switch (type) {
case Type.Sub:
return FunnyName.Type.Water
default:
return FunnyName.Type.None
}
}
/**
* Vehicle abstraction
*/
export abstract class AbstractVehicle<Modes> {
// Information used to identify vehicle on frontend and specialized functionalities
_firmware: Firmware
_type: Type
// Unique identification number and name to check between vehicles
_cockpitRegistrationUUID: string
_funnyName: string
// Used to store generic data that does not belong to any specific subclass
// Usually used for development purposes (e.g.: test the implementation of new sensors)
_genericVariables: Record<string, unknown> = {}
// Signals
onArm = new Signal<boolean>()
onAttitude = new Signal<Attitude>()
onAltitude = new Signal<Altitude>()
onBatteries = new Signal<Battery[]>()
onCpuLoad = new Signal<number>()
onCommandAck = new Signal<CommandAck>()
onGenericVariables = new Signal<Record<string, unknown>>()
onMode = new Signal<Modes>()
onPosition = new Signal<Coordinates>()
onPowerSupply = new Signal<PowerSupply>()
onParameter = new Signal<[Parameter, number | undefined]>()
onStatusGPS = new Signal<StatusGPS>()
onStatusText = new Signal<StatusText>()
onTakeoff = new Signal<boolean>()
onVelocity = new Signal<Velocity>()
/**
* Constructor for the abstract vehicle type
* @param {Firmware} firmware
* @param {Type} type
*/
constructor(firmware: Firmware, type: Type) {
this._firmware = firmware
this._type = type
unimplemented('We need to have a better way to generate a unique id based on the vehicle hardware')
this._cockpitRegistrationUUID = uuid4()
this._funnyName = FunnyName.generate(toFunnyNameType(type))
console.info(
`Creating a new Vehicle:\n`,
`\tname: ${this._funnyName}\n`,
`\tidentification: ${this._cockpitRegistrationUUID}\n`,
`\ttype: ${this._type}\n`,
`\tfirmware: ${this._firmware}\n`
)
this.onArm.register_caller(() => this.isArmed())
this.onAltitude.register_caller(() => this.altitude())
this.onAttitude.register_caller(() => this.attitude())
this.onBatteries.register_caller(() => this.batteries())
this.onCpuLoad.register_caller(() => this.cpuLoad())
this.onGenericVariables.register_caller(() => this.genericVariables())
this.onMode.register_caller(() => this.mode())
this.onPosition.register_caller(() => this.position())
this.onPowerSupply.register_caller(() => this.powerSupply())
this.onParameter.register_caller(() => [this.lastParameter(), this.totalParametersCount()])
this.onStatusText.register_caller(() => this.statusText())
this.onStatusGPS.register_caller(() => this.statusGPS())
this.onTakeoff.register_caller(() => this.flying())
this.onVelocity.register_caller(() => this.velocity())
}
/**
* Return the firmware type of the vehicle
* @returns {Firmware}
*/
firmware(): Firmware {
return this._firmware
}
/**
* Return the vehicle type
* @returns {Type}
*/
type(): Type {
return this._type
}
/**
* Return the generic variables hashmap of the vehicle
* @returns {Record<string, unknown>}
*/
genericVariables(): Record<string, unknown> {
return this._genericVariables
}
/**
* Return the icon based on the vehicle type
* @returns {string}
*/
icon(): string {
switch (this.type()) {
case Type.Antenna:
return 'mdi-satellite-uplink'
case Type.Blimp:
return 'mdi-airballoon'
case Type.Copter:
return 'mdi-quadcopter'
case Type.Plane:
return 'mdi-airplane'
case Type.Rover:
return 'mdi-car-wireless'
case Type.Sub:
return 'mdi-submarine'
}
}
abstract onMessage(message: Uint8Array): void
abstract arm(): boolean
abstract altitude(): Altitude
abstract attitude(): Attitude
abstract batteries(): Battery[]
abstract configurationPages(): PageDescription[]
abstract cpuLoad(): number // Percentage
abstract disarm(): boolean
abstract isArmed(): boolean
abstract mode(): Modes
abstract modesAvailable(): Map<string, Modes>
abstract position(): Coordinates
abstract velocity(): Velocity
abstract powerSupply(): PowerSupply
abstract totalParametersCount(): number | undefined
abstract lastParameter(): Parameter
abstract statusText(): StatusText
abstract statusGPS(): StatusGPS
abstract setMode(mode: Modes): void
abstract flying(): boolean
}