@ramme-io/kernel 7.2.0 → 7.3.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.
@@ -0,0 +1,214 @@
1
+ import { S, h as E } from "./index-kBkq1FSR.js";
2
+ import f from "react";
3
+ const p = (t) => {
4
+ let s;
5
+ const e = /* @__PURE__ */ new Set(), n = (r, u) => {
6
+ const o = typeof r == "function" ? r(s) : r;
7
+ if (!Object.is(o, s)) {
8
+ const _ = s;
9
+ s = u ?? (typeof o != "object" || o === null) ? o : Object.assign({}, s, o), e.forEach((d) => d(s, _));
10
+ }
11
+ }, i = () => s, l = { setState: n, getState: i, getInitialState: () => m, subscribe: (r) => (e.add(r), () => e.delete(r)) }, m = s = t(n, i, l);
12
+ return l;
13
+ }, v = (t) => t ? p(t) : p, A = (t) => t;
14
+ function L(t, s = A) {
15
+ const e = f.useSyncExternalStore(
16
+ t.subscribe,
17
+ f.useCallback(() => s(t.getState()), [t, s]),
18
+ f.useCallback(() => s(t.getInitialState()), [t, s])
19
+ );
20
+ return f.useDebugValue(e), e;
21
+ }
22
+ const y = (t) => {
23
+ const s = v(t), e = (n) => L(s, n);
24
+ return Object.assign(e, s), e;
25
+ }, I = (t) => t ? y(t) : y, k = 1e3 / 15, g = /* @__PURE__ */ new Map();
26
+ let h = 0;
27
+ const b = I((t, s) => ({
28
+ signals: {},
29
+ lastUpdated: null,
30
+ setSignal: (e) => {
31
+ const n = S.safeParse(e);
32
+ if (!n.success) {
33
+ console.warn(
34
+ "[SignalStore] Rejected invalid signal packet — does not conform to Law 12 SignalSchema.",
35
+ `
36
+ Packet:`,
37
+ e,
38
+ `
39
+ Errors:`,
40
+ n.error.flatten().fieldErrors
41
+ );
42
+ return;
43
+ }
44
+ const i = n.data;
45
+ t((a) => ({
46
+ signals: { ...a.signals, [i.id]: i },
47
+ lastUpdated: i.timestamp
48
+ }));
49
+ },
50
+ bufferSignal: (e, n) => {
51
+ g.set(e, n);
52
+ },
53
+ getSignal: (e) => s().signals[e],
54
+ clearSignals: () => t({ signals: {}, lastUpdated: null }),
55
+ removeSignal: (e) => t((n) => {
56
+ const i = { ...n.signals };
57
+ return delete i[e], { signals: i };
58
+ })
59
+ }));
60
+ function w(t) {
61
+ if (g.size > 0 && t - h >= k) {
62
+ const s = {};
63
+ g.forEach((e, n) => {
64
+ s[n] = e;
65
+ }), g.clear(), h = t, b.setState((e) => ({
66
+ signals: { ...e.signals, ...s },
67
+ lastUpdated: Date.now()
68
+ }));
69
+ }
70
+ requestAnimationFrame(w);
71
+ }
72
+ typeof requestAnimationFrame < "u" && requestAnimationFrame(w);
73
+ const x = (t) => (s) => s.signals[t], C = (t) => (s) => Object.values(s.signals).filter((e) => e.domain === t), T = (t) => (s) => Object.values(s.signals).filter((e) => e.source === t);
74
+ class M {
75
+ constructor() {
76
+ this.status = "idle";
77
+ }
78
+ /**
79
+ * Validates a raw payload and writes the resulting Signal to the store.
80
+ *
81
+ * Call this from your message handler AFTER mapToSignal() produces a shape.
82
+ * This is the Law 12 enforcement boundary — no Signal reaches the store
83
+ * without passing SignalSchema.parse().
84
+ *
85
+ * @param raw The raw source payload to adapt
86
+ * @param context Optional context (e.g. MQTT topic name)
87
+ * @returns The validated Signal object, or null if validation failed
88
+ */
89
+ emit(s, e) {
90
+ try {
91
+ const i = { ...this.mapToSignal(s, e), source: this.source }, a = S.safeParse(i);
92
+ return a.success ? (b.getState().setSignal(a.data), a.data) : (console.warn(
93
+ `[${this.name}] emit() rejected — Signal does not conform to Law 12.`,
94
+ `
95
+ Raw:`,
96
+ s,
97
+ `
98
+ Candidate:`,
99
+ i,
100
+ `
101
+ Errors:`,
102
+ a.error.flatten().fieldErrors
103
+ ), null);
104
+ } catch (n) {
105
+ return console.error(`[${this.name}] emit() threw during mapToSignal():`, n), null;
106
+ }
107
+ }
108
+ /**
109
+ * Returns the current adapter status.
110
+ */
111
+ getStatus() {
112
+ return this.status;
113
+ }
114
+ }
115
+ const O = {
116
+ soil_vwc: { key: "moisture", unit: "VWC%" },
117
+ soil_moisture_raw: { key: "moisture_raw", unit: "counts" },
118
+ soil_moisture: { key: "moisture_raw", unit: "counts" },
119
+ air_temp: { key: "temperature", unit: "°C" },
120
+ humidity: { key: "humidity", unit: "%RH" },
121
+ air_humid: { key: "humidity", unit: "%RH" },
122
+ pressure: { key: "pressure", unit: "hPa" },
123
+ air_pressure: { key: "pressure", unit: "hPa" },
124
+ light_lux: { key: "illuminance", unit: "lux" },
125
+ soil_temp_stemma: { key: "soil_temperature", unit: "°C" },
126
+ soil_ph: { key: "ph", unit: "pH" },
127
+ soil_ec: { key: "conductivity", unit: "mS/cm" },
128
+ fw_version: { key: "firmware", unit: void 0 }
129
+ }, N = [
130
+ "soil_vwc",
131
+ "soil_moisture_raw",
132
+ "soil_moisture",
133
+ "air_temp",
134
+ "air_humid",
135
+ "air_pressure",
136
+ "light_lux",
137
+ "soil_temp_stemma",
138
+ "soil_ph",
139
+ "soil_ec",
140
+ "fw_version",
141
+ "stick_id"
142
+ ], R = ["mode", "device_type", "hardwareStatus", "photo_url"];
143
+ function j(t, s) {
144
+ if (!t || typeof t != "object") return [];
145
+ const { rigId: e, source: n, receivedAt: i } = s, a = typeof t.timestamp == "number" && Number.isFinite(t.timestamp) ? t.timestamp * 1e3 : i ?? Date.now(), c = {};
146
+ for (const r of R)
147
+ t[r] !== void 0 && t[r] !== null && (c[r] = t[r]);
148
+ const l = [], m = /* @__PURE__ */ new Set();
149
+ for (const [r, u] of Object.entries(O)) {
150
+ const o = t[r];
151
+ if (o == null || typeof o == "number" && !Number.isFinite(o) || typeof o != "number" && typeof o != "string" && typeof o != "boolean" || m.has(u.key)) continue;
152
+ m.add(u.key);
153
+ const _ = {
154
+ id: `${e}:${u.key}`,
155
+ source: n,
156
+ domain: E.AGRICULTURE,
157
+ key: u.key,
158
+ value: o,
159
+ ...u.unit ? { unit: u.unit } : {},
160
+ timestamp: a,
161
+ ...Object.keys(c).length ? { meta: c } : {}
162
+ }, d = S.safeParse(_);
163
+ d.success && l.push(d.data);
164
+ }
165
+ return l;
166
+ }
167
+ function P(t, s) {
168
+ const e = {};
169
+ for (const n of j(t, s)) e[n.key] = n;
170
+ return e;
171
+ }
172
+ const B = ["timestamp", "soil_vwc", "air_temp", "light_lux"], H = B.join(",");
173
+ function U(t) {
174
+ if (!Array.isArray(t)) return [];
175
+ const s = [];
176
+ for (const e of t) {
177
+ if (!e || typeof e != "object") continue;
178
+ const n = e;
179
+ if (typeof n.timestamp != "number" || !Number.isFinite(n.timestamp)) continue;
180
+ const i = (a) => typeof a == "number" && Number.isFinite(a) ? a : void 0;
181
+ s.push({
182
+ timestamp: n.timestamp,
183
+ moisture: i(n.soil_vwc),
184
+ temperature: i(n.air_temp),
185
+ illuminance: i(n.light_lux)
186
+ });
187
+ }
188
+ return s;
189
+ }
190
+ function W(t) {
191
+ if (!t || typeof t != "object") return;
192
+ const s = t, e = s.device_id ?? s.stick_id;
193
+ return typeof e == "string" && e.length ? e : void 0;
194
+ }
195
+ function G(t, s, e) {
196
+ return { stick_id: t, type: s, value: e };
197
+ }
198
+ export {
199
+ M as B,
200
+ O as a,
201
+ N as b,
202
+ P as c,
203
+ B as d,
204
+ H as e,
205
+ U as f,
206
+ W as g,
207
+ G as h,
208
+ I as i,
209
+ C as j,
210
+ T as k,
211
+ x as s,
212
+ j as t,
213
+ b as u
214
+ };