protected override void OnMessage(MessageEventArgs e) { var message = JsonConvert.DeserializeObject <Dictionary <string, int> >(e.Data); Console.WriteLine(e.Data); if (message["commandType"] == 1) { var device = DevicesManager.devices[(byte)message["deviceID"]]; var valueID = (byte)message["valueID"]; var value = (short)message["value"]; device.SendMessage(CommandID.Set, valueID, value); message["commandType"] = 2; Send(JsonConvert.SerializeObject(message)); device.values[valueID] = value; Console.WriteLine("Updating device on Rest server"); RestManager.Instance.SendDevice(device); } else if (message["commandType"] == 3) { var device = DevicesManager.devices[(byte)message["deviceID"]]; var valueID = (byte)message["valueID"]; device.SendMessage(CommandID.Get, valueID, 0); Message response; try { response = Message.Read(); } catch (System.TimeoutException) { return; } var value = response.GetShortData(); device.values[valueID] = value; Console.WriteLine("Updating device on Rest server"); RestManager.Instance.SendDevice(device); message.Add("value", value); message["commandType"] = 2; Send(JsonConvert.SerializeObject(message)); } else if (message["commandType"] == 4) { var confirmation = message["confirmation"]; if (confirmation == 0) { DevicesManager.BeginPairingDevices(); message["confirmation"] = 1; Send(JsonConvert.SerializeObject(message)); } } else if (message["commandType"] == 5) { var confirmation = message["confirmation"]; if (confirmation == 0) { DevicesManager.StopPairingDevices(); message["confirmation"] = 1; Send(JsonConvert.SerializeObject(message)); } } else if (message["commandType"] == 6) { var confirmation = message["confirmation"]; if (confirmation != 0) { return; } DevicesManager.ScanDevices(); DevicesManager.UpdateDevicesValues(); RestManager.Instance.SynchronizeDevices(DevicesManager.devices); message["confirmation"] = 1; Send(JsonConvert.SerializeObject(message)); } }
public static void PairDevices() { Message message; var expectedHeader = Message.CreateHeader(0, CommandIDPair.Ask); Byte?[] emptyArray = { 0, 0, 0 }; Console.WriteLine("Going on an adventure!"); while (pairingThread.ThreadState != ThreadState.AbortRequested) { try { message = new Message(0, CommandID.Error, emptyArray); message = Message.Read(); Console.WriteLine("Yay, a message!"); if (message.GetHeader() == expectedHeader) { Device newDevice = new Device(); Console.WriteLine("Assigning Device id"); newDevice.deviceID = avaliableDeviceIDs[0]; avaliableDeviceIDs.RemoveAt(0); Console.WriteLine("Getting product id"); newDevice.productID = message.GetProductID(); Console.WriteLine($"It's a device with id {newDevice.deviceID} and has product id {newDevice.productID}"); Thread.Sleep(1000); var retMessage = Message.CreatePairReturn(newDevice.deviceID); retMessage.Send(); try { var pingBack = Message.Read(); } catch (System.TimeoutException) { Console.WriteLine("Device didn't respond to ping"); throw new SystemException("Sadness"); } newDevice.SetupValues(ProductsManager.products[newDevice.productID]); devices.Add(newDevice.deviceID, newDevice); if (DevicesManager.OnDevicePaired != null) { DevicesManager.OnDevicePaired(newDevice); } } } catch (System.TimeoutException) { Console.WriteLine("didn't get a message #ForeverAlone"); } } }
static void Main(string[] args) { // Initializing Pin 11 which is the pin we are using to switch between read and write on the RS486 IC Message.rwPinConfig = ConnectorPin.P1Pin11.Output(); Message.rwPinConnection = new GpioConnection(Message.rwPinConfig); // Getting path to folder with settings from command line argument // If there's no command line argument, we make a path based on the OS's preferences if (args[0] != null) { configFolderPath = args[0]; } else { configFolderPath = Path.Combine( Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "hjemat-app"); } // We create the path if it doesn't exists. if (!File.Exists(configFolderPath)) { Directory.CreateDirectory(args[0] ?? configFolderPath); } // Loading settings from the settings file in the config path config = LoadSettings(configFolderPath); // If we failed to load the settings, we tell the user and exits if (config == null) { Console.WriteLine("Failed to load settings file"); return; } // If the serial config in the settings are invalid we tell the user and exits if (config.serialConfig == null) { Console.WriteLine("Error getting SerialConfig from settings"); return; } Console.WriteLine("Loaded settings"); // Loads the products from the products file ProductsManager.products = GetProductsDict(configFolderPath); // Sets up the serial port according to the settings file // The serial port is what we use to communicate with RS485 serialPort = config.CreateSerialPort(); Message.serialPort = serialPort; Device.serialPort = serialPort; // Opens the serial port so we can use it. // If it fails to open the program exits and the error is shown to ther user. try { serialPort.Open(); } catch (System.Exception exp) { Console.WriteLine($"Error opening serial port. Make sure device is connected and that {serialPort.PortName} is the correct port"); Console.WriteLine($"Error message: {exp.Message}"); return; } Console.WriteLine("Giving port time to open..."); Delay(400); Console.WriteLine(""); // Tries to ping all device ids, so we can find devices which already has been assigned an id DevicesManager.ScanDevices(); // Gets values from devices DevicesManager.UpdateDevicesValues(); // Sets up connection to the database restManager = new RestManager(config.serverUrl); // Synchronize devices with the database restManager.SynchronizeDevices(DevicesManager.devices); // Starts the WebSocket server var wssv = WebSocket.CreateServer(); wssv.Start(); Console.WriteLine($"Started WebSocket server on port {wssv.Port}"); // Prepares for sending device data over WebSocket when devices are paired DevicesManager.OnDevicePaired += WebSocket.SendDevice; // Infinite loop to keep the program running, until isRunning is set to false while (isRunning) { Loop(); } // Closes serial port, pin connection and websocket to clean up wssv.Stop(); serialPort.Close(); Message.rwPinConnection.Close(); }