data/edge5i/modbus

This commit is contained in:
2026-07-02 14:40:49 +05:00
parent 16c1712752
commit 540a8ba1e9
4 changed files with 67 additions and 3 deletions

View File

@@ -27,7 +27,7 @@ function toMetrics(values: number[], timestamp: string): DemoModbusMetric[] {
return values.map((value, index) => ({ return values.map((value, index) => ({
edge: EDGE, edge: EDGE,
timestamp, timestamp,
tag: `${EDGE}.modbus.${index + 1}`, tag: `register-${index}`,
value, value,
})) }))
} }
@@ -42,11 +42,11 @@ async function postMetrics(metrics: DemoModbusMetric[]): Promise<void> {
} }
} }
console.log('edge5.modbus.posted', { count: metrics.length }) console.log(`${EDGE}.modbus.posted ${metrics.length} metrics`)
} }
export const handleDemoModbus: TopicHandler = async (topic, payload) => { export const handleDemoModbus: TopicHandler = async (topic, payload) => {
console.log('edge5.modbus.received', { topic, bytes: payload.length }) console.log(`${EDGE}.modbus.received ${topic} ${payload.length} bytes`)
const values = parseValues(payload) const values = parseValues(payload)
const metrics = toMetrics(values, new Date().toISOString()) const metrics = toMetrics(values, new Date().toISOString())

View File

@@ -0,0 +1,54 @@
import { postCloudIngest } from '../cloud-ingest.js'
import type { TopicHandler } from '../types.js'
const EDGE = 'edge5i'
const REGISTER_COUNT = 100
type DemoModbusMetric = {
edge: string
timestamp: string
tag: string
value: number
}
function parseValues(payload: Buffer): number[] {
const values = JSON.parse(payload.toString('utf8')) as number[]
assertRegisterCount(values)
return values
}
function assertRegisterCount(values: number[]): void {
if (values.length !== REGISTER_COUNT) {
throw new Error(`Expected ${REGISTER_COUNT} modbus values, got ${values.length}`)
}
}
function toMetrics(values: number[], timestamp: string): DemoModbusMetric[] {
return values.map((value, index) => ({
edge: EDGE,
timestamp,
tag: `register-${index}`,
value,
}))
}
async function postMetrics(metrics: DemoModbusMetric[]): Promise<void> {
for (const metric of metrics) {
const response = await postCloudIngest(metric)
if (!response.ok) {
const text = await response.text()
throw new Error(text)
}
}
console.log(`${EDGE}.modbus.posted ${metrics.length} metrics`)
}
export const handle: TopicHandler = async (topic, payload) => {
console.log(`${EDGE}.modbus.received ${topic} ${payload.length} bytes`)
const values = parseValues(payload)
const metrics = toMetrics(values, new Date().toISOString())
await postMetrics(metrics)
}

View File

@@ -5,6 +5,8 @@ import { handleDemoModbus } from './demo-modbus.js'
import { handleEdge5ModbusV2 } from './edge5-modbus-v2.js' import { handleEdge5ModbusV2 } from './edge5-modbus-v2.js'
import { handleEdge5Video } from './edge5-video.js' import { handleEdge5Video } from './edge5-video.js'
import { handle as interpretation } from './edge5i-modbus.js'
const DEMO_PLC_TOPIC = 'data/demo/plc/v1' const DEMO_PLC_TOPIC = 'data/demo/plc/v1'
const DEMO_MODBUS_TOPIC = 'data/demo/modbus/v1' const DEMO_MODBUS_TOPIC = 'data/demo/modbus/v1'
const EDGE5_MODBUS_TOPIC = 'data/edge5/modbus/v2' const EDGE5_MODBUS_TOPIC = 'data/edge5/modbus/v2'
@@ -14,3 +16,5 @@ register(DEMO_PLC_TOPIC, handleDemoPlc)
register(DEMO_MODBUS_TOPIC, handleDemoModbus) register(DEMO_MODBUS_TOPIC, handleDemoModbus)
register(EDGE5_MODBUS_TOPIC, handleEdge5ModbusV2) register(EDGE5_MODBUS_TOPIC, handleEdge5ModbusV2)
register(EDGE5_VIDEO_TOPIC, handleEdge5Video) register(EDGE5_VIDEO_TOPIC, handleEdge5Video)
register('data/edge5i/modbus', interpretation);

6
package-lock.json generated Normal file
View File

@@ -0,0 +1,6 @@
{
"name": "mqtt-ingest",
"lockfileVersion": 3,
"requires": true,
"packages": {}
}