diff --git a/builder/simulator-config/new-simulator-modal.component.ts b/builder/simulator-config/new-simulator-modal.component.ts index e23f1f3..55827f6 100644 --- a/builder/simulator-config/new-simulator-modal.component.ts +++ b/builder/simulator-config/new-simulator-modal.component.ts @@ -214,7 +214,10 @@ export class NewSimulatorModalComponent implements OnInit{ name: this.simulatorName, c8y_RequiredAvailability: { responseInterval: 5 - } + }, + c8y_SupportedOperations: [ + "c8y_Connection_status" + ] })).data; this.deviceName = this.simulatorName; this.deviceId = device.id; @@ -311,7 +314,10 @@ export class NewSimulatorModalComponent implements OnInit{ name: this.simulatorName + '-' + (index + 1), c8y_RequiredAvailability: { responseInterval: 5 - } + }, + c8y_SupportedOperations: [ + "c8y_Connection_status" + ] }; await this.inventoryService.childAssetsCreate(childManageObject, group.id); } diff --git a/builder/simulator/device-interval-simulator.ts b/builder/simulator/device-interval-simulator.ts index 4d23cba..d0b068c 100644 --- a/builder/simulator/device-interval-simulator.ts +++ b/builder/simulator/device-interval-simulator.ts @@ -17,7 +17,7 @@ */ import { Injector } from '@angular/core'; -import { InventoryService } from '@c8y/client'; +import { FetchClient, IEvent, IMeasurementCreate, InventoryService } from '@c8y/client'; import {DeviceSimulator} from "./device-simulator"; /** @@ -31,9 +31,11 @@ export abstract class DeviceIntervalSimulator extends DeviceSimulator { abstract onTick(deviceId?:string); private inventoryService: InventoryService; + private fetchClient: FetchClient; constructor(protected injector: Injector) { super(); this.inventoryService = injector.get(InventoryService); + this.fetchClient = injector.get(FetchClient); } onStart() { this.intervalHandle = setInterval(() => { @@ -70,4 +72,26 @@ export abstract class DeviceIntervalSimulator extends DeviceSimulator { return response; } + + async createMeasurement(entity: Partial) { + return this.fetchClient.fetch("/measurement/measurements", { + headers: { + "content-type": "application/json", + "Accept": "application/json" + }, + body: JSON.stringify(entity), + method: 'POST' + }); + } + + async createEvent(entity: IEvent) { + return this.fetchClient.fetch("/event/events", { + headers: { + "content-type": "application/json", + "Accept": "application/json" + }, + body: JSON.stringify(entity), + method: 'POST' + }); + } } diff --git a/simulation-strategies/dtdl/dtdl.simulation-strategy.ts b/simulation-strategies/dtdl/dtdl.simulation-strategy.ts index 258ec19..7b92cbe 100644 --- a/simulation-strategies/dtdl/dtdl.simulation-strategy.ts +++ b/simulation-strategies/dtdl/dtdl.simulation-strategy.ts @@ -265,8 +265,8 @@ export class DtdlSimulationStrategy extends DeviceIntervalSimulator { }) }); const modelFragmentObject = Array.from(fragementmap.entries()).reduce((main, [key, value]) => ({ ...main, [key]: value }), {}); - this.measurementService.create({ - sourceId: deviceId, + this.createMeasurement({ + source: { id: deviceId}, time: new Date(), [fragment]: { ...modelFragmentObject @@ -287,8 +287,8 @@ export class DtdlSimulationStrategy extends DeviceIntervalSimulator { }) }); const modelFragmentObject = Array.from(fragementmap.entries()).reduce((main, [key, value]) => ({ ...main, [key]: value }), {}); - this.measurementService.create({ - sourceId: deviceId, + this.createMeasurement({ + source: { id: deviceId}, time: new Date(), [modelConfig.fragment]: { ...modelFragmentObject @@ -298,8 +298,8 @@ export class DtdlSimulationStrategy extends DeviceIntervalSimulator { } } else { - this.measurementService.create({ - sourceId: deviceId, + this.createMeasurement({ + source: { id: deviceId}, time: new Date(), [modelConfig.fragment]: { [modelConfig.series]: { @@ -341,7 +341,7 @@ export class DtdlSimulationStrategy extends DeviceIntervalSimulator { const eventText = simulatorTypeConfigParam.eventText[simulatorTypeConfigParam.eventCounter++]; this.updateSimulatorConfigParam(eventCreationConfigParam, simulatorTypeConfigParam); - this.eventService.create({ + this.createEvent({ source: { id: deviceId }, @@ -387,7 +387,7 @@ export class DtdlSimulationStrategy extends DeviceIntervalSimulator { }; this.invService.update(deviceToUpdate); - this.eventService.create({ + this.createEvent({ source: { id: deviceId }, diff --git a/simulation-strategies/position-update/position-update.simulation-strategy.ts b/simulation-strategies/position-update/position-update.simulation-strategy.ts index 60f95da..54d00c5 100644 --- a/simulation-strategies/position-update/position-update.simulation-strategy.ts +++ b/simulation-strategies/position-update/position-update.simulation-strategy.ts @@ -85,7 +85,7 @@ export class PositionUpdateSimulationStrategy extends DeviceIntervalSimulator { }; this.updatePosition(deviceId, position); - this.eventService.create({ + this.createEvent({ source: { id: deviceId }, diff --git a/simulation-strategies/random-values/random-value.simulation-strategy.ts b/simulation-strategies/random-values/random-value.simulation-strategy.ts index d22c166..2fca818 100644 --- a/simulation-strategies/random-values/random-value.simulation-strategy.ts +++ b/simulation-strategies/random-values/random-value.simulation-strategy.ts @@ -80,8 +80,8 @@ export class RandomValueSimulationStrategy extends DeviceIntervalSimulator { onTick(groupDeviceId?: any) { const measurementValue = Math.floor(Math.random() * (this.config.maxValue - this.config.minValue + 1)) + this.config.minValue; - this.measurementService.create({ - sourceId: (groupDeviceId ? groupDeviceId : this.config.deviceId), + this.createMeasurement({ + source: { id: (groupDeviceId ? groupDeviceId : this.config.deviceId)}, time: new Date(), [this.config.fragment]: { [this.config.series]: { diff --git a/simulation-strategies/random-walk/random-walk.simulation-strategy.ts b/simulation-strategies/random-walk/random-walk.simulation-strategy.ts index 517856d..0759273 100644 --- a/simulation-strategies/random-walk/random-walk.simulation-strategy.ts +++ b/simulation-strategies/random-walk/random-walk.simulation-strategy.ts @@ -103,8 +103,8 @@ export class RandomWalkSimulationStrategy extends DeviceIntervalSimulator { randomWalkConfigParam.previousValue = measurementValue; this.updateConfigParam(randomWalkConfigParam); - this.measurementService.create({ - sourceId: deviceId, + this.createMeasurement({ + source: { id: deviceId}, time: new Date(), [this.config.fragment]: { [this.config.series]: { diff --git a/simulation-strategies/series-values/series-value.simulation-strategy.ts b/simulation-strategies/series-values/series-value.simulation-strategy.ts index e592f5c..a1f0aff 100644 --- a/simulation-strategies/series-values/series-value.simulation-strategy.ts +++ b/simulation-strategies/series-values/series-value.simulation-strategy.ts @@ -88,8 +88,8 @@ export class SeriesValueSimulationStrategy extends DeviceIntervalSimulator { const measurementValue = valueSeriesConfigParam.seriesvalues[valueSeriesConfigParam.seriesValueMeasurementCounter++]; this.updateConfigParam(valueSeriesConfigParam); - this.measurementService.create({ - sourceId: deviceId, + this.createMeasurement({ + source: { id: deviceId}, time: new Date(), [this.config.fragment]: { [this.config.series]: { diff --git a/simulation-strategies/wave/wave.simulation-strategy.ts b/simulation-strategies/wave/wave.simulation-strategy.ts index 691219a..2c63173 100644 --- a/simulation-strategies/wave/wave.simulation-strategy.ts +++ b/simulation-strategies/wave/wave.simulation-strategy.ts @@ -109,8 +109,8 @@ export class WaveSimulationStrategy extends DeviceIntervalSimulator { break; } - this.measurementService.create({ - sourceId: (groupDeviceId? groupDeviceId : this.config.deviceId), + this.createMeasurement({ + source: { id: (groupDeviceId ? groupDeviceId : this.config.deviceId)}, time: new Date(), [this.config.fragment]: { [this.config.series]: {