@corvina/device-example
Version:
Corvina Device Example base on @corvina/device-client
56 lines (53 loc) • 1.88 kB
text/typescript
import { Logger, Controller, Injectable, Post, Query, Inject } from "@nestjs/common";
import { DeviceService } from "@corvina/device-client";
import { DataPoint } from "@corvina/device-client";
import { ApiOperation, ApiQuery, ApiTags } from "@nestjs/swagger";
import { DataPointDTO } from "./dto/datapoint.dto";
("device")
("/device/sine")
()
export class Sine {
private readonly l = new Logger(Sine.name);
() private readonly deviceService: DeviceService;
({
summary: "Post a new value sampled from a sine wave with the given parameters, using wall clock as time base",
})
({
name: "tagName",
description: "device identifier (name) of data source",
schema: { default: "Tag" },
required: false,
})
({
name: "period",
description: "period in ms of the sine wave",
required: false,
schema: { default: 1000 },
})
({
name: "amplitude",
description: "amplitude of the sine wave",
required: false,
schema: { default: 1000 },
})
({ name: "phase", description: "phase of the sine wave", required: false, schema: { default: 0 } })
()
async post(
("tagName") tagName = "Tag",
("period") period = 1000,
("amplitude") amplitude = 1000,
("phase") phase = 0,
): Promise<DataPointDTO[]> {
const t = Date.now();
const v = amplitude * Math.sin(phase + (Date.now() / period) * 2 * Math.PI);
const dataPoints = new Array<DataPoint>();
const dp: DataPoint = {
tagName,
value: v,
timestamp: t,
};
dataPoints.push(dp);
await this.deviceService.post(dataPoints);
return dataPoints;
}
}