Compare commits
3 Commits
adde96d8ea
...
edge5modbu
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
644c7a8a78 | ||
|
|
c8fef3e025 | ||
|
|
de4d6eccbe |
63
app/src/handlers/demo-modbus.ts
Normal file
63
app/src/handlers/demo-modbus.ts
Normal file
@@ -0,0 +1,63 @@
|
||||
import type { TopicHandler } from '../types.js'
|
||||
|
||||
export const TOPIC = 'data/demo/modbus/v1'
|
||||
|
||||
const EDGE = 'demo'
|
||||
const REGISTER_COUNT = 125
|
||||
const DEMO_CLOUD_INGEST_URL = process.env.DEMO_CLOUD_INGEST_URL
|
||||
|
||||
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: `${EDGE}.modbus.${index + 1}`,
|
||||
value,
|
||||
}))
|
||||
}
|
||||
|
||||
async function postMetrics(metrics: DemoModbusMetric[]): Promise<void> {
|
||||
for (const metric of metrics) {
|
||||
const response = await fetch(DEMO_CLOUD_INGEST_URL as string, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'content-type': 'application/json',
|
||||
'x-api-key': process.env.CLOUD_INGEST_API_KEY as string,
|
||||
},
|
||||
body: JSON.stringify(metric),
|
||||
})
|
||||
|
||||
if (!response.ok) {
|
||||
const text = await response.text()
|
||||
throw new Error(text)
|
||||
}
|
||||
}
|
||||
|
||||
console.log('demo.modbus.posted', { count: metrics.length })
|
||||
}
|
||||
|
||||
export const handleDemoModbus: TopicHandler = async (topic, payload) => {
|
||||
console.log('demo.modbus.received', { topic, bytes: payload.length })
|
||||
|
||||
const values = parseValues(payload)
|
||||
const metrics = toMetrics(values, new Date().toISOString())
|
||||
await postMetrics(metrics)
|
||||
}
|
||||
@@ -1,10 +1,10 @@
|
||||
import { postCloudIngest } from '../cloud-ingest.js'
|
||||
import { parseJson } from '../helpers/parse.js'
|
||||
import type { TopicHandler } from '../types.js'
|
||||
|
||||
export const TOPIC = 'data/demo/plc/v1'
|
||||
|
||||
const EDGE = 'demo'
|
||||
const DEMO_CLOUD_INGEST_URL = process.env.DEMO_CLOUD_INGEST_URL
|
||||
|
||||
type DemoPlcPayload = {
|
||||
tag: string
|
||||
@@ -13,12 +13,12 @@ type DemoPlcPayload = {
|
||||
|
||||
type DemoPlcMetric = {
|
||||
edge: string
|
||||
timestamp: number
|
||||
timestamp: string
|
||||
tag: string
|
||||
value: number
|
||||
}
|
||||
|
||||
function toMetric(payload: DemoPlcPayload, timestamp: number): DemoPlcMetric {
|
||||
function toMetric(payload: DemoPlcPayload, timestamp: string): DemoPlcMetric {
|
||||
return {
|
||||
edge: EDGE,
|
||||
timestamp,
|
||||
@@ -28,7 +28,14 @@ function toMetric(payload: DemoPlcPayload, timestamp: number): DemoPlcMetric {
|
||||
}
|
||||
|
||||
async function postMetric(metric: DemoPlcMetric): Promise<void> {
|
||||
const response = await postCloudIngest([metric])
|
||||
const response = await fetch(DEMO_CLOUD_INGEST_URL as string, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'content-type': 'application/json',
|
||||
'x-api-key': process.env.CLOUD_INGEST_API_KEY as string,
|
||||
},
|
||||
body: JSON.stringify(metric),
|
||||
})
|
||||
|
||||
if (!response.ok) {
|
||||
const text = await response.text()
|
||||
@@ -42,6 +49,6 @@ export const handleDemoPlc: TopicHandler = async (topic, payload) => {
|
||||
console.log('demo.plc.received', { topic, bytes: payload.length })
|
||||
|
||||
const value = parseJson<DemoPlcPayload>(payload)
|
||||
const metric = toMetric(value, Date.now())
|
||||
const metric = toMetric(value, new Date().toISOString())
|
||||
await postMetric(metric)
|
||||
}
|
||||
|
||||
@@ -22,20 +22,38 @@ function assertRegisterCount(values: number[]): void {
|
||||
}
|
||||
|
||||
async function postMetrics(metrics: MappedMetric[]): Promise<void> {
|
||||
const response = await postCloudIngest(metrics)
|
||||
const errors: Error[] = []
|
||||
|
||||
if (!response.ok) {
|
||||
const text = await response.text()
|
||||
throw new Error(text)
|
||||
for (const metric of metrics) {
|
||||
try {
|
||||
const response = await postCloudIngest(metric)
|
||||
|
||||
if (!response.ok) {
|
||||
const text = await response.text()
|
||||
throw new Error(text || `${response.status} ${response.statusText}`)
|
||||
}
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : String(error)
|
||||
errors.push(new Error(`${metric.tag}: ${message}`))
|
||||
console.error('edge5.modbus.v2.metric.failed', metric.tag, message)
|
||||
}
|
||||
}
|
||||
|
||||
console.log('edge5.modbus.v2.posted', { count: metrics.length })
|
||||
console.log('edge5.modbus.v2.posted', {
|
||||
total: metrics.length,
|
||||
succeeded: metrics.length - errors.length,
|
||||
failed: errors.length,
|
||||
})
|
||||
|
||||
if (errors.length) {
|
||||
throw new Error(`Failed to post ${errors.length}/${metrics.length} edge5 modbus v2 metrics`)
|
||||
}
|
||||
}
|
||||
|
||||
export const handleEdge5ModbusV2: TopicHandler = async (topic, payload) => {
|
||||
console.log('edge5.modbus.v2.received', { topic, bytes: payload.length })
|
||||
|
||||
const values = parseValues(payload)
|
||||
const metrics = mapper.mapValues(values, Date.now())
|
||||
const metrics = mapper.mapValues(values, new Date().toISOString())
|
||||
await postMetrics(metrics)
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ const REGISTER_COUNT = 125
|
||||
|
||||
type Edge5ModbusMetric = {
|
||||
edge: string
|
||||
timestamp: number
|
||||
timestamp: string
|
||||
tag: string
|
||||
value: number
|
||||
}
|
||||
@@ -25,7 +25,7 @@ function assertRegisterCount(values: number[]): void {
|
||||
}
|
||||
}
|
||||
|
||||
function toMetrics(values: number[], timestamp: number): Edge5ModbusMetric[] {
|
||||
function toMetrics(values: number[], timestamp: string): Edge5ModbusMetric[] {
|
||||
return values.map((value, index) => ({
|
||||
edge: EDGE,
|
||||
timestamp,
|
||||
@@ -35,11 +35,13 @@ function toMetrics(values: number[], timestamp: number): Edge5ModbusMetric[] {
|
||||
}
|
||||
|
||||
async function postMetrics(metrics: Edge5ModbusMetric[]): Promise<void> {
|
||||
const response = await postCloudIngest(metrics)
|
||||
for (const metric of metrics) {
|
||||
const response = await postCloudIngest(metric)
|
||||
|
||||
if (!response.ok) {
|
||||
const text = await response.text()
|
||||
throw new Error(text)
|
||||
if (!response.ok) {
|
||||
const text = await response.text()
|
||||
throw new Error(text)
|
||||
}
|
||||
}
|
||||
|
||||
console.log('edge5.modbus.posted', { count: metrics.length })
|
||||
@@ -49,6 +51,6 @@ export const handleEdge5Modbus: TopicHandler = async (topic, payload) => {
|
||||
console.log('edge5.modbus.received', { topic, bytes: payload.length })
|
||||
|
||||
const values = parseValues(payload)
|
||||
const metrics = toMetrics(values, Date.now())
|
||||
const metrics = toMetrics(values, new Date().toISOString())
|
||||
await postMetrics(metrics)
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { register } from '../registry.js'
|
||||
import { handleDemoModbus, TOPIC as demoModbus } from './demo-modbus.js'
|
||||
import { handleDemoPlc, TOPIC as demoPlc } from './demo-plc.js'
|
||||
import { handleEdge5Modbus, TOPIC as edge5Modbus } from './edge5-modbus.js'
|
||||
import {
|
||||
@@ -7,6 +8,7 @@ import {
|
||||
} from './edge5-modbus-v2.js'
|
||||
import { handleEdge5Video, TOPIC as edge5Video } from './edge5-video.js'
|
||||
|
||||
register(demoModbus, handleDemoModbus)
|
||||
register(demoPlc, handleDemoPlc)
|
||||
register(edge5Modbus, handleEdge5Modbus)
|
||||
register(edge5ModbusV2, handleEdge5ModbusV2)
|
||||
|
||||
@@ -2,7 +2,7 @@ import { readFileSync } from 'node:fs'
|
||||
|
||||
export type MappedMetric = {
|
||||
edge: string
|
||||
timestamp: number
|
||||
timestamp: string
|
||||
tag: string
|
||||
value: number
|
||||
}
|
||||
@@ -14,7 +14,7 @@ type MappingConfig = {
|
||||
|
||||
export type MetricMapper = {
|
||||
edge: string
|
||||
mapValues: (values: number[], timestamp: number) => MappedMetric[]
|
||||
mapValues: (values: number[], timestamp: string) => MappedMetric[]
|
||||
}
|
||||
|
||||
type MetricMapperOptions = {
|
||||
|
||||
@@ -21,6 +21,7 @@ services:
|
||||
WS_PORT: "9090"
|
||||
CLOUD_INGEST_URL: ${CLOUD_INGEST_URL:-https://demo.backend.drill.greact.ru/ingest}
|
||||
CLOUD_INGEST_API_KEY: ${CLOUD_INGEST_API_KEY:-}
|
||||
DEMO_CLOUD_INGEST_URL: ${DEMO_CLOUD_INGEST_URL:-}
|
||||
EDGE5_MODBUS_V2_MAPPING_FILE: ${EDGE5_MODBUS_V2_MAPPING_FILE:-/app/src/mappings/edge5-modbus.json}
|
||||
|
||||
volumes:
|
||||
|
||||
Reference in New Issue
Block a user