2017-05-31 06:05:48 +03:00
|
|
|
// Copyright (c) Microsoft. All rights reserved.
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2017-08-15 20:39:11 +03:00
|
|
|
using System.Threading.Tasks;
|
2017-07-07 23:49:03 +03:00
|
|
|
using Microsoft.Azure.IoTSolutions.DeviceSimulation.Services.Diagnostics;
|
2017-05-31 06:05:48 +03:00
|
|
|
using Microsoft.Azure.IoTSolutions.DeviceSimulation.Services.Exceptions;
|
|
|
|
using Microsoft.Azure.IoTSolutions.DeviceSimulation.Services.Models;
|
2017-09-23 04:34:25 +03:00
|
|
|
using Microsoft.Azure.IoTSolutions.DeviceSimulation.Services.StorageAdapter;
|
2017-05-31 06:05:48 +03:00
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
|
|
|
namespace Microsoft.Azure.IoTSolutions.DeviceSimulation.Services
|
|
|
|
{
|
|
|
|
public interface ISimulations
|
|
|
|
{
|
2017-08-15 20:39:11 +03:00
|
|
|
Task<IList<Models.Simulation>> GetListAsync();
|
|
|
|
Task<Models.Simulation> GetAsync(string id);
|
|
|
|
Task<Models.Simulation> InsertAsync(Models.Simulation simulation, string template = "");
|
|
|
|
Task<Models.Simulation> UpsertAsync(Models.Simulation simulation);
|
|
|
|
Task<Models.Simulation> MergeAsync(SimulationPatch patch);
|
2017-08-22 03:47:50 +03:00
|
|
|
Task DeleteAsync(string id);
|
2017-05-31 06:05:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public class Simulations : ISimulations
|
|
|
|
{
|
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 STORAGE_COLLECTION = "simulations";
|
|
|
|
private const string SIMULATION_ID = "1";
|
|
|
|
private const int DEVICES_PER_MODEL_IN_DEFAULT_TEMPLATE = 1;
|
2017-05-31 06:05:48 +03:00
|
|
|
|
2017-08-16 22:37:56 +03:00
|
|
|
private readonly IDeviceModels deviceModels;
|
2017-08-15 20:39:11 +03:00
|
|
|
private readonly IStorageAdapterClient storage;
|
2017-07-07 23:49:03 +03:00
|
|
|
private readonly ILogger log;
|
2017-05-31 06:05:48 +03:00
|
|
|
|
2017-07-07 23:49:03 +03:00
|
|
|
public Simulations(
|
2017-08-16 22:37:56 +03:00
|
|
|
IDeviceModels deviceModels,
|
2017-08-15 20:39:11 +03:00
|
|
|
IStorageAdapterClient storage,
|
2017-07-07 23:49:03 +03:00
|
|
|
ILogger logger)
|
2017-05-31 06:05:48 +03:00
|
|
|
{
|
2017-08-16 22:37:56 +03:00
|
|
|
this.deviceModels = deviceModels;
|
2017-08-15 20:39:11 +03:00
|
|
|
this.storage = storage;
|
2017-07-07 23:49:03 +03:00
|
|
|
this.log = logger;
|
2017-05-31 06:05:48 +03:00
|
|
|
}
|
|
|
|
|
2017-08-15 20:39:11 +03:00
|
|
|
public async Task<IList<Models.Simulation>> GetListAsync()
|
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
|
|
|
var data = await this.storage.GetAllAsync(STORAGE_COLLECTION);
|
2017-08-15 20:39:11 +03:00
|
|
|
var result = new List<Models.Simulation>();
|
|
|
|
foreach (var item in data.Items)
|
2017-05-31 06:05:48 +03:00
|
|
|
{
|
2017-08-15 20:39:11 +03:00
|
|
|
var simulation = JsonConvert.DeserializeObject<Models.Simulation>(item.Data);
|
|
|
|
simulation.Etag = item.ETag;
|
|
|
|
result.Add(simulation);
|
2017-05-31 06:05:48 +03:00
|
|
|
}
|
|
|
|
|
2017-08-15 20:39:11 +03:00
|
|
|
return result;
|
|
|
|
}
|
2017-07-07 23:49:03 +03:00
|
|
|
|
2017-08-15 20:39:11 +03:00
|
|
|
public async Task<Models.Simulation> GetAsync(string id)
|
|
|
|
{
|
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 = await this.storage.GetAsync(STORAGE_COLLECTION, id);
|
2017-08-15 20:39:11 +03:00
|
|
|
var simulation = JsonConvert.DeserializeObject<Models.Simulation>(item.Data);
|
|
|
|
simulation.Etag = item.ETag;
|
|
|
|
return simulation;
|
2017-05-31 06:05:48 +03:00
|
|
|
}
|
|
|
|
|
2017-08-15 20:39:11 +03:00
|
|
|
public async Task<Models.Simulation> InsertAsync(Models.Simulation simulation, string template = "")
|
2017-05-31 06:05:48 +03:00
|
|
|
{
|
|
|
|
// TODO: complete validation
|
|
|
|
if (!string.IsNullOrEmpty(template) && template.ToLowerInvariant() != "default")
|
2017-07-07 23:49:03 +03:00
|
|
|
{
|
|
|
|
this.log.Warn("Unknown template name", () => new { template });
|
2017-05-31 06:05:48 +03:00
|
|
|
throw new InvalidInputException("Unknown template name. Try 'default'.");
|
2017-07-07 23:49:03 +03:00
|
|
|
}
|
2017-05-31 06:05:48 +03:00
|
|
|
|
2017-08-15 20:39:11 +03:00
|
|
|
var simulations = await this.GetListAsync();
|
2017-05-31 06:05:48 +03:00
|
|
|
if (simulations.Count > 0)
|
|
|
|
{
|
2017-08-15 20:39:11 +03:00
|
|
|
this.log.Warn("There is already a simulation", () => { });
|
2017-05-31 06:05:48 +03:00
|
|
|
throw new ConflictingResourceException(
|
|
|
|
"There is already a simulation. Only one simulation can be created.");
|
|
|
|
}
|
|
|
|
|
2017-08-15 20:39:11 +03:00
|
|
|
// Note: forcing the ID because only one simulation can be created
|
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
|
|
|
simulation.Id = SIMULATION_ID;
|
2017-05-31 06:05:48 +03:00
|
|
|
simulation.Created = DateTimeOffset.UtcNow;
|
|
|
|
simulation.Modified = simulation.Created;
|
|
|
|
simulation.Version = 1;
|
|
|
|
|
|
|
|
// Create default simulation
|
|
|
|
if (!string.IsNullOrEmpty(template) && template.ToLowerInvariant() == "default")
|
|
|
|
{
|
2017-08-16 22:37:56 +03:00
|
|
|
var types = this.deviceModels.GetList();
|
|
|
|
simulation.DeviceModels = new List<Models.Simulation.DeviceModelRef>();
|
2017-05-31 06:05:48 +03:00
|
|
|
foreach (var type in types)
|
|
|
|
{
|
2017-08-16 22:37:56 +03:00
|
|
|
simulation.DeviceModels.Add(new Models.Simulation.DeviceModelRef
|
2017-05-31 06:05:48 +03:00
|
|
|
{
|
|
|
|
Id = type.Id,
|
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
|
|
|
Count = DEVICES_PER_MODEL_IN_DEFAULT_TEMPLATE
|
2017-05-31 06:05:48 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-15 20:39:11 +03:00
|
|
|
// Note: using UpdateAsync because the service generates the ID
|
2017-08-16 22:37:56 +03:00
|
|
|
var result = await this.storage.UpdateAsync(
|
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
|
|
|
STORAGE_COLLECTION,
|
|
|
|
SIMULATION_ID,
|
2017-08-15 20:39:11 +03:00
|
|
|
JsonConvert.SerializeObject(simulation),
|
|
|
|
"*");
|
2017-05-31 06:05:48 +03:00
|
|
|
|
2017-08-16 22:37:56 +03:00
|
|
|
simulation.Etag = result.ETag;
|
|
|
|
|
2017-05-31 06:05:48 +03:00
|
|
|
return simulation;
|
|
|
|
}
|
|
|
|
|
2017-08-15 20:39:11 +03:00
|
|
|
/// <summary>
|
|
|
|
/// Upsert the simulation. The logic works under the assumption that
|
|
|
|
/// there is only one simulation with id "1".
|
|
|
|
/// </summary>
|
|
|
|
public async Task<Models.Simulation> UpsertAsync(Models.Simulation simulation)
|
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
|
|
|
if (simulation.Id != SIMULATION_ID)
|
2017-05-31 06:05:48 +03:00
|
|
|
{
|
2017-08-15 20:39:11 +03:00
|
|
|
this.log.Warn("Invalid simulation ID. Only one simulation is allowed", () => { });
|
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
|
|
|
throw new InvalidInputException("Invalid simulation ID. Use ID '" + SIMULATION_ID + "'.");
|
2017-05-31 06:05:48 +03:00
|
|
|
}
|
|
|
|
|
2017-08-15 20:39:11 +03:00
|
|
|
var simulations = await this.GetListAsync();
|
|
|
|
if (simulations.Count > 0)
|
2017-05-31 06:05:48 +03:00
|
|
|
{
|
2017-09-19 04:33:50 +03:00
|
|
|
this.log.Info("Modifying simulation via PUT.", () => { });
|
|
|
|
|
2017-10-19 08:21:01 +03:00
|
|
|
if (simulation.Etag != simulations[0].Etag)
|
|
|
|
{
|
2017-09-19 04:33:50 +03:00
|
|
|
this.log.Error("Invalid Etag. Running simulation Etag is:'", () => new { simulations });
|
|
|
|
throw new InvalidInputException("Invalid Etag. Running simulation Etag is:'" + simulations[0].Etag + "'.");
|
|
|
|
}
|
|
|
|
|
|
|
|
simulation.Created = simulations[0].Created;
|
|
|
|
simulation.Modified = DateTimeOffset.UtcNow;
|
|
|
|
simulation.Version = simulations[0].Version + 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.log.Info("Creating new simulation via PUT.", () => { });
|
|
|
|
// new simulation
|
|
|
|
simulation.Created = DateTimeOffset.UtcNow;
|
|
|
|
simulation.Modified = simulation.Created;
|
|
|
|
simulation.Version = 1;
|
2017-05-31 06:05:48 +03:00
|
|
|
}
|
|
|
|
|
2017-08-15 20:39:11 +03:00
|
|
|
// Note: forcing the ID because only one simulation can be created
|
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
|
|
|
simulation.Id = SIMULATION_ID;
|
2017-10-19 08:21:01 +03:00
|
|
|
var item = await this.storage.UpdateAsync(
|
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
|
|
|
STORAGE_COLLECTION,
|
|
|
|
SIMULATION_ID,
|
2017-08-15 20:39:11 +03:00
|
|
|
JsonConvert.SerializeObject(simulation),
|
|
|
|
simulation.Etag);
|
2017-05-31 06:05:48 +03:00
|
|
|
|
2017-10-19 08:21:01 +03:00
|
|
|
// Return the new etag provided by the storage
|
|
|
|
simulation.Etag = item.ETag;
|
|
|
|
|
2017-05-31 06:05:48 +03:00
|
|
|
return simulation;
|
|
|
|
}
|
|
|
|
|
2017-08-15 20:39:11 +03:00
|
|
|
public async Task<Models.Simulation> MergeAsync(SimulationPatch patch)
|
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
|
|
|
if (patch.Id != SIMULATION_ID)
|
2017-05-31 06:05:48 +03:00
|
|
|
{
|
2017-08-15 20:39:11 +03:00
|
|
|
this.log.Warn("Invalid simulation ID.", () => new { patch.Id });
|
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
|
|
|
throw new InvalidInputException("Invalid simulation ID. Use ID '" + SIMULATION_ID + "'.");
|
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
|
|
|
var item = await this.storage.GetAsync(STORAGE_COLLECTION, patch.Id);
|
2017-08-15 20:39:11 +03:00
|
|
|
var simulation = JsonConvert.DeserializeObject<Models.Simulation>(item.Data);
|
|
|
|
simulation.Etag = item.ETag;
|
2017-05-31 06:05:48 +03:00
|
|
|
|
2017-08-15 20:39:11 +03:00
|
|
|
// Even when there's nothing to do, verify the etag mismatch
|
|
|
|
if (patch.Etag != simulation.Etag)
|
2017-05-31 06:05:48 +03:00
|
|
|
{
|
2017-08-15 20:39:11 +03:00
|
|
|
this.log.Warn("Etag mismatch",
|
|
|
|
() => new { Current = simulation.Etag, Provided = patch.Etag });
|
|
|
|
throw new ConflictingResourceException(
|
|
|
|
$"The ETag provided doesn't match the current resource ETag ({simulation.Etag}).");
|
2017-05-31 06:05:48 +03:00
|
|
|
}
|
|
|
|
|
2017-08-15 20:39:11 +03:00
|
|
|
if (!patch.Enabled.HasValue || patch.Enabled.Value == simulation.Enabled)
|
2017-05-31 06:05:48 +03:00
|
|
|
{
|
2017-08-15 20:39:11 +03:00
|
|
|
// Nothing to do
|
|
|
|
return simulation;
|
2017-05-31 06:05:48 +03:00
|
|
|
}
|
|
|
|
|
2017-08-15 20:39:11 +03:00
|
|
|
simulation.Enabled = patch.Enabled.Value;
|
|
|
|
simulation.Modified = DateTimeOffset.UtcNow;
|
|
|
|
simulation.Version += 1;
|
2017-05-31 06:05:48 +03:00
|
|
|
|
2017-08-15 20:39:11 +03:00
|
|
|
item = await this.storage.UpdateAsync(
|
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
|
|
|
STORAGE_COLLECTION,
|
|
|
|
SIMULATION_ID,
|
2017-08-15 20:39:11 +03:00
|
|
|
JsonConvert.SerializeObject(simulation),
|
|
|
|
patch.Etag);
|
2017-07-07 23:49:03 +03:00
|
|
|
|
2017-08-15 20:39:11 +03:00
|
|
|
simulation.Etag = item.ETag;
|
2017-05-31 06:05:48 +03:00
|
|
|
|
2017-08-15 20:39:11 +03:00
|
|
|
return simulation;
|
2017-05-31 06:05:48 +03:00
|
|
|
}
|
2017-08-22 03:47:50 +03:00
|
|
|
|
|
|
|
public async Task DeleteAsync(string id)
|
|
|
|
{
|
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
|
|
|
await this.storage.DeleteAsync(STORAGE_COLLECTION, id);
|
2017-08-22 03:47:50 +03:00
|
|
|
}
|
2017-05-31 06:05:48 +03:00
|
|
|
}
|
|
|
|
}
|