diff --git a/lib/AdroitVW/adroitvw-converter.ts b/lib/AdroitVW/adroitvw-converter.ts new file mode 100644 index 0000000..c9d607e --- /dev/null +++ b/lib/AdroitVW/adroitvw-converter.ts @@ -0,0 +1,29 @@ +import { JtsDocument, TimeSeries } from '@eagle-io/timeseries'; +import { Converter } from '../../converter'; +import moment from 'moment-timezone'; + +/** +* Convert the JSON data from Adroit VW Logger +* example: https://adroitplatform.com/api/points/34404/feeds/54553 +*/ +export class AdroitVWJsonConverter extends Converter { + convert(input: Buffer): JtsDocument { + // Parse the input buffer to JSON object + const record = JSON.parse(input.toString()); + // Convert the epoch time to a moment object + let timestamp = moment.utc(record.lastUpdate).tz('Pacific/Auckland').toDate(); + + // Create a TimeSeries object + const series1 = new TimeSeries({ name: record.name }); + + // Convert the value to a number + const value = Number(record.value); + + // Insert the time series data + series1.insert({ timestamp, value }); + + // Return the JTS document + return new JtsDocument({ series: [series1] }); + } +} + diff --git a/lib/AdroitVW/test/adroitvw-converter.test.ts b/lib/AdroitVW/test/adroitvw-converter.test.ts new file mode 100644 index 0000000..dc39dab --- /dev/null +++ b/lib/AdroitVW/test/adroitvw-converter.test.ts @@ -0,0 +1,18 @@ +import * as fs from 'fs' +import { AdroitVWJsonConverter } from '../AdroitVW-converter' + +describe('Unit test for AdroitVWJson', function () { + it('converts sample file', async () => { + const converter = new AdroitVWJsonConverter() + const buff = fs.readFileSync('lib/AdroitVW/test/input.dat') + const result = converter.convert(buff) + + // Corrected quotes and numeric timestamp for lastUpdate + expect(result.series[0].first.timestamp).toEqual(new Date('2024-09-16T08:00:00Z')) + expect(result.series[0].values).toEqual([2928.3]) + + }) +}) + + + diff --git a/lib/AdroitVW/test/input.dat b/lib/AdroitVW/test/input.dat new file mode 100644 index 0000000..4eb32fe --- /dev/null +++ b/lib/AdroitVW/test/input.dat @@ -0,0 +1,44 @@ +{ + "id": 54553, + "name": "VWS1 Frequency", + "point": 34404, + "creationDate": 1724640746000, + "lastUpdate": 1726430400000, + "value": "2928.3", + "sourceStatus": "ACTIVE", + "path": "Site/dvwt-01-st12429/VWS1 Frequency", + "pathId": "17597/34404/54553/", + "alarmState": "NORMAL", + "alarmMessage": "STATE_NORMAL", + "sourceType": "DEVICE_SENSOR", + "sourceId": 56680, + "contextId": 9138, + "unit": { + "id": 28, + "name": "HERTZ", + "symbol": "Hz", + "readingTypeId": 40, + "system": "SI", + "precision": 1, + "context": null, + "guid": "7c805b6e-a879-11e9-83cd-0a0027000005", + "readingTypeName": "FREQUENCY", + "inUse": true + }, + "minimum": null, + "maximum": null, + "trend": "STEADY", + "validity": 30, + "thresholdsEnabled": false, + "originType": "FEED", + "readingType": { + "id": 40, + "name": "FREQUENCY", + "dataType": "NUMERIC", + "size": 1, + "labels": null, + "iconName": null + }, + "originId": 54553, + "message": "STATE_NORMAL" +} \ No newline at end of file