-
Notifications
You must be signed in to change notification settings - Fork 7
/
controller.ts
197 lines (182 loc) · 4.23 KB
/
controller.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
import { ActionStatusCode, EapiTopic } from "./types";
import { OID } from "./oid";
import { Bus, QoS } from "busrt";
import { pack } from "./tools";
import { stringify as uuidStringify, parse as uuidParse } from "uuid";
/** EAPI action status payload */
export interface BusActionStatus {
uuid: Array<number> | Uint8Array;
status: ActionStatusCode;
out?: any;
err?: any;
exitcode?: number;
}
/** EAPI action call payload */
export interface BusAction {
uuid: Array<number> | Buffer;
i: string;
timeout: number; // microseconds
priority: number;
params?: ActionParams;
config?: any;
}
/** EAPI action call params payload */
export interface ActionParams {
/** for unit actions */
value?: any;
/** for lmacro actions */
args?: Array<any>;
/** for lmacro actions */
kwargs?: object;
}
/** EAPI action call class */
export class Action {
uuid: string;
oid: OID;
timeout: number;
priority: number;
params?: ActionParams;
config?: any;
/**
* @param {event} BusAction - construct an instance from the bus call payload
*/
constructor(event: BusAction) {
this.uuid = uuidStringify(event.uuid);
this.oid = new OID(event.i);
this.timeout = event.timeout;
this.priority = event.priority;
this.params = event.params;
this.config = event.config;
}
}
/** Action controller */
export class Controller {
/** @ignore */
bus: Bus;
/** @ignore */
constructor(bus: Bus) {
this.bus = bus;
}
/**
* Notify the bus about action pending event
*
* @param {Action} action - action to notify about
*
* @returns {Promise<void>}
*/
eventPending(action: Action): Promise<void> {
return this._sendEvent(action, ActionStatusCode.Pending);
}
/**
* Notify the bus about action running event
*
* @param {Action} action - action to notify about
*
* @returns {Promise<void>}
*/
eventRunning(action: Action): Promise<void> {
return this._sendEvent(action, ActionStatusCode.Running);
}
/**
* Notify the bus about action completed event
*
* @param {Action} action - action to notify about
* @param {any} [out] - output result
*
* @returns {Promise<void>}
*/
eventCompleted(action: Action, out?: any): Promise<void> {
return this._sendEvent(
action,
ActionStatusCode.Completed,
out,
undefined,
0
);
}
/**
* Notify the bus about action failed event
*
* @param {Action} action - action to notify about
* @param {any} [out] - output result
* @param {any} [err] - error message
* @param {number} [exitcode] - exit code (default: -1)
*
* @returns {Promise<void>}
*/
eventFailed(
action: Action,
out?: any,
err?: any,
exitcode?: number
): Promise<void> {
return this._sendEvent(
action,
ActionStatusCode.Failed,
out,
err,
exitcode === undefined ? -1 : exitcode
);
}
/**
* Notify the bus about action canceled event
*
* @param {Action} action - action to notify about
*
* @returns {Promise<void>}
*/
eventCanceled(action: Action) {
return this._sendEvent(action, ActionStatusCode.Canceled);
}
/**
* Notify the bus about action terminated event
*
* @param {Action} action - action to notify about
*
* @returns {Promise<void>}
*/
eventTerminated(action: Action) {
return this._sendEvent(action, ActionStatusCode.Terminated);
}
/** @ignore */
async _sendEvent(
action: Action,
statusCode: ActionStatusCode,
out?: any,
err?: any,
exitcode?: number
): Promise<void> {
const path = action.oid.asPath();
const payload = this._actionEventPayload(
action.uuid,
statusCode,
out,
err,
exitcode
);
this.bus.publish(`${EapiTopic.ActionStatus}${path}`, payload, QoS.No);
}
/** @ignore */
_actionEventPayload(
u: string,
status: ActionStatusCode,
out?: any,
err?: any,
exitcode?: number
): Buffer {
let payload: BusActionStatus = {
uuid: uuidParse(u),
status: status
};
if (out !== undefined) {
payload.out = out;
}
if (err !== undefined) {
payload.err = err;
}
if (exitcode !== undefined) {
payload.exitcode = exitcode;
}
return pack(payload);
}
}