UNPKG

@kvaser/canking-api

Version:

CanKing API to communicate with the CanKing service using Node.js.

603 lines (602 loc) 18.5 kB
var Y = Object.defineProperty; var Z = (e, t, s) => t in e ? Y(e, t, { enumerable: !0, configurable: !0, writable: !0, value: s }) : e[t] = s; var I = (e, t, s) => Z(e, typeof t != "symbol" ? t + "" : t, s); import { createContext as w, useState as m, useEffect as p, useRef as V, useCallback as E, useContext as P, useMemo as S } from "react"; import { v4 as h } from "uuid"; import "long"; import { e as U } from "./ElectronAPIFake.mjs"; import { S as z } from "./status_params.mjs"; import { jsx as u, jsxs as ee } from "react/jsx-runtime"; import { createTheme as B, ThemeProvider as se, CssBaseline as te } from "@mui/material"; import { getLogger as ne } from "./ipc.mjs"; import { defaultUserSettings as re, DataBuffer as ae } from "./models.mjs"; const oe = (e, t, ...s) => { let a = t; for (let r = 0; r < s.length; r += 1) { const n = `{${r}}`; if (a.indexOf(n) !== -1) { const c = s[r]; a = typeof c == "string" ? a.replace(n, c) : a.replace(n, c.toLocaleString(e)); } } return a; }; class ie { constructor(t, s) { I(this, "language"); I(this, "strings"); this.language = t, this.strings = s; } getString(t, ...s) { if (this.strings && t in this.strings) { const a = this.strings[t]; return s.length > 0 ? oe(this.language, a, ...s) : a; } return t; } } const d = ne("DataProviders"), l = window.electron ?? U, A = (e) => ({ palette: { mode: e, // primary: { // main: '#e4002b', // Kvaser red // }, // secondary: { // main: '#87d300', // Kvaser green // }, primary: { main: e === "light" ? "#006782" : "#03648A" // '#60D4FE', }, secondary: { main: e === "light" ? "#44439A" : "#AFA7FD" }, error: { main: e === "light" ? "#BA1A1A" : "#FD452F" }, divider: e === "light" ? "#33859B" : "#3583A1" }, spacing: 4, typography: { button: { lineHeight: 1, textTransform: void 0 }, fontSize: 12 }, shape: { borderRadius: 4 }, components: { MuiButton: { defaultProps: { size: "small" } }, MuiFilledInput: { defaultProps: { margin: "dense" } }, MuiFormControl: { defaultProps: { margin: "dense" } }, MuiFormHelperText: { defaultProps: { margin: "dense" } }, MuiIconButton: { defaultProps: { size: "small" } }, MuiInputBase: { defaultProps: { margin: "dense" } }, MuiInputLabel: { defaultProps: { margin: "dense" } }, MuiListItem: { defaultProps: { dense: !0 } }, MuiOutlinedInput: { defaultProps: { margin: "dense" } }, MuiFab: { defaultProps: { size: "small" } }, MuiTable: { defaultProps: { size: "small" } }, MuiTextField: { defaultProps: { margin: "dense" } }, MuiToolbar: { defaultProps: { variant: "dense" } } } }); function ce({ children: e }) { const t = M(), [s, a] = m(B(A(t.theme))); return p(() => { a(B(A(t.theme))); }, [t.theme]), /* @__PURE__ */ ee(se, { theme: s, children: [ /* @__PURE__ */ u(te, {}), e ] }); } const T = /* @__PURE__ */ new Map(), L = /* @__PURE__ */ new Map(), R = /* @__PURE__ */ new Map(), D = /* @__PURE__ */ new Map(), C = /* @__PURE__ */ new Map(), x = /* @__PURE__ */ new Map(), F = /* @__PURE__ */ new Map(), O = /* @__PURE__ */ new Map(), y = /* @__PURE__ */ new Map(), j = /* @__PURE__ */ new Map(); async function v(e, t, s, a) { switch (t) { case "devices-changed": { if (T.has(e)) d.warn("The callback has already been added as listener to this event!"); else { const r = s, n = (c, i) => r(i), o = l.ipcRenderer.on("dm-devices-changed", n); T.set(e, { remover: o, listener: n }); } return l.ipcRenderer.invoke("rpc-get-devices", !1); } case "user-settings-changed": { if (L.has(e)) d.warn("The callback has already been added as listener to this event!"); else { const r = s, n = (c, i) => r(i), o = l.ipcRenderer.on("user-settings-changed", n); L.set(e, { remover: o, listener: n }); } return l.ipcRenderer.invoke("app-get-user-settings"); } case "new-log-messages": { if (R.has(e)) d.warn("The callback has already been added as listener to this event!"); else { const r = s, n = (c, i) => r(i), o = l.ipcRenderer.on("new-log-messages", n); R.set(e, { remover: o, listener: n }); } return Promise.resolve(); } case "online-status-changed": { if (D.has(e)) d.warn("The callback has already been added as listener to this event!"); else { const r = s, n = (c, i) => r(i), o = l.ipcRenderer.on("mm-is-online-changed", n); D.set(e, { remover: o, listener: n }); } return l.ipcRenderer.invoke("rpc-get-online-status"); } case "periodic-trans-started": { if (C.has(e)) d.warn("The callback has already been added as listener to this event!"); else { const r = s, n = (c, i) => r(i), o = l.ipcRenderer.on("mm-periodic-trans-started", n); C.set(e, { remover: o, listener: n }); } return Promise.resolve(""); } case "periodic-trans-stopped": { if (x.has(e)) d.warn("The callback has already been added as listener to this event!"); else { const r = s, n = (c, i) => r(i), o = l.ipcRenderer.on("mm-periodic-trans-stopped", n); x.set(e, { remover: o, listener: n }); } return Promise.resolve(""); } case "new-measurement-data": { if (F.has(e)) d.warn("The callback has already been added as listener to this event!"); else { const r = s, n = (c, i) => r(i), o = l.ipcRenderer.on("mm-new-measurement-data", n); F.set(e, { remover: o, listener: n }); } return (a == null ? void 0 : a.numberOfHistoryFrames) > 0 ? l.ipcRenderer.invoke("mm-get-latest-measurement-data", a.numberOfHistoryFrames) : Promise.resolve([]); } case "new-measurement-status": { if (O.has(e)) d.warn("The callback has already been added as listener to this event!"); else { const r = s, n = (c, i) => r(i), o = l.ipcRenderer.on("mm-new-measurement-status", n); O.set(e, { remover: o, listener: n }); } return l.ipcRenderer.invoke("rpc-get-node-statuses"); } case "meas-setup-changed": { if (y.has(e)) d.warn("The callback has already been added as listener to this event!"); else { const r = s, n = (c, i) => r(i), o = l.ipcRenderer.on("project-meas-setup-changed", n); y.set(e, { remover: o, listener: n }); } return l.ipcRenderer.invoke("prj-get-meas-setup"); } case "workspace-panes-changed": { if (j.has(e)) d.warn("The callback has already been added as listener to this event!"); else { const r = s, n = (c, i) => r(i), o = l.ipcRenderer.on("project-workspace-panes-changed", n); j.set(e, { remover: o, listener: n }); } return l.ipcRenderer.invoke("prj-get-workspace-panes"); } } return Promise.reject(new Error("Invalid event type")); } const f = async (e, t) => { switch (t) { case "devices-changed": { const s = T.get(e); s ? (s.remover(), T.delete(e)) : d.warn("The callback has not been added as listener to this event!"); } break; case "user-settings-changed": { const s = L.get(e); s ? (s.remover(), L.delete(e)) : d.warn("The callback has not been added as listener to this event!"); } break; case "new-log-messages": { const s = R.get(e); s ? (s.remover(), R.delete(e)) : d.warn("The callback has not been added as listener to this event!"); } break; case "online-status-changed": { const s = D.get(e); s ? (s.remover(), D.delete(e)) : d.warn("The callback has not been added as listener to this event!"); } break; case "periodic-trans-started": { const s = C.get(e); s ? (s.remover(), C.delete(e)) : d.warn("The callback has not been added as listener to this event!"); } break; case "periodic-trans-stopped": { const s = x.get(e); s ? (s.remover(), x.delete(e)) : d.warn("The callback has not been added as listener to this event!"); } break; case "new-measurement-data": { const s = F.get(e); s ? (s.remover(), F.delete(e)) : d.warn("The callback has not been added as listener to this event!"); } break; case "new-measurement-status": { const s = O.get(e); s ? (s.remover(), O.delete(e)) : d.warn("The callback has not been added as listener to this event!"); } break; case "meas-setup-changed": { const s = y.get(e); s ? (s.remover(), y.delete(e)) : d.warn("The callback has not been added as listener to this event!"); } break; case "workspace-panes-changed": { const s = j.get(e); s ? (s.remover(), j.delete(e)) : d.warn("The callback has not been added as listener to this event!"); } break; } }, de = window != null && window.matchMedia ? window.matchMedia("(prefers-color-scheme: dark)").matches : !1, W = Object.assign({}, re, { theme: de ? "dark" : "light" }), H = w(W), le = ({ initialUserSettings: e = void 0, children: t = void 0 }) => { const [s, a] = m(e ?? W); return p(() => { const r = h(); return v(r, "user-settings-changed", a).then((n) => a(n)).catch(), () => { f(r, "user-settings-changed"); }; }, []), /* @__PURE__ */ u(H.Provider, { value: s, children: t }); }, J = w(null), ue = ({ children: e = void 0 }) => { const [t, s] = m(null); return p(() => { const a = (n) => { try { s(n); } catch (o) { d.error("DevicesProvider", o); } }, r = h(); return v(r, "devices-changed", a).then((n) => a(n)).catch(), () => { f(r, "devices-changed"); }; }, []), /* @__PURE__ */ u(J.Provider, { value: t, children: e }); }, K = w([]), ge = ({ children: e = void 0 }) => { const [t, s] = m([]); return p(() => { const a = (n) => { try { s(n); } catch (o) { d.error("MeasurementStatusProvider", o); } }, r = h(); return v(r, "new-measurement-status", a).then((n) => a(n)).catch(), () => { f(r, "new-measurement-status"); }; }, []), /* @__PURE__ */ u(K.Provider, { value: t, children: e }); }, $ = w({ language: "en", sources: [], dataProcessors: [], targets: [] }), me = ({ children: e = void 0 }) => { const [t, s] = m({ language: "en", sources: [], dataProcessors: [], targets: [] }); return p(() => { const a = (n) => { try { s(n); } catch (o) { d.error("MeasurementSetupProvider", o); } }, r = h(); return v(r, "meas-setup-changed", a).then((n) => a(n)).catch(), () => { f(r, "meas-setup-changed"); }; }, []), /* @__PURE__ */ u($.Provider, { value: t, children: e }); }, q = w({ isOnline: !1 }), pe = ({ children: e = void 0 }) => { const [t, s] = m({ isOnline: !1 }); return p(() => { const a = (n) => { try { s(n); } catch (o) { d.error("OnlineStatusProvider", o); } }, r = h(); return v(r, "online-status-changed", a).then((n) => a(n)).catch(), () => { f(r, "online-status-changed"); }; }, []), /* @__PURE__ */ u(q.Provider, { value: t, children: e }); }, Q = w([]), he = ({ children: e = void 0 }) => { const [t, s] = m([]); return p(() => { const a = (c) => { try { s((i) => [...i, c]); } catch (i) { d.error("RunningPeriodicTransmissionsProvider - started", i); } }, r = (c) => { try { s((i) => { const g = i.findIndex((k) => k === c); if (g === -1) return i; const b = [...i]; return b.splice(g, 1), b; }); } catch (i) { d.error("RunningPeriodicTransmissionsProvider - stopped", i); } }, n = h(); v(n, "periodic-trans-started", a); const o = h(); return v(o, "periodic-trans-stopped", r), () => { f(n, "periodic-trans-started"), f(o, "periodic-trans-stopped"); }; }, []), /* @__PURE__ */ u(Q.Provider, { value: t, children: e }); }, G = w([]), ve = ({ children: e = void 0 }) => { const t = V(new ae(1e3)), [s, a] = m([]); return p(() => { const r = (o) => { try { o === null ? t.current.clear() : t.current.push(...o), a(Array.from(t.current)); } catch (c) { d.error("LogMessagesProvider", c); } }, n = h(); return v(n, "new-log-messages", r), () => { f(n, "new-log-messages"); }; }, []), /* @__PURE__ */ u(G.Provider, { value: s, children: e }); }, _ = w([[], () => { }]), fe = ({ children: e = void 0 }) => { const [t, s] = m(null), a = E( (r) => { if (t !== null) if (typeof r == "function") { const n = r(t); l.ipcRenderer.invoke("prj-update-project", { workspacePanes: n }); } else l.ipcRenderer.invoke("prj-update-project", { workspacePanes: r }); }, [t] ); return p(() => { const r = (o) => { try { s(o); } catch (c) { d.error("WorkspacePanesProvider", c); } }, n = h(); return v(n, "workspace-panes-changed", r).then((o) => r(o)).catch(), () => { f(n, "workspace-panes-changed"); }; }, []), /* @__PURE__ */ u(_.Provider, { value: [t, a], children: e }); }, xe = ({ initialUserSettings: e = void 0, children: t = void 0 }) => /* @__PURE__ */ u(le, { initialUserSettings: e, children: /* @__PURE__ */ u(ue, { children: /* @__PURE__ */ u(me, { children: /* @__PURE__ */ u(ge, { children: /* @__PURE__ */ u(pe, { children: /* @__PURE__ */ u(he, { children: /* @__PURE__ */ u(ve, { children: /* @__PURE__ */ u(fe, { children: /* @__PURE__ */ u(ce, { children: t }) }) }) }) }) }) }) }) }), N = window.electron ?? U, M = () => P(H), Fe = () => { const e = M(); return S(() => e.useHexNumericBase ? 16 : 10, [e.useHexNumericBase]); }, Oe = () => { const e = M(); return S(() => e.maxDataBytes, [e.maxDataBytes]); }, ye = () => { const e = M(); return S(() => e.language, [e.language]); }, je = () => { const e = M(); return S( () => new ie(e.language, e.strings), [e.language, e.strings] ); }, Ne = () => { const e = M(); return S(() => e.recentProjectFiles, [e.recentProjectFiles]); }, Ie = () => P(J), ze = () => P($), X = () => P(K), _e = (e) => { const t = X(); return S(() => t.find((a) => a.nodeId === e) ?? null, [e, t]); }, Be = () => { const e = X(); return S(() => { const s = e.filter((a) => a.busStatus).map((a) => Math.max( Number(a.busStatus.busOnStatus) ?? z.STATUS_LEVEL_NONE, Number(a.busStatus.errorStatus) ?? z.STATUS_LEVEL_NONE, Number(a.busStatus.overrunStatus) ?? z.STATUS_LEVEL_NONE )); return Math.max(...s); }, [e]); }, Se = () => P(q), Ae = () => { const e = Se(); return S(() => (e == null ? void 0 : e.isOnline) === !0, [e == null ? void 0 : e.isOnline]); }, Ee = () => P(Q), Ue = () => P(G), We = (e, t) => { const [s, a] = m([]), [r, n] = m([]); return p(() => { const o = h(); return v(o, "new-measurement-data", n, { numberOfHistoryFrames: e }).then((c) => n(c)).catch(), () => { f(o, "new-measurement-data"); }; }, []), p(() => { r !== null && a((o) => { const c = o.map((g) => g.hashCode), i = r.filter((g) => !c.includes(g.hashCode)); if (t) { const g = i.filter(t); return o.length === 0 && g.length === 0 ? o : g; } return o.length === 0 && i.length === 0 ? o : i; }); }, [r, t]), s; }, He = (e, t) => { const [s, a] = P(_), [r, n] = S(() => { var c; try { if (s === null) return [!1, t ?? {}]; const i = s == null ? void 0 : s.find((b) => b.id === e), g = (c = i == null ? void 0 : i.componentData) != null && c.componentProps ? JSON.parse(i.componentData.componentProps) : {}; return t !== void 0 ? [!0, Object.assign({}, t, g)] : [!0, g]; } catch { return [!1, t ?? {}]; } }, [t, e, s]), o = E( (c) => { try { a((i) => { const g = [...i], b = g.find((k) => k.id === e); if (b != null && b.componentData) { const k = c instanceof Function ? JSON.stringify(c(n)) : JSON.stringify(c); return b.componentData.componentProps = k, g; } return i; }); } catch { } }, [e, n, a] ); return { projectDataIsInitialized: r, projectData: n, setProjectData: o }; }, Je = (e, t) => { const [s, a] = m(!1), [r, n] = m(t ?? {}); return p(() => { N.ipcRenderer.invoke("app-get-workspace-view-session-data", e).then((o) => { try { const c = o ? JSON.parse(o) : {}; t !== void 0 ? (n(Object.assign({}, t, c)), a(!0)) : (n(c), a(!0)); } catch { a(!1), n(t ?? {}); } }).catch(); }, [t, e]), p(() => { try { if (s) { const o = JSON.stringify(r); N.ipcRenderer.invoke("app-set-workspace-view-session-data", e, o).catch(); } } catch { } }, [e, r, s]), { sessionDataIsInitialized: s, sessionData: r, setSessionData: n }; }, Ke = (e) => { const [t] = P(_); return S(() => t.find((a) => a.id === e) ?? null, [e, t]); }, $e = (e = !1) => { const [t, s] = m([]); return p(() => { N.ipcRenderer.invoke("rpc-get-message-log-file-formats", e).then((a) => s(a)).catch(); }, [e]), t; }, qe = () => { const [e, t] = m([]); return p(() => { N.ipcRenderer.invoke("rpc-get-signal-log-file-formats").then((s) => t(s)).catch(); }, []), e; }; export { xe as C, J as D, ie as L, K as M, q as O, Q as R, H as U, _ as W, ye as a, v as b, Fe as c, ze as d, X as e, $ as f, G as g, M as h, Oe as i, Ne as j, Ie as k, _e as l, Be as m, Se as n, Ae as o, Ee as p, Ue as q, f as r, We as s, He as t, je as u, Je as v, Ke as w, $e as x, qe as y };