コード例 #1
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);
        }
コード例 #2
0
        public void SendSensorState(int nodeId, int sensorId, string state)
        {
            Node node = GetNode(nodeId);

            if (node == null)
            {
                LogError($"Can`t send message. Node[{nodeId}] does not exist.");
                return;
            }
            Sensor sensor = node.GetSensor(sensorId);

            if (sensor == null)
            {
                LogError($"Can`t send message. Node[{nodeId}] Sensor[{sensorId}] does not exist.");
                return;
            }
            sensor.state = state;


            SendSensorState(sensor);
        }
コード例 #3
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);
            }
        }