public void AddNewMessage(Message message) { if (messages.Count >= maxMessaages) messages.Remove(messages.First()); messages.Add(message); if (OnNewMessageLogged != null) OnNewMessageLogged(message); }
public void AddMessage(Message message) { using (var db = new SqlConnection(connectionString)) { db.Open(); var sqlQuery = "INSERT INTO Messages (nodeId, sensorId, messageType, ack, subType ,payload, isValid, incoming, dateTime) " + "VALUES(@nodeId, @sensorId, @messageType, @ack, @subType, @payload, @isValid, @incoming, @dateTime); " + "SELECT CAST(SCOPE_IDENTITY() as int)"; db.Query(sqlQuery, message); } }
private void WriteNewMessages() { using (var db = new SqlConnection(connectionString)) { db.Open(); //to prevent changing of collection while writing to db is not yet finished Message[] messages = new Message[newMessages.Count]; newMessages.CopyTo(messages); newMessages.Clear(); var sqlQuery = "INSERT INTO Messages (nodeId, sensorId, messageType, ack, subType ,payload, isValid, incoming, dateTime) " + "VALUES(@nodeId, @sensorId, @messageType, @ack, @subType, @payload, @isValid, @incoming, @dateTime); " + "SELECT CAST(SCOPE_IDENTITY() as int)"; db.Execute(sqlQuery, messages); } }
private void OnNewMessage(Message message) { if (!storeTxRxMessages) return; if (writeInterval == 0) AddMessage(message); else newMessages.Add(message); }
public void SendReboot(int nodeId) { Message message = new Message(); message.ack = false; message.messageType = MessageType.C_INTERNAL; message.nodeId = nodeId; message.payload = "0"; message.sensorId = 0; message.subType = (int)InternalDataType.I_REBOOT; SendMessage(message); }
private void SendMetricResponse(int nodeId) { Message mess = new Message(); mess.nodeId = nodeId; mess.sensorId = 255; mess.messageType = MessageType.C_INTERNAL; mess.ack = false; mess.subType = (int)InternalDataType.I_CONFIG; mess.payload = "M"; SendMessage(mess); }
public void SendNewIdResponse() { int freeId = GetFreeNodeId(); Message mess = new Message(); mess.nodeId = 255; mess.sensorId = 255; mess.messageType = MessageType.C_INTERNAL; mess.ack = false; mess.subType = (int)InternalDataType.I_ID_RESPONSE; mess.payload = freeId.ToString(); SendMessage(mess); }
private void SendSensorState(Sensor sensor) { Message message = new Message(); message.ack = false; message.messageType = MessageType.C_SET; message.nodeId = sensor.nodeId; message.payload = sensor.state; message.sensorId = sensor.sensorId; message.subType = (int)sensor.dataType; SendMessage(message); }
public Message ParseMessageFromString(string message) { var mes = new Message(); try { string[] arguments = message.Split(new char[] { ';' }, 6); mes.nodeId = Int32.Parse(arguments[0]); mes.sensorId = Int32.Parse(arguments[1]); mes.messageType = (MessageType)Int32.Parse(arguments[2]); mes.ack = arguments[3] == "1"; mes.subType = Int32.Parse(arguments[4]); mes.payload = arguments[5]; } catch { mes = new Message(); mes.isValid = false; mes.payload = message; } return mes; }
public void UpdateSensorFromMessage(Message mes) { //if internal node message if (mes.sensorId == 255) return; if (mes.messageType != MessageType.C_PRESENTATION && mes.messageType != MessageType.C_SET) return; Node node = GetNode(mes.nodeId); Sensor sensor = node.GetSensor(mes.sensorId); bool isNewSensor = false; if (sensor == null) { sensor = node.AddSensor(mes.sensorId); isNewSensor = true; } if (mes.messageType == MessageType.C_SET) { sensor.dataType= (SensorDataType)mes.subType; sensor.state = mes.payload; sensor.RemapSensorData(); } else if (mes.messageType == MessageType.C_PRESENTATION) { if (mes.subType < 0 || mes.subType > (int)Enum.GetValues(typeof(SensorType)).Cast<SensorType>().Max()) { DebugGatewayState("Exception occurs when the serial port does not have time to write the data"); throw new ArgumentOutOfRangeException( "Exception occurs when the serial port does not have time to write the data"); } sensor.SetSensorType((SensorType)mes.subType); if (!String.IsNullOrEmpty(mes.payload)) sensor.description = mes.payload; } if (isNewSensor) { if (OnNewSensorEvent != null) OnNewSensorEvent(sensor); DebugGatewayState($"New sensor (node id {sensor.nodeId}, sensor id: {sensor.sensorId}) registered"); } else { if (OnSensorUpdatedEvent != null) OnSensorUpdatedEvent(sensor); } }
private void UpdateNodeFromMessage(Message mes) { Node node = GetNode(mes.nodeId); if (node == null) { node = new Node(mes.nodeId); nodes.Add(node); if (OnNewNodeEvent != null) OnNewNodeEvent(node); DebugGatewayState($"New node (id: {node.nodeId}) registered"); } node.UpdateLastSeenNow(); if (OnNodeLastSeenUpdatedEvent != null) OnNodeLastSeenUpdatedEvent(node); if (mes.sensorId == 255) { if (mes.messageType == MessageType.C_PRESENTATION) { if (mes.subType == (int)SensorType.S_ARDUINO_NODE) { node.isRepeatingNode = false; } else if (mes.subType == (int)SensorType.S_ARDUINO_REPEATER_NODE) { node.isRepeatingNode = true; } if (OnNodeUpdatedEvent != null) OnNodeUpdatedEvent(node); DebugGatewayState($"Node {node.nodeId} updated"); } else if (mes.messageType == MessageType.C_INTERNAL) { if (mes.subType == (int)InternalDataType.I_SKETCH_NAME) { node.name = mes.payload; if (OnNodeUpdatedEvent != null) OnNodeUpdatedEvent(node); DebugGatewayState($"Node {node.nodeId} updated"); } else if (mes.subType == (int)InternalDataType.I_SKETCH_VERSION) { node.version = mes.payload; if (OnNodeUpdatedEvent != null) OnNodeUpdatedEvent(node); DebugGatewayState($"Node {node.nodeId} updated"); } else if (mes.subType == (int)InternalDataType.I_BATTERY_LEVEL) { node.batteryLevel = Int32.Parse(mes.payload); if (OnNodeBatteryUpdatedEvent != null) OnNodeBatteryUpdatedEvent(node); return; } } } }
private void ProceedRequestMessage(Message mes) { if (mes.messageType != MessageType.C_REQ) return; Node node = GetNode(mes.nodeId); if (node == null) return; Sensor sensor = node.GetSensor(mes.sensorId); if (sensor == null) return; sensor.dataType = (SensorDataType)mes.subType; sensor.state = mes.payload; SendSensorState(sensor); }
public void RecieveMessage(Message message) { message.incoming = true; if (storeMessages) messagesLog.AddNewMessage(message); DebugTxRx($"RX: { message.ToString()}"); if (OnMessageRecievedEvent != null) OnMessageRecievedEvent(message); if (message.isValid) { //Gateway ready if (message.messageType == MessageType.C_INTERNAL && message.subType == (int)InternalDataType.I_GATEWAY_READY) return; //Gateway log message if (message.messageType == MessageType.C_INTERNAL && message.subType == (int)InternalDataType.I_LOG_MESSAGE) return; //New ID request if (message.nodeId == 255) { if (message.messageType == MessageType.C_INTERNAL && message.subType == (int)InternalDataType.I_ID_REQUEST) if (enableAutoAssignId) SendNewIdResponse(); return; } //Metric system request if (message.messageType == MessageType.C_INTERNAL && message.subType == (int)InternalDataType.I_CONFIG) SendMetricResponse(message.nodeId); //Sensor request if (message.messageType == MessageType.C_REQ) ProceedRequestMessage(message); UpdateNodeFromMessage(message); UpdateSensorFromMessage(message); } }
private void SendMessage(Message message) { message.incoming = false; if (OnMessageSendEvent != null) OnMessageSendEvent(message); UpdateSensorFromMessage(message); //todo if (message.messageType == MessageType.C_SET) //message = DeRemapMessage(message); DebugTxRx($"TX: {message.ToString()}"); string ack = (message.ack) ? "1" : "0"; string mes = $"{message.nodeId};{message.sensorId};{(int)message.messageType};{ack};{message.subType};{message.payload}\n"; SendToSerial(mes); if (storeMessages) messagesLog.AddNewMessage(message); }