busroot-sdk 0.0.2 → 0.0.3
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/{common.d.ts → busroot.d.ts} +1 -0
- package/build/client.d.ts +8 -5
- package/build/client.js +38 -34
- package/build/hooks.d.ts +9 -4
- package/build/hooks.js +13 -3
- package/build/index.d.ts +1 -0
- package/package.json +1 -1
- package/build/example.d.ts +0 -1
- package/build/example.js +0 -31
|
@@ -170,6 +170,7 @@ declare const ScheduleViewModel = z.object({
|
|
|
170
170
|
idealEndAtTimestamp: z.number().optional(),
|
|
171
171
|
quantityGood: z.number().optional(),
|
|
172
172
|
quantityBad: z.number().optional(),
|
|
173
|
+
quantityTotal: z.number().optional(),
|
|
173
174
|
skuGroupCode: z.string().optional(),
|
|
174
175
|
skuGroupName: z.string().optional(),
|
|
175
176
|
extendShift: z.boolean().optional(),
|
package/build/client.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { PlantSchema, PlantViewModel, ScheduleViewModel, BusrootSignals, SkuViewModel, StationSchema, StationViewModel, AccountViewModel, ApiKeyViewModel } from "./
|
|
1
|
+
import { PlantSchema, PlantViewModel, ScheduleViewModel, BusrootSignals, SkuViewModel, StationSchema, StationViewModel, AccountViewModel, ApiKeyViewModel } from "./busroot";
|
|
2
2
|
import mqtt from "mqtt/dist/mqtt.esm";
|
|
3
3
|
export declare const MqttEventType: {
|
|
4
4
|
StationMetricNew: string;
|
|
@@ -14,15 +14,18 @@ declare class Client {
|
|
|
14
14
|
config: {
|
|
15
15
|
accountId: string;
|
|
16
16
|
apiKey: string;
|
|
17
|
-
|
|
17
|
+
endpoint: string;
|
|
18
|
+
mqttHost?: string;
|
|
19
|
+
enableMqtt?: boolean;
|
|
18
20
|
};
|
|
19
|
-
mqttClient: mqtt.MqttClient;
|
|
21
|
+
mqttClient: mqtt.MqttClient | undefined;
|
|
20
22
|
listenersByEventType: Map<"StationMetricNew" | "StationChanged" | "ScheduleChanged" | "ScheduleStarted" | "ScheduleEnded" | "ProductionNew", Set<Listener>>;
|
|
21
|
-
apiUrl: string;
|
|
22
23
|
constructor(config: {
|
|
23
24
|
accountId: string;
|
|
24
25
|
apiKey: string;
|
|
25
|
-
|
|
26
|
+
endpoint: string;
|
|
27
|
+
mqttHost?: string;
|
|
28
|
+
enableMqtt?: boolean;
|
|
26
29
|
});
|
|
27
30
|
destroy(): void;
|
|
28
31
|
private get;
|
package/build/client.js
CHANGED
|
@@ -37,46 +37,50 @@ class Client {
|
|
|
37
37
|
return res.result;
|
|
38
38
|
},
|
|
39
39
|
};
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
for (const listener of listeners) {
|
|
55
|
-
try {
|
|
56
|
-
listener(JSON.parse(payload.toString()));
|
|
40
|
+
const trimmedAccountId = config.accountId.replace("account_", "");
|
|
41
|
+
if (config.enableMqtt === true && config.mqttHost !== null) {
|
|
42
|
+
this.mqttClient = mqtt_esm_1.default.connect({
|
|
43
|
+
host: config.mqttHost,
|
|
44
|
+
port: 8883,
|
|
45
|
+
username: "api-key",
|
|
46
|
+
password: this.config.apiKey,
|
|
47
|
+
clientId: trimmedAccountId + "/sdk",
|
|
48
|
+
});
|
|
49
|
+
this.mqttClient.on("message", (topic, payload) => {
|
|
50
|
+
const eventType = topic.toUpperCase().split("/").at(-1);
|
|
51
|
+
for (const [listenerEventType, listeners] of this.listenersByEventType) {
|
|
52
|
+
if (exports.MqttEventType[listenerEventType] !== eventType) {
|
|
53
|
+
continue;
|
|
57
54
|
}
|
|
58
|
-
|
|
59
|
-
|
|
55
|
+
for (const listener of listeners) {
|
|
56
|
+
try {
|
|
57
|
+
listener(JSON.parse(payload.toString()));
|
|
58
|
+
}
|
|
59
|
+
catch (e) {
|
|
60
|
+
//
|
|
61
|
+
}
|
|
60
62
|
}
|
|
61
63
|
}
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
64
|
+
});
|
|
65
|
+
this.mqttClient.on("error", (error) => {
|
|
66
|
+
console.error(error);
|
|
67
|
+
});
|
|
68
|
+
this.mqttClient.on("connect", () => {
|
|
69
|
+
console.log("Connected to Busroot MQTT...");
|
|
70
|
+
});
|
|
71
|
+
this.mqttClient.on("reconnect", () => {
|
|
72
|
+
console.log("Disconnected from Busroot MQTT...");
|
|
73
|
+
});
|
|
74
|
+
this.mqttClient.subscribe(`busroot/v2/${trimmedAccountId}/events/#`);
|
|
75
|
+
}
|
|
74
76
|
}
|
|
75
77
|
destroy() {
|
|
76
|
-
this.mqttClient
|
|
78
|
+
if (this.mqttClient != null) {
|
|
79
|
+
this.mqttClient.end();
|
|
80
|
+
}
|
|
77
81
|
}
|
|
78
82
|
async get(path, query) {
|
|
79
|
-
let url = this.
|
|
83
|
+
let url = this.config.endpoint + path;
|
|
80
84
|
if (query != null) {
|
|
81
85
|
url +=
|
|
82
86
|
"?" +
|
|
@@ -95,7 +99,7 @@ class Client {
|
|
|
95
99
|
return resJson;
|
|
96
100
|
}
|
|
97
101
|
async post(path, body) {
|
|
98
|
-
const res = await fetch(this.
|
|
102
|
+
const res = await fetch(this.config.endpoint + path, {
|
|
99
103
|
headers: { "x-api-key": this.config.apiKey, "Content-Type": "application/json" },
|
|
100
104
|
method: "POST",
|
|
101
105
|
body: JSON.stringify(body),
|
package/build/hooks.d.ts
CHANGED
|
@@ -3,17 +3,22 @@ import z from "zod";
|
|
|
3
3
|
export declare const ConfigPayloadSchema: z.ZodObject<{
|
|
4
4
|
accountId: z.ZodString;
|
|
5
5
|
apiKey: z.ZodString;
|
|
6
|
-
|
|
6
|
+
endpoint: z.ZodString;
|
|
7
|
+
mqttHost: z.ZodString;
|
|
7
8
|
}, "strip", z.ZodTypeAny, {
|
|
8
9
|
accountId: string;
|
|
9
|
-
host: string;
|
|
10
10
|
apiKey: string;
|
|
11
|
+
endpoint: string;
|
|
12
|
+
mqttHost: string;
|
|
11
13
|
}, {
|
|
12
14
|
accountId: string;
|
|
13
|
-
host: string;
|
|
14
15
|
apiKey: string;
|
|
16
|
+
endpoint: string;
|
|
17
|
+
mqttHost: string;
|
|
15
18
|
}>;
|
|
16
|
-
export declare function useBusroot(
|
|
19
|
+
export declare function useBusroot(props?: {
|
|
20
|
+
enableMqtt?: boolean;
|
|
21
|
+
}): {
|
|
17
22
|
client: Client | undefined;
|
|
18
23
|
logout: () => void;
|
|
19
24
|
};
|
package/build/hooks.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
"use client";
|
|
2
3
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
4
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
5
|
};
|
|
@@ -8,14 +9,17 @@ exports.useBusroot = useBusroot;
|
|
|
8
9
|
const react_1 = require("react");
|
|
9
10
|
const client_1 = require("./client");
|
|
10
11
|
const zod_1 = __importDefault(require("zod"));
|
|
12
|
+
// TODO: Should accept a JWT token with a limited expiry time, instead of an API key. MQTT auth will need to be altered to make this work.
|
|
11
13
|
// TODO: Account ID and API key should be saved to a local cookie and removed from the query param.
|
|
12
14
|
// TODO: Add logout functionality, that clears the cookies.
|
|
13
15
|
exports.ConfigPayloadSchema = zod_1.default.object({
|
|
14
16
|
accountId: zod_1.default.string().max(255),
|
|
15
17
|
apiKey: zod_1.default.string().max(255),
|
|
16
|
-
|
|
18
|
+
endpoint: zod_1.default.string().max(255),
|
|
19
|
+
mqttHost: zod_1.default.string().max(255),
|
|
17
20
|
});
|
|
18
|
-
function useBusroot() {
|
|
21
|
+
function useBusroot(props) {
|
|
22
|
+
const enableMqtt = props && props.enableMqtt;
|
|
19
23
|
const [client, setClient] = (0, react_1.useState)();
|
|
20
24
|
(0, react_1.useEffect)(() => {
|
|
21
25
|
if (typeof window === undefined) {
|
|
@@ -26,7 +30,13 @@ function useBusroot() {
|
|
|
26
30
|
if (tokenParam != null && client == null) {
|
|
27
31
|
try {
|
|
28
32
|
const config = exports.ConfigPayloadSchema.parse(JSON.parse(atob(tokenParam)));
|
|
29
|
-
setClient(new client_1.Client({
|
|
33
|
+
setClient(new client_1.Client({
|
|
34
|
+
accountId: config.accountId,
|
|
35
|
+
apiKey: config.apiKey,
|
|
36
|
+
endpoint: config.endpoint,
|
|
37
|
+
mqttHost: config.mqttHost,
|
|
38
|
+
enableMqtt,
|
|
39
|
+
}));
|
|
30
40
|
}
|
|
31
41
|
catch (e) {
|
|
32
42
|
console.log(e.message);
|
package/build/index.d.ts
CHANGED
package/package.json
CHANGED
package/build/example.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/build/example.js
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
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
|
-
const moment_1 = __importDefault(require("moment"));
|
|
7
|
-
const client_1 = require("./client");
|
|
8
|
-
async function main() {
|
|
9
|
-
const busrootOiClient = new client_1.Client({ accountId: "account_000", apiKey: "admin_api_key", host: "localhost" });
|
|
10
|
-
// const profile = await busroot.profile.get();
|
|
11
|
-
// console.log(profile);
|
|
12
|
-
const account = await busrootOiClient.account.create("opind.co", "London");
|
|
13
|
-
// console.log(account);
|
|
14
|
-
const busrootClient = new client_1.Client({ accountId: account.account.id, apiKey: account.apiKey, host: "localhost" });
|
|
15
|
-
// const profile = await busrootClient.profile.get();
|
|
16
|
-
// console.log(profile);
|
|
17
|
-
const station1 = await busrootClient.station.createUpdate({ code: "machine1", name: "Machine 1", groupCode: "unknown" });
|
|
18
|
-
const sku1 = await busrootClient.sku.createUpdate({ code: "PRODUCT1", name: "Product 1" });
|
|
19
|
-
const { id: scheduleId } = await busrootClient.schedule.createUpdate({
|
|
20
|
-
stationCode: station1.code,
|
|
21
|
-
skuCode: sku1.code,
|
|
22
|
-
plannedStartAt: (0, moment_1.default)().toISOString(),
|
|
23
|
-
});
|
|
24
|
-
console.log(scheduleId);
|
|
25
|
-
// busrootClient.station.signal(station1.code, { production_complete: 1 });
|
|
26
|
-
const schedule = await busrootClient.schedule.get(scheduleId);
|
|
27
|
-
console.log(schedule[0]);
|
|
28
|
-
busrootOiClient.destroy();
|
|
29
|
-
busrootClient.destroy();
|
|
30
|
-
}
|
|
31
|
-
main();
|