private void OnMessageRecieved(Message message)
 {
     //Gateway vesrion (alive) respond
     if (message.nodeId == 0
         && message.messageType == MessageType.C_INTERNAL
         && message.subType == (int)InternalDataType.I_VERSION)
         ProceedAliveMessage(message);
 }
Ejemplo n.º 2
0
 private void SendMetricResponse(int nodeId)
 {
     Message mess = new Message
     {
         nodeId = nodeId,
         sensorId = 255,
         messageType = MessageType.C_INTERNAL,
         ack = false,
         subType = (int)InternalDataType.I_CONFIG,
         payload = "M"
     };
     SendMessage(mess);
 }
Ejemplo n.º 3
0
        public void SendNewIdResponse()
        {
            int freeId = GetFreeNodeId();

            Message mess = new Message
            {
                nodeId = 255,
                sensorId = 255,
                messageType = MessageType.C_INTERNAL,
                ack = false,
                subType = (int)InternalDataType.I_ID_RESPONSE,
                payload = freeId.ToString()
            };
            SendMessage(mess);
        }
Ejemplo n.º 4
0
 private void SendSensorState(Sensor sensor)
 {
     Message message = new Message
     {
         ack = false,
         messageType = MessageType.C_SET,
         nodeId = sensor.nodeId,
         payload = sensor.state,
         sensorId = sensor.sensorId,
         subType = (int)sensor.dataType
     };
     SendMessage(message);
 }
Ejemplo n.º 5
0
        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);

            if (sensor == null)
            {
                sensor = node.AddSensor(mes.sensorId);
                LogInfo($"Node[{sensor.nodeId}] Sensor[{sensor.sensorId}] registered");

                db?.AddSensor(sensor);
                OnNewSensor?.Invoke(sensor);
            }

            if (mes.messageType == MessageType.C_SET)
            {
                sensor.dataType = (SensorDataType)mes.subType;
                sensor.state = mes.payload;
                LogInfo($"Node[{sensor.nodeId}] Sensor[{sensor.sensorId}] datatype: [{sensor.dataType}] state: [{sensor.state}]");

                db?.UpdateSensor(sensor);
                OnSensorUpdated?.Invoke(sensor);
            }
            else if (mes.messageType == MessageType.C_PRESENTATION)
            {
                try
                {
                    sensor.type = ((SensorType)mes.subType);
                    sensor.SetDefaultDataType();
                }
                catch
                {
                    LogError($"Can`t set sensor type for Node[{mes.nodeId}] Sensor[{mes.sensorId}]: [{mes.subType}]");
                    return;
                }

                if (!String.IsNullOrEmpty(mes.payload))
                    sensor.description = mes.payload;

                LogInfo($"Node[{sensor.nodeId}] Sensor[{sensor.sensorId}] presented type: [{sensor.type}]");
                LogInfo($"Node[{sensor.nodeId}] Sensor[{sensor.sensorId}] datatype set to default: [{sensor.dataType}]");
                if (!String.IsNullOrEmpty(sensor.description))
                    LogInfo($"Node[{sensor.nodeId}] Sensor[{sensor.sensorId}] description: [{sensor.description}]");
                db?.UpdateSensor(sensor);
                OnSensorUpdated?.Invoke(sensor);
            }
        }
Ejemplo n.º 6
0
        private void UpdateNodeFromMessage(Message mes)
        {
            Node node = GetNode(mes.nodeId);

            if (node == null)
            {
                node = new Node(mes.nodeId);

                nodes.Add(node);
                db?.AddNode(node);

                OnNewNode?.Invoke(node);
                LogInfo($"Node[{node.Id}] registered.");
            }

            node.UpdateLastSeenNow();

            if (mes.sensorId == 255)
            {
                if (mes.messageType == MessageType.C_PRESENTATION)
                {
                    if (mes.subType == (int)SensorType.S_ARDUINO_NODE)
                    {
                        node.isRepeatingNode = false;

                        OnNodeUpdated?.Invoke(node);
                    }
                    else if (mes.subType == (int)SensorType.S_ARDUINO_REPEATER_NODE)
                    {
                        node.isRepeatingNode = true;

                        OnNodeUpdated?.Invoke(node);
                    }
                }
                else if (mes.messageType == MessageType.C_INTERNAL)
                {
                    if (mes.subType == (int)InternalDataType.I_SKETCH_NAME)
                    {
                        node.name = mes.payload;
                        LogInfo($"Node[{node.Id}] name: [{node.name}]");

                        OnNodeUpdated?.Invoke(node);
                    }
                    else if (mes.subType == (int)InternalDataType.I_SKETCH_VERSION)
                    {
                        node.version = mes.payload;
                        LogInfo($"Node[{node.Id}] version: [{node.version}]");

                        OnNodeUpdated?.Invoke(node);
                    }
                    else if (mes.subType == (int)InternalDataType.I_BATTERY_LEVEL)
                    {
                        node.batteryLevel = Int32.Parse(mes.payload);
                        LogInfo($"Node[{node.Id}] battery level: [{node.batteryLevel}]");

                        OnNodeBatteryUpdated?.Invoke(node);
                    }
                }
            }

            OnNodeLastSeenUpdated?.Invoke(node);
            db?.UpdateNode(node);
        }
 private void ProceedAliveMessage(Message message)
 {
     checkGatewayLastResponseTime = DateTime.Now;
 }
        private void WriteNewMessages()
        {
            Message[] messages = new Message[newMessages.Count];
            newMessages.CopyTo(messages);
            newMessages.Clear();

            db.Messages.AddRange(messages);
            db.SaveChanges();
        }
