remove redundant parse
This commit is contained in:
@@ -3,8 +3,8 @@
|
|||||||
"private": true,
|
"private": true,
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"dev": "tsx watch src/index.ts",
|
"dev": "node --env-file-if-exists=.env ./node_modules/tsx/dist/cli.mjs watch src/index.ts",
|
||||||
"start": "tsx src/index.ts",
|
"start": "node --env-file-if-exists=.env ./node_modules/tsx/dist/cli.mjs src/index.ts",
|
||||||
"build": "tsc",
|
"build": "tsc",
|
||||||
"typecheck": "tsc --noEmit"
|
"typecheck": "tsc --noEmit"
|
||||||
},
|
},
|
||||||
|
|||||||
25
app/src/cloud-ingest.ts
Normal file
25
app/src/cloud-ingest.ts
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
const DEFAULT_CLOUD_INGEST_URL = 'http://localhost:3100/ingest'
|
||||||
|
|
||||||
|
export const cloudIngestUrl =
|
||||||
|
process.env.CLOUD_INGEST_URL ?? DEFAULT_CLOUD_INGEST_URL
|
||||||
|
|
||||||
|
function getHeaders(): Record<string, string> {
|
||||||
|
const headers: Record<string, string> = {
|
||||||
|
'content-type': 'application/json',
|
||||||
|
}
|
||||||
|
|
||||||
|
const apiKey = process.env.CLOUD_INGEST_API_KEY?.trim()
|
||||||
|
if (apiKey) {
|
||||||
|
headers['x-api-key'] = apiKey
|
||||||
|
}
|
||||||
|
|
||||||
|
return headers
|
||||||
|
}
|
||||||
|
|
||||||
|
export function postCloudIngest(body: unknown): Promise<Response> {
|
||||||
|
return fetch(cloudIngestUrl, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: getHeaders(),
|
||||||
|
body: JSON.stringify(body),
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -1,11 +1,10 @@
|
|||||||
|
import { postCloudIngest } from '../cloud-ingest.js'
|
||||||
import { parseJson } from '../helpers/parse.js'
|
import { parseJson } from '../helpers/parse.js'
|
||||||
import type { TopicHandler } from '../types.js'
|
import type { TopicHandler } from '../types.js'
|
||||||
|
|
||||||
export const TOPIC = 'data/demo/plc/v1'
|
export const TOPIC = 'data/demo/plc/v1'
|
||||||
|
|
||||||
const EDGE = 'demo'
|
const EDGE = 'demo'
|
||||||
const postUrl =
|
|
||||||
process.env.CLOUD_INGEST_URL ?? 'https://demo.backend.drill.greact.ru/ingest'
|
|
||||||
|
|
||||||
type DemoPlcPayload = {
|
type DemoPlcPayload = {
|
||||||
tag: string
|
tag: string
|
||||||
@@ -29,13 +28,7 @@ function toMetric(payload: DemoPlcPayload, timestamp: number): DemoPlcMetric {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function postMetric(metric: DemoPlcMetric): Promise<void> {
|
async function postMetric(metric: DemoPlcMetric): Promise<void> {
|
||||||
const response = await fetch(postUrl, {
|
const response = await postCloudIngest([metric])
|
||||||
method: 'POST',
|
|
||||||
headers: {
|
|
||||||
'content-type': 'application/json',
|
|
||||||
},
|
|
||||||
body: JSON.stringify(metric),
|
|
||||||
})
|
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
const text = await response.text()
|
const text = await response.text()
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import { postCloudIngest } from '../cloud-ingest.js'
|
||||||
import { createMetricMapper, type MappedMetric } from '../mapping.js'
|
import { createMetricMapper, type MappedMetric } from '../mapping.js'
|
||||||
import type { TopicHandler } from '../types.js'
|
import type { TopicHandler } from '../types.js'
|
||||||
|
|
||||||
@@ -9,17 +10,9 @@ const mapper = createMetricMapper({
|
|||||||
new URL('../mappings/edge5-modbus.json', import.meta.url),
|
new URL('../mappings/edge5-modbus.json', import.meta.url),
|
||||||
})
|
})
|
||||||
const REGISTER_COUNT = mapper.tagCount
|
const REGISTER_COUNT = mapper.tagCount
|
||||||
const postUrl =
|
|
||||||
process.env.CLOUD_INGEST_URL ?? 'https://demo.backend.drill.greact.ru/ingest'
|
|
||||||
|
|
||||||
function parseValues(payload: Buffer): number[] {
|
function parseValues(payload: Buffer): number[] {
|
||||||
let value: unknown
|
const value = JSON.parse(payload.toString('utf8')) as unknown
|
||||||
|
|
||||||
try {
|
|
||||||
value = JSON.parse(payload.toString('utf8')) as unknown
|
|
||||||
} catch {
|
|
||||||
throw new Error(`Expected JSON number[${REGISTER_COUNT}]`)
|
|
||||||
}
|
|
||||||
|
|
||||||
if (
|
if (
|
||||||
Array.isArray(value) &&
|
Array.isArray(value) &&
|
||||||
@@ -33,13 +26,7 @@ function parseValues(payload: Buffer): number[] {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function postMetrics(metrics: MappedMetric[]): Promise<void> {
|
async function postMetrics(metrics: MappedMetric[]): Promise<void> {
|
||||||
const response = await fetch(postUrl, {
|
const response = await postCloudIngest(metrics)
|
||||||
method: 'POST',
|
|
||||||
headers: {
|
|
||||||
'content-type': 'application/json',
|
|
||||||
},
|
|
||||||
body: JSON.stringify(metrics),
|
|
||||||
})
|
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
const text = await response.text()
|
const text = await response.text()
|
||||||
|
|||||||
@@ -1,11 +1,10 @@
|
|||||||
|
import { postCloudIngest } from '../cloud-ingest.js'
|
||||||
import type { TopicHandler } from '../types.js'
|
import type { TopicHandler } from '../types.js'
|
||||||
|
|
||||||
export const TOPIC = 'data/edge5/modbus/v1'
|
export const TOPIC = 'data/edge5/modbus/v1'
|
||||||
|
|
||||||
const EDGE = 'edge5'
|
const EDGE = 'edge5'
|
||||||
const REGISTER_COUNT = 125
|
const REGISTER_COUNT = 125
|
||||||
const postUrl =
|
|
||||||
process.env.CLOUD_INGEST_URL ?? 'https://demo.backend.drill.greact.ru/ingest'
|
|
||||||
|
|
||||||
type Edge5ModbusMetric = {
|
type Edge5ModbusMetric = {
|
||||||
edge: string
|
edge: string
|
||||||
@@ -44,13 +43,7 @@ function toMetrics(values: number[], timestamp: number): Edge5ModbusMetric[] {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function postMetrics(metrics: Edge5ModbusMetric[]): Promise<void> {
|
async function postMetrics(metrics: Edge5ModbusMetric[]): Promise<void> {
|
||||||
const response = await fetch(postUrl, {
|
const response = await postCloudIngest(metrics)
|
||||||
method: 'POST',
|
|
||||||
headers: {
|
|
||||||
'content-type': 'application/json',
|
|
||||||
},
|
|
||||||
body: JSON.stringify(metrics),
|
|
||||||
})
|
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
const text = await response.text()
|
const text = await response.text()
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ services:
|
|||||||
HTTP_PORT: "80"
|
HTTP_PORT: "80"
|
||||||
WS_PORT: "9090"
|
WS_PORT: "9090"
|
||||||
CLOUD_INGEST_URL: ${CLOUD_INGEST_URL:-https://demo.backend.drill.greact.ru/ingest}
|
CLOUD_INGEST_URL: ${CLOUD_INGEST_URL:-https://demo.backend.drill.greact.ru/ingest}
|
||||||
|
CLOUD_INGEST_API_KEY: ${CLOUD_INGEST_API_KEY:-}
|
||||||
EDGE5_MODBUS_V2_MAPPING_FILE: ${EDGE5_MODBUS_V2_MAPPING_FILE:-/app/src/mappings/edge5-modbus.json}
|
EDGE5_MODBUS_V2_MAPPING_FILE: ${EDGE5_MODBUS_V2_MAPPING_FILE:-/app/src/mappings/edge5-modbus.json}
|
||||||
|
|
||||||
volumes:
|
volumes:
|
||||||
|
|||||||
Reference in New Issue
Block a user