Skip to content
This repository has been archived by the owner on Nov 15, 2024. It is now read-only.

Commit

Permalink
Device availability issue fixed for browser based simulator
Browse files Browse the repository at this point in the history
  • Loading branch information
DarpanLalani committed Nov 27, 2023
1 parent fa9dfe9 commit 10b0274
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 20 deletions.
10 changes: 8 additions & 2 deletions builder/simulator-config/new-simulator-modal.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
26 changes: 25 additions & 1 deletion builder/simulator/device-interval-simulator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

/**
Expand All @@ -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(() => {
Expand Down Expand Up @@ -70,4 +72,26 @@ export abstract class DeviceIntervalSimulator extends DeviceSimulator {

return response;
}

async createMeasurement(entity: Partial<IMeasurementCreate>) {
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'
});
}
}
16 changes: 8 additions & 8 deletions simulation-strategies/dtdl/dtdl.simulation-strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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]: {
Expand Down Expand Up @@ -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
},
Expand Down Expand Up @@ -387,7 +387,7 @@ export class DtdlSimulationStrategy extends DeviceIntervalSimulator {
};
this.invService.update(deviceToUpdate);

this.eventService.create({
this.createEvent({
source: {
id: deviceId
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export class PositionUpdateSimulationStrategy extends DeviceIntervalSimulator {
};
this.updatePosition(deviceId, position);

this.eventService.create({
this.createEvent({
source: {
id: deviceId
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]: {
Expand Down
4 changes: 2 additions & 2 deletions simulation-strategies/wave/wave.simulation-strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]: {
Expand Down

0 comments on commit 10b0274

Please sign in to comment.