device-simulation-dotnet/Services/DeviceModels.cs

97 строки
2.9 KiB
C#
Исходник Обычный вид История

// Copyright (c) Microsoft. All rights reserved.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.Azure.IoTSolutions.DeviceSimulation.Services.Diagnostics;
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
{
public interface IDeviceModels
{
IEnumerable<DeviceModel> GetList();
DeviceModel Get(string id);
}
public class DeviceModels : IDeviceModels
{
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";
private readonly IServicesConfig config;
private readonly ILogger log;
private List<string> deviceModelFiles;
private List<DeviceModel> deviceModels;
public DeviceModels(
IServicesConfig config,
ILogger logger)
{
this.config = config;
this.log = logger;
this.deviceModelFiles = null;
this.deviceModels = null;
}
public IEnumerable<DeviceModel> GetList()
{
if (this.deviceModels != null) return this.deviceModels;
this.deviceModels = new List<DeviceModel>();
try
{
var files = this.GetDeviceModelFiles();
foreach (var f in files)
{
var c = JsonConvert.DeserializeObject<DeviceModel>(File.ReadAllText(f));
this.deviceModels.Add(c);
}
}
catch (Exception e)
{
this.log.Error("Unable to load Device Model configuration",
() => new { e.Message, Exception = e });
throw new InvalidConfigurationException("Unable to load Device Model configuration: " + e.Message, e);
}
return this.deviceModels;
}
public DeviceModel Get(string id)
{
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;
this.log.Warn("Device model not found", () => new { id });
throw new ResourceNotFoundException();
}
private List<string> GetDeviceModelFiles()
{
if (this.deviceModelFiles != null) return this.deviceModelFiles;
this.log.Debug("Device models folder", () => new { this.config.DeviceModelsFolder });
var fileEntries = Directory.GetFiles(this.config.DeviceModelsFolder);
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();
this.log.Debug("Device model files", () => new { this.deviceModelFiles });
return this.deviceModelFiles;
}
}
}