-
Notifications
You must be signed in to change notification settings - Fork 0
/
socket.ts
250 lines (215 loc) · 8.23 KB
/
socket.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
import { type WebSocketHook } from 'react-use-websocket/dist/lib/types'
import { pnp } from '@/proto/pnp/v1/pnp'
import { type Position, type MachineState } from '@/app/place/PlaceInterface'
const INT16_CONSTANTS = {
RANGE: 65535,
MINIMUM: -32768,
MAXIMUM: 32767,
} as const
export const GantryDirection = pnp.v1.Message.Step.Direction
export const HeadOperation = pnp.v1.Message.HeadOperation.Operation
export type ActionPayloads = {
['NO_OPERATION']: null,
['MOVE_TARGET']: Position,
['DRAW_TARGETS']: Position[],
['UPDATE_STATE']: MachineState,
}
type ActionType = keyof ActionPayloads
type ActionWithoutPayload<T extends ActionType> = {
actionType: T;
messageType: pnp.v1.Message.Tags;
rawPayload: string | Uint8Array;
silent?: boolean;
}
type ActionPayloadIfNotNull<T extends ActionType> = ActionPayloads[T] extends null
? Record<never, never>
: {
payload: ActionPayloads[T]
}
export type Action = {
[T in ActionType]: ActionWithoutPayload<T> & ActionPayloadIfNotNull<T>;
}[ActionType]
export async function processMessage(data: unknown): Promise<Action> {
try {
if (!(data instanceof Blob)) {
throw new Error('Non-Blob data received!')
}
const rawPayload = new Uint8Array(await data?.arrayBuffer())
const decodedMessage = pnp.v1.Message.deserializeBinary(rawPayload)
switch (decodedMessage.tag) {
case pnp.v1.Message.Tags.MOVED_DELTAS:
if (!decodedMessage.has_deltas) {
console.error('Missing deltas payload: ', decodedMessage)
throw new Error('Missing deltas payload')
}
return {
actionType: 'MOVE_TARGET',
messageType: decodedMessage.tag,
rawPayload,
payload: denormalisePosition(decodedMessage.deltas),
silent: true,
}
case pnp.v1.Message.Tags.TARGET_POSITIONS:
if (!decodedMessage.has_positions) {
console.error('Missing positions payload: ', decodedMessage)
throw new Error('Missing positions payload')
}
return {
actionType: 'DRAW_TARGETS',
messageType: decodedMessage.tag,
rawPayload,
payload: decodedMessage.positions.offsets.map(denormalisePosition),
silent: true,
}
case pnp.v1.Message.Tags.MACHINE_STATE:
if (!decodedMessage.has_machineState) {
console.error('Missing machine state payload: ', decodedMessage)
throw new Error('Missing machine state payload')
}
return {
actionType: 'UPDATE_STATE',
messageType: decodedMessage.tag,
rawPayload,
payload: {
gantryPosition: denormalisePosition(decodedMessage.machineState.gantryPosition),
isHeadDown: decodedMessage.machineState.isHeadDown,
isVacuumEngaged: decodedMessage.machineState.isVacuumEngaged,
isComponentPicked: decodedMessage.machineState.isComponentPicked,
},
silent: true,
}
case pnp.v1.Message.Tags.HEARTBEAT:
case pnp.v1.Message.Tags.TARGET_DELTAS:
case pnp.v1.Message.Tags.CALIBRATE_DELTAS:
case pnp.v1.Message.Tags.OPERATE_HEAD:
case pnp.v1.Message.Tags.ROTATE_NOZZLE:
case pnp.v1.Message.Tags.STEP_GANTRY:
return {
actionType: 'NO_OPERATION',
messageType: decodedMessage.tag,
rawPayload,
silent: true,
}
case pnp.v1.Message.Tags.INVALID:
return {
actionType: 'NO_OPERATION',
messageType: decodedMessage.tag,
rawPayload,
}
}
}
catch (error) {
console.warn('Wire error: ', data, error)
return {
actionType: 'NO_OPERATION',
messageType: pnp.v1.Message.Tags.INVALID,
rawPayload: String(data),
}
}
}
function sendMessage(webSocket: WebSocketHook, message: pnp.v1.Message) {
console.info('Sending message: ', { message })
webSocket.sendMessage(message.serializeBinary())
}
export function getHeartbeatMessage(): Uint8Array {
return new pnp.v1.Message({
tag: pnp.v1.Message.Tags.HEARTBEAT,
})
.serializeBinary()
}
export function sendHeartbeat(webSocket: WebSocketHook) {
sendMessage(webSocket, new pnp.v1.Message({
tag: pnp.v1.Message.Tags.HEARTBEAT,
}))
}
function normalisePosition(position: Position): Position {
return {
x: (position.x * INT16_CONSTANTS.RANGE),
y: (position.y * INT16_CONSTANTS.RANGE),
}
}
function denormalisePosition(position: Position): Position {
return {
x: (position.x / INT16_CONSTANTS.RANGE),
y: (position.y / INT16_CONSTANTS.RANGE),
}
}
/**
* Send the delta counts between the current position to the target position over the WebSocket.
*
* The delta is normalised into an absolute, invariant range.
*
* @param webSocket The WebSocket connection.
* @param targetOffset The raw target position, as a percentage offset from the top left corner of the overlay.
*/
export function sendTargetDeltas(webSocket: WebSocketHook, targetOffset: Position) {
/*
* This normalises the delta towards the clicked target to be on an Int16 coordinate space that is:
* - Centred at (0, 0), defined as the present position of the head (ie no delta)
* - Most negative at -32,768
* - Most positive at 32,767
*
* This was originally done to ensure that the delta information passed back to the controller via
* the WebSocket was agnostic of both the client viewport and the streamed video size
*
* Now that rawTargetPosition is provided as a percentage offset, this is performed such that integral
* types are sent across the socket rather than floating point numbers
*
* In other words, the burden is left to the controller to map the Int16 delta into real camera pixels
*/
const normalisedDeltas = normalisePosition({
x: (targetOffset.x - 0.5),
y: (targetOffset.y - 0.5),
})
normalisedDeltas.x = Math.max(INT16_CONSTANTS.MINIMUM, Math.min(INT16_CONSTANTS.MAXIMUM, Math.floor(normalisedDeltas.x)))
normalisedDeltas.y = Math.max(INT16_CONSTANTS.MINIMUM, Math.min(INT16_CONSTANTS.MAXIMUM, Math.floor(normalisedDeltas.y)))
console.info(`Delta normalised to (${normalisedDeltas.x}, ${normalisedDeltas.y})`)
sendMessage(webSocket, new pnp.v1.Message({
tag: pnp.v1.Message.Tags.TARGET_DELTAS,
deltas: new pnp.v1.Message.Deltas(normalisedDeltas),
}))
}
export function sendGantryStep(webSocket: WebSocketHook, direction: pnp.v1.Message.Step.Direction) {
console.info(`Stepping gantry in direction ${direction}`)
sendMessage(webSocket, new pnp.v1.Message({
tag: pnp.v1.Message.Tags.STEP_GANTRY,
step: new pnp.v1.Message.Step({ direction }),
}))
}
export function sendCalibrationDeltas(webSocket: WebSocketHook, targetOffset: Position, realOffset: Position) {
const normalisedDeltas = {
target: normalisePosition({
x: (targetOffset.x - 0.5),
y: (targetOffset.y - 0.5),
}),
real: normalisePosition({
x: (realOffset.x - 0.5),
y: (realOffset.y - 0.5),
}),
}
normalisedDeltas.target.x = Math.max(INT16_CONSTANTS.MINIMUM, Math.min(INT16_CONSTANTS.MAXIMUM, Math.floor(normalisedDeltas.target.x)))
normalisedDeltas.target.y = Math.max(INT16_CONSTANTS.MINIMUM, Math.min(INT16_CONSTANTS.MAXIMUM, Math.floor(normalisedDeltas.target.y)))
normalisedDeltas.real.x = Math.max(INT16_CONSTANTS.MINIMUM, Math.min(INT16_CONSTANTS.MAXIMUM, Math.floor(normalisedDeltas.real.x)))
normalisedDeltas.real.y = Math.max(INT16_CONSTANTS.MINIMUM, Math.min(INT16_CONSTANTS.MAXIMUM, Math.floor(normalisedDeltas.real.y)))
sendMessage(webSocket, new pnp.v1.Message({
tag: pnp.v1.Message.Tags.CALIBRATE_DELTAS,
calibration: new pnp.v1.Message.Calibration({
target: new pnp.v1.Message.Deltas(normalisedDeltas.target),
real: new pnp.v1.Message.Deltas(normalisedDeltas.real),
}),
}))
}
export function sendHeadOperation(webSocket: WebSocketHook, operation: pnp.v1.Message.HeadOperation.Operation) {
console.info(`Performing ${operation} head operation`)
sendMessage(webSocket, new pnp.v1.Message({
tag: pnp.v1.Message.Tags.OPERATE_HEAD,
headOperation: new pnp.v1.Message.HeadOperation({ operation }),
}))
}
export function sendNozzleRotation(webSocket: WebSocketHook, degrees: number) {
console.info(`Rotating nozzle ${degrees} degrees`)
sendMessage(webSocket, new pnp.v1.Message({
tag: pnp.v1.Message.Tags.ROTATE_NOZZLE,
nozzleRotation: new pnp.v1.Message.NozzleRotation({ degrees }),
}))
}