2017-05-31 06:05:48 +03:00
|
|
|
// Copyright (c) Microsoft. All rights reserved.
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2018-10-30 10:10:37 +03:00
|
|
|
using System.IO;
|
Merge azure-iot-pcs-simulation into master (#197)
* Rewrite to use fewer threads and increase throughput
* Expose simulation metrics in the status endpoint
* When available, return a URL to the Azure Portal metrics of the pre-provisioned IoT Hub
* Allow to run a simulation against a custom IoT Hub, passing the connection string
* Allow to schedule a simulation, defining start and end time
* Improve security by running the service as a non-root user
* Support multiple behavior scripts in a device model
* Support custom device with custom sensors, behavior and frequency
* Allow to override the initial device state when creating a simulation
* When a simulation starts, create all the devices in batches
* When a simulation is deleted, delete also the devices
* Refactor timers to start sending telemetry as soon as possible
* Refactor and improve how simulation scripts access state and properties
* Change stock models to use AMQP (#189)
* Overall improvements to exceptions handling
* Disable SDK retry, and change timeout from default (4 minutes) to 10 seconds
* Do not retry sending telemetry on failure, skip message
* Use IoT Hub S2 SKU limits by default
* Upgrade to .NET Core 2.0.3
* Upgrade Azure IoT SDK: Devices 1.4.1 to 1.6.0, Devices Client 1.5.2 to 1.7.0
* Run simulation engine in the same process used for the web service
* Move docs, move wiki, new API specs, fix scripts for Windows Bash
* Fix the spelling of “ETag” and allow ETag=*
* Add internal scripts for increasing/decreasing/random telemetry
* Add env vars documentation ENVIRONMENT_VARIABLES.md
* Add more optional logging by device and by actor
* Use logging level and other logging settings from configuration
* Adjust unit test precision to mitigate flaky test
* Add system properties to telemetry messages
* Removing the squash flag from Docker scripts
* Use env vars in launchSettings.json
2018-04-13 03:42:46 +03:00
|
|
|
using System.Linq;
|
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;
|
Merge azure-iot-pcs-simulation into master (#197)
* Rewrite to use fewer threads and increase throughput
* Expose simulation metrics in the status endpoint
* When available, return a URL to the Azure Portal metrics of the pre-provisioned IoT Hub
* Allow to run a simulation against a custom IoT Hub, passing the connection string
* Allow to schedule a simulation, defining start and end time
* Improve security by running the service as a non-root user
* Support multiple behavior scripts in a device model
* Support custom device with custom sensors, behavior and frequency
* Allow to override the initial device state when creating a simulation
* When a simulation starts, create all the devices in batches
* When a simulation is deleted, delete also the devices
* Refactor timers to start sending telemetry as soon as possible
* Refactor and improve how simulation scripts access state and properties
* Change stock models to use AMQP (#189)
* Overall improvements to exceptions handling
* Disable SDK retry, and change timeout from default (4 minutes) to 10 seconds
* Do not retry sending telemetry on failure, skip message
* Use IoT Hub S2 SKU limits by default
* Upgrade to .NET Core 2.0.3
* Upgrade Azure IoT SDK: Devices 1.4.1 to 1.6.0, Devices Client 1.5.2 to 1.7.0
* Run simulation engine in the same process used for the web service
* Move docs, move wiki, new API specs, fix scripts for Windows Bash
* Fix the spelling of “ETag” and allow ETag=*
* Add internal scripts for increasing/decreasing/random telemetry
* Add env vars documentation ENVIRONMENT_VARIABLES.md
* Add more optional logging by device and by actor
* Use logging level and other logging settings from configuration
* Adjust unit test precision to mitigate flaky test
* Add system properties to telemetry messages
* Removing the squash flag from Docker scripts
* Use env vars in launchSettings.json
2018-04-13 03:42:46 +03:00
|
|
|
using Microsoft.Azure.IoTSolutions.DeviceSimulation.Services.IotHub;
|
2017-05-31 06:05:48 +03:00
|
|
|
using Microsoft.Azure.IoTSolutions.DeviceSimulation.Services.Models;
|
2018-09-19 08:22:47 +03:00
|
|
|
using Microsoft.Azure.IoTSolutions.DeviceSimulation.Services.Runtime;
|
2018-10-27 01:49:22 +03:00
|
|
|
using Microsoft.Azure.IoTSolutions.DeviceSimulation.Services.Statistics;
|
2018-09-19 08:22:47 +03:00
|
|
|
using Microsoft.Azure.IoTSolutions.DeviceSimulation.Services.Storage;
|
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
|
|
|
|
{
|
2018-10-01 20:08:31 +03:00
|
|
|
// Get list of simulations.
|
2018-10-30 02:36:27 +03:00
|
|
|
// This will have better perf than GetListWithStatisticsAsync.
|
2017-08-15 20:39:11 +03:00
|
|
|
Task<IList<Models.Simulation>> GetListAsync();
|
Merge azure-iot-pcs-simulation into master (#197)
* Rewrite to use fewer threads and increase throughput
* Expose simulation metrics in the status endpoint
* When available, return a URL to the Azure Portal metrics of the pre-provisioned IoT Hub
* Allow to run a simulation against a custom IoT Hub, passing the connection string
* Allow to schedule a simulation, defining start and end time
* Improve security by running the service as a non-root user
* Support multiple behavior scripts in a device model
* Support custom device with custom sensors, behavior and frequency
* Allow to override the initial device state when creating a simulation
* When a simulation starts, create all the devices in batches
* When a simulation is deleted, delete also the devices
* Refactor timers to start sending telemetry as soon as possible
* Refactor and improve how simulation scripts access state and properties
* Change stock models to use AMQP (#189)
* Overall improvements to exceptions handling
* Disable SDK retry, and change timeout from default (4 minutes) to 10 seconds
* Do not retry sending telemetry on failure, skip message
* Use IoT Hub S2 SKU limits by default
* Upgrade to .NET Core 2.0.3
* Upgrade Azure IoT SDK: Devices 1.4.1 to 1.6.0, Devices Client 1.5.2 to 1.7.0
* Run simulation engine in the same process used for the web service
* Move docs, move wiki, new API specs, fix scripts for Windows Bash
* Fix the spelling of “ETag” and allow ETag=*
* Add internal scripts for increasing/decreasing/random telemetry
* Add env vars documentation ENVIRONMENT_VARIABLES.md
* Add more optional logging by device and by actor
* Use logging level and other logging settings from configuration
* Adjust unit test precision to mitigate flaky test
* Add system properties to telemetry messages
* Removing the squash flag from Docker scripts
* Use env vars in launchSettings.json
2018-04-13 03:42:46 +03:00
|
|
|
|
2018-10-30 02:36:27 +03:00
|
|
|
// Get list of simulations with statistics.
|
|
|
|
Task<IList<Models.Simulation>> GetListWithStatisticsAsync();
|
|
|
|
|
2018-10-01 20:08:31 +03:00
|
|
|
// Get a simulation.
|
2018-10-30 02:36:27 +03:00
|
|
|
// This will have better perf than GetWithStatisticsAsync.
|
2017-08-15 20:39:11 +03:00
|
|
|
Task<Models.Simulation> GetAsync(string id);
|
Merge azure-iot-pcs-simulation into master (#197)
* Rewrite to use fewer threads and increase throughput
* Expose simulation metrics in the status endpoint
* When available, return a URL to the Azure Portal metrics of the pre-provisioned IoT Hub
* Allow to run a simulation against a custom IoT Hub, passing the connection string
* Allow to schedule a simulation, defining start and end time
* Improve security by running the service as a non-root user
* Support multiple behavior scripts in a device model
* Support custom device with custom sensors, behavior and frequency
* Allow to override the initial device state when creating a simulation
* When a simulation starts, create all the devices in batches
* When a simulation is deleted, delete also the devices
* Refactor timers to start sending telemetry as soon as possible
* Refactor and improve how simulation scripts access state and properties
* Change stock models to use AMQP (#189)
* Overall improvements to exceptions handling
* Disable SDK retry, and change timeout from default (4 minutes) to 10 seconds
* Do not retry sending telemetry on failure, skip message
* Use IoT Hub S2 SKU limits by default
* Upgrade to .NET Core 2.0.3
* Upgrade Azure IoT SDK: Devices 1.4.1 to 1.6.0, Devices Client 1.5.2 to 1.7.0
* Run simulation engine in the same process used for the web service
* Move docs, move wiki, new API specs, fix scripts for Windows Bash
* Fix the spelling of “ETag” and allow ETag=*
* Add internal scripts for increasing/decreasing/random telemetry
* Add env vars documentation ENVIRONMENT_VARIABLES.md
* Add more optional logging by device and by actor
* Use logging level and other logging settings from configuration
* Adjust unit test precision to mitigate flaky test
* Add system properties to telemetry messages
* Removing the squash flag from Docker scripts
* Use env vars in launchSettings.json
2018-04-13 03:42:46 +03:00
|
|
|
|
2018-10-27 01:49:22 +03:00
|
|
|
// Get a simulation with statistics.
|
|
|
|
Task<Models.Simulation> GetWithStatisticsAsync(string id);
|
|
|
|
|
2018-10-01 20:08:31 +03:00
|
|
|
// Create a simulation.
|
2017-08-15 20:39:11 +03:00
|
|
|
Task<Models.Simulation> InsertAsync(Models.Simulation simulation, string template = "");
|
Merge azure-iot-pcs-simulation into master (#197)
* Rewrite to use fewer threads and increase throughput
* Expose simulation metrics in the status endpoint
* When available, return a URL to the Azure Portal metrics of the pre-provisioned IoT Hub
* Allow to run a simulation against a custom IoT Hub, passing the connection string
* Allow to schedule a simulation, defining start and end time
* Improve security by running the service as a non-root user
* Support multiple behavior scripts in a device model
* Support custom device with custom sensors, behavior and frequency
* Allow to override the initial device state when creating a simulation
* When a simulation starts, create all the devices in batches
* When a simulation is deleted, delete also the devices
* Refactor timers to start sending telemetry as soon as possible
* Refactor and improve how simulation scripts access state and properties
* Change stock models to use AMQP (#189)
* Overall improvements to exceptions handling
* Disable SDK retry, and change timeout from default (4 minutes) to 10 seconds
* Do not retry sending telemetry on failure, skip message
* Use IoT Hub S2 SKU limits by default
* Upgrade to .NET Core 2.0.3
* Upgrade Azure IoT SDK: Devices 1.4.1 to 1.6.0, Devices Client 1.5.2 to 1.7.0
* Run simulation engine in the same process used for the web service
* Move docs, move wiki, new API specs, fix scripts for Windows Bash
* Fix the spelling of “ETag” and allow ETag=*
* Add internal scripts for increasing/decreasing/random telemetry
* Add env vars documentation ENVIRONMENT_VARIABLES.md
* Add more optional logging by device and by actor
* Use logging level and other logging settings from configuration
* Adjust unit test precision to mitigate flaky test
* Add system properties to telemetry messages
* Removing the squash flag from Docker scripts
* Use env vars in launchSettings.json
2018-04-13 03:42:46 +03:00
|
|
|
|
2018-10-01 20:08:31 +03:00
|
|
|
// Create or Replace a simulation.
|
2018-12-11 01:59:37 +03:00
|
|
|
Task<Models.Simulation> UpsertAsync(Models.Simulation simulation, bool validateHubCredentials);
|
Merge azure-iot-pcs-simulation into master (#197)
* Rewrite to use fewer threads and increase throughput
* Expose simulation metrics in the status endpoint
* When available, return a URL to the Azure Portal metrics of the pre-provisioned IoT Hub
* Allow to run a simulation against a custom IoT Hub, passing the connection string
* Allow to schedule a simulation, defining start and end time
* Improve security by running the service as a non-root user
* Support multiple behavior scripts in a device model
* Support custom device with custom sensors, behavior and frequency
* Allow to override the initial device state when creating a simulation
* When a simulation starts, create all the devices in batches
* When a simulation is deleted, delete also the devices
* Refactor timers to start sending telemetry as soon as possible
* Refactor and improve how simulation scripts access state and properties
* Change stock models to use AMQP (#189)
* Overall improvements to exceptions handling
* Disable SDK retry, and change timeout from default (4 minutes) to 10 seconds
* Do not retry sending telemetry on failure, skip message
* Use IoT Hub S2 SKU limits by default
* Upgrade to .NET Core 2.0.3
* Upgrade Azure IoT SDK: Devices 1.4.1 to 1.6.0, Devices Client 1.5.2 to 1.7.0
* Run simulation engine in the same process used for the web service
* Move docs, move wiki, new API specs, fix scripts for Windows Bash
* Fix the spelling of “ETag” and allow ETag=*
* Add internal scripts for increasing/decreasing/random telemetry
* Add env vars documentation ENVIRONMENT_VARIABLES.md
* Add more optional logging by device and by actor
* Use logging level and other logging settings from configuration
* Adjust unit test precision to mitigate flaky test
* Add system properties to telemetry messages
* Removing the squash flag from Docker scripts
* Use env vars in launchSettings.json
2018-04-13 03:42:46 +03:00
|
|
|
|
2018-10-01 20:08:31 +03:00
|
|
|
// Modify a simulation.
|
2017-08-15 20:39:11 +03:00
|
|
|
Task<Models.Simulation> MergeAsync(SimulationPatch patch);
|
Merge azure-iot-pcs-simulation into master (#197)
* Rewrite to use fewer threads and increase throughput
* Expose simulation metrics in the status endpoint
* When available, return a URL to the Azure Portal metrics of the pre-provisioned IoT Hub
* Allow to run a simulation against a custom IoT Hub, passing the connection string
* Allow to schedule a simulation, defining start and end time
* Improve security by running the service as a non-root user
* Support multiple behavior scripts in a device model
* Support custom device with custom sensors, behavior and frequency
* Allow to override the initial device state when creating a simulation
* When a simulation starts, create all the devices in batches
* When a simulation is deleted, delete also the devices
* Refactor timers to start sending telemetry as soon as possible
* Refactor and improve how simulation scripts access state and properties
* Change stock models to use AMQP (#189)
* Overall improvements to exceptions handling
* Disable SDK retry, and change timeout from default (4 minutes) to 10 seconds
* Do not retry sending telemetry on failure, skip message
* Use IoT Hub S2 SKU limits by default
* Upgrade to .NET Core 2.0.3
* Upgrade Azure IoT SDK: Devices 1.4.1 to 1.6.0, Devices Client 1.5.2 to 1.7.0
* Run simulation engine in the same process used for the web service
* Move docs, move wiki, new API specs, fix scripts for Windows Bash
* Fix the spelling of “ETag” and allow ETag=*
* Add internal scripts for increasing/decreasing/random telemetry
* Add env vars documentation ENVIRONMENT_VARIABLES.md
* Add more optional logging by device and by actor
* Use logging level and other logging settings from configuration
* Adjust unit test precision to mitigate flaky test
* Add system properties to telemetry messages
* Removing the squash flag from Docker scripts
* Use env vars in launchSettings.json
2018-04-13 03:42:46 +03:00
|
|
|
|
2018-10-01 20:08:31 +03:00
|
|
|
// Add a device to simulation
|
2018-08-27 21:38:58 +03:00
|
|
|
Task AddDeviceAsync(string id);
|
|
|
|
|
2018-11-30 04:29:58 +03:00
|
|
|
// Delete a simulation and its statistics (not the devices).
|
2017-08-22 03:47:50 +03:00
|
|
|
Task DeleteAsync(string id);
|
Merge azure-iot-pcs-simulation into master (#197)
* Rewrite to use fewer threads and increase throughput
* Expose simulation metrics in the status endpoint
* When available, return a URL to the Azure Portal metrics of the pre-provisioned IoT Hub
* Allow to run a simulation against a custom IoT Hub, passing the connection string
* Allow to schedule a simulation, defining start and end time
* Improve security by running the service as a non-root user
* Support multiple behavior scripts in a device model
* Support custom device with custom sensors, behavior and frequency
* Allow to override the initial device state when creating a simulation
* When a simulation starts, create all the devices in batches
* When a simulation is deleted, delete also the devices
* Refactor timers to start sending telemetry as soon as possible
* Refactor and improve how simulation scripts access state and properties
* Change stock models to use AMQP (#189)
* Overall improvements to exceptions handling
* Disable SDK retry, and change timeout from default (4 minutes) to 10 seconds
* Do not retry sending telemetry on failure, skip message
* Use IoT Hub S2 SKU limits by default
* Upgrade to .NET Core 2.0.3
* Upgrade Azure IoT SDK: Devices 1.4.1 to 1.6.0, Devices Client 1.5.2 to 1.7.0
* Run simulation engine in the same process used for the web service
* Move docs, move wiki, new API specs, fix scripts for Windows Bash
* Fix the spelling of “ETag” and allow ETag=*
* Add internal scripts for increasing/decreasing/random telemetry
* Add env vars documentation ENVIRONMENT_VARIABLES.md
* Add more optional logging by device and by actor
* Use logging level and other logging settings from configuration
* Adjust unit test precision to mitigate flaky test
* Add system properties to telemetry messages
* Removing the squash flag from Docker scripts
* Use env vars in launchSettings.json
2018-04-13 03:42:46 +03:00
|
|
|
|
2018-10-30 10:10:37 +03:00
|
|
|
// Seed default simulations
|
|
|
|
Task TrySeedAsync();
|
|
|
|
|
2018-10-01 20:08:31 +03:00
|
|
|
// Try to start a job to create all the devices
|
|
|
|
Task<bool> TryToStartDevicesCreationAsync(string simulationId, IDevices devices);
|
|
|
|
|
|
|
|
// Change the simulation, setting the device creation complete
|
|
|
|
Task<bool> TryToSetDeviceCreationCompleteAsync(string simulationId);
|
|
|
|
|
2018-11-09 01:59:18 +03:00
|
|
|
// Create a job to delete all the devices (used by web service on user request)
|
|
|
|
Task<(bool jobCreated, string jobId)> DeleteAllDevicesAsync(string simulationId, IDevices devices);
|
|
|
|
|
|
|
|
// Try to start a job to delete all the devices (used by simulation agent when a simulation ends)
|
2018-10-13 02:49:29 +03:00
|
|
|
Task<bool> TryToStartDevicesDeletionAsync(string simulationId, IDevices devices);
|
|
|
|
|
|
|
|
// Change the simulation, setting the device deletion complete
|
|
|
|
Task<bool> TryToSetDeviceDeletionCompleteAsync(string simulationId);
|
|
|
|
|
2018-10-01 20:08:31 +03:00
|
|
|
// Get the ID of the devices in a simulation.
|
Merge azure-iot-pcs-simulation into master (#197)
* Rewrite to use fewer threads and increase throughput
* Expose simulation metrics in the status endpoint
* When available, return a URL to the Azure Portal metrics of the pre-provisioned IoT Hub
* Allow to run a simulation against a custom IoT Hub, passing the connection string
* Allow to schedule a simulation, defining start and end time
* Improve security by running the service as a non-root user
* Support multiple behavior scripts in a device model
* Support custom device with custom sensors, behavior and frequency
* Allow to override the initial device state when creating a simulation
* When a simulation starts, create all the devices in batches
* When a simulation is deleted, delete also the devices
* Refactor timers to start sending telemetry as soon as possible
* Refactor and improve how simulation scripts access state and properties
* Change stock models to use AMQP (#189)
* Overall improvements to exceptions handling
* Disable SDK retry, and change timeout from default (4 minutes) to 10 seconds
* Do not retry sending telemetry on failure, skip message
* Use IoT Hub S2 SKU limits by default
* Upgrade to .NET Core 2.0.3
* Upgrade Azure IoT SDK: Devices 1.4.1 to 1.6.0, Devices Client 1.5.2 to 1.7.0
* Run simulation engine in the same process used for the web service
* Move docs, move wiki, new API specs, fix scripts for Windows Bash
* Fix the spelling of “ETag” and allow ETag=*
* Add internal scripts for increasing/decreasing/random telemetry
* Add env vars documentation ENVIRONMENT_VARIABLES.md
* Add more optional logging by device and by actor
* Use logging level and other logging settings from configuration
* Adjust unit test precision to mitigate flaky test
* Add system properties to telemetry messages
* Removing the squash flag from Docker scripts
* Use env vars in launchSettings.json
2018-04-13 03:42:46 +03:00
|
|
|
IEnumerable<string> GetDeviceIds(Models.Simulation simulation);
|
2018-09-18 08:16:21 +03:00
|
|
|
|
|
|
|
// Get the ID of the devices in a simulation, grouped by device model ID.
|
|
|
|
Dictionary<string, List<string>> GetDeviceIdsByModel(Models.Simulation simulation);
|
2018-11-30 04:29:58 +03:00
|
|
|
|
|
|
|
// Generate a device Id
|
|
|
|
string GenerateId(string simulationId, string deviceModelId, int position);
|
2017-05-31 06:05:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public class Simulations : ISimulations
|
|
|
|
{
|
2018-09-01 06:34:27 +03:00
|
|
|
private const string DEFAULT_SIMULATION_ID = "1";
|
|
|
|
private const string DEFAULT_TEMPLATE_NAME = "default";
|
2018-08-27 21:38:58 +03:00
|
|
|
private const string DEVICES_COLLECTION = "SimulatedDevices";
|
2018-10-30 10:10:37 +03:00
|
|
|
private const string SEED_STATUS_KEY = "SeedCompleted";
|
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 int DEVICES_PER_MODEL_IN_DEFAULT_TEMPLATE = 1;
|
2017-05-31 06:05:48 +03:00
|
|
|
|
2018-10-30 10:10:37 +03:00
|
|
|
private readonly IServicesConfig config;
|
2017-08-16 22:37:56 +03:00
|
|
|
private readonly IDeviceModels deviceModels;
|
2018-09-19 08:22:47 +03:00
|
|
|
private readonly IStorageAdapterClient storageAdapterClient;
|
2018-12-11 01:59:37 +03:00
|
|
|
private readonly IEngine mainStorage;
|
|
|
|
private readonly IEngine simulationsStorage;
|
2018-11-19 22:44:28 +03:00
|
|
|
private readonly IConnectionStrings connectionStrings;
|
2018-10-27 01:49:22 +03:00
|
|
|
private readonly ISimulationStatistics simulationStatistics;
|
2018-10-30 10:10:37 +03:00
|
|
|
private readonly IFileSystem fileSystem;
|
2017-07-07 23:49:03 +03:00
|
|
|
private readonly ILogger log;
|
2018-09-12 01:40:24 +03:00
|
|
|
private readonly IDiagnosticsLogger diagnosticsLogger;
|
2017-05-31 06:05:48 +03:00
|
|
|
|
2017-07-07 23:49:03 +03:00
|
|
|
public Simulations(
|
2018-09-19 08:22:47 +03:00
|
|
|
IServicesConfig config,
|
2017-08-16 22:37:56 +03:00
|
|
|
IDeviceModels deviceModels,
|
2018-12-11 01:59:37 +03:00
|
|
|
IEngines engines,
|
2018-09-19 08:22:47 +03:00
|
|
|
IStorageAdapterClient storageAdapterClient,
|
2018-11-19 22:44:28 +03:00
|
|
|
IConnectionStrings connectionStrings,
|
2018-10-30 10:10:37 +03:00
|
|
|
IFileSystem fileSystem,
|
2018-09-12 01:40:24 +03:00
|
|
|
ILogger logger,
|
2018-10-27 01:49:22 +03:00
|
|
|
IDiagnosticsLogger diagnosticsLogger,
|
|
|
|
ISimulationStatistics simulationStatistics)
|
2017-05-31 06:05:48 +03:00
|
|
|
{
|
2018-10-30 10:10:37 +03:00
|
|
|
this.config = config;
|
2017-08-16 22:37:56 +03:00
|
|
|
this.deviceModels = deviceModels;
|
2018-09-19 08:22:47 +03:00
|
|
|
this.storageAdapterClient = storageAdapterClient;
|
2018-12-11 01:59:37 +03:00
|
|
|
this.mainStorage = engines.Build(config.MainStorage);
|
|
|
|
this.simulationsStorage = engines.Build(config.SimulationsStorage);
|
2018-11-19 22:44:28 +03:00
|
|
|
this.connectionStrings = connectionStrings;
|
2018-10-30 10:10:37 +03:00
|
|
|
this.fileSystem = fileSystem;
|
2017-07-07 23:49:03 +03:00
|
|
|
this.log = logger;
|
2018-09-12 01:40:24 +03:00
|
|
|
this.diagnosticsLogger = diagnosticsLogger;
|
2018-10-27 01:49:22 +03:00
|
|
|
this.simulationStatistics = simulationStatistics;
|
2017-05-31 06:05:48 +03:00
|
|
|
}
|
|
|
|
|
Merge azure-iot-pcs-simulation into master (#197)
* Rewrite to use fewer threads and increase throughput
* Expose simulation metrics in the status endpoint
* When available, return a URL to the Azure Portal metrics of the pre-provisioned IoT Hub
* Allow to run a simulation against a custom IoT Hub, passing the connection string
* Allow to schedule a simulation, defining start and end time
* Improve security by running the service as a non-root user
* Support multiple behavior scripts in a device model
* Support custom device with custom sensors, behavior and frequency
* Allow to override the initial device state when creating a simulation
* When a simulation starts, create all the devices in batches
* When a simulation is deleted, delete also the devices
* Refactor timers to start sending telemetry as soon as possible
* Refactor and improve how simulation scripts access state and properties
* Change stock models to use AMQP (#189)
* Overall improvements to exceptions handling
* Disable SDK retry, and change timeout from default (4 minutes) to 10 seconds
* Do not retry sending telemetry on failure, skip message
* Use IoT Hub S2 SKU limits by default
* Upgrade to .NET Core 2.0.3
* Upgrade Azure IoT SDK: Devices 1.4.1 to 1.6.0, Devices Client 1.5.2 to 1.7.0
* Run simulation engine in the same process used for the web service
* Move docs, move wiki, new API specs, fix scripts for Windows Bash
* Fix the spelling of “ETag” and allow ETag=*
* Add internal scripts for increasing/decreasing/random telemetry
* Add env vars documentation ENVIRONMENT_VARIABLES.md
* Add more optional logging by device and by actor
* Use logging level and other logging settings from configuration
* Adjust unit test precision to mitigate flaky test
* Add system properties to telemetry messages
* Removing the squash flag from Docker scripts
* Use env vars in launchSettings.json
2018-04-13 03:42:46 +03:00
|
|
|
/// <summary>
|
|
|
|
/// Get list of simulations.
|
|
|
|
/// </summary>
|
2017-08-15 20:39:11 +03:00
|
|
|
public async Task<IList<Models.Simulation>> GetListAsync()
|
2017-05-31 06:05:48 +03:00
|
|
|
{
|
2018-09-19 08:22:47 +03:00
|
|
|
var items = await this.simulationsStorage.GetAllAsync();
|
2017-08-15 20:39:11 +03:00
|
|
|
var result = new List<Models.Simulation>();
|
2018-09-19 08:22:47 +03:00
|
|
|
foreach (var item in items)
|
2017-05-31 06:05:48 +03:00
|
|
|
{
|
2018-12-11 01:59:37 +03:00
|
|
|
var simulation = JsonConvert.DeserializeObject<Models.Simulation>(item.GetData());
|
|
|
|
simulation.ETag = item.GetETag();
|
|
|
|
simulation.Id = item.GetId();
|
2017-08-15 20:39:11 +03:00
|
|
|
result.Add(simulation);
|
2017-05-31 06:05:48 +03:00
|
|
|
}
|
|
|
|
|
2018-09-01 06:34:27 +03:00
|
|
|
// TODO: This will need changes to support pagination. Also order should be by simulation Id.
|
|
|
|
return result.OrderByDescending(s => s.Created).ToList();
|
2017-08-15 20:39:11 +03:00
|
|
|
}
|
2017-07-07 23:49:03 +03:00
|
|
|
|
2018-10-30 02:36:27 +03:00
|
|
|
/// <summary>
|
|
|
|
/// Get list of simulations with statistics.
|
|
|
|
/// </summary>
|
|
|
|
public async Task<IList<Models.Simulation>> GetListWithStatisticsAsync()
|
|
|
|
{
|
|
|
|
var items = await this.GetListAsync();
|
|
|
|
|
|
|
|
foreach (var item in items)
|
|
|
|
{
|
|
|
|
item.Statistics = await this.simulationStatistics.GetSimulationStatisticsAsync(item.Id);
|
|
|
|
}
|
|
|
|
|
|
|
|
return items;
|
|
|
|
}
|
|
|
|
|
Merge azure-iot-pcs-simulation into master (#197)
* Rewrite to use fewer threads and increase throughput
* Expose simulation metrics in the status endpoint
* When available, return a URL to the Azure Portal metrics of the pre-provisioned IoT Hub
* Allow to run a simulation against a custom IoT Hub, passing the connection string
* Allow to schedule a simulation, defining start and end time
* Improve security by running the service as a non-root user
* Support multiple behavior scripts in a device model
* Support custom device with custom sensors, behavior and frequency
* Allow to override the initial device state when creating a simulation
* When a simulation starts, create all the devices in batches
* When a simulation is deleted, delete also the devices
* Refactor timers to start sending telemetry as soon as possible
* Refactor and improve how simulation scripts access state and properties
* Change stock models to use AMQP (#189)
* Overall improvements to exceptions handling
* Disable SDK retry, and change timeout from default (4 minutes) to 10 seconds
* Do not retry sending telemetry on failure, skip message
* Use IoT Hub S2 SKU limits by default
* Upgrade to .NET Core 2.0.3
* Upgrade Azure IoT SDK: Devices 1.4.1 to 1.6.0, Devices Client 1.5.2 to 1.7.0
* Run simulation engine in the same process used for the web service
* Move docs, move wiki, new API specs, fix scripts for Windows Bash
* Fix the spelling of “ETag” and allow ETag=*
* Add internal scripts for increasing/decreasing/random telemetry
* Add env vars documentation ENVIRONMENT_VARIABLES.md
* Add more optional logging by device and by actor
* Use logging level and other logging settings from configuration
* Adjust unit test precision to mitigate flaky test
* Add system properties to telemetry messages
* Removing the squash flag from Docker scripts
* Use env vars in launchSettings.json
2018-04-13 03:42:46 +03:00
|
|
|
/// <summary>
|
|
|
|
/// Get a simulation.
|
|
|
|
/// </summary>
|
2017-08-15 20:39:11 +03:00
|
|
|
public async Task<Models.Simulation> GetAsync(string id)
|
|
|
|
{
|
2018-09-19 08:22:47 +03:00
|
|
|
var item = await this.simulationsStorage.GetAsync(id);
|
2018-09-01 06:34:27 +03:00
|
|
|
if (item == null) return null;
|
|
|
|
|
2018-12-11 01:59:37 +03:00
|
|
|
var simulation = JsonConvert.DeserializeObject<Models.Simulation>(item.GetData());
|
|
|
|
simulation.ETag = item.GetETag();
|
|
|
|
simulation.Id = item.GetId();
|
2017-08-15 20:39:11 +03:00
|
|
|
return simulation;
|
2017-05-31 06:05:48 +03:00
|
|
|
}
|
|
|
|
|
2018-10-27 01:49:22 +03:00
|
|
|
public async Task<Models.Simulation> GetWithStatisticsAsync(string id)
|
|
|
|
{
|
|
|
|
var simulation = await this.GetAsync(id);
|
|
|
|
|
|
|
|
if (simulation == null) return null;
|
|
|
|
|
|
|
|
simulation.Statistics = await this.simulationStatistics.GetSimulationStatisticsAsync(id);
|
|
|
|
return simulation;
|
|
|
|
}
|
|
|
|
|
Merge azure-iot-pcs-simulation into master (#197)
* Rewrite to use fewer threads and increase throughput
* Expose simulation metrics in the status endpoint
* When available, return a URL to the Azure Portal metrics of the pre-provisioned IoT Hub
* Allow to run a simulation against a custom IoT Hub, passing the connection string
* Allow to schedule a simulation, defining start and end time
* Improve security by running the service as a non-root user
* Support multiple behavior scripts in a device model
* Support custom device with custom sensors, behavior and frequency
* Allow to override the initial device state when creating a simulation
* When a simulation starts, create all the devices in batches
* When a simulation is deleted, delete also the devices
* Refactor timers to start sending telemetry as soon as possible
* Refactor and improve how simulation scripts access state and properties
* Change stock models to use AMQP (#189)
* Overall improvements to exceptions handling
* Disable SDK retry, and change timeout from default (4 minutes) to 10 seconds
* Do not retry sending telemetry on failure, skip message
* Use IoT Hub S2 SKU limits by default
* Upgrade to .NET Core 2.0.3
* Upgrade Azure IoT SDK: Devices 1.4.1 to 1.6.0, Devices Client 1.5.2 to 1.7.0
* Run simulation engine in the same process used for the web service
* Move docs, move wiki, new API specs, fix scripts for Windows Bash
* Fix the spelling of “ETag” and allow ETag=*
* Add internal scripts for increasing/decreasing/random telemetry
* Add env vars documentation ENVIRONMENT_VARIABLES.md
* Add more optional logging by device and by actor
* Use logging level and other logging settings from configuration
* Adjust unit test precision to mitigate flaky test
* Add system properties to telemetry messages
* Removing the squash flag from Docker scripts
* Use env vars in launchSettings.json
2018-04-13 03:42:46 +03:00
|
|
|
/// <summary>
|
|
|
|
/// Create a simulation.
|
|
|
|
/// </summary>
|
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
|
|
|
{
|
2018-09-01 06:34:27 +03:00
|
|
|
var usingDefaultTemplate = !string.IsNullOrEmpty(template) && template.ToLowerInvariant() == DEFAULT_TEMPLATE_NAME;
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(template) && template.ToLowerInvariant() != DEFAULT_TEMPLATE_NAME)
|
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
|
|
|
|
2018-09-01 06:34:27 +03:00
|
|
|
if (!usingDefaultTemplate && string.IsNullOrEmpty(simulation.Name))
|
2017-05-31 06:05:48 +03:00
|
|
|
{
|
2018-09-01 06:34:27 +03:00
|
|
|
this.log.Warn("Missing simulation name", () => new { });
|
|
|
|
throw new InvalidInputException("Simulation name is required.");
|
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
|
2018-09-01 06:34:27 +03:00
|
|
|
simulation.Id = usingDefaultTemplate ? DEFAULT_SIMULATION_ID : Guid.NewGuid().ToString();
|
2017-05-31 06:05:48 +03:00
|
|
|
simulation.Created = DateTimeOffset.UtcNow;
|
|
|
|
|
|
|
|
// Create default simulation
|
2018-09-01 06:34:27 +03:00
|
|
|
if (usingDefaultTemplate)
|
2017-05-31 06:05:48 +03:00
|
|
|
{
|
2018-09-01 06:34:27 +03:00
|
|
|
simulation.Name = "Default Simulation";
|
2018-05-23 03:24:57 +03:00
|
|
|
var types = await this.deviceModels.GetListAsync();
|
2017-08-16 22:37:56 +03:00
|
|
|
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
|
|
|
});
|
|
|
|
}
|
2018-09-26 22:29:40 +03:00
|
|
|
|
|
|
|
simulation.IotHubConnectionStrings = new List<string> { ServicesConfig.USE_DEFAULT_IOTHUB };
|
2017-05-31 06:05:48 +03:00
|
|
|
}
|
|
|
|
|
2018-09-21 20:23:51 +03:00
|
|
|
for (var index = 0; index < simulation.IotHubConnectionStrings.Count; index++)
|
2018-09-01 06:34:27 +03:00
|
|
|
{
|
2018-12-11 01:59:37 +03:00
|
|
|
var connString = await this.connectionStrings.SaveAsync(simulation.IotHubConnectionStrings[index], true);
|
2018-09-21 20:23:51 +03:00
|
|
|
|
|
|
|
if (!simulation.IotHubConnectionStrings.Contains(connString))
|
|
|
|
{
|
|
|
|
simulation.IotHubConnectionStrings[index] = connString;
|
|
|
|
}
|
2018-09-01 06:34:27 +03:00
|
|
|
}
|
Merge azure-iot-pcs-simulation into master (#197)
* Rewrite to use fewer threads and increase throughput
* Expose simulation metrics in the status endpoint
* When available, return a URL to the Azure Portal metrics of the pre-provisioned IoT Hub
* Allow to run a simulation against a custom IoT Hub, passing the connection string
* Allow to schedule a simulation, defining start and end time
* Improve security by running the service as a non-root user
* Support multiple behavior scripts in a device model
* Support custom device with custom sensors, behavior and frequency
* Allow to override the initial device state when creating a simulation
* When a simulation starts, create all the devices in batches
* When a simulation is deleted, delete also the devices
* Refactor timers to start sending telemetry as soon as possible
* Refactor and improve how simulation scripts access state and properties
* Change stock models to use AMQP (#189)
* Overall improvements to exceptions handling
* Disable SDK retry, and change timeout from default (4 minutes) to 10 seconds
* Do not retry sending telemetry on failure, skip message
* Use IoT Hub S2 SKU limits by default
* Upgrade to .NET Core 2.0.3
* Upgrade Azure IoT SDK: Devices 1.4.1 to 1.6.0, Devices Client 1.5.2 to 1.7.0
* Run simulation engine in the same process used for the web service
* Move docs, move wiki, new API specs, fix scripts for Windows Bash
* Fix the spelling of “ETag” and allow ETag=*
* Add internal scripts for increasing/decreasing/random telemetry
* Add env vars documentation ENVIRONMENT_VARIABLES.md
* Add more optional logging by device and by actor
* Use logging level and other logging settings from configuration
* Adjust unit test precision to mitigate flaky test
* Add system properties to telemetry messages
* Removing the squash flag from Docker scripts
* Use env vars in launchSettings.json
2018-04-13 03:42:46 +03:00
|
|
|
|
2018-09-18 08:16:21 +03:00
|
|
|
// This value cannot be set by the user, we set it here and make sure it's "false"
|
|
|
|
simulation.PartitioningComplete = false;
|
2017-08-16 22:37:56 +03:00
|
|
|
|
2018-09-18 08:16:21 +03:00
|
|
|
return await this.SaveAsync(simulation, "*");
|
2017-05-31 06:05:48 +03:00
|
|
|
}
|
|
|
|
|
2017-08-15 20:39:11 +03:00
|
|
|
/// <summary>
|
Merge azure-iot-pcs-simulation into master (#197)
* Rewrite to use fewer threads and increase throughput
* Expose simulation metrics in the status endpoint
* When available, return a URL to the Azure Portal metrics of the pre-provisioned IoT Hub
* Allow to run a simulation against a custom IoT Hub, passing the connection string
* Allow to schedule a simulation, defining start and end time
* Improve security by running the service as a non-root user
* Support multiple behavior scripts in a device model
* Support custom device with custom sensors, behavior and frequency
* Allow to override the initial device state when creating a simulation
* When a simulation starts, create all the devices in batches
* When a simulation is deleted, delete also the devices
* Refactor timers to start sending telemetry as soon as possible
* Refactor and improve how simulation scripts access state and properties
* Change stock models to use AMQP (#189)
* Overall improvements to exceptions handling
* Disable SDK retry, and change timeout from default (4 minutes) to 10 seconds
* Do not retry sending telemetry on failure, skip message
* Use IoT Hub S2 SKU limits by default
* Upgrade to .NET Core 2.0.3
* Upgrade Azure IoT SDK: Devices 1.4.1 to 1.6.0, Devices Client 1.5.2 to 1.7.0
* Run simulation engine in the same process used for the web service
* Move docs, move wiki, new API specs, fix scripts for Windows Bash
* Fix the spelling of “ETag” and allow ETag=*
* Add internal scripts for increasing/decreasing/random telemetry
* Add env vars documentation ENVIRONMENT_VARIABLES.md
* Add more optional logging by device and by actor
* Use logging level and other logging settings from configuration
* Adjust unit test precision to mitigate flaky test
* Add system properties to telemetry messages
* Removing the squash flag from Docker scripts
* Use env vars in launchSettings.json
2018-04-13 03:42:46 +03:00
|
|
|
/// Create or Replace a simulation.
|
|
|
|
/// The logic works under the assumption that there is only one simulation with id "1".
|
2017-08-15 20:39:11 +03:00
|
|
|
/// </summary>
|
2018-12-11 01:59:37 +03:00
|
|
|
public async Task<Models.Simulation> UpsertAsync(Models.Simulation simulation, bool validateHubCredentials)
|
2017-05-31 06:05:48 +03:00
|
|
|
{
|
2018-09-01 06:34:27 +03:00
|
|
|
if (string.IsNullOrEmpty(simulation.Id))
|
2017-05-31 06:05:48 +03:00
|
|
|
{
|
2018-09-01 06:34:27 +03:00
|
|
|
throw new InvalidInputException("Simulation ID is not specified.");
|
2017-05-31 06:05:48 +03:00
|
|
|
}
|
|
|
|
|
2018-09-21 20:23:51 +03:00
|
|
|
Models.Simulation existingSimulation = null;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
existingSimulation = await this.GetAsync(simulation.Id);
|
|
|
|
}
|
|
|
|
catch (ResourceNotFoundException)
|
|
|
|
{
|
|
|
|
// Do nothing - this mean simulation does not exist in storage
|
|
|
|
// Therefore allow to create a new simulation
|
|
|
|
this.log.Info("Simulation not found in storage, hence proceeding to create new simulation");
|
|
|
|
}
|
|
|
|
|
2018-10-27 01:49:22 +03:00
|
|
|
var simulationIsRestarting = false;
|
|
|
|
var simulationIsStopping = false;
|
|
|
|
|
2018-09-01 06:34:27 +03:00
|
|
|
if (existingSimulation != null)
|
2017-05-31 06:05:48 +03:00
|
|
|
{
|
2018-08-13 01:36:40 +03:00
|
|
|
this.log.Info("Modifying simulation");
|
2017-09-19 04:33:50 +03:00
|
|
|
|
Merge azure-iot-pcs-simulation into master (#197)
* Rewrite to use fewer threads and increase throughput
* Expose simulation metrics in the status endpoint
* When available, return a URL to the Azure Portal metrics of the pre-provisioned IoT Hub
* Allow to run a simulation against a custom IoT Hub, passing the connection string
* Allow to schedule a simulation, defining start and end time
* Improve security by running the service as a non-root user
* Support multiple behavior scripts in a device model
* Support custom device with custom sensors, behavior and frequency
* Allow to override the initial device state when creating a simulation
* When a simulation starts, create all the devices in batches
* When a simulation is deleted, delete also the devices
* Refactor timers to start sending telemetry as soon as possible
* Refactor and improve how simulation scripts access state and properties
* Change stock models to use AMQP (#189)
* Overall improvements to exceptions handling
* Disable SDK retry, and change timeout from default (4 minutes) to 10 seconds
* Do not retry sending telemetry on failure, skip message
* Use IoT Hub S2 SKU limits by default
* Upgrade to .NET Core 2.0.3
* Upgrade Azure IoT SDK: Devices 1.4.1 to 1.6.0, Devices Client 1.5.2 to 1.7.0
* Run simulation engine in the same process used for the web service
* Move docs, move wiki, new API specs, fix scripts for Windows Bash
* Fix the spelling of “ETag” and allow ETag=*
* Add internal scripts for increasing/decreasing/random telemetry
* Add env vars documentation ENVIRONMENT_VARIABLES.md
* Add more optional logging by device and by actor
* Use logging level and other logging settings from configuration
* Adjust unit test precision to mitigate flaky test
* Add system properties to telemetry messages
* Removing the squash flag from Docker scripts
* Use env vars in launchSettings.json
2018-04-13 03:42:46 +03:00
|
|
|
if (simulation.ETag == "*")
|
|
|
|
{
|
2018-09-01 06:34:27 +03:00
|
|
|
simulation.ETag = existingSimulation.ETag;
|
2018-08-13 01:36:40 +03:00
|
|
|
this.log.Warn("The client used ETag='*' choosing to overwrite the current simulation");
|
Merge azure-iot-pcs-simulation into master (#197)
* Rewrite to use fewer threads and increase throughput
* Expose simulation metrics in the status endpoint
* When available, return a URL to the Azure Portal metrics of the pre-provisioned IoT Hub
* Allow to run a simulation against a custom IoT Hub, passing the connection string
* Allow to schedule a simulation, defining start and end time
* Improve security by running the service as a non-root user
* Support multiple behavior scripts in a device model
* Support custom device with custom sensors, behavior and frequency
* Allow to override the initial device state when creating a simulation
* When a simulation starts, create all the devices in batches
* When a simulation is deleted, delete also the devices
* Refactor timers to start sending telemetry as soon as possible
* Refactor and improve how simulation scripts access state and properties
* Change stock models to use AMQP (#189)
* Overall improvements to exceptions handling
* Disable SDK retry, and change timeout from default (4 minutes) to 10 seconds
* Do not retry sending telemetry on failure, skip message
* Use IoT Hub S2 SKU limits by default
* Upgrade to .NET Core 2.0.3
* Upgrade Azure IoT SDK: Devices 1.4.1 to 1.6.0, Devices Client 1.5.2 to 1.7.0
* Run simulation engine in the same process used for the web service
* Move docs, move wiki, new API specs, fix scripts for Windows Bash
* Fix the spelling of “ETag” and allow ETag=*
* Add internal scripts for increasing/decreasing/random telemetry
* Add env vars documentation ENVIRONMENT_VARIABLES.md
* Add more optional logging by device and by actor
* Use logging level and other logging settings from configuration
* Adjust unit test precision to mitigate flaky test
* Add system properties to telemetry messages
* Removing the squash flag from Docker scripts
* Use env vars in launchSettings.json
2018-04-13 03:42:46 +03:00
|
|
|
}
|
|
|
|
|
2018-09-01 06:34:27 +03:00
|
|
|
if (simulation.ETag != existingSimulation.ETag)
|
2017-10-19 08:21:01 +03:00
|
|
|
{
|
2018-09-13 20:25:29 +03:00
|
|
|
this.log.Error("Invalid simulation ETag",
|
|
|
|
() => new { simulation.Id, ETagInStorage = existingSimulation.ETag, ETagInRequest = simulation.ETag });
|
|
|
|
this.diagnosticsLogger.LogServiceError($"Invalid simulation ETag", new { simulation.Id, simulation.Name });
|
|
|
|
throw new ResourceOutOfDateException($"Invalid ETag. The simulation ETag is '{existingSimulation.ETag}'");
|
2017-09-19 04:33:50 +03:00
|
|
|
}
|
|
|
|
|
2018-10-27 01:49:22 +03:00
|
|
|
// Define the user intent looking at the simulation status and the patch content
|
|
|
|
simulationIsRestarting = !existingSimulation.Enabled && simulation.Enabled;
|
|
|
|
simulationIsStopping = existingSimulation.Enabled && !simulation.Enabled;
|
|
|
|
|
2018-09-01 06:34:27 +03:00
|
|
|
simulation.Created = existingSimulation.Created;
|
2017-09-19 04:33:50 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-08-13 01:36:40 +03:00
|
|
|
this.log.Info("Creating new simulation");
|
2018-09-18 08:16:21 +03:00
|
|
|
|
2017-09-19 04:33:50 +03:00
|
|
|
simulation.Created = DateTimeOffset.UtcNow;
|
2018-09-18 08:16:21 +03:00
|
|
|
|
|
|
|
// This value cannot be set by the user, we set it here and make sure it's "false"
|
|
|
|
simulation.PartitioningComplete = false;
|
2017-05-31 06:05:48 +03:00
|
|
|
}
|
|
|
|
|
2018-09-01 06:34:27 +03:00
|
|
|
for (var index = 0; index < simulation.IotHubConnectionStrings.Count; index++)
|
|
|
|
{
|
2018-12-11 01:59:37 +03:00
|
|
|
var connString = await this.connectionStrings.SaveAsync(simulation.IotHubConnectionStrings[index], validateHubCredentials);
|
2018-09-21 20:23:51 +03:00
|
|
|
|
|
|
|
if (!simulation.IotHubConnectionStrings.Contains(connString))
|
|
|
|
{
|
|
|
|
simulation.IotHubConnectionStrings[index] = connString;
|
|
|
|
}
|
2018-09-01 06:34:27 +03:00
|
|
|
}
|
Merge azure-iot-pcs-simulation into master (#197)
* Rewrite to use fewer threads and increase throughput
* Expose simulation metrics in the status endpoint
* When available, return a URL to the Azure Portal metrics of the pre-provisioned IoT Hub
* Allow to run a simulation against a custom IoT Hub, passing the connection string
* Allow to schedule a simulation, defining start and end time
* Improve security by running the service as a non-root user
* Support multiple behavior scripts in a device model
* Support custom device with custom sensors, behavior and frequency
* Allow to override the initial device state when creating a simulation
* When a simulation starts, create all the devices in batches
* When a simulation is deleted, delete also the devices
* Refactor timers to start sending telemetry as soon as possible
* Refactor and improve how simulation scripts access state and properties
* Change stock models to use AMQP (#189)
* Overall improvements to exceptions handling
* Disable SDK retry, and change timeout from default (4 minutes) to 10 seconds
* Do not retry sending telemetry on failure, skip message
* Use IoT Hub S2 SKU limits by default
* Upgrade to .NET Core 2.0.3
* Upgrade Azure IoT SDK: Devices 1.4.1 to 1.6.0, Devices Client 1.5.2 to 1.7.0
* Run simulation engine in the same process used for the web service
* Move docs, move wiki, new API specs, fix scripts for Windows Bash
* Fix the spelling of “ETag” and allow ETag=*
* Add internal scripts for increasing/decreasing/random telemetry
* Add env vars documentation ENVIRONMENT_VARIABLES.md
* Add more optional logging by device and by actor
* Use logging level and other logging settings from configuration
* Adjust unit test precision to mitigate flaky test
* Add system properties to telemetry messages
* Removing the squash flag from Docker scripts
* Use env vars in launchSettings.json
2018-04-13 03:42:46 +03:00
|
|
|
|
2018-10-27 01:49:22 +03:00
|
|
|
// Note: this code is also in MergeAsync
|
|
|
|
// (consider refactoring it out unless the code becomes harder to follow)
|
|
|
|
if (simulationIsRestarting)
|
|
|
|
{
|
|
|
|
simulation = await this.ResetSimulationStatisticsAsync(simulation);
|
|
|
|
}
|
|
|
|
else if (simulationIsStopping)
|
|
|
|
{
|
|
|
|
simulation.StoppedTime = DateTimeOffset.UtcNow;
|
|
|
|
|
|
|
|
// When a simulation is disabled, its partitions are deleted.
|
|
|
|
// This boolean triggers the deletion of partitions from the storage
|
|
|
|
// in the partitioning agent.
|
|
|
|
simulation.PartitioningComplete = false;
|
|
|
|
}
|
|
|
|
|
2018-09-18 08:16:21 +03:00
|
|
|
return await this.SaveAsync(simulation, simulation.ETag);
|
2017-05-31 06:05:48 +03:00
|
|
|
}
|
|
|
|
|
Merge azure-iot-pcs-simulation into master (#197)
* Rewrite to use fewer threads and increase throughput
* Expose simulation metrics in the status endpoint
* When available, return a URL to the Azure Portal metrics of the pre-provisioned IoT Hub
* Allow to run a simulation against a custom IoT Hub, passing the connection string
* Allow to schedule a simulation, defining start and end time
* Improve security by running the service as a non-root user
* Support multiple behavior scripts in a device model
* Support custom device with custom sensors, behavior and frequency
* Allow to override the initial device state when creating a simulation
* When a simulation starts, create all the devices in batches
* When a simulation is deleted, delete also the devices
* Refactor timers to start sending telemetry as soon as possible
* Refactor and improve how simulation scripts access state and properties
* Change stock models to use AMQP (#189)
* Overall improvements to exceptions handling
* Disable SDK retry, and change timeout from default (4 minutes) to 10 seconds
* Do not retry sending telemetry on failure, skip message
* Use IoT Hub S2 SKU limits by default
* Upgrade to .NET Core 2.0.3
* Upgrade Azure IoT SDK: Devices 1.4.1 to 1.6.0, Devices Client 1.5.2 to 1.7.0
* Run simulation engine in the same process used for the web service
* Move docs, move wiki, new API specs, fix scripts for Windows Bash
* Fix the spelling of “ETag” and allow ETag=*
* Add internal scripts for increasing/decreasing/random telemetry
* Add env vars documentation ENVIRONMENT_VARIABLES.md
* Add more optional logging by device and by actor
* Use logging level and other logging settings from configuration
* Adjust unit test precision to mitigate flaky test
* Add system properties to telemetry messages
* Removing the squash flag from Docker scripts
* Use env vars in launchSettings.json
2018-04-13 03:42:46 +03:00
|
|
|
/// <summary>
|
2018-10-27 01:49:22 +03:00
|
|
|
/// Modify some simulation details
|
Merge azure-iot-pcs-simulation into master (#197)
* Rewrite to use fewer threads and increase throughput
* Expose simulation metrics in the status endpoint
* When available, return a URL to the Azure Portal metrics of the pre-provisioned IoT Hub
* Allow to run a simulation against a custom IoT Hub, passing the connection string
* Allow to schedule a simulation, defining start and end time
* Improve security by running the service as a non-root user
* Support multiple behavior scripts in a device model
* Support custom device with custom sensors, behavior and frequency
* Allow to override the initial device state when creating a simulation
* When a simulation starts, create all the devices in batches
* When a simulation is deleted, delete also the devices
* Refactor timers to start sending telemetry as soon as possible
* Refactor and improve how simulation scripts access state and properties
* Change stock models to use AMQP (#189)
* Overall improvements to exceptions handling
* Disable SDK retry, and change timeout from default (4 minutes) to 10 seconds
* Do not retry sending telemetry on failure, skip message
* Use IoT Hub S2 SKU limits by default
* Upgrade to .NET Core 2.0.3
* Upgrade Azure IoT SDK: Devices 1.4.1 to 1.6.0, Devices Client 1.5.2 to 1.7.0
* Run simulation engine in the same process used for the web service
* Move docs, move wiki, new API specs, fix scripts for Windows Bash
* Fix the spelling of “ETag” and allow ETag=*
* Add internal scripts for increasing/decreasing/random telemetry
* Add env vars documentation ENVIRONMENT_VARIABLES.md
* Add more optional logging by device and by actor
* Use logging level and other logging settings from configuration
* Adjust unit test precision to mitigate flaky test
* Add system properties to telemetry messages
* Removing the squash flag from Docker scripts
* Use env vars in launchSettings.json
2018-04-13 03:42:46 +03:00
|
|
|
/// </summary>
|
2017-08-15 20:39:11 +03:00
|
|
|
public async Task<Models.Simulation> MergeAsync(SimulationPatch patch)
|
2017-05-31 06:05:48 +03:00
|
|
|
{
|
2018-09-19 08:22:47 +03:00
|
|
|
if (string.IsNullOrEmpty(patch.Id))
|
|
|
|
{
|
|
|
|
this.log.Warn("Invalid simulation ID.", () => new { patch.Id });
|
|
|
|
throw new InvalidInputException("Invalid simulation ID.");
|
|
|
|
}
|
|
|
|
|
|
|
|
var item = await this.simulationsStorage.GetAsync(patch.Id);
|
2018-12-11 01:59:37 +03:00
|
|
|
var simulation = JsonConvert.DeserializeObject<Models.Simulation>(item.GetData());
|
|
|
|
simulation.ETag = item.GetETag();
|
|
|
|
simulation.Id = item.GetId();
|
2017-05-31 06:05:48 +03:00
|
|
|
|
Merge azure-iot-pcs-simulation into master (#197)
* Rewrite to use fewer threads and increase throughput
* Expose simulation metrics in the status endpoint
* When available, return a URL to the Azure Portal metrics of the pre-provisioned IoT Hub
* Allow to run a simulation against a custom IoT Hub, passing the connection string
* Allow to schedule a simulation, defining start and end time
* Improve security by running the service as a non-root user
* Support multiple behavior scripts in a device model
* Support custom device with custom sensors, behavior and frequency
* Allow to override the initial device state when creating a simulation
* When a simulation starts, create all the devices in batches
* When a simulation is deleted, delete also the devices
* Refactor timers to start sending telemetry as soon as possible
* Refactor and improve how simulation scripts access state and properties
* Change stock models to use AMQP (#189)
* Overall improvements to exceptions handling
* Disable SDK retry, and change timeout from default (4 minutes) to 10 seconds
* Do not retry sending telemetry on failure, skip message
* Use IoT Hub S2 SKU limits by default
* Upgrade to .NET Core 2.0.3
* Upgrade Azure IoT SDK: Devices 1.4.1 to 1.6.0, Devices Client 1.5.2 to 1.7.0
* Run simulation engine in the same process used for the web service
* Move docs, move wiki, new API specs, fix scripts for Windows Bash
* Fix the spelling of “ETag” and allow ETag=*
* Add internal scripts for increasing/decreasing/random telemetry
* Add env vars documentation ENVIRONMENT_VARIABLES.md
* Add more optional logging by device and by actor
* Use logging level and other logging settings from configuration
* Adjust unit test precision to mitigate flaky test
* Add system properties to telemetry messages
* Removing the squash flag from Docker scripts
* Use env vars in launchSettings.json
2018-04-13 03:42:46 +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
|
|
|
{
|
Merge azure-iot-pcs-simulation into master (#197)
* Rewrite to use fewer threads and increase throughput
* Expose simulation metrics in the status endpoint
* When available, return a URL to the Azure Portal metrics of the pre-provisioned IoT Hub
* Allow to run a simulation against a custom IoT Hub, passing the connection string
* Allow to schedule a simulation, defining start and end time
* Improve security by running the service as a non-root user
* Support multiple behavior scripts in a device model
* Support custom device with custom sensors, behavior and frequency
* Allow to override the initial device state when creating a simulation
* When a simulation starts, create all the devices in batches
* When a simulation is deleted, delete also the devices
* Refactor timers to start sending telemetry as soon as possible
* Refactor and improve how simulation scripts access state and properties
* Change stock models to use AMQP (#189)
* Overall improvements to exceptions handling
* Disable SDK retry, and change timeout from default (4 minutes) to 10 seconds
* Do not retry sending telemetry on failure, skip message
* Use IoT Hub S2 SKU limits by default
* Upgrade to .NET Core 2.0.3
* Upgrade Azure IoT SDK: Devices 1.4.1 to 1.6.0, Devices Client 1.5.2 to 1.7.0
* Run simulation engine in the same process used for the web service
* Move docs, move wiki, new API specs, fix scripts for Windows Bash
* Fix the spelling of “ETag” and allow ETag=*
* Add internal scripts for increasing/decreasing/random telemetry
* Add env vars documentation ENVIRONMENT_VARIABLES.md
* Add more optional logging by device and by actor
* Use logging level and other logging settings from configuration
* Adjust unit test precision to mitigate flaky test
* Add system properties to telemetry messages
* Removing the squash flag from Docker scripts
* Use env vars in launchSettings.json
2018-04-13 03:42:46 +03:00
|
|
|
this.log.Warn("ETag mismatch",
|
|
|
|
() => new { Current = simulation.ETag, Provided = patch.ETag });
|
2017-08-15 20:39:11 +03:00
|
|
|
throw new ConflictingResourceException(
|
Merge azure-iot-pcs-simulation into master (#197)
* Rewrite to use fewer threads and increase throughput
* Expose simulation metrics in the status endpoint
* When available, return a URL to the Azure Portal metrics of the pre-provisioned IoT Hub
* Allow to run a simulation against a custom IoT Hub, passing the connection string
* Allow to schedule a simulation, defining start and end time
* Improve security by running the service as a non-root user
* Support multiple behavior scripts in a device model
* Support custom device with custom sensors, behavior and frequency
* Allow to override the initial device state when creating a simulation
* When a simulation starts, create all the devices in batches
* When a simulation is deleted, delete also the devices
* Refactor timers to start sending telemetry as soon as possible
* Refactor and improve how simulation scripts access state and properties
* Change stock models to use AMQP (#189)
* Overall improvements to exceptions handling
* Disable SDK retry, and change timeout from default (4 minutes) to 10 seconds
* Do not retry sending telemetry on failure, skip message
* Use IoT Hub S2 SKU limits by default
* Upgrade to .NET Core 2.0.3
* Upgrade Azure IoT SDK: Devices 1.4.1 to 1.6.0, Devices Client 1.5.2 to 1.7.0
* Run simulation engine in the same process used for the web service
* Move docs, move wiki, new API specs, fix scripts for Windows Bash
* Fix the spelling of “ETag” and allow ETag=*
* Add internal scripts for increasing/decreasing/random telemetry
* Add env vars documentation ENVIRONMENT_VARIABLES.md
* Add more optional logging by device and by actor
* Use logging level and other logging settings from configuration
* Adjust unit test precision to mitigate flaky test
* Add system properties to telemetry messages
* Removing the squash flag from Docker scripts
* Use env vars in launchSettings.json
2018-04-13 03:42:46 +03:00
|
|
|
$"The ETag provided doesn't match the current resource ETag ({simulation.ETag}).");
|
2017-05-31 06:05:48 +03:00
|
|
|
}
|
|
|
|
|
2018-10-27 01:49:22 +03:00
|
|
|
// Define the user intent looking at the simulation status and the patch content
|
|
|
|
var simulationIsRestarting = !simulation.Enabled && patch.Enabled.HasValue && patch.Enabled.Value;
|
|
|
|
var simulationIsStopping = simulation.Enabled && patch.Enabled.HasValue && !patch.Enabled.Value;
|
|
|
|
|
|
|
|
// Note: this code is also in UpsertAsync
|
|
|
|
// (consider refactoring it out unless the code becomes harder to follow)
|
|
|
|
if (simulationIsRestarting)
|
2017-05-31 06:05:48 +03:00
|
|
|
{
|
2018-10-27 01:49:22 +03:00
|
|
|
simulation = await this.ResetSimulationStatisticsAsync(simulation);
|
2017-05-31 06:05:48 +03:00
|
|
|
}
|
2018-10-27 01:49:22 +03:00
|
|
|
else if (simulationIsStopping)
|
2018-09-01 06:34:27 +03:00
|
|
|
{
|
2018-10-27 01:49:22 +03:00
|
|
|
simulation.StoppedTime = DateTimeOffset.UtcNow;
|
2018-09-01 06:34:27 +03:00
|
|
|
|
2018-10-27 01:49:22 +03:00
|
|
|
// When a simulation is disabled, its partitions are deleted.
|
|
|
|
// This boolean triggers the deletion of partitions from the storage
|
|
|
|
// in the partitioning agent.
|
|
|
|
simulation.PartitioningComplete = false;
|
2018-10-31 01:57:38 +03:00
|
|
|
|
|
|
|
// Reset active device count to 0
|
|
|
|
await this.ResetActiveDevicesStatistics(simulation);
|
2018-09-18 08:16:21 +03:00
|
|
|
}
|
2017-05-31 06:05:48 +03:00
|
|
|
|
2018-10-27 01:49:22 +03:00
|
|
|
// The Enabled field is optional, e.g. in case PATCH is extended to
|
|
|
|
// modify other fields, so we need to check for null
|
|
|
|
if (patch.Enabled != null) simulation.Enabled = patch.Enabled.Value;
|
|
|
|
|
2018-12-04 23:52:39 +03:00
|
|
|
if (patch.DeleteDevicesOnce.HasValue)
|
|
|
|
{
|
|
|
|
simulation.DeleteDevicesOnce = patch.DeleteDevicesOnce.Value;
|
|
|
|
}
|
|
|
|
|
2018-10-27 01:49:22 +03:00
|
|
|
// TODO: can we use this.SaveAsync() here too and avoid the duplication?
|
2018-12-11 01:59:37 +03:00
|
|
|
var record = this.simulationsStorage.BuildRecord(simulation.Id, JsonConvert.SerializeObject(simulation));
|
|
|
|
item = await this.simulationsStorage.UpsertAsync(record, patch.ETag);
|
2018-09-19 08:22:47 +03:00
|
|
|
|
2018-12-11 01:59:37 +03:00
|
|
|
simulation.ETag = item.GetETag();
|
2018-09-19 08:22:47 +03:00
|
|
|
|
|
|
|
return simulation;
|
2017-05-31 06:05:48 +03:00
|
|
|
}
|
2017-08-22 03:47:50 +03:00
|
|
|
|
Merge azure-iot-pcs-simulation into master (#197)
* Rewrite to use fewer threads and increase throughput
* Expose simulation metrics in the status endpoint
* When available, return a URL to the Azure Portal metrics of the pre-provisioned IoT Hub
* Allow to run a simulation against a custom IoT Hub, passing the connection string
* Allow to schedule a simulation, defining start and end time
* Improve security by running the service as a non-root user
* Support multiple behavior scripts in a device model
* Support custom device with custom sensors, behavior and frequency
* Allow to override the initial device state when creating a simulation
* When a simulation starts, create all the devices in batches
* When a simulation is deleted, delete also the devices
* Refactor timers to start sending telemetry as soon as possible
* Refactor and improve how simulation scripts access state and properties
* Change stock models to use AMQP (#189)
* Overall improvements to exceptions handling
* Disable SDK retry, and change timeout from default (4 minutes) to 10 seconds
* Do not retry sending telemetry on failure, skip message
* Use IoT Hub S2 SKU limits by default
* Upgrade to .NET Core 2.0.3
* Upgrade Azure IoT SDK: Devices 1.4.1 to 1.6.0, Devices Client 1.5.2 to 1.7.0
* Run simulation engine in the same process used for the web service
* Move docs, move wiki, new API specs, fix scripts for Windows Bash
* Fix the spelling of “ETag” and allow ETag=*
* Add internal scripts for increasing/decreasing/random telemetry
* Add env vars documentation ENVIRONMENT_VARIABLES.md
* Add more optional logging by device and by actor
* Use logging level and other logging settings from configuration
* Adjust unit test precision to mitigate flaky test
* Add system properties to telemetry messages
* Removing the squash flag from Docker scripts
* Use env vars in launchSettings.json
2018-04-13 03:42:46 +03:00
|
|
|
/// <summary>
|
2018-11-30 04:29:58 +03:00
|
|
|
/// Delete a simulation and its statistics (not the devices).
|
Merge azure-iot-pcs-simulation into master (#197)
* Rewrite to use fewer threads and increase throughput
* Expose simulation metrics in the status endpoint
* When available, return a URL to the Azure Portal metrics of the pre-provisioned IoT Hub
* Allow to run a simulation against a custom IoT Hub, passing the connection string
* Allow to schedule a simulation, defining start and end time
* Improve security by running the service as a non-root user
* Support multiple behavior scripts in a device model
* Support custom device with custom sensors, behavior and frequency
* Allow to override the initial device state when creating a simulation
* When a simulation starts, create all the devices in batches
* When a simulation is deleted, delete also the devices
* Refactor timers to start sending telemetry as soon as possible
* Refactor and improve how simulation scripts access state and properties
* Change stock models to use AMQP (#189)
* Overall improvements to exceptions handling
* Disable SDK retry, and change timeout from default (4 minutes) to 10 seconds
* Do not retry sending telemetry on failure, skip message
* Use IoT Hub S2 SKU limits by default
* Upgrade to .NET Core 2.0.3
* Upgrade Azure IoT SDK: Devices 1.4.1 to 1.6.0, Devices Client 1.5.2 to 1.7.0
* Run simulation engine in the same process used for the web service
* Move docs, move wiki, new API specs, fix scripts for Windows Bash
* Fix the spelling of “ETag” and allow ETag=*
* Add internal scripts for increasing/decreasing/random telemetry
* Add env vars documentation ENVIRONMENT_VARIABLES.md
* Add more optional logging by device and by actor
* Use logging level and other logging settings from configuration
* Adjust unit test precision to mitigate flaky test
* Add system properties to telemetry messages
* Removing the squash flag from Docker scripts
* Use env vars in launchSettings.json
2018-04-13 03:42:46 +03:00
|
|
|
/// </summary>
|
2017-08-22 03:47:50 +03:00
|
|
|
public async Task DeleteAsync(string id)
|
|
|
|
{
|
2018-10-27 01:49:22 +03:00
|
|
|
await this.simulationStatistics.DeleteSimulationStatisticsAsync(id);
|
2018-09-19 08:22:47 +03:00
|
|
|
await this.simulationsStorage.DeleteAsync(id);
|
2017-08-22 03:47:50 +03:00
|
|
|
}
|
Merge azure-iot-pcs-simulation into master (#197)
* Rewrite to use fewer threads and increase throughput
* Expose simulation metrics in the status endpoint
* When available, return a URL to the Azure Portal metrics of the pre-provisioned IoT Hub
* Allow to run a simulation against a custom IoT Hub, passing the connection string
* Allow to schedule a simulation, defining start and end time
* Improve security by running the service as a non-root user
* Support multiple behavior scripts in a device model
* Support custom device with custom sensors, behavior and frequency
* Allow to override the initial device state when creating a simulation
* When a simulation starts, create all the devices in batches
* When a simulation is deleted, delete also the devices
* Refactor timers to start sending telemetry as soon as possible
* Refactor and improve how simulation scripts access state and properties
* Change stock models to use AMQP (#189)
* Overall improvements to exceptions handling
* Disable SDK retry, and change timeout from default (4 minutes) to 10 seconds
* Do not retry sending telemetry on failure, skip message
* Use IoT Hub S2 SKU limits by default
* Upgrade to .NET Core 2.0.3
* Upgrade Azure IoT SDK: Devices 1.4.1 to 1.6.0, Devices Client 1.5.2 to 1.7.0
* Run simulation engine in the same process used for the web service
* Move docs, move wiki, new API specs, fix scripts for Windows Bash
* Fix the spelling of “ETag” and allow ETag=*
* Add internal scripts for increasing/decreasing/random telemetry
* Add env vars documentation ENVIRONMENT_VARIABLES.md
* Add more optional logging by device and by actor
* Use logging level and other logging settings from configuration
* Adjust unit test precision to mitigate flaky test
* Add system properties to telemetry messages
* Removing the squash flag from Docker scripts
* Use env vars in launchSettings.json
2018-04-13 03:42:46 +03:00
|
|
|
|
2018-10-30 10:10:37 +03:00
|
|
|
public async Task TrySeedAsync()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
var templateName = this.config.SeedTemplate;
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(templateName)) return;
|
|
|
|
|
|
|
|
// Skips if the seeding has already been complete
|
|
|
|
if (!await this.mainStorage.ExistsAsync(SEED_STATUS_KEY))
|
|
|
|
{
|
|
|
|
await this.SeedSimulationsAsync(templateName);
|
2018-12-11 01:59:37 +03:00
|
|
|
var record = this.mainStorage.BuildRecord(SEED_STATUS_KEY, "Seed Completed");
|
2018-10-30 10:10:37 +03:00
|
|
|
await this.mainStorage.CreateAsync(record);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.log.Info("Seeding skipped.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
var msg = "Failed to seed default simulations." + e;
|
|
|
|
this.log.Error(msg);
|
|
|
|
this.diagnosticsLogger.LogServiceError(msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-01 20:08:31 +03:00
|
|
|
public async Task<bool> TryToStartDevicesCreationAsync(string simulationId, IDevices devices)
|
|
|
|
{
|
|
|
|
// Fetch latest record
|
|
|
|
var simulation = await this.GetAsync(simulationId);
|
|
|
|
|
|
|
|
// Edit the record only if required
|
|
|
|
if (!simulation.DevicesCreationStarted)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
Dictionary<string, List<string>> deviceList = this.GetDeviceIdsByModel(simulation);
|
|
|
|
var deviceIds = deviceList.SelectMany(x => x.Value);
|
|
|
|
this.log.Info("Creating devices...", () => new { simulationId });
|
|
|
|
|
|
|
|
simulation.DeviceCreationJobId = await devices.CreateListUsingJobsAsync(deviceIds);
|
|
|
|
simulation.DevicesCreationStarted = true;
|
|
|
|
|
2018-10-18 03:16:05 +03:00
|
|
|
this.log.Info("Import job created for bulk device creation", () => new { simulationId, simulation.DeviceCreationJobId });
|
2018-10-13 02:49:29 +03:00
|
|
|
|
|
|
|
await this.SaveAsync(simulation, simulation.ETag);
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
2018-10-18 03:16:05 +03:00
|
|
|
this.log.Error("Failed to create bulk-device-creation job", e);
|
2018-10-13 02:49:29 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-11-09 01:59:18 +03:00
|
|
|
/// <summary>
|
|
|
|
/// Try to create a job to delete all the devices in a simulation.
|
|
|
|
/// Throw exception in case of errors.
|
|
|
|
///
|
|
|
|
/// This operation is not meant to modify the simulation object, so if the simulation
|
|
|
|
/// is still running it will cause errors due to the missing devices. The simulation
|
|
|
|
/// might as well recreate the devices if it is running, that is not a problem.
|
|
|
|
///
|
|
|
|
/// This method is invoked by the web service, exceptions are thrown to tell the user
|
|
|
|
/// what happened. At this point the UI doesn't consume this logic, which is used only
|
|
|
|
/// for maintenance.
|
|
|
|
/// </summary>
|
|
|
|
public async Task<(bool jobCreated, string jobId)> DeleteAllDevicesAsync(string simulationId, IDevices devices)
|
2018-10-13 02:49:29 +03:00
|
|
|
{
|
2018-11-09 01:59:18 +03:00
|
|
|
try
|
|
|
|
{
|
|
|
|
var (simulation, jobCreated, jobId) = await this.CreateJobToDeleteDevices(simulationId, devices);
|
|
|
|
return (jobCreated, jobId);
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
this.log.Error("Failed to start bulk device deletion", e);
|
|
|
|
throw new ExternalDependencyException("Failed to start bulk device deletion", e);
|
|
|
|
}
|
|
|
|
}
|
2018-10-13 02:49:29 +03:00
|
|
|
|
2018-11-09 01:59:18 +03:00
|
|
|
/// <summary>
|
|
|
|
/// Try to create a job to delete all the devices in a simulation.
|
|
|
|
/// This method is used by the simulation agent when a simulation ends.
|
|
|
|
/// In case of errors the method returns 'false' and the caller is expected to retry.
|
|
|
|
/// </summary>
|
|
|
|
public async Task<bool> TryToStartDevicesDeletionAsync(string simulationId, IDevices devices)
|
|
|
|
{
|
|
|
|
try
|
2018-10-13 02:49:29 +03:00
|
|
|
{
|
2018-11-09 01:59:18 +03:00
|
|
|
var (simulation, jobCreated, jobId) = await this.CreateJobToDeleteDevices(simulationId, devices);
|
2018-10-13 02:49:29 +03:00
|
|
|
|
2018-11-09 01:59:18 +03:00
|
|
|
if (!jobCreated) return true;
|
2018-10-13 02:49:29 +03:00
|
|
|
|
2018-11-09 01:59:18 +03:00
|
|
|
simulation.DeviceDeletionJobId = jobId;
|
|
|
|
simulation.DevicesDeletionStarted = true;
|
2018-10-01 20:08:31 +03:00
|
|
|
|
2018-11-09 01:59:18 +03:00
|
|
|
await this.SaveAsync(simulation, simulation.ETag);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
this.log.Error("Failed to start bulk device deletion", e);
|
|
|
|
return false;
|
2018-10-01 20:08:31 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Task<bool> TryToSetDeviceCreationCompleteAsync(string simulationId)
|
|
|
|
{
|
|
|
|
var simulation = await this.GetAsync(simulationId);
|
|
|
|
|
|
|
|
// Edit the record only if required
|
|
|
|
if (simulation.DevicesCreationComplete) return true;
|
|
|
|
|
2018-10-13 02:49:29 +03:00
|
|
|
simulation.DevicesCreationComplete = true;
|
|
|
|
|
2018-10-24 03:28:38 +03:00
|
|
|
// Reset device deletion state
|
|
|
|
simulation.DevicesDeletionComplete = false;
|
|
|
|
simulation.DeviceDeletionJobId = null;
|
|
|
|
simulation.DevicesDeletionStarted = false;
|
|
|
|
|
2018-10-27 01:49:22 +03:00
|
|
|
return await this.TryToUpdateSimulationAsync(simulation);
|
2018-10-13 02:49:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public async Task<bool> TryToSetDeviceDeletionCompleteAsync(string simulationId)
|
|
|
|
{
|
|
|
|
var simulation = await this.GetAsync(simulationId);
|
|
|
|
|
|
|
|
// Edit the record only if required
|
|
|
|
if (simulation.DevicesDeletionComplete) return true;
|
|
|
|
|
|
|
|
simulation.DevicesDeletionComplete = true;
|
|
|
|
|
|
|
|
// Reset device creation state
|
|
|
|
simulation.DevicesCreationComplete = false;
|
|
|
|
simulation.DeviceCreationJobId = null;
|
|
|
|
simulation.DevicesCreationStarted = false;
|
2018-12-04 23:52:39 +03:00
|
|
|
simulation.DeleteDevicesOnce = false;
|
2018-10-13 02:49:29 +03:00
|
|
|
|
2018-10-27 01:49:22 +03:00
|
|
|
return await this.TryToUpdateSimulationAsync(simulation);
|
2018-10-01 20:08:31 +03:00
|
|
|
}
|
|
|
|
|
2018-08-27 21:38:58 +03:00
|
|
|
public async Task AddDeviceAsync(string id)
|
|
|
|
{
|
2018-09-19 08:22:47 +03:00
|
|
|
await this.storageAdapterClient.CreateAsync(DEVICES_COLLECTION, id, id);
|
2018-08-27 21:38:58 +03:00
|
|
|
}
|
|
|
|
|
Merge azure-iot-pcs-simulation into master (#197)
* Rewrite to use fewer threads and increase throughput
* Expose simulation metrics in the status endpoint
* When available, return a URL to the Azure Portal metrics of the pre-provisioned IoT Hub
* Allow to run a simulation against a custom IoT Hub, passing the connection string
* Allow to schedule a simulation, defining start and end time
* Improve security by running the service as a non-root user
* Support multiple behavior scripts in a device model
* Support custom device with custom sensors, behavior and frequency
* Allow to override the initial device state when creating a simulation
* When a simulation starts, create all the devices in batches
* When a simulation is deleted, delete also the devices
* Refactor timers to start sending telemetry as soon as possible
* Refactor and improve how simulation scripts access state and properties
* Change stock models to use AMQP (#189)
* Overall improvements to exceptions handling
* Disable SDK retry, and change timeout from default (4 minutes) to 10 seconds
* Do not retry sending telemetry on failure, skip message
* Use IoT Hub S2 SKU limits by default
* Upgrade to .NET Core 2.0.3
* Upgrade Azure IoT SDK: Devices 1.4.1 to 1.6.0, Devices Client 1.5.2 to 1.7.0
* Run simulation engine in the same process used for the web service
* Move docs, move wiki, new API specs, fix scripts for Windows Bash
* Fix the spelling of “ETag” and allow ETag=*
* Add internal scripts for increasing/decreasing/random telemetry
* Add env vars documentation ENVIRONMENT_VARIABLES.md
* Add more optional logging by device and by actor
* Use logging level and other logging settings from configuration
* Adjust unit test precision to mitigate flaky test
* Add system properties to telemetry messages
* Removing the squash flag from Docker scripts
* Use env vars in launchSettings.json
2018-04-13 03:42:46 +03:00
|
|
|
/// <summary>
|
|
|
|
/// Get the ID of the devices in a simulation.
|
|
|
|
/// </summary>
|
|
|
|
public IEnumerable<string> GetDeviceIds(Models.Simulation simulation)
|
|
|
|
{
|
|
|
|
var deviceIds = new List<string>();
|
|
|
|
|
|
|
|
// Calculate the device IDs used in the simulation
|
|
|
|
var models = (from model in simulation.DeviceModels where model.Count > 0 select model).ToList();
|
|
|
|
foreach (var model in models)
|
|
|
|
{
|
|
|
|
for (var i = 0; i < model.Count; i++)
|
|
|
|
{
|
2018-11-30 04:29:58 +03:00
|
|
|
deviceIds.Add(this.GenerateId(simulation.Id, model.Id, i));
|
Merge azure-iot-pcs-simulation into master (#197)
* Rewrite to use fewer threads and increase throughput
* Expose simulation metrics in the status endpoint
* When available, return a URL to the Azure Portal metrics of the pre-provisioned IoT Hub
* Allow to run a simulation against a custom IoT Hub, passing the connection string
* Allow to schedule a simulation, defining start and end time
* Improve security by running the service as a non-root user
* Support multiple behavior scripts in a device model
* Support custom device with custom sensors, behavior and frequency
* Allow to override the initial device state when creating a simulation
* When a simulation starts, create all the devices in batches
* When a simulation is deleted, delete also the devices
* Refactor timers to start sending telemetry as soon as possible
* Refactor and improve how simulation scripts access state and properties
* Change stock models to use AMQP (#189)
* Overall improvements to exceptions handling
* Disable SDK retry, and change timeout from default (4 minutes) to 10 seconds
* Do not retry sending telemetry on failure, skip message
* Use IoT Hub S2 SKU limits by default
* Upgrade to .NET Core 2.0.3
* Upgrade Azure IoT SDK: Devices 1.4.1 to 1.6.0, Devices Client 1.5.2 to 1.7.0
* Run simulation engine in the same process used for the web service
* Move docs, move wiki, new API specs, fix scripts for Windows Bash
* Fix the spelling of “ETag” and allow ETag=*
* Add internal scripts for increasing/decreasing/random telemetry
* Add env vars documentation ENVIRONMENT_VARIABLES.md
* Add more optional logging by device and by actor
* Use logging level and other logging settings from configuration
* Adjust unit test precision to mitigate flaky test
* Add system properties to telemetry messages
* Removing the squash flag from Docker scripts
* Use env vars in launchSettings.json
2018-04-13 03:42:46 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return deviceIds;
|
|
|
|
}
|
2018-09-18 08:16:21 +03:00
|
|
|
|
|
|
|
public Dictionary<string, List<string>> GetDeviceIdsByModel(Models.Simulation simulation)
|
|
|
|
{
|
|
|
|
var result = new Dictionary<string, List<string>>();
|
|
|
|
var deviceCount = 0;
|
|
|
|
|
|
|
|
// Load the simulation models with at least 1 device to simulate (ignoring the custom device ID for now)
|
|
|
|
List<Models.Simulation.DeviceModelRef> models = (from model in simulation.DeviceModels where model.Count > 0 select model).ToList();
|
|
|
|
|
|
|
|
// Generate ID, e.g. "1.chiller-01.1", "1.chiller-01.2", etc
|
|
|
|
foreach (var model in models)
|
|
|
|
{
|
|
|
|
var deviceIds = new List<string>();
|
|
|
|
for (var i = 1; i <= model.Count; i++)
|
|
|
|
{
|
2018-11-30 04:29:58 +03:00
|
|
|
deviceIds.Add(this.GenerateId(simulation.Id, model.Id, i));
|
2018-09-18 08:16:21 +03:00
|
|
|
deviceCount++;
|
|
|
|
}
|
|
|
|
|
|
|
|
result.Add(model.Id, deviceIds);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add custom device IDs
|
|
|
|
foreach (var device in simulation.CustomDevices)
|
|
|
|
{
|
|
|
|
if (!result.ContainsKey(device.DeviceModel.Id))
|
|
|
|
{
|
|
|
|
result.Add(device.DeviceModel.Id, new List<string>());
|
|
|
|
}
|
|
|
|
|
|
|
|
result[device.DeviceModel.Id].Add(device.DeviceId);
|
|
|
|
deviceCount++;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.log.Debug("Device IDs loaded", () => new { Simulation = simulation.Id, deviceCount });
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-11-30 04:29:58 +03:00
|
|
|
/// <summary>
|
|
|
|
/// Generate a device Id. The method is here to avoid the need to call Dispose
|
|
|
|
/// on the registry used by the Devices class.
|
|
|
|
/// </summary>
|
|
|
|
public string GenerateId(string simulationId, string deviceModelId, int position)
|
|
|
|
{
|
|
|
|
return simulationId + "." + deviceModelId + "." + position;
|
|
|
|
}
|
|
|
|
|
2018-11-09 01:59:18 +03:00
|
|
|
private async Task<(Models.Simulation simulation, bool jobCreated, string jobId)> CreateJobToDeleteDevices(string simulationId, IDevices devices)
|
|
|
|
{
|
|
|
|
// Fetch latest record, so that updates have the latest ETag if needed
|
|
|
|
var simulation = await this.GetAsync(simulationId);
|
|
|
|
|
|
|
|
// If deletion already happened there's nothing to do, return an empty Job ID
|
|
|
|
if (simulation.DevicesDeletionStarted || simulation.DevicesDeletionComplete)
|
|
|
|
{
|
|
|
|
this.log.Info("Bulk device deletion not needed, devices have already been deleted", () => new { simulation.Id });
|
|
|
|
return (simulation, false, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
Dictionary<string, List<string>> deviceList = this.GetDeviceIdsByModel(simulation);
|
|
|
|
var deviceIds = deviceList.SelectMany(x => x.Value).ToList();
|
|
|
|
|
|
|
|
string jobId = await devices.DeleteListUsingJobsAsync(deviceIds);
|
|
|
|
this.log.Info("Bulk device deletion started", () => new { simulation.Id, jobId, deviceIds.Count });
|
|
|
|
|
|
|
|
return (simulation, true, jobId);
|
|
|
|
}
|
|
|
|
|
2018-10-27 01:49:22 +03:00
|
|
|
private async Task<bool> TryToUpdateSimulationAsync(Models.Simulation simulation)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
await this.SaveAsync(simulation, simulation.ETag);
|
|
|
|
}
|
|
|
|
catch (ConflictingResourceException e)
|
|
|
|
{
|
|
|
|
this.log.Warn("Update failed, another client modified the simulation record", e);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-09-18 08:16:21 +03:00
|
|
|
private async Task<Models.Simulation> SaveAsync(Models.Simulation simulation, string eTag)
|
|
|
|
{
|
|
|
|
simulation.Modified = DateTimeOffset.UtcNow;
|
|
|
|
|
|
|
|
// When a simulation is disabled, its partitions are deleted - this triggers the deletion
|
|
|
|
if (!simulation.Enabled)
|
|
|
|
{
|
|
|
|
simulation.PartitioningComplete = false;
|
|
|
|
}
|
|
|
|
|
2018-12-11 01:59:37 +03:00
|
|
|
IDataRecord record = this.simulationsStorage.BuildRecord(simulation.Id, JsonConvert.SerializeObject(simulation));
|
|
|
|
IDataRecord result = await this.simulationsStorage.UpsertAsync(record, eTag);
|
2018-09-19 08:22:47 +03:00
|
|
|
|
|
|
|
// Use the new ETag provided by the storage
|
2018-12-11 01:59:37 +03:00
|
|
|
simulation.ETag = result.GetETag();
|
|
|
|
simulation.Id = result.GetId();
|
2018-09-18 08:16:21 +03:00
|
|
|
|
|
|
|
this.log.Info("Simulation written to storage",
|
|
|
|
() => new
|
|
|
|
{
|
2018-09-19 08:22:47 +03:00
|
|
|
simulation.Id,
|
|
|
|
simulation.Enabled,
|
2018-09-18 08:16:21 +03:00
|
|
|
simulation.PartitioningComplete
|
|
|
|
});
|
|
|
|
|
|
|
|
return simulation;
|
|
|
|
}
|
2018-10-27 01:49:22 +03:00
|
|
|
|
2018-10-30 10:10:37 +03:00
|
|
|
// This creates sample simulations that will be shown on simulation dashboard by default
|
|
|
|
private async Task SeedSimulationsAsync(string templateName)
|
|
|
|
{
|
|
|
|
string content;
|
|
|
|
var fileName = templateName + ".json";
|
|
|
|
var filePath = Path.Combine(this.config.SeedTemplateFolder, fileName);
|
|
|
|
if (this.fileSystem.Exists(filePath))
|
|
|
|
{
|
|
|
|
content = this.fileSystem.ReadAllText(filePath);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.log.Debug("Template not found for setting sample simulations.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Models.Simulation simulation = null;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
var simulationList = JsonConvert.DeserializeObject<List<Models.Simulation>>(content);
|
|
|
|
if (simulationList == null || simulationList.Count == 0) return;
|
|
|
|
|
|
|
|
for (int index = 0; index < simulationList.Count; index++)
|
|
|
|
{
|
|
|
|
// We need to start creating simulations starting with id 1 as it is treated as default simulation
|
|
|
|
// and is referenced in Welcome page on UI
|
|
|
|
simulation = simulationList[index];
|
|
|
|
var simulationId = index + 1;
|
|
|
|
simulation.Id = simulationId.ToString();
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
// Check if there is an existing simulation with the given id
|
|
|
|
// if it exists then skip creating a new simulation
|
|
|
|
await this.GetAsync(simulation.Id);
|
|
|
|
}
|
|
|
|
catch (ResourceNotFoundException)
|
|
|
|
{
|
|
|
|
// create a simulation if no sample simulation exists with provided id.
|
|
|
|
simulation.StartTime = DateTimeOffset.UtcNow;
|
2018-12-11 01:59:37 +03:00
|
|
|
await this.UpsertAsync(simulation, false);
|
2018-10-30 10:10:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
var message = "Failed to create sample simulation. " + " template: " + JsonConvert.SerializeObject(simulation);
|
|
|
|
this.log.Error(message, ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-27 01:49:22 +03:00
|
|
|
private async Task<Models.Simulation> ResetSimulationStatisticsAsync(Models.Simulation simulation)
|
|
|
|
{
|
|
|
|
// Reset ActualStartTime, which is used to calculate statistics
|
|
|
|
// from the moment when the simulation starts connecting devices and sending telemetry,
|
|
|
|
// i.e. after the devices have been created.
|
|
|
|
simulation.ActualStartTime = null;
|
|
|
|
|
|
|
|
// Delete statistics records on simulation start
|
|
|
|
await this.simulationStatistics.DeleteSimulationStatisticsAsync(simulation.Id);
|
|
|
|
|
|
|
|
return simulation;
|
|
|
|
}
|
2018-10-31 01:57:38 +03:00
|
|
|
|
|
|
|
private async Task ResetActiveDevicesStatistics(Models.Simulation simulation)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
var currentStats = await this.simulationStatistics.GetSimulationStatisticsAsync(simulation.Id);
|
|
|
|
|
|
|
|
if (currentStats != null)
|
|
|
|
{
|
|
|
|
var simulationModel = new SimulationStatisticsModel
|
|
|
|
{
|
|
|
|
ActiveDevices = 0, // Reset active devices to 0
|
|
|
|
TotalMessagesSent = currentStats.TotalMessagesSent,
|
|
|
|
FailedMessages = currentStats.FailedMessages,
|
|
|
|
FailedDeviceConnections = currentStats.FailedDeviceConnections,
|
|
|
|
FailedDevicePropertiesUpdates = currentStats.FailedDevicePropertiesUpdates,
|
|
|
|
};
|
|
|
|
|
|
|
|
await this.simulationStatistics.UpdateAsync(simulation.Id, simulationModel);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
this.log.Error("Error updating active device count statistics.", () => new { simulation.Id, e });
|
|
|
|
}
|
|
|
|
}
|
2017-05-31 06:05:48 +03:00
|
|
|
}
|
|
|
|
}
|