public async Task SendCommand(SmartBrickMessage message) { //serialize byte[] data = message.SerializeData(); //encrypt //pack var sockMsg = new List <byte>(); sockMsg.AddRange(message.PipeAddress); sockMsg.AddRange(data); try { _sending = true; //send await _socket.SendTask(sockMsg.ToArray(), 0, sockMsg.Count, SocketFlags.None); _sending = false; } catch (Exception ex) { log.Error(ex.Message); } finally { _sending = false; } }
public IProcessingResult ProcessCommand(IMessage command) { //log.Info("Processing in EchoModule"); var webSocketMessage = (WebSocketMessage)command; byte smartBrickId = 5; var brickMsg = new SmartBrickMessage() { SmartBrickID = smartBrickId, CommandCode = Convert.ToByte(webSocketMessage.Message), Payload = null, PipeAddress = GetPipeAddress(smartBrickId) }; //var wsResponse = new { // WidgetId //} var result = new ProcessingResult(new SmartBrickMessage[] { brickMsg }, new WebSocketMessage[] { webSocketMessage }); return(result); }
public IProcessingResult ProcessCommand(IMessage command) { log.Info("Processing in ColorPickerSwitchModule"); var webSocketMessage = (WebSocketMessage)command; var colorPickerSwitchWidgetMsg = JsonConvert.DeserializeObject <ColorPickerSwitchWidgetMsg>(webSocketMessage.Message); var ledDriverMsg = new SmartBrickMessage(); ledDriverMsg.SmartBrickID = 20; ledDriverMsg.CommandCode = (byte)(colorPickerSwitchWidgetMsg.IsTurnedOn ? 1 : 2); if (colorPickerSwitchWidgetMsg.IsTurnedOn) { ledDriverMsg.Payload = new byte[3] { colorPickerSwitchWidgetMsg.Color1, colorPickerSwitchWidgetMsg.Color2, colorPickerSwitchWidgetMsg.Color3 }; } else { ledDriverMsg.Payload = new byte[3] { 0, 0, 0 }; } ledDriverMsg.PipeAddress = GetPipeAddress(ledDriverMsg.SmartBrickID); var dimmerMsg = new SmartBrickMessage(); dimmerMsg.SmartBrickID = 40; dimmerMsg.CommandCode = (byte)DimmerCommands.UPDATE; dimmerMsg.Payload = ledDriverMsg.Payload; dimmerMsg.PipeAddress = GetPipeAddress(dimmerMsg.SmartBrickID); var wsMsgList = new List <WebSocketMessage>(); var webSocketPayload = new WebSocketPayload() { WidgetID = 2, WidgetType = WidgetType.ColorPickerSwitch, Message = JObject.Parse(webSocketMessage.Message) }; foreach (string key in WebSocketEndpoint.SocketDict.Keys) { wsMsgList.Add(new WebSocketMessage() { SocketSessionID = key, Message = JsonConvert.SerializeObject(webSocketPayload) }); } //Update other clients with current value return(new ProcessingResult(new SmartBrickMessage[] { ledDriverMsg, dimmerMsg }, wsMsgList)); }
public IProcessingResult ProcessCommand(IMessage message) { log.Info("Processing in WarmFloorWebModule"); var webSocketMessage = (WebSocketMessage)message; var warmFloorWidgetMsg = JsonConvert.DeserializeObject <WarmFloorWidgetMsg>(webSocketMessage.Message); var sbMsg = new SmartBrickMessage() { SmartBrickID = 50, PipeAddress = GetPipeAddress(50) }; if (warmFloorWidgetMsg.Action == "ToggledState") { if (warmFloorWidgetMsg.IsTurnedOn) { sbMsg.CommandCode = (byte)WarmFloorCommands.AutoHeat; } else { sbMsg.CommandCode = (byte)WarmFloorCommands.TurnOff; } } if (warmFloorWidgetMsg.Action == "ChangedTemp") { sbMsg.CommandCode = (byte)WarmFloorCommands.SetTemp; sbMsg.Payload = BitConverter.GetBytes(warmFloorWidgetMsg.TargetTemperature); } var wsMsgList = new List <WebSocketMessage>(); var webSocketPayload = new WebSocketPayload() { WidgetID = 11, WidgetType = WidgetType.WarmFloor, Message = JObject.Parse(webSocketMessage.Message) }; foreach (string key in WebSocketEndpoint.SocketDict.Keys) { wsMsgList.Add(new WebSocketMessage() { SocketSessionID = key, Message = JsonConvert.SerializeObject(webSocketPayload) }); } //Update other clients with current value return(new ProcessingResult(new SmartBrickMessage[] { sbMsg }, wsMsgList)); }
private void OnMessageReceived(byte[] payload) { //decrypt byte[] decryptedPayload = payload; //deserialize SmartBrickMessage brickMessage = null; try { brickMessage = SmartBrickMessage.DeserializeData(decryptedPayload); } catch (Exception ex) { log.Error("Error during deserialization", ex); return; } ProcessCommand(brickMessage); }
public IModule GetProcessingModule(IMessage message) { if (message.Source == MessageSource.WebSocket) { WebSocketMessage webSocketMessage = (WebSocketMessage)message; if (webSocketMessage.WidgetType == WidgetType.ColorPickerSwitch) { return(new ColorPickerSwitchModule()); } if (webSocketMessage.WidgetType == WidgetType.LightSwitch) { return(new LightSwitchModule()); } if (webSocketMessage.WidgetType == WidgetType.WarmFloor) { return(new WarmFloorWebModule()); } return(new EchoModule()); } if (message.Source == MessageSource.UnixSocket) { SmartBrickMessage smartBrickMessage = (SmartBrickMessage)message; if (smartBrickMessage.SmartBrickID == 25) { return(new ThermoModule()); } if (smartBrickMessage.SmartBrickID == 40) { return(new DimmerModule()); } if (smartBrickMessage.SmartBrickID == 50) { return(new WarmFloorModule()); } return(new TestUnixModule()); } throw new Exception("No module found"); }
public IProcessingResult ProcessCommand(IMessage command) { log.Info("Processing in LightSwitchModule"); var webSocketMessage = (WebSocketMessage)command; var msg = JsonConvert.DeserializeObject <LightSwitchWidgetMsg>(webSocketMessage.Message); byte smartBrickId = 15; var brickMsg = new SmartBrickMessage() { SmartBrickID = smartBrickId, CommandCode = (byte)RelayDriverCommands.Switch, Payload = new byte[] { (byte)msg.LightID }, PipeAddress = GetPipeAddress(smartBrickId) }; //Do not send response to UI. Widget should be updated by SmartBrick that checks light status var result = new ProcessingResult(new SmartBrickMessage[] { brickMsg }, null); return(result); }
public IProcessingResult ProcessCommand(IMessage message) { var incomingMsg = (SmartBrickMessage)message; byte rotationValue = 0; byte color1 = 0; byte color2 = 1; byte color3 = 2; if (incomingMsg.CommandCode == (byte)DimmerCommands.PRESSED_BTN) { } if (incomingMsg.CommandCode == (byte)DimmerCommands.ROTATED) { //0-255 //rotationValue = incomingMsg.Payload[0]; //color = incomingMsg.Payload[1]; color1 = incomingMsg.Payload[0]; color2 = incomingMsg.Payload[1]; color3 = incomingMsg.Payload[2]; } var ledDriverMessage = new SmartBrickMessage(); LEDDriverCommands commandCode; /* if (rotationValue == 0) * { * //all colors * if (color == 0) * { * commandCode = LEDDriverCommands.TURN_OFF; * } * else * { * commandCode = LEDDriverCommands.UPDATE_COLOR; * } * } * else * { * //all colors * if (color == 0) * { * commandCode = LEDDriverCommands.TURN_ON; * } * else * { * commandCode = LEDDriverCommands.UPDATE_COLOR; * * } * }*/ ledDriverMessage.SmartBrickID = 20; ledDriverMessage.CommandCode = (byte)LEDDriverCommands.TURN_ON; ledDriverMessage.Payload = incomingMsg.Payload;// GetColorPayload(color, rotationValue); ledDriverMessage.PipeAddress = GetPipeAddress(ledDriverMessage.SmartBrickID); var wsMessageList = new List <WebSocketMessage>(); var widgetMsg = new ColorPickerSwitchWidgetMsg() { IsTurnedOn = true,//rotationValue > 0 || color != 0, Color1 = ledDriverMessage.Payload[1], Color2 = ledDriverMessage.Payload[2], Color3 = ledDriverMessage.Payload[3], }; WebSocketPayload webSocketPayload = new WebSocketPayload() { WidgetID = 2, WidgetType = WidgetType.ColorPickerSwitch, Message = JsonConvert.SerializeObject(widgetMsg) }; foreach (string key in WebSocketEndpoint.SocketDict.Keys) { wsMessageList.Add(new WebSocketMessage() { SocketSessionID = key, Message = JsonConvert.SerializeObject(webSocketPayload) }); } //Update other clients with current value return(new ProcessingResult(new SmartBrickMessage[] { ledDriverMessage }, wsMessageList)); }