private static void processCommand(C2DCommand c2dCommand) { switch (c2dCommand.command) { case C2DCommand.COMMAND_CUTOUT_SPEED_WARNING: displayReceivedCommand(c2dCommand, ConsoleColor.Yellow); break; case C2DCommand.COMMAND_REPAIR_WARNING: displayReceivedCommand(c2dCommand, ConsoleColor.Red); break; case C2DCommand.COMMAND_TURN_ONOFF: displayReceivedCommand(c2dCommand, ConsoleColor.Green); _isStopped = c2dCommand.value.Equals("0"); // 0 means turn the machine off, otherwise is turning on. break; case C2DCommand.COMMAND_RESET_DEPRECIATION: displayReceivedCommand(c2dCommand, ConsoleColor.Cyan); try { _currentDepreciation = Convert.ToDouble(c2dCommand.value); } catch (Exception ex) { Console.WriteLine("Unable to convert '{0}' to a Double. \n\nException={1}", c2dCommand.value, ex.ToString()); } break; default: Console.WriteLine("IT IS NOT A SUPPORTED COMMAND!"); break; } }
private static async void receiveCloudToDeviceMessageAsync() { Console.WriteLine("\nReceiving cloud to device messages from service"); while (true) { Message receivedMessage = await _deviceClient.ReceiveAsync(); if (receivedMessage == null) { continue; // It returns null after a specifiable timeout period (in this case, the default of one minute is used) } string msg = Encoding.ASCII.GetString(receivedMessage.GetBytes()); try { C2DCommand c2dCommand = JsonConvert.DeserializeObject <C2DCommand>(msg); processCommand(c2dCommand); } catch (Exception e) { Console.WriteLine("CANNOT PROCESS THIS COMMAND!"); } await _deviceClient.CompleteAsync(receivedMessage); } }
private static void displayReceivedCommand(C2DCommand c2dCommand, ConsoleColor color) { Console.ForegroundColor = color; Console.WriteLine("Received message: {0}, {1}, {2}\n", c2dCommand.command, c2dCommand.value, c2dCommand.time); Console.ResetColor(); }