busroot-sdk 0.0.0 → 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/client.d.ts +63 -0
- package/build/client.d.ts.map +1 -0
- package/build/client.js +176 -0
- package/build/client.test.d.ts +2 -0
- package/build/client.test.d.ts.map +1 -0
- package/build/client.test.js +6 -0
- package/build/common.d.ts +973 -0
- package/build/example.d.ts +2 -0
- package/build/example.d.ts.map +1 -0
- package/build/example.js +31 -0
- package/build/hooks.d.ts +20 -0
- package/build/hooks.d.ts.map +1 -0
- package/build/hooks.js +41 -0
- package/{src/index.ts → build/index.d.ts} +1 -0
- package/build/index.d.ts.map +1 -0
- package/build/index.js +18 -0
- package/package.json +17 -5
- package/eslint.config.mjs +0 -8
- package/jest.config.ts +0 -10
- package/src/client.test.ts +0 -5
- package/src/client.ts +0 -226
- package/src/example.ts +0 -43
- package/src/hooks.ts +0 -43
- package/tsconfig.json +0 -10
- package/tsconfig.tsbuildinfo +0 -1
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { PlantSchema, PlantViewModel, ScheduleViewModel, BusrootSignals, SkuViewModel, StationSchema, StationViewModel, AccountViewModel, ApiKeyViewModel } from "../build/common";
|
|
2
|
+
import mqtt from "mqtt/dist/mqtt.esm";
|
|
3
|
+
export declare const MqttEventType: {
|
|
4
|
+
StationMetricNew: string;
|
|
5
|
+
StationChanged: string;
|
|
6
|
+
ScheduleChanged: string;
|
|
7
|
+
ScheduleStarted: string;
|
|
8
|
+
ScheduleEnded: string;
|
|
9
|
+
ProductionNew: string;
|
|
10
|
+
};
|
|
11
|
+
export type MqttEventType = keyof typeof MqttEventType;
|
|
12
|
+
export type Listener = (msg: any) => void;
|
|
13
|
+
declare class Client {
|
|
14
|
+
config: {
|
|
15
|
+
accountId: string;
|
|
16
|
+
apiKey: string;
|
|
17
|
+
host: string;
|
|
18
|
+
};
|
|
19
|
+
mqttClient: mqtt.MqttClient;
|
|
20
|
+
listenersByEventType: Map<"StationMetricNew" | "StationChanged" | "ScheduleChanged" | "ScheduleStarted" | "ScheduleEnded" | "ProductionNew", Set<Listener>>;
|
|
21
|
+
apiUrl: string;
|
|
22
|
+
constructor(config: {
|
|
23
|
+
accountId: string;
|
|
24
|
+
apiKey: string;
|
|
25
|
+
host: string;
|
|
26
|
+
});
|
|
27
|
+
destroy(): void;
|
|
28
|
+
private get;
|
|
29
|
+
private post;
|
|
30
|
+
get profile(): {
|
|
31
|
+
get: () => Promise<any>;
|
|
32
|
+
};
|
|
33
|
+
get account(): {
|
|
34
|
+
create: (domain: string, plantName: string) => Promise<{
|
|
35
|
+
account: AccountViewModel;
|
|
36
|
+
apiKey: string;
|
|
37
|
+
}>;
|
|
38
|
+
};
|
|
39
|
+
get apiKey(): {
|
|
40
|
+
create: () => Promise<ApiKeyViewModel>;
|
|
41
|
+
};
|
|
42
|
+
get plant(): {
|
|
43
|
+
get: (plantCode: string) => Promise<PlantViewModel>;
|
|
44
|
+
createUpdate: (station: Partial<PlantSchema>) => Promise<PlantViewModel>;
|
|
45
|
+
};
|
|
46
|
+
get station(): {
|
|
47
|
+
get: (stationCode: string) => Promise<StationViewModel[]>;
|
|
48
|
+
createUpdate: (station: Partial<StationSchema>) => Promise<StationViewModel>;
|
|
49
|
+
signal: (stationCode: string, payload: BusrootSignals) => void;
|
|
50
|
+
};
|
|
51
|
+
sku: {
|
|
52
|
+
get: (skuCode: string) => Promise<SkuViewModel>;
|
|
53
|
+
createUpdate: (sku: Partial<SkuViewModel>) => Promise<SkuViewModel>;
|
|
54
|
+
};
|
|
55
|
+
schedule: {
|
|
56
|
+
get: (scheduleId: number) => Promise<ScheduleViewModel[]>;
|
|
57
|
+
createUpdate: (schedule: Partial<ScheduleViewModel>) => Promise<ScheduleViewModel>;
|
|
58
|
+
};
|
|
59
|
+
on(event: MqttEventType, listener: Listener): void;
|
|
60
|
+
off(event: MqttEventType, listener: Listener): void;
|
|
61
|
+
}
|
|
62
|
+
export {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;MAM/E;IAED,IAAI,OAAO;2BAEkB,MAAM,KAAG,OAAO,CAAC,gBAAgB,EAAE,CAAC;gCAK/B,OAAO,CAAC,aAAa,CAAC,KAAG,OAAO,CAAC,gBAAgB,CAAC;8BAK1D,MAAM,WAAW,cAAc,KAAG,IAAI;MAI/D;IAED,GAAG;uBACoB,MAAM,KAAG,OAAO,CAAC,YAAY,CAAC;4BAKzB,OAAO,CAAC,YAAY,CAAC,KAAG,OAAO,CAAC,YAAY,CAAC;MAKvE;IAEF,QAAQ;0BACkB,MAAM,KAAG,OAAO,CAAC,iBAAiB,EAAE,CAAC;iCAK9B,OAAO,CAAC,iBAAiB,CAAC,KAAG,OAAO,CAAC,iBAAiB,CAAC;MAKtF;IAEF,EAAE,CAAC,KAAK,EAAE,aAAa,EAAE,QAAQ,EAAE,QAAQ;IAU3C,GAAG,CAAC,KAAK,EAAE,aAAa,EAAE,QAAQ,EAAE,QAAQ;CAW7C;AAED,OAAO,EAAE,MAAM,EAAE,CAAC"}
|
package/build/client.js
ADDED
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.Client = exports.MqttEventType = void 0;
|
|
7
|
+
const mqtt_esm_1 = __importDefault(require("mqtt/dist/mqtt.esm"));
|
|
8
|
+
exports.MqttEventType = {
|
|
9
|
+
StationMetricNew: "STATION_METRIC_NEW", // When a new station window is created and saved to DB.
|
|
10
|
+
StationChanged: "STATION_CHANGED", // When a station is added or edited. NOT IMPLEMENTED.
|
|
11
|
+
ScheduleChanged: "SCHEDULE_CHANGED", // When a schedule is added or edited. NOT IMPLEMENTED.
|
|
12
|
+
ScheduleStarted: "SCHEDULE_STARTED", // When a schedule starts (actualStartAt). NOT IMPLEMENTED.
|
|
13
|
+
ScheduleEnded: "SCHEDULE_ENDED", // When a schedule ends (actualEndAt). NOT IMPLEMENTED.
|
|
14
|
+
ProductionNew: "PRODUCTION_NEW", // When new production is added. NOT IMPLEMENTED FOR MANUAL PRODUCTION YET.
|
|
15
|
+
};
|
|
16
|
+
class Client {
|
|
17
|
+
constructor(config) {
|
|
18
|
+
this.config = config;
|
|
19
|
+
this.listenersByEventType = new Map();
|
|
20
|
+
this.sku = {
|
|
21
|
+
get: async (skuCode) => {
|
|
22
|
+
const res = await this.get("/sku", { skucodes: skuCode });
|
|
23
|
+
return res.result;
|
|
24
|
+
},
|
|
25
|
+
createUpdate: async (sku) => {
|
|
26
|
+
const res = await this.post("/sku", sku);
|
|
27
|
+
return res.result;
|
|
28
|
+
},
|
|
29
|
+
};
|
|
30
|
+
this.schedule = {
|
|
31
|
+
get: async (scheduleId) => {
|
|
32
|
+
const res = await this.get("/schedule", { ids: String(scheduleId) });
|
|
33
|
+
return res.results;
|
|
34
|
+
},
|
|
35
|
+
createUpdate: async (schedule) => {
|
|
36
|
+
const res = await this.post("/schedule", schedule);
|
|
37
|
+
return res.result;
|
|
38
|
+
},
|
|
39
|
+
};
|
|
40
|
+
this.apiUrl = "http://" + config.host + ":3000/api";
|
|
41
|
+
this.mqttClient = mqtt_esm_1.default.connect({
|
|
42
|
+
host: config.host,
|
|
43
|
+
port: 8883,
|
|
44
|
+
username: "api-key",
|
|
45
|
+
password: this.config.apiKey,
|
|
46
|
+
clientId: this.config.accountId + "/sdk",
|
|
47
|
+
});
|
|
48
|
+
this.mqttClient.on("message", (topic, payload) => {
|
|
49
|
+
const eventType = topic.toUpperCase().split("/").at(-1);
|
|
50
|
+
for (const [listenerEventType, listeners] of this.listenersByEventType) {
|
|
51
|
+
if (exports.MqttEventType[listenerEventType] !== eventType) {
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
54
|
+
for (const listener of listeners) {
|
|
55
|
+
try {
|
|
56
|
+
listener(JSON.parse(payload.toString()));
|
|
57
|
+
}
|
|
58
|
+
catch (e) {
|
|
59
|
+
//
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
this.mqttClient.on("error", (error) => {
|
|
65
|
+
console.error(error);
|
|
66
|
+
});
|
|
67
|
+
this.mqttClient.on("connect", () => {
|
|
68
|
+
console.log("Connected to Busroot MQTT...");
|
|
69
|
+
});
|
|
70
|
+
this.mqttClient.on("reconnect", () => {
|
|
71
|
+
console.log("Disconnected from Busroot MQTT...");
|
|
72
|
+
});
|
|
73
|
+
this.mqttClient.subscribe(`busroot/v2/${this.config.accountId}/events/#`);
|
|
74
|
+
}
|
|
75
|
+
destroy() {
|
|
76
|
+
this.mqttClient.end();
|
|
77
|
+
}
|
|
78
|
+
async get(path, query) {
|
|
79
|
+
let url = this.apiUrl + path;
|
|
80
|
+
if (query != null) {
|
|
81
|
+
url +=
|
|
82
|
+
"?" +
|
|
83
|
+
Object.entries(query)
|
|
84
|
+
.map((value) => value[0] + "=" + value[1])
|
|
85
|
+
.join("&");
|
|
86
|
+
}
|
|
87
|
+
const res = await fetch(url, {
|
|
88
|
+
headers: { "x-api-key": this.config.apiKey },
|
|
89
|
+
method: "GET",
|
|
90
|
+
});
|
|
91
|
+
if (res.status !== 200) {
|
|
92
|
+
throw new Error(`${res.status}: ${res.statusText}`);
|
|
93
|
+
}
|
|
94
|
+
const resJson = await res.json();
|
|
95
|
+
return resJson;
|
|
96
|
+
}
|
|
97
|
+
async post(path, body) {
|
|
98
|
+
const res = await fetch(this.apiUrl + path, {
|
|
99
|
+
headers: { "x-api-key": this.config.apiKey, "Content-Type": "application/json" },
|
|
100
|
+
method: "POST",
|
|
101
|
+
body: JSON.stringify(body),
|
|
102
|
+
});
|
|
103
|
+
const resJson = await res.json();
|
|
104
|
+
return resJson;
|
|
105
|
+
}
|
|
106
|
+
get profile() {
|
|
107
|
+
return {
|
|
108
|
+
get: async () => {
|
|
109
|
+
const res = await this.get("/auth/profile");
|
|
110
|
+
return res.result;
|
|
111
|
+
},
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
get account() {
|
|
115
|
+
return {
|
|
116
|
+
create: async (domain, plantName) => {
|
|
117
|
+
const res = await this.post("/account/new", { domain, plantName });
|
|
118
|
+
return res.result;
|
|
119
|
+
},
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
get apiKey() {
|
|
123
|
+
return {
|
|
124
|
+
create: async () => {
|
|
125
|
+
const res = await this.post("/api-key", {});
|
|
126
|
+
return res.result;
|
|
127
|
+
},
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
get plant() {
|
|
131
|
+
return {
|
|
132
|
+
get: async (plantCode) => {
|
|
133
|
+
const res = await this.get("/plant", { plantCode });
|
|
134
|
+
return res.result;
|
|
135
|
+
},
|
|
136
|
+
createUpdate: async (station) => {
|
|
137
|
+
const res = await this.post("/station", station);
|
|
138
|
+
return res.result;
|
|
139
|
+
},
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
get station() {
|
|
143
|
+
return {
|
|
144
|
+
get: async (stationCode) => {
|
|
145
|
+
const res = await this.get("/station", { stationcodes: stationCode });
|
|
146
|
+
return res.results;
|
|
147
|
+
},
|
|
148
|
+
createUpdate: async (station) => {
|
|
149
|
+
const res = await this.post("/station", station);
|
|
150
|
+
return res.result;
|
|
151
|
+
},
|
|
152
|
+
signal: (stationCode, payload) => {
|
|
153
|
+
throw new Error("noop");
|
|
154
|
+
},
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
on(event, listener) {
|
|
158
|
+
let set = this.listenersByEventType.get(event);
|
|
159
|
+
if (!set) {
|
|
160
|
+
set = new Set();
|
|
161
|
+
this.listenersByEventType.set(event, set);
|
|
162
|
+
}
|
|
163
|
+
set.add(listener);
|
|
164
|
+
}
|
|
165
|
+
off(event, listener) {
|
|
166
|
+
const set = this.listenersByEventType.get(event);
|
|
167
|
+
if (!set) {
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
set.delete(listener);
|
|
171
|
+
if (set.size === 0) {
|
|
172
|
+
this.listenersByEventType.delete(event);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
exports.Client = Client;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.test.d.ts","sourceRoot":"","sources":["../src/client.test.ts"],"names":[],"mappings":""}
|