Ejemplo n.º 9
0
        public void RecieveMessage(string data)
        {
            //string[] messages = data.Split(new char[] { '\r', '\n' },
            //    StringSplitOptions.None);

            List<string> messages = SplitMessages(data);

            foreach (var message in messages)
            {
                Message mes = null;
                try
                {
                    mes = new Message(message);
                }
                catch
                {
                    LogError($"Failed to process incoming message: [{message}]");
                }

                if (mes != null)
                    RecieveMessage(mes);
            }
        }
Ejemplo n.º 10
0
        public void AddDecodedMessageToLog(Message message)
        {
            //ignore check alive message
            if (message.nodeId == 0
                && message.messageType == MessageType.C_INTERNAL
                && message.subType == (int)InternalDataType.I_VERSION)
                return;

            OnLogDecodedMessage?.Invoke(message);
        }
Ejemplo n.º 11
0
        private void SendMessage(Message message)
        {
            if (gatewayState != GatewayState.Connected
                && gatewayState != GatewayState.ConnectingToGateway)
            {
                LogError("Failed to send message. Gateway is not connected.");
                return;
            }

            message.incoming = false;

            OnMessageSend?.Invoke(message);

            UpdateSensorFromMessage(message);

            string mes = message.ParseToMySensorsMessage();
            connectionPort.SendMessage(mes);


            AddDecodedMessageToLog(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 MySensorsMessages (nodeId, sensorId, messageType, ack, subType ,payload, incoming, dateTime) "
                               +
                               "VALUES(@nodeId, @sensorId, @messageType, @ack, @subType, @payload, @incoming, @dateTime); "
                               + "SELECT CAST(SCOPE_IDENTITY() as int)";
                db.Execute(sqlQuery, messages);
            }
        }
        public void AddMessage(Message message)
        {
            if (!enable) return;

            if (writeInterval == 0)
            {
                using (var db = new SqlConnection(connectionString))
                {
                    db.Open();
                    var sqlQuery = "INSERT INTO MySensorsMessages (nodeId, sensorId, messageType, ack, subType ,payload, incoming, dateTime) "
                                   +
                                   "VALUES(@nodeId, @sensorId, @messageType, @ack, @subType, @payload, @incoming, @dateTime); "
                                   + "SELECT CAST(SCOPE_IDENTITY() as int)";
                    db.Query(sqlQuery, message);
                }
            }
            else
                newMessages.Add(message);
        }
Ejemplo n.º 14
0
        public void AddGatewayDecodedMessage(Message message)
        {
            LogRecord logRecord = new LogRecord(LogRecordSource.GatewayDecodedMessages, LogRecordType.Info, message.ToString());

            if (consoleConfig.ShowGatewayDecodedMessages)
                Show(logRecord);

            OnGatewayDecodedMessageLog?.Invoke(message);

            if (config.StoreGatewayDecodedMessages)
            {
                lock (gatewayDecodedMessagesLogLock)
                {
                    gatewayDecodedMessagesLog.Add(message);
                    if (gatewayDecodedMessagesLog.Count > config.MaxGatewayDecodedMessages)
                        gatewayDecodedMessagesLog.RemoveAt(0);
                }
            }
        }
Ejemplo n.º 15
0
 public void SendGetwayVersionRequest()
 {
     Message mess = new Message
     {
         nodeId = 0,
         sensorId = 0,
         messageType = MessageType.C_INTERNAL,
         ack = false,
         subType = (int)InternalDataType.I_VERSION,
         payload = ""
     };
     SendMessage(mess);
 }
Ejemplo n.º 16
0
 public void SendReboot(int nodeId)
 {
     Message message = new Message
     {
         ack = false,
         messageType = MessageType.C_INTERNAL,
         nodeId = nodeId,
         payload = "0",
         sensorId = 0,
         subType = (int)InternalDataType.I_REBOOT
     };
     SendMessage(message);
 }
Ejemplo n.º 17
0
        public void RecieveMessage(Message message)
        {
            message.incoming = true;

            AddDecodedMessageToLog(message);

            OnMessageRecieved?.Invoke(message);


            //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);

            //Gateway vesrion (alive) response
            if (message.nodeId == 0
                && message.messageType == MessageType.C_INTERNAL
                && message.subType == (int)InternalDataType.I_VERSION)
            {
                if (gatewayState != GatewayState.Connected)
                    SetGatewayState(GatewayState.Connected);
            }

            //request to node
            if (message.nodeId == 0)
                return;

            UpdateNodeFromMessage(message);
            UpdateSensorFromMessage(message);
        }
 public void AddMessage(Message message)
 {
     if (writeInterval == 0)
     {
         db.Messages.Add(message);
         db.SaveChanges();
     }
     else
         newMessages.Add(message);
 }
Ejemplo n.º 19
0
        private void ProceedRequestMessage(Message mes)
        {
            if (mes.messageType != MessageType.C_REQ)
                return;

            Node node = GetNode(mes.nodeId);
            Sensor sensor = node?.GetSensor(mes.sensorId);
            if (sensor == null)
            {
                LogInfo($"Can't answer to REQ message. Node[{mes.nodeId}] Sensor[{mes.sensorId}] does not registered.");
                return;
            }

            //update sensor data type
            if (sensor.dataType != (SensorDataType)mes.subType)
            {
                sensor.dataType = (SensorDataType)mes.subType;
                db?.UpdateSensor(sensor);
            }

            SendSensorState(sensor);
        }