@yarkivaev/scada 2.6.22 → 2.6.25

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yarkivaev/scada",
3
- "version": "2.6.22",
3
+ "version": "2.6.25",
4
4
  "description": "SCADA domain objects, state persistence, and plant monitoring",
5
5
  "repository": {
6
6
  "type": "git",
@@ -40,7 +40,7 @@
40
40
  "@clickhouse/client": "^1.0.0",
41
41
  "@modelcontextprotocol/sdk": "^1.24.3",
42
42
  "@yarkivaev/simple-server": "1.0.2",
43
- "@yarkivaev/source-to-sink": "2.0.0",
43
+ "@yarkivaev/source-to-sink": "^2.0.1",
44
44
  "amqplib": "^0.10.9",
45
45
  "better-sqlite3": "^11.0.0",
46
46
  "modbus-serial": "^8.0.0",
@@ -30,17 +30,27 @@ function decode(low, high) {
30
30
  * @param {object} collector - Collector with accept(record)
31
31
  * @returns {object} Transformer with accept(registers)
32
32
  */
33
+ function publish(collector, device, channel, ts, value) {
34
+ return collector.accept({
35
+ topic: `MX210/${device}/GET/${channel}/VALUE`,
36
+ ts,
37
+ value
38
+ });
39
+ }
40
+
33
41
  export function mx210Metrics(device, collector) {
34
42
  return {
35
43
  accept(registers) {
36
44
  const ts = Date.now();
37
- return Promise.all(layout.map((entry) => {
38
- return collector.accept({
39
- topic: `MX210/${device}/GET/${entry.channel}/VALUE`,
40
- ts,
41
- value: decode(registers[entry.offset], registers[entry.offset + 1])
42
- });
43
- }));
45
+ return Promise.all([
46
+ ...layout.map((entry) => {
47
+ return publish(collector, device, entry.channel, ts, decode(registers[entry.offset], registers[entry.offset + 1]));
48
+ }),
49
+ publish(collector, device, 'source_alive', ts, 1)
50
+ ]);
51
+ },
52
+ miss(row) {
53
+ return publish(collector, device, 'source_alive', Number(row?.ts) || Date.now(), 0);
44
54
  }
45
55
  };
46
56
  }
@@ -53,6 +53,28 @@ export function measurementPlans(stream, mapping) {
53
53
  * @param {object} collector - collector with accept()
54
54
  * @returns {object} transformer with accept(registers)
55
55
  */
56
+ function aliveTopic(topic) {
57
+ const parts = String(topic).split('/');
58
+ const index = parts.indexOf('GET');
59
+ if (index >= 0 && index + 1 < parts.length) {
60
+ parts[index + 1] = 'source_alive';
61
+ return parts.join('/');
62
+ }
63
+ parts[parts.length - 1] = 'source_alive';
64
+ return parts.join('/');
65
+ }
66
+
67
+ function emit(collector, topic, value, ts) {
68
+ return collector.accept({
69
+ payload: JSON.stringify({ ts, value }),
70
+ topic
71
+ });
72
+ }
73
+
74
+ function marks(rows) {
75
+ return [...new Set(rows.map((item) => { return aliveTopic(item.topic); }))];
76
+ }
77
+
56
78
  export default function registerPublish(plans, origin, collector) {
57
79
  if (!collector || typeof collector.accept !== 'function') {
58
80
  throw new Error('Collector must have an accept() method');
@@ -70,15 +92,22 @@ export default function registerPublish(plans, origin, collector) {
70
92
  * @param {Array<number>} registers - uint16 window
71
93
  */
72
94
  accept(registers) {
73
- return Promise.all(rows.map((item) => {
74
- return collector.accept({
75
- payload: JSON.stringify({
76
- ts: Date.now(),
77
- value: decodePoint(item.point, origin, registers)
78
- }),
79
- topic: item.topic
80
- });
81
- }));
95
+ const ts = Date.now();
96
+ return Promise.all([
97
+ ...rows.map((item) => {
98
+ return emit(collector, item.topic, decodePoint(item.point, origin, registers), ts);
99
+ }),
100
+ ...marks(rows).map((topic) => { return emit(collector, topic, 1, ts); })
101
+ ]);
102
+ },
103
+ /**
104
+ * Publishes a collector-clock watermark while the register source is dark.
105
+ *
106
+ * @param {{ts?: number}} row - collector timestamp
107
+ */
108
+ miss(row) {
109
+ const ts = Number(row?.ts) || Date.now();
110
+ return Promise.all(marks(rows).map((topic) => { return emit(collector, topic, 0, ts); }));
82
111
  }
83
112
  };
84
113
  }
@@ -13,6 +13,10 @@ export const historyProtocol = 'stateful-correction-v1';
13
13
  function machineName(machine) {
14
14
  if (typeof machine !== 'string' || !machine || machine.length > 200) { throw new TypeError('machine required'); }
15
15
  }
16
+ async function ready(connection, machine, hold) {
17
+ try { await assertHistoryReady(connection, machine); return null; }
18
+ catch (error) { if (error.statusCode===409 && hold) { return hold; } throw error; }
19
+ }
16
20
  async function transaction(pool, machine, action, topology=false) {
17
21
  machineName(machine);
18
22
  const client = await pool.connect();
@@ -59,6 +63,13 @@ function context(pool,machine,from,until,options) {
59
63
  machineName(machine);
60
64
  return historyContext(pool,machine,from,until,options);
61
65
  }
66
+ async function snapshot(connection, machine, range, page, observationSource) {
67
+ if (observationSource) {
68
+ const held=await ready(connection,machine,page.classifierKey&&page.workId&&{available:false,items:[],segments:[],annotations:[],context:{from:range.from,until:range.until,paddingMs:page.padding,...page}});
69
+ if (held) { return held; }
70
+ }
71
+ return context(connection,machine,range.from,range.until,{...page,observations:observationSource?.(connection)});
72
+ }
62
73
  async function unchanged(client, machine, base, {observationSource,timeScale}) {
63
74
  await client.query('LOCK TABLE segments IN SHARE ROW EXCLUSIVE MODE');
64
75
  const latest = await context(client, machine, base.from, base.until, { padding: base.paddingMs, cutoff: -Infinity,cursor:base.cursor ?? null,end:base.end ?? null,classifierKey:base.classifierKey,workId:base.workId,workToken:base.workToken,timeScale,observations:observationSource?.(client) });
@@ -89,7 +100,7 @@ function commit(pool, machine, workId, segments, {base, observationSource,timeSc
89
100
  if (!work.rowCount) { return { applied: false }; }
90
101
  checkClaim(work.rows[0],base);
91
102
  if (segments.length && !base?.revision) { throw new TypeError('History context revision required'); }
92
- if (observationSource) { await assertHistoryReady(client,machine); }
103
+ if (observationSource) { const held=await ready(client,machine,{applied:false}); if (held) { return held; } }
93
104
  const latest = base?.revision ? await unchanged(client, machine, base, {observationSource,timeScale}) : null;
94
105
  const exteriors = nativeHistoryBounds(segments, latest, base, timeScale);
95
106
  for (const item of exteriors) {
@@ -160,10 +171,8 @@ export default function historyPg(pool, { retentionDays = 30, now = Date.now, ob
160
171
  if (observationSource) { throw new TypeError('External observations require durable telemetry admission'); }
161
172
  return accept(pool, machine, items, cutoff()); },
162
173
  context(machine, from, until, padding = 60000*timeScale, page = {}) {
163
- async function read(connection){
164
- if(observationSource){await assertHistoryReady(connection,machine);}
165
- return context(connection,machine,from,until,{padding,cutoff:cutoff(),...page,timeScale,observations:observationSource?.(connection)});
166
- }
174
+ const options={padding,cutoff:cutoff(),...page,timeScale};
175
+ function read(connection) { return snapshot(connection,machine,{from,until},options,observationSource); }
167
176
  return page.classifierKey && page.workId ? transaction(pool,machine,read) : read(pool);
168
177
  },
169
178
  async work(machine) {