2017-05-31 05:50:03 +03:00
|
|
|
// Copyright (c) Microsoft. All rights reserved.
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
2017-07-07 23:49:03 +03:00
|
|
|
using Microsoft.Azure.IoTSolutions.DeviceSimulation.Services.Diagnostics;
|
2017-05-31 05:50:03 +03:00
|
|
|
using Microsoft.Azure.IoTSolutions.DeviceSimulation.Services.Exceptions;
|
|
|
|
using Microsoft.Azure.IoTSolutions.DeviceSimulation.Services.Models;
|
|
|
|
using Microsoft.Azure.IoTSolutions.DeviceSimulation.Services.Runtime;
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
|
|
|
// TODO: tests
|
|
|
|
// TODO: handle errors
|
|
|
|
namespace Microsoft.Azure.IoTSolutions.DeviceSimulation.Services
|
|
|
|
{
|
2017-08-16 22:37:56 +03:00
|
|
|
public interface IDeviceModels
|
2017-05-31 05:50:03 +03:00
|
|
|
{
|
2017-08-16 22:37:56 +03:00
|
|
|
IEnumerable<DeviceModel> GetList();
|
|
|
|
DeviceModel Get(string id);
|
2017-05-31 05:50:03 +03:00
|
|
|
}
|
|
|
|
|
2017-08-16 22:37:56 +03:00
|
|
|
public class DeviceModels : IDeviceModels
|
2017-05-31 05:50:03 +03:00
|
|
|
{
|
Methods and docs (#40)
This PR provides method support for chillers in the default simulation: reboot, firmware update, increase pressure, and decrease pressure. It also keeps reported properties in sync with desired properties (by polling for value changes).
* DeviceMethod implmentation class
* First round of methods, added: 1) infra for registering methods for devices, 2) walking avail. methods for each device. Main functionality missing is allowing specifation and then running the method script.
* remove todo from actor
* nits to code style
* Move methods ownership from devicebootstrap to deviceclient.
* error handling and todos.
* retry method registration, minor fixes.
* nits, logging, removed exception handling for registering methods (it's handled by the parent method)
* bumped version
* code style nits
* Methods use scripts.
Desired properties are polled in UpdateDeviceState and delta is copied into Reported property values.
Telemetry is not sent if the device is marked other than online == "True"
* code style/cleanup changes.
change get query for simulations to linq query rather than iterating the list.
* update version
* Exception handling for updatedevicestate
nit for rename method execution endpoint to include Async suffix.
* nit fix comment code style
* Inject scriptinterpreter. refactoring.
* nits
* state update bug
* no need for scriptengine null check
* Bootstrap individual methods, implement decreasepressure aka EMergencyValveRelease
* remove connection string :(
* fixed threading problem w/ methods, implemented & tested first four methods
* Implement switch to turn random telemetry off; e.g. when increasepressure is called, wait until decreasepressure is called to turn it back on.
* remove clearing of DeviceMethodStatus
* Code style changes + fix some jslint errors
* Fix js methods files
* DeviceMethods code style changes
* Devices.cs code style
* JavascriptInterpreter.cs code style
* script changes for messages, docs
* Cleanup up Javascript functions, create Issue to track implementing functions not yet done.
* Halve the amount of telemetry sent
* Address PR comments.
* issue for script interpreter todo
* todos, nits, pr feedback
2017-09-08 01:08:26 +03:00
|
|
|
private const string EXT = ".json";
|
2017-07-07 23:49:03 +03:00
|
|
|
|
2017-05-31 05:50:03 +03:00
|
|
|
private readonly IServicesConfig config;
|
2017-07-07 23:49:03 +03:00
|
|
|
private readonly ILogger log;
|
2017-05-31 05:50:03 +03:00
|
|
|
|
2017-08-16 22:37:56 +03:00
|
|
|
private List<string> deviceModelFiles;
|
|
|
|
private List<DeviceModel> deviceModels;
|
2017-05-31 05:50:03 +03:00
|
|
|
|
2017-08-16 22:37:56 +03:00
|
|
|
public DeviceModels(
|
2017-07-07 23:49:03 +03:00
|
|
|
IServicesConfig config,
|
|
|
|
ILogger logger)
|
2017-05-31 05:50:03 +03:00
|
|
|
{
|
|
|
|
this.config = config;
|
2017-07-07 23:49:03 +03:00
|
|
|
this.log = logger;
|
2017-08-16 22:37:56 +03:00
|
|
|
this.deviceModelFiles = null;
|
|
|
|
this.deviceModels = null;
|
2017-05-31 05:50:03 +03:00
|
|
|
}
|
|
|
|
|
2017-08-16 22:37:56 +03:00
|
|
|
public IEnumerable<DeviceModel> GetList()
|
2017-05-31 05:50:03 +03:00
|
|
|
{
|
2017-08-16 22:37:56 +03:00
|
|
|
if (this.deviceModels != null) return this.deviceModels;
|
2017-05-31 05:50:03 +03:00
|
|
|
|
2017-08-16 22:37:56 +03:00
|
|
|
this.deviceModels = new List<DeviceModel>();
|
2017-05-31 05:50:03 +03:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2017-08-16 22:37:56 +03:00
|
|
|
var files = this.GetDeviceModelFiles();
|
2017-05-31 05:50:03 +03:00
|
|
|
foreach (var f in files)
|
|
|
|
{
|
2017-08-16 22:37:56 +03:00
|
|
|
var c = JsonConvert.DeserializeObject<DeviceModel>(File.ReadAllText(f));
|
|
|
|
this.deviceModels.Add(c);
|
2017-05-31 05:50:03 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
2017-08-16 22:37:56 +03:00
|
|
|
this.log.Error("Unable to load Device Model configuration",
|
2017-07-07 23:49:03 +03:00
|
|
|
() => new { e.Message, Exception = e });
|
|
|
|
|
2017-08-16 22:37:56 +03:00
|
|
|
throw new InvalidConfigurationException("Unable to load Device Model configuration: " + e.Message, e);
|
2017-05-31 05:50:03 +03:00
|
|
|
}
|
|
|
|
|
2017-08-16 22:37:56 +03:00
|
|
|
return this.deviceModels;
|
2017-05-31 05:50:03 +03:00
|
|
|
}
|
|
|
|
|
2017-08-16 22:37:56 +03:00
|
|
|
public DeviceModel Get(string id)
|
2017-05-31 05:50:03 +03:00
|
|
|
{
|
|
|
|
var list = this.GetList();
|
Methods and docs (#40)
This PR provides method support for chillers in the default simulation: reboot, firmware update, increase pressure, and decrease pressure. It also keeps reported properties in sync with desired properties (by polling for value changes).
* DeviceMethod implmentation class
* First round of methods, added: 1) infra for registering methods for devices, 2) walking avail. methods for each device. Main functionality missing is allowing specifation and then running the method script.
* remove todo from actor
* nits to code style
* Move methods ownership from devicebootstrap to deviceclient.
* error handling and todos.
* retry method registration, minor fixes.
* nits, logging, removed exception handling for registering methods (it's handled by the parent method)
* bumped version
* code style nits
* Methods use scripts.
Desired properties are polled in UpdateDeviceState and delta is copied into Reported property values.
Telemetry is not sent if the device is marked other than online == "True"
* code style/cleanup changes.
change get query for simulations to linq query rather than iterating the list.
* update version
* Exception handling for updatedevicestate
nit for rename method execution endpoint to include Async suffix.
* nit fix comment code style
* Inject scriptinterpreter. refactoring.
* nits
* state update bug
* no need for scriptengine null check
* Bootstrap individual methods, implement decreasepressure aka EMergencyValveRelease
* remove connection string :(
* fixed threading problem w/ methods, implemented & tested first four methods
* Implement switch to turn random telemetry off; e.g. when increasepressure is called, wait until decreasepressure is called to turn it back on.
* remove clearing of DeviceMethodStatus
* Code style changes + fix some jslint errors
* Fix js methods files
* DeviceMethods code style changes
* Devices.cs code style
* JavascriptInterpreter.cs code style
* script changes for messages, docs
* Cleanup up Javascript functions, create Issue to track implementing functions not yet done.
* Halve the amount of telemetry sent
* Address PR comments.
* issue for script interpreter todo
* todos, nits, pr feedback
2017-09-08 01:08:26 +03:00
|
|
|
var item = list.FirstOrDefault(i => i.Id == id);
|
|
|
|
if (item != null)
|
|
|
|
return item;
|
2017-05-31 06:05:48 +03:00
|
|
|
|
2017-08-16 22:37:56 +03:00
|
|
|
this.log.Warn("Device model not found", () => new { id });
|
2017-07-07 23:49:03 +03:00
|
|
|
|
2017-05-31 06:05:48 +03:00
|
|
|
throw new ResourceNotFoundException();
|
2017-05-31 05:50:03 +03:00
|
|
|
}
|
|
|
|
|
2017-08-16 22:37:56 +03:00
|
|
|
private List<string> GetDeviceModelFiles()
|
2017-05-31 05:50:03 +03:00
|
|
|
{
|
2017-08-16 22:37:56 +03:00
|
|
|
if (this.deviceModelFiles != null) return this.deviceModelFiles;
|
2017-05-31 05:50:03 +03:00
|
|
|
|
2017-08-16 22:37:56 +03:00
|
|
|
this.log.Debug("Device models folder", () => new { this.config.DeviceModelsFolder });
|
2017-07-07 23:49:03 +03:00
|
|
|
|
2017-08-16 22:37:56 +03:00
|
|
|
var fileEntries = Directory.GetFiles(this.config.DeviceModelsFolder);
|
2017-05-31 06:05:48 +03:00
|
|
|
|
Methods and docs (#40)
This PR provides method support for chillers in the default simulation: reboot, firmware update, increase pressure, and decrease pressure. It also keeps reported properties in sync with desired properties (by polling for value changes).
* DeviceMethod implmentation class
* First round of methods, added: 1) infra for registering methods for devices, 2) walking avail. methods for each device. Main functionality missing is allowing specifation and then running the method script.
* remove todo from actor
* nits to code style
* Move methods ownership from devicebootstrap to deviceclient.
* error handling and todos.
* retry method registration, minor fixes.
* nits, logging, removed exception handling for registering methods (it's handled by the parent method)
* bumped version
* code style nits
* Methods use scripts.
Desired properties are polled in UpdateDeviceState and delta is copied into Reported property values.
Telemetry is not sent if the device is marked other than online == "True"
* code style/cleanup changes.
change get query for simulations to linq query rather than iterating the list.
* update version
* Exception handling for updatedevicestate
nit for rename method execution endpoint to include Async suffix.
* nit fix comment code style
* Inject scriptinterpreter. refactoring.
* nits
* state update bug
* no need for scriptengine null check
* Bootstrap individual methods, implement decreasepressure aka EMergencyValveRelease
* remove connection string :(
* fixed threading problem w/ methods, implemented & tested first four methods
* Implement switch to turn random telemetry off; e.g. when increasepressure is called, wait until decreasepressure is called to turn it back on.
* remove clearing of DeviceMethodStatus
* Code style changes + fix some jslint errors
* Fix js methods files
* DeviceMethods code style changes
* Devices.cs code style
* JavascriptInterpreter.cs code style
* script changes for messages, docs
* Cleanup up Javascript functions, create Issue to track implementing functions not yet done.
* Halve the amount of telemetry sent
* Address PR comments.
* issue for script interpreter todo
* todos, nits, pr feedback
2017-09-08 01:08:26 +03:00
|
|
|
this.deviceModelFiles = fileEntries.Where(fileName => fileName.EndsWith(EXT)).ToList();
|
2017-05-31 05:50:03 +03:00
|
|
|
|
2017-08-16 22:37:56 +03:00
|
|
|
this.log.Debug("Device model files", () => new { this.deviceModelFiles });
|
2017-07-07 23:49:03 +03:00
|
|
|
|
2017-08-16 22:37:56 +03:00
|
|
|
return this.deviceModelFiles;
|
2017-05-31 05:50:03 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|