public async Task TestSensor() { List <IoTDevice> iotDevices = await _iotDeviceHelper.GetAllInputDevices(); if (iotDevices == null || iotDevices.Count == 0) { ConsoleHelper.WriteWarning($"No devices found! Make sure to run '1. Setup list of devices' first."); return; } int i = 0; int valueParsed = 0; IoTDevice device = null; do { bool waitingForNumber = true; do { ConsoleHelper.ClearConsole(); ConsoleHelper.WriteHighlight($"{i} messages sent from {(iotDevices.Count)} device(s)."); ConsoleHelper.WriteInfo(""); for (int n = 0; n < iotDevices.Count; n++) { ConsoleHelper.WriteInfo($"{n}. {iotDevices[n].DeviceType} {iotDevices[n].Name} [{iotDevices[n].Longitude}|{iotDevices[n].Latitude}]"); } ConsoleHelper.WriteInfo(""); ConsoleHelper.WriteInfo("Type a number corresponding to the device to trigger. Or stop to terminate the simulation"); string value = Console.ReadLine(); if (int.TryParse(value, out valueParsed)) { waitingForNumber = false; } if (value.ToLower() == "stop") { return; } }while (waitingForNumber); device = iotDevices[valueParsed]; string eventType = device.DeviceType == "SoundSensor" ? "Sound" : "Button"; object messageObj = new ButtonSensorMessage(); if (device.DeviceType == "SoundSensor") { messageObj = new SoundSensorMessage() { Decibel = GetSoundMetadata() }; } await _iotDeviceHelper.SendMessage(device, eventType, messageObj); i++; await Task.Delay(1000); }while (true); }
public async void TestSensors() { _InterruptSimulation = false; List <IoTDevice> iotDevices = await _iotDeviceHelper.GetAllInputDevices(); if (iotDevices == null || iotDevices.Count == 0) { ConsoleHelper.WriteWarning($"No devices found! Make sure to run '1. Setup list of devices' first."); _InterruptSimulation = true; return; } int i = 0; IoTDevice device = null; do { device = iotDevices[_rand.Next(0, iotDevices.Count - 1)]; string eventType = device.DeviceType == "SoundSensor" ? "Sound" : "Button"; object messageObj = new ButtonSensorMessage(); if (device.DeviceType == "SoundSensor") { messageObj = new SoundSensorMessage() { Decibel = GetSoundMetadata() }; } await _iotDeviceHelper.SendMessage(device, eventType, messageObj); i++; ConsoleHelper.ClearConsole(); ConsoleHelper.WriteInfo("Press Escape when you want to end the simulation."); ConsoleHelper.WriteInfo(""); ConsoleHelper.WriteHighlight($"{i} messages sent from {(iotDevices.Count)} device(s)."); ConsoleHelper.WriteInfo(""); ConsoleHelper.WriteInfo($"Last message sent from {device.DeviceId}"); ConsoleHelper.WriteInfo($"-Device Type: {device.DeviceType}"); ConsoleHelper.WriteInfo($"-Event Type: {eventType}"); ConsoleHelper.WriteInfo($"-Location Name: {device.Name}"); ConsoleHelper.WriteInfo($"-Location 1: {device.Location1}"); ConsoleHelper.WriteInfo($"-Location 2: {device.Location2}"); ConsoleHelper.WriteInfo($"-Location 3: {device.Location3}"); ConsoleHelper.WriteInfo($"-Latitude: {device.Latitude}"); ConsoleHelper.WriteInfo($"-Longitude: {device.Longitude}"); await Task.Delay(1000); }while (!_InterruptSimulation); }
public async void Simulate(string path) { _InterruptSimulation = false; //Get devices List <IoTDevice> iotDevices = await _iotDeviceHelper.GetAllInputDevices(); if (iotDevices == null || iotDevices.Count == 0) { ConsoleHelper.WriteWarning($"No devices found! Make sure to run '1. Setup list of devices' first."); _InterruptSimulation = true; return; } //Get story messages and validation List <StoryMessage> storyMessages = GetSimulationStory(path); if (storyMessages == null || storyMessages.Count == 0) { ConsoleHelper.WriteWarning($"No story message found! Choose a valid csv file."); _InterruptSimulation = true; return; } List <string> deviceIds = storyMessages.GroupBy(p => p.DeviceId).Select(p => p.Key).ToList(); if (deviceIds.Intersect(iotDevices.Select(p => p.DeviceId)).Count() != deviceIds.Count()) { ConsoleHelper.WriteWarning($"Some devices in the story aren't properly set in IoT Hub. Please run the sensors csv file."); _InterruptSimulation = true; return; } int elapsedTime = 0; IoTDevice device = null; StoryMessage storyLog = null; for (int i = 0; i < storyMessages.Count; i++) { StoryMessage storyMessage = storyMessages[i]; if (_InterruptSimulation) { return; } if (storyMessage.Timestamp - elapsedTime > 0) { await Task.Delay(storyMessage.Timestamp - elapsedTime); elapsedTime += storyMessage.Timestamp - elapsedTime; } if (_InterruptSimulation) { return; } device = iotDevices.Find(p => p.DeviceId == storyMessage.DeviceId); object messageObj = new ButtonSensorMessage(); if (device.DeviceType == "SoundSensor") { messageObj = new SoundSensorMessage() { Decibel = GetSoundMetadata() }; } await _iotDeviceHelper.SendMessage(device, storyMessage.EventType, messageObj); i++; int start = i > (SIMULATION_WINDOW_SIZE - 1) ? i - (SIMULATION_WINDOW_SIZE - 1) : 0; ConsoleHelper.ClearConsole(); ConsoleHelper.WriteInfo("Press Escape when you want to end the simulation."); ConsoleHelper.WriteInfo(""); ConsoleHelper.WriteHighlight($"{i} messages sent from {(deviceIds.Count)} demo device(s)."); ConsoleHelper.WriteInfo(""); ConsoleHelper.WriteInfo($"================================="); for (int j = start; j < start + SIMULATION_WINDOW_SIZE; j++) { storyLog = storyMessages[j]; if (j < i) { ConsoleHelper.WriteInfo($"--{storyLog.Timestamp}-{storyLog.DeviceId}"); } else if (j > i) { ConsoleHelper.WriteInfo($""); } else { ConsoleHelper.WriteHighlight($"--{storyLog.Timestamp}-{storyLog.DeviceId}"); } } ConsoleHelper.WriteInfo($"================================="); } ConsoleHelper.WriteInfo(""); ConsoleHelper.WriteHighlight("Done!"); await Task.Delay(5000); }