busroot-sdk 0.0.5 → 0.0.7-alpha

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 CHANGED
@@ -1,5 +1,6 @@
1
- import { PlantSchema, PlantViewModel, ScheduleViewModel, BusrootSignals, SkuViewModel, StationSchema, StationViewModel, AccountViewModel, ApiKeyViewModel, AggregatedStationWindowsByStation } from "./busroot";
2
- import mqtt from "mqtt/dist/mqtt.min";
1
+ import z from "zod";
2
+ import { PlantSchema, PlantViewModel, ScheduleViewModel, BusrootSignals, SkuViewModel, StationSchema, StationViewModel, AccountViewModel, ApiKeyViewModel, AggregatedStationWindowsByStation, ProductionViewModel } from "./types";
3
+ import mqtt from "mqtt";
3
4
  export declare const MqttEventType: {
4
5
  StationMetricNew: string;
5
6
  StationChanged: string;
@@ -10,23 +11,35 @@ export declare const MqttEventType: {
10
11
  };
11
12
  export type MqttEventType = keyof typeof MqttEventType;
12
13
  export type Listener = (msg: any) => void;
14
+ export declare const Config: z.ZodObject<{
15
+ accountId: z.ZodString;
16
+ apiKey: z.ZodString;
17
+ apiEndpoint: z.ZodString;
18
+ mqttHost: z.ZodString;
19
+ enableMqtt: z.ZodOptional<z.ZodBoolean>;
20
+ enableDebug: z.ZodOptional<z.ZodBoolean>;
21
+ }, "strip", z.ZodTypeAny, {
22
+ accountId: string;
23
+ apiKey: string;
24
+ apiEndpoint: string;
25
+ mqttHost: string;
26
+ enableMqtt?: boolean | undefined;
27
+ enableDebug?: boolean | undefined;
28
+ }, {
29
+ accountId: string;
30
+ apiKey: string;
31
+ apiEndpoint: string;
32
+ mqttHost: string;
33
+ enableMqtt?: boolean | undefined;
34
+ enableDebug?: boolean | undefined;
35
+ }>;
36
+ export type Config = z.infer<typeof Config>;
13
37
  declare class BusrootClient {
14
- config: {
15
- accountId: string;
16
- apiKey: string;
17
- apiEndpoint: string;
18
- mqttHost?: string;
19
- enableMqtt?: boolean;
20
- };
38
+ config: Config;
21
39
  mqttClient: mqtt.MqttClient | undefined;
22
40
  listenersByEventType: Map<"StationMetricNew" | "StationChanged" | "ScheduleChanged" | "ScheduleStarted" | "ScheduleEnded" | "ProductionNew", Set<Listener>>;
23
- constructor(config: {
24
- accountId: string;
25
- apiKey: string;
26
- apiEndpoint: string;
27
- mqttHost?: string;
28
- enableMqtt?: boolean;
29
- });
41
+ static version: string;
42
+ constructor(config: Config);
30
43
  destroy(): void;
31
44
  private get;
32
45
  private post;
@@ -51,10 +64,22 @@ declare class BusrootClient {
51
64
  createUpdate: (station: Partial<StationSchema>) => Promise<StationViewModel>;
52
65
  signal: (stationCode: string, payload: BusrootSignals) => void;
53
66
  };
67
+ get production(): {
68
+ get: ({ plantCode, stationCodes, rangeCode, }: {
69
+ plantCode: string;
70
+ stationCodes?: string[];
71
+ rangeCode: string;
72
+ }) => Promise<ProductionViewModel[]>;
73
+ getRaw: ({ plantCode, stationCodes, rangeCode, }: {
74
+ plantCode: string;
75
+ stationCodes?: string[];
76
+ rangeCode: string;
77
+ }) => Promise<ProductionViewModel[]>;
78
+ };
54
79
  get metrics(): {
55
80
  get: ({ plantCode, stationCodes, rangeCode, aggregateWindow, }: {
56
81
  plantCode: string;
57
- stationCodes: string[];
82
+ stationCodes?: string[];
58
83
  rangeCode: string;
59
84
  aggregateWindow: string;
60
85
  }) => Promise<AggregatedStationWindowsByStation[]>;
@@ -71,3 +96,4 @@ declare class BusrootClient {
71
96
  off(event: MqttEventType, listener: Listener): void;
72
97
  }
73
98
  export { BusrootClient };
99
+ //# sourceMappingURL=client.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA,OAAO,CAAC,MAAM,KAAK,CAAC;AACpB,OAAO,EACL,WAAW,EACX,cAAc,EACd,iBAAiB,EACjB,cAAc,EACd,YAAY,EACZ,aAAa,EACb,gBAAgB,EAChB,gBAAgB,EAChB,eAAe,EACf,iCAAiC,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AAC;YACxB,SAAS,EAAE,MAAM,CAAC;SACnB,KAAG,OAAO,CAAC,mBAAmB,EAAE,CAAC;0DAa/B;YACD,SAAS,EAAE,MAAM,CAAC;YAClB,YAAY,CAAC,EAAE,MAAM,EAAE,CAAC;YACxB,SAAS,EAAE,MAAM,CAAC;SACnB,KAAG,OAAO,CAAC,mBAAmB,EAAE,CAAC;MAUrC;IAED,IAAI,OAAO;wEAOJ;YACD,SAAS,EAAE,MAAM,CAAC;YAClB,YAAY,CAAC,EAAE,MAAM,EAAE,CAAC;YACxB,SAAS,EAAE,MAAM,CAAC;YAClB,eAAe,EAAE,MAAM,CAAC;SACzB,KAAG,OAAO,CAAC,iCAAiC,EAAE,CAAC;MAWnD;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,aAAa,EAAE,CAAC"}
package/build/client.js CHANGED
@@ -3,8 +3,9 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.BusrootClient = exports.MqttEventType = void 0;
7
- const mqtt_min_1 = __importDefault(require("mqtt/dist/mqtt.min"));
6
+ exports.BusrootClient = exports.Config = exports.MqttEventType = void 0;
7
+ const zod_1 = __importDefault(require("zod"));
8
+ const mqtt_1 = __importDefault(require("mqtt"));
8
9
  exports.MqttEventType = {
9
10
  StationMetricNew: "STATION_METRIC_NEW", // When a new station window is created and saved to DB.
10
11
  StationChanged: "STATION_CHANGED", // When a station is added or edited. NOT IMPLEMENTED.
@@ -13,33 +14,24 @@ exports.MqttEventType = {
13
14
  ScheduleEnded: "SCHEDULE_ENDED", // When a schedule ends (actualEndAt). NOT IMPLEMENTED.
14
15
  ProductionNew: "PRODUCTION_NEW", // When new production is added. NOT IMPLEMENTED FOR MANUAL PRODUCTION YET.
15
16
  };
17
+ exports.Config = zod_1.default.object({
18
+ accountId: zod_1.default.string().max(255),
19
+ apiKey: zod_1.default.string().max(255),
20
+ apiEndpoint: zod_1.default.string().max(255),
21
+ mqttHost: zod_1.default.string().max(255),
22
+ enableMqtt: zod_1.default.boolean().optional(),
23
+ enableDebug: zod_1.default.boolean().optional(),
24
+ });
16
25
  class BusrootClient {
26
+ config;
27
+ mqttClient;
28
+ listenersByEventType = new Map();
29
+ static version = "v0";
17
30
  constructor(config) {
18
31
  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
32
  const trimmedAccountId = config.accountId.replace("account_", "");
41
33
  if (config.enableMqtt === true && config.mqttHost !== null) {
42
- this.mqttClient = mqtt_min_1.default.connect({
34
+ this.mqttClient = mqtt_1.default.connect({
43
35
  host: config.mqttHost,
44
36
  port: 8883,
45
37
  username: "api-key",
@@ -85,15 +77,20 @@ class BusrootClient {
85
77
  url +=
86
78
  "?" +
87
79
  Object.entries(query)
80
+ .filter((value) => value[1] != null)
88
81
  .map((value) => value[0] + "=" + value[1])
89
82
  .join("&");
90
83
  }
84
+ if (this.config.enableDebug === true) {
85
+ console.log("Get Request: " + url);
86
+ }
91
87
  const res = await fetch(url, {
92
88
  headers: { "x-api-key": this.config.apiKey },
93
89
  method: "GET",
94
90
  });
95
91
  if (res.status !== 200) {
96
- throw new Error(`${res.status}: ${res.statusText}`);
92
+ const resBody = await res.json();
93
+ throw new Error(`${res.status}: ${res.statusText}: ${resBody.errorCode}`);
97
94
  }
98
95
  const resJson = await res.json();
99
96
  return resJson;
@@ -105,7 +102,8 @@ class BusrootClient {
105
102
  body: JSON.stringify(body),
106
103
  });
107
104
  if (res.status !== 200) {
108
- throw new Error(`${res.status}: ${res.statusText}`);
105
+ const resBody = await res.json();
106
+ throw new Error(`${res.status}: ${res.statusText}: ${resBody.errorCode}`);
109
107
  }
110
108
  const resJson = await res.json();
111
109
  return resJson;
@@ -161,12 +159,32 @@ class BusrootClient {
161
159
  },
162
160
  };
163
161
  }
162
+ get production() {
163
+ return {
164
+ get: async ({ plantCode, stationCodes, rangeCode, }) => {
165
+ const res = await this.get("/production", {
166
+ plantcode: plantCode,
167
+ stationcodes: stationCodes?.join(","),
168
+ rangecode: rangeCode,
169
+ });
170
+ return res.results;
171
+ },
172
+ getRaw: async ({ plantCode, stationCodes, rangeCode, }) => {
173
+ const res = await this.get("/production/raw", {
174
+ plantcode: plantCode,
175
+ stationcodes: stationCodes?.join(","),
176
+ rangecode: rangeCode,
177
+ });
178
+ return res.results;
179
+ },
180
+ };
181
+ }
164
182
  get metrics() {
165
183
  return {
166
184
  get: async ({ plantCode, stationCodes, rangeCode, aggregateWindow, }) => {
167
185
  const res = await this.get("/station-windows", {
168
186
  plantcode: plantCode,
169
- stationcodes: stationCodes.join(","),
187
+ stationcodes: stationCodes?.join(","),
170
188
  rangecode: rangeCode,
171
189
  aggregatewindow: aggregateWindow,
172
190
  });
@@ -174,6 +192,26 @@ class BusrootClient {
174
192
  },
175
193
  };
176
194
  }
195
+ sku = {
196
+ get: async (skuCode) => {
197
+ const res = await this.get("/sku", { skucodes: skuCode });
198
+ return res.result;
199
+ },
200
+ createUpdate: async (sku) => {
201
+ const res = await this.post("/sku", sku);
202
+ return res.result;
203
+ },
204
+ };
205
+ schedule = {
206
+ get: async (scheduleId) => {
207
+ const res = await this.get("/schedule", { ids: String(scheduleId) });
208
+ return res.results;
209
+ },
210
+ createUpdate: async (schedule) => {
211
+ const res = await this.post("/schedule", schedule);
212
+ return res.result;
213
+ },
214
+ };
177
215
  on(event, listener) {
178
216
  let set = this.listenersByEventType.get(event);
179
217
  if (!set) {
@@ -1 +1,2 @@
1
1
  export {};
2
+ //# sourceMappingURL=client.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client.test.d.ts","sourceRoot":"","sources":["../src/client.test.ts"],"names":[],"mappings":""}
package/build/hooks.d.ts CHANGED
@@ -1,24 +1,8 @@
1
- import { BusrootClient } from "./client";
2
- import z from "zod";
3
- export declare const ConfigPayloadSchema: z.ZodObject<{
4
- accountId: z.ZodString;
5
- apiKey: z.ZodString;
6
- apiEndpoint: z.ZodString;
7
- mqttHost: z.ZodString;
8
- }, "strip", z.ZodTypeAny, {
9
- accountId: string;
10
- apiKey: string;
11
- apiEndpoint: string;
12
- mqttHost: string;
13
- }, {
14
- accountId: string;
15
- apiKey: string;
16
- apiEndpoint: string;
17
- mqttHost: string;
18
- }>;
1
+ import { BusrootClient, Config } from "./client";
19
2
  export declare function useBusroot(props?: {
20
- enableMqtt?: boolean;
3
+ config?: Config;
21
4
  }): {
22
5
  client: BusrootClient | undefined;
23
6
  logout: () => void;
24
7
  };
8
+ //# sourceMappingURL=hooks.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hooks.d.ts","sourceRoot":"","sources":["../src/hooks.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,aAAa,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAMjD,wBAAgB,UAAU,CAAC,KAAK,CAAC,EAAE;IAAE,MAAM,CAAC,EAAE,MAAM,CAAA;CAAE;;;EA2CrD"}
package/build/hooks.js CHANGED
@@ -1,45 +1,39 @@
1
1
  "use strict";
2
2
  "use client";
3
- var __importDefault = (this && this.__importDefault) || function (mod) {
4
- return (mod && mod.__esModule) ? mod : { "default": mod };
5
- };
6
3
  Object.defineProperty(exports, "__esModule", { value: true });
7
- exports.ConfigPayloadSchema = void 0;
8
4
  exports.useBusroot = useBusroot;
9
5
  const react_1 = require("react");
10
6
  const client_1 = require("./client");
11
- const zod_1 = __importDefault(require("zod"));
12
7
  // 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.
13
8
  // TODO: Account ID and API key should be saved to a local cookie and removed from the query param.
14
9
  // TODO: Add logout functionality, that clears the cookies.
15
- exports.ConfigPayloadSchema = zod_1.default.object({
16
- accountId: zod_1.default.string().max(255),
17
- apiKey: zod_1.default.string().max(255),
18
- apiEndpoint: zod_1.default.string().max(255),
19
- mqttHost: zod_1.default.string().max(255),
20
- });
21
10
  function useBusroot(props) {
22
- const enableMqtt = props && props.enableMqtt;
11
+ const enableMqtt = props && props.config != null && props.config.enableMqtt === true;
23
12
  const [client, setClient] = (0, react_1.useState)();
24
13
  (0, react_1.useEffect)(() => {
25
14
  if (typeof window === undefined) {
26
15
  return;
27
16
  }
28
- const searchParams = new URLSearchParams(window.location.search);
29
- const tokenParam = searchParams.get("busroot_token");
30
- if (tokenParam != null && client == null) {
31
- try {
32
- const config = exports.ConfigPayloadSchema.parse(JSON.parse(atob(tokenParam)));
33
- setClient(new client_1.BusrootClient({
34
- accountId: config.accountId,
35
- apiKey: config.apiKey,
36
- apiEndpoint: config.apiEndpoint,
37
- mqttHost: config.mqttHost,
38
- enableMqtt,
39
- }));
40
- }
41
- catch (e) {
42
- console.log(e.message);
17
+ if (props != null && props.config != null) {
18
+ setClient(new client_1.BusrootClient(props.config));
19
+ }
20
+ else {
21
+ const searchParams = new URLSearchParams(window.location.search);
22
+ const tokenParam = searchParams.get("busroot_token");
23
+ if (tokenParam != null && client == null) {
24
+ try {
25
+ const config = client_1.Config.parse(JSON.parse(atob(tokenParam)));
26
+ setClient(new client_1.BusrootClient({
27
+ accountId: config.accountId,
28
+ apiKey: config.apiKey,
29
+ apiEndpoint: config.apiEndpoint,
30
+ mqttHost: config.mqttHost,
31
+ enableMqtt,
32
+ }));
33
+ }
34
+ catch (e) {
35
+ console.log(e.message);
36
+ }
43
37
  }
44
38
  }
45
39
  return;
package/build/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from "./client";
2
2
  export * from "./hooks";
3
3
  export * from "./utils";
4
- export type { PlantSchema, PlantViewModel, ScheduleViewModel, BusrootSignals, SkuViewModel, StationSchema, StationViewModel, AccountViewModel, ApiKeyViewModel, } from "./busroot";
4
+ export type { PlantSchema, PlantViewModel, ScheduleViewModel, BusrootSignals, SkuViewModel, StationSchema, StationViewModel, AccountViewModel, ApiKeyViewModel, } from "./types";
5
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC;AACzB,cAAc,SAAS,CAAC;AACxB,cAAc,SAAS,CAAC;AAExB,YAAY,EACV,WAAW,EACX,cAAc,EACd,iBAAiB,EACjB,cAAc,EACd,YAAY,EACZ,aAAa,EACb,gBAAgB,EAChB,gBAAgB,EAChB,eAAe,GAChB,MAAM,SAAS,CAAC"}