remove redundant parse
This commit is contained in:
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 type { TopicHandler } from '../types.js'
|
||||
|
||||
export const TOPIC = 'data/demo/plc/v1'
|
||||
|
||||
const EDGE = 'demo'
|
||||
const postUrl =
|
||||
process.env.CLOUD_INGEST_URL ?? 'https://demo.backend.drill.greact.ru/ingest'
|
||||
|
||||
type DemoPlcPayload = {
|
||||
tag: string
|
||||
@@ -29,13 +28,7 @@ function toMetric(payload: DemoPlcPayload, timestamp: number): DemoPlcMetric {
|
||||
}
|
||||
|
||||
async function postMetric(metric: DemoPlcMetric): Promise<void> {
|
||||
const response = await fetch(postUrl, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'content-type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify(metric),
|
||||
})
|
||||
const response = await postCloudIngest([metric])
|
||||
|
||||
if (!response.ok) {
|
||||
const text = await response.text()
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { postCloudIngest } from '../cloud-ingest.js'
|
||||
import { createMetricMapper, type MappedMetric } from '../mapping.js'
|
||||
import type { TopicHandler } from '../types.js'
|
||||
|
||||
@@ -9,17 +10,9 @@ const mapper = createMetricMapper({
|
||||
new URL('../mappings/edge5-modbus.json', import.meta.url),
|
||||
})
|
||||
const REGISTER_COUNT = mapper.tagCount
|
||||
const postUrl =
|
||||
process.env.CLOUD_INGEST_URL ?? 'https://demo.backend.drill.greact.ru/ingest'
|
||||
|
||||
function parseValues(payload: Buffer): number[] {
|
||||
let value: unknown
|
||||
|
||||
try {
|
||||
value = JSON.parse(payload.toString('utf8')) as unknown
|
||||
} catch {
|
||||
throw new Error(`Expected JSON number[${REGISTER_COUNT}]`)
|
||||
}
|
||||
const value = JSON.parse(payload.toString('utf8')) as unknown
|
||||
|
||||
if (
|
||||
Array.isArray(value) &&
|
||||
@@ -33,13 +26,7 @@ function parseValues(payload: Buffer): number[] {
|
||||
}
|
||||
|
||||
async function postMetrics(metrics: MappedMetric[]): Promise<void> {
|
||||
const response = await fetch(postUrl, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'content-type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify(metrics),
|
||||
})
|
||||
const response = await postCloudIngest(metrics)
|
||||
|
||||
if (!response.ok) {
|
||||
const text = await response.text()
|
||||
|
||||
@@ -1,11 +1,10 @@
|
||||
import { postCloudIngest } from '../cloud-ingest.js'
|
||||
import type { TopicHandler } from '../types.js'
|
||||
|
||||
export const TOPIC = 'data/edge5/modbus/v1'
|
||||
|
||||
const EDGE = 'edge5'
|
||||
const REGISTER_COUNT = 125
|
||||
const postUrl =
|
||||
process.env.CLOUD_INGEST_URL ?? 'https://demo.backend.drill.greact.ru/ingest'
|
||||
|
||||
type Edge5ModbusMetric = {
|
||||
edge: string
|
||||
@@ -44,13 +43,7 @@ function toMetrics(values: number[], timestamp: number): Edge5ModbusMetric[] {
|
||||
}
|
||||
|
||||
async function postMetrics(metrics: Edge5ModbusMetric[]): Promise<void> {
|
||||
const response = await fetch(postUrl, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'content-type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify(metrics),
|
||||
})
|
||||
const response = await postCloudIngest(metrics)
|
||||
|
||||
if (!response.ok) {
|
||||
const text = await response.text()
|
||||
|
||||
Reference in New Issue
Block a user