Ejemplo n.º 1
0
        public DeviceSimulationService(IOptions <DeviceSettings> deviceSettings, IDTDLMessageService dtdlMessageService, IDTDLCommandService dtdlCommandService, ILoggerFactory loggerFactory)
        {
            if (deviceSettings == null)
            {
                throw new ArgumentNullException(nameof(deviceSettings));
            }

            if (deviceSettings.Value == null)
            {
                throw new ArgumentNullException("deviceSettings.Value", "No device configuration has been loaded.");
            }

            if (deviceSettings.Value.SimulationSettings == null)
            {
                throw new ArgumentNullException("deviceSettings.Value.SimulationSettings");
            }

            if (dtdlMessageService == null)
            {
                throw new ArgumentNullException(nameof(dtdlMessageService));
            }

            if (dtdlCommandService == null)
            {
                throw new ArgumentNullException(nameof(dtdlCommandService));
            }

            if (loggerFactory == null)
            {
                throw new ArgumentNullException(nameof(loggerFactory), "No logger factory has been provided.");
            }

            _deviceSettings     = deviceSettings.Value;
            _simulationSettings = deviceSettings.Value.SimulationSettings;

            _deviceId = _deviceSettings.DeviceId;
            _iotHub   = _deviceSettings.HostName;

            _telemetryInterval = _simulationSettings.TelemetryFrecuency;

            _logger = loggerFactory.CreateLogger <DeviceSimulationService>();

            _dtdlMessageService = dtdlMessageService;
            _dtdlCommandService = dtdlCommandService;

            string logPrefix = "system".BuildLogPrefix();

            _logger.LogDebug($"{logPrefix}::{_deviceSettings.ArtifactId}::Logger created.");
            _logger.LogDebug($"{logPrefix}::{_deviceSettings.ArtifactId}::Device simulator created.");

            //Default DTDL Model
            _defaultModel = _deviceSettings?.SupportedModels?.SingleOrDefault(i => i.ModelId == _deviceSettings.DefaultModelId);
            if (_defaultModel == null)
            {
                throw new Exception("No supported model corresponds to the default model Id.");
            }
        }
        public ModuleSimulationService(ModuleSettings settings, SimulationSettingsModule simulationSettings, IDTDLMessageService dtdlMessagingService, IDTDLCommandService dtdlCommandService, ILoggerFactory loggerFactory)
        {
            if (settings == null)
            {
                throw new ArgumentNullException(nameof(settings));
            }

            if (simulationSettings == null)
            {
                throw new ArgumentNullException(nameof(simulationSettings));
            }

            if (dtdlMessagingService == null)
            {
                throw new ArgumentNullException(nameof(dtdlMessagingService));
            }

            if (loggerFactory == null)
            {
                throw new ArgumentNullException(nameof(loggerFactory));
            }

            string logPrefix = "system".BuildLogPrefix();

            ModuleSettings     = settings;
            SimulationSettings = simulationSettings;
            _logger            = loggerFactory.CreateLogger <ModuleSimulationService>();

            _dtdlMessagingService = dtdlMessagingService;
            _dtdlCommandService   = dtdlCommandService;

            _telemetryInterval = 10;
            _stopProcessing    = false;

            _moduleClient = ModuleClient.CreateFromConnectionString(ModuleSettings.ConnectionString, Microsoft.Azure.Devices.Client.TransportType.Mqtt);
            _logger.LogDebug($"{logPrefix}::{ModuleSettings.ArtifactId}::Logger created.");
            _logger.LogDebug($"{logPrefix}::{ModuleSettings.ArtifactId}::Module simulator created.");

            //Default DTDL Model
            _defaultModel = ModuleSettings?.SupportedModels?.SingleOrDefault(i => i.ModelId == ModuleSettings.DefaultModelId);
            if (_defaultModel == null)
            {
                throw new Exception("No supported model corresponds to the default model Id.");
            }
        }