예제 #1
0
 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";
     mess.isValid = true;
     SendMessage(mess);
 }
예제 #2
0
 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;
     message.isValid = true;
     SendMessage(message);
 }
예제 #3
0
        public void SendSensorState(int nodeId, int sensorId, SensorData data)
        {
            Sensor sensor = GetNode(nodeId).GetSensor(sensorId);
            sensor.AddOrUpdateData(data);

            Message message = new Message();
            message.ack = false;
            message.messageType = MessageType.C_SET;
            message.nodeId = nodeId;
            message.payload = data.state;
            message.sensorId = sensorId;
            message.subType = (int)data.dataType;
            message.isValid = true;
            SendMessage(message);

            if (OnSensorUpdatedEvent != null)
                OnSensorUpdatedEvent(sensor);
        }
예제 #4
0
        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();
            mess.isValid = true;
            SendMessage(mess);
        }
예제 #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);
            bool isNewSensor = false;

            if (sensor == null)
            {
                sensor = node.AddSensor(mes.sensorId);
                isNewSensor = true;
            }

            if (mes.messageType == MessageType.C_SET)
            {
                SensorDataType dataType = (SensorDataType)mes.subType;
                sensor.AddOrUpdateData(dataType, mes.payload);
            }
            else if (mes.messageType == MessageType.C_PRESENTATION)
            {
                if (mes.subType < 0 || mes.subType > (int)Enum.GetValues(typeof(SensorType)).Cast<SensorType>().Max())
                {
                    throw new ArgumentOutOfRangeException(
                        "This 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(String.Format("New sensor (node id {0}, sensor id: {1}) registered", sensor.ownerNodeId,
                    sensor.sensorId));
            }
            else
            {
                if (OnSensorUpdatedEvent != null)
                    OnSensorUpdatedEvent(sensor);
            }

        }
예제 #6
0
        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];
                mes.isValid = true;
            }
            catch
            {
                mes = new Message();
                mes.isValid = false;
                mes.payload = message;
            }
            return mes;
        }
예제 #7
0
        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;

            SensorDataType dataType = (SensorDataType)mes.subType;
            SensorData data = sensor.GetData(dataType);
            if (data == null) return;

            SendSensorState(mes.nodeId, mes.sensorId, data);
        }
예제 #8
0
        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(String.Format("New node (id: {0}) registered", node.nodeId));
            }

            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(String.Format("Node {0} updated", node.nodeId));
                }
                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(String.Format("Node {0} updated", node.nodeId));
                    }
                    else if (mes.subType == (int)InternalDataType.I_SKETCH_VERSION)
                    {
                        node.version = mes.payload;

                        if (OnNodeUpdatedEvent != null)
                            OnNodeUpdatedEvent(node);

                        DebugGatewayState(String.Format("Node {0} updated", node.nodeId));
                    }
                    else if (mes.subType == (int)InternalDataType.I_BATTERY_LEVEL)
                    {
                        node.batteryLevel = Int32.Parse(mes.payload);
                        if (OnNodeBatteryUpdatedEvent != null)
                            OnNodeBatteryUpdatedEvent(node);
                        return;
                    }
                }
            }

        }
예제 #9
0
        public void SendMessage(Message message)
        {
            message.incoming = false;

            if (OnMessageSendEvent != null)
                OnMessageSendEvent(message);

            DebugTxRx(String.Format("TX: {0}", message.ToString()));

            string mes = String.Format("{0};{1};{2};{3};{4};{5}\n",
                message.nodeId,
                message.sensorId,
                (int)message.messageType,
                (message.ack) ? "1" : "0",
                message.subType,
                message.payload);

            SendMessage(mes);

            if (storeMessagesToLog)
                messagesLog.AddNewMessage(message);
        }
예제 #10
0
 public void AddMessage(Message message)
 {
     db.Messages.Add(message);
     db.SaveChanges();
 }
예제 #11
0
        private void StoreNewMessages()
        {
            //to prevent changing of collection while writing to db is not yet finished
            Message[] messages = new Message[newMessages.Count];
            newMessages.CopyTo(messages);
            newMessages.Clear();

            db.Messages.AddRange(messages);
            db.SaveChanges();
        }
예제 #12
0
        private void OnNewMessage(Message message)
        {
            if (!storeTxRxMessages)return;

            if (storeTimeInterval == 0)
                AddMessage(message);
            else
                newMessages.Add(message);
        }
        private void OnMessageSendEvent(Message message)
        {
            if (!IsConnected()) return;

            hubProxy.Invoke("OnMessageSendEvent", message);
        }