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.
@@ -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 "./common";
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
- host: string;
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
- host: string;
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
- 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()));
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
- catch (e) {
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
- 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/#`);
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.end();
78
+ if (this.mqttClient != null) {
79
+ this.mqttClient.end();
80
+ }
77
81
  }
78
82
  async get(path, query) {
79
- let url = this.apiUrl + path;
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.apiUrl + path, {
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
- host: z.ZodString;
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
- host: zod_1.default.string().max(255),
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({ accountId: config.accountId, apiKey: config.apiKey, host: config.host }));
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
@@ -1,2 +1,3 @@
1
1
  export * from "./client";
2
2
  export * from "./hooks";
3
+ export type { PlantSchema, PlantViewModel, ScheduleViewModel, BusrootSignals, SkuViewModel, StationSchema, StationViewModel, AccountViewModel, ApiKeyViewModel, } from "./busroot";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "busroot-sdk",
3
- "version": "0.0.2",
3
+ "version": "0.0.3",
4
4
  "description": "An SDK for accessing Busroot from output.industries",
5
5
  "homepage": "https://www.output.industries",
6
6
  "main": "./build/index.js",
@@ -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();