Compare commits
3 Commits
7c67b9e34a
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| fe9e7b5438 | |||
| 3c94cd92c1 | |||
| 5b97022116 |
@@ -1,4 +1,5 @@
|
|||||||
export const cloudIngestUrl = process.env.CLOUD_INGEST_URL as string
|
export const cloudIngestUrl = process.env.CLOUD_INGEST_URL as string
|
||||||
|
export const demoCloudIngestUrl = process.env.DEMO_CLOUD_INGEST_URL as string
|
||||||
|
|
||||||
export function postCloudIngest(body: unknown): Promise<Response> {
|
export function postCloudIngest(body: unknown): Promise<Response> {
|
||||||
return fetch(cloudIngestUrl, {
|
return fetch(cloudIngestUrl, {
|
||||||
@@ -10,3 +11,14 @@ export function postCloudIngest(body: unknown): Promise<Response> {
|
|||||||
body: JSON.stringify(body),
|
body: JSON.stringify(body),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function postDemoCloudIngest(body: unknown): Promise<Response> {
|
||||||
|
return fetch(demoCloudIngestUrl, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'content-type': 'application/json',
|
||||||
|
'x-api-key': process.env.CLOUD_INGEST_API_KEY as string,
|
||||||
|
},
|
||||||
|
body: JSON.stringify(body),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { postCloudIngest } from '../cloud-ingest.js'
|
import { postCloudIngest, postDemoCloudIngest } from '../cloud-ingest.js'
|
||||||
import { log } from '../helpers/log.js'
|
import { log } from '../helpers/log.js'
|
||||||
import type { TopicHandler } from '../types.js'
|
import type { TopicHandler } from '../types.js'
|
||||||
|
|
||||||
@@ -36,10 +36,10 @@ type Edge5ModbusV3Metric = {
|
|||||||
edge: string
|
edge: string
|
||||||
timestamp: string
|
timestamp: string
|
||||||
tag: string
|
tag: string
|
||||||
value: number
|
value: number | null
|
||||||
}
|
}
|
||||||
|
|
||||||
function toFloatCDAB(reg0: number, reg1: number): number {
|
function toFloatCDAB(reg0: number, reg1: number): number | null {
|
||||||
const buf = Buffer.allocUnsafe(4)
|
const buf = Buffer.allocUnsafe(4)
|
||||||
buf.writeUInt16BE(reg1, 0)
|
buf.writeUInt16BE(reg1, 0)
|
||||||
buf.writeUInt16BE(reg0, 2)
|
buf.writeUInt16BE(reg0, 2)
|
||||||
@@ -63,16 +63,7 @@ function toMetrics(values: number[], timestamp: string): Edge5ModbusV3Metric[] {
|
|||||||
|
|
||||||
for (let i = 0; i < values.length; i += 2) {
|
for (let i = 0; i < values.length; i += 2) {
|
||||||
const tag = TAGS[i / 2]
|
const tag = TAGS[i / 2]
|
||||||
const value = toFloatCDAB(values[i], values[i + 1])
|
const value = toFloatCDAB(values[i], values[i + 1]); // null is possible to send
|
||||||
if (isNaN(value)) {
|
|
||||||
if (CLOUD_INGEST_TAGS.has(tag)) {
|
|
||||||
log(
|
|
||||||
`edge5.modbus.v3: не удалось собрать число для ${tag}, ` +
|
|
||||||
`исходные values[${i}]=${values[i]}, values[${i + 1}]=${values[i + 1]}, пропуск`,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
metrics.push({
|
metrics.push({
|
||||||
edge: EDGE,
|
edge: EDGE,
|
||||||
@@ -97,6 +88,9 @@ async function postMetrics(metrics: Edge5ModbusV3Metric[]): Promise<void> {
|
|||||||
const text = await response.text()
|
const text = await response.text()
|
||||||
throw new Error(text || `${response.status} ${response.statusText}`)
|
throw new Error(text || `${response.status} ${response.statusText}`)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void postDemoCloudIngest(metric); // для демо-системы
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
const message = error instanceof Error ? error.message : String(error)
|
const message = error instanceof Error ? error.message : String(error)
|
||||||
errors.push(new Error(`${metric.tag}: ${message}`))
|
errors.push(new Error(`${metric.tag}: ${message}`))
|
||||||
@@ -104,7 +98,7 @@ async function postMetrics(metrics: Edge5ModbusV3Metric[]): Promise<void> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
log('edge5.modbus.v3.posted', { total: toPost.length });
|
log(`edge5.modbus.v3.data ↑ ${toPost.length} tags`);
|
||||||
|
|
||||||
if (errors.length) {
|
if (errors.length) {
|
||||||
throw new Error(`Failed to post ${errors.length}/${toPost.length} edge5 modbus v3 metrics`)
|
throw new Error(`Failed to post ${errors.length}/${toPost.length} edge5 modbus v3 metrics`)
|
||||||
@@ -113,8 +107,6 @@ async function postMetrics(metrics: Edge5ModbusV3Metric[]): Promise<void> {
|
|||||||
|
|
||||||
export const handleEdge5ModbusV3: TopicHandler = async (topic, payload) => {
|
export const handleEdge5ModbusV3: TopicHandler = async (topic, payload) => {
|
||||||
const timestamp = new Date().toISOString();
|
const timestamp = new Date().toISOString();
|
||||||
log('edge5.modbus.v3.received', { topic, bytes: payload.length, timestamp })
|
|
||||||
|
|
||||||
const values = parseValues(payload);
|
const values = parseValues(payload);
|
||||||
const metrics = toMetrics(values, timestamp);
|
const metrics = toMetrics(values, timestamp);
|
||||||
await postMetrics(metrics);
|
await postMetrics(metrics);
|
||||||
|
|||||||
@@ -9,10 +9,10 @@ type Edge5iiModbusMetric = {
|
|||||||
edge: string
|
edge: string
|
||||||
timestamp: string
|
timestamp: string
|
||||||
tag: string
|
tag: string
|
||||||
value: number
|
value: number | null
|
||||||
};
|
};
|
||||||
|
|
||||||
function toFloatCDAB(reg0: number, reg1: number): number {
|
function toFloatCDAB(reg0: number, reg1: number): number | null {
|
||||||
const buf = Buffer.allocUnsafe(4)
|
const buf = Buffer.allocUnsafe(4)
|
||||||
buf.writeUInt16BE(reg1, 0)
|
buf.writeUInt16BE(reg1, 0)
|
||||||
buf.writeUInt16BE(reg0, 2)
|
buf.writeUInt16BE(reg0, 2)
|
||||||
@@ -36,14 +36,7 @@ function toMetrics(values: number[], timestamp: string): Edge5iiModbusMetric[] {
|
|||||||
|
|
||||||
for (let i = 0; i < values.length; i += 2) {
|
for (let i = 0; i < values.length; i += 2) {
|
||||||
const tag = `register-${String(i).padStart(3, '0')}`
|
const tag = `register-${String(i).padStart(3, '0')}`
|
||||||
const value = toFloatCDAB(values[i], values[i + 1])
|
const value = toFloatCDAB(values[i], values[i + 1]); // null is possible to send
|
||||||
if (isNaN(value)) {
|
|
||||||
log(
|
|
||||||
`${EDGE}.modbus: не удалось собрать число для ${tag}, ` +
|
|
||||||
`исходные values[${i}]=${values[i]}, values[${i + 1}]=${values[i + 1]}, пропуск`,
|
|
||||||
)
|
|
||||||
continue
|
|
||||||
};
|
|
||||||
|
|
||||||
metrics.push({
|
metrics.push({
|
||||||
edge: EDGE,
|
edge: EDGE,
|
||||||
|
|||||||
Reference in New Issue
Block a user