示例#1
0
        //[OneToMany(CascadeOperations = CascadeOperation.All)]      // One to many relationship with Valuation
        //public List<SensorDto> Sensors { get; set; }


        public static NodeDto FromModel(Node item)
        {
            if (item == null)
                return null;

            return new NodeDto()
            {
                ID = item.ID,
                Type = (byte)item.Type,
                ProtocolVersion = item.ProtocolVersion,
                SketchName = item.SketchName,
                SketchVersion = item.SketchVersion
            };
        }
示例#2
0
        private void GetNextAvailableNodeID()
        {
            var nds = nodes.OrderBy(node => node.ID).ToList();

            byte id = 1;
            for (byte i = 0; i < nds.Count; i++)
                if (nds[i].ID > i + 1)
                {
                    id = (byte)(i + 1);
                    break;
                }

            if (id < 255)
            {
                Node result = new Node(id);
                dbService.Insert(result);
                nodes.Add(result);

                gatewayProxy.Send(new SensorMessage(255, 255, SensorMessageType.Internal, false, (byte)InternalValueType.IDResponse, id.ToString()));
            }
        }
示例#3
0
        private void gatewayProxy_MessageReceived(IGatewayProxy sender, SensorMessageEventArgs args)
        {
            if (!IsStarted)
                return;

            SensorMessage message = args.Message;

            // for debug!!!
            if (Log != null)
                Log(this, message.ToString(), true, LogLevel.Normal);

            bool isNodeMessage = message.NodeID == 0 || message.SensorID == 255;
            Node node = GetNode(message.NodeID);
            Sensor sensor = GetSensor(message.NodeID, message.SensorID); // if message.SensorID == 255 it returns null

            switch (message.Type)
            {
                #region Presentation
                case SensorMessageType.Presentation: // sent by a nodes when they present attached sensors.
                    if (isNodeMessage)
                    {
                        if (node == null)
                        {
                            node = new Node(message.NodeID, (SensorType)message.SubType, message.Payload);
                            dbService.Insert(node);
                            nodes.Add(node);
                        }
                        else
                        {
                            node.Type = (SensorType)message.SubType;
                            node.ProtocolVersion = message.Payload;
                            dbService.Update(node);
                        }
                        communicator.Broadcast(new NetworkMessage(NetworkMessageID.NodePresentation, JsonConvert.SerializeObject(node)));
                    }
                    else
                    {
                        if (node != null)
                        {
                            if (sensor == null)
                            {
                                sensor = new Sensor(message.NodeID, message.SensorID, (SensorType)message.SubType, message.Payload);
                                dbService.Insert(sensor);
                                node.Sensors.Add(sensor);
                            }
                            else
                            {
                                sensor.Type = (SensorType)message.SubType;
                                sensor.ProtocolVersion = message.Payload;
                                dbService.Update(sensor);
                            }
                            communicator.Broadcast(new NetworkMessage(NetworkMessageID.SensorPresentation, JsonConvert.SerializeObject(sensor)));
                        }
                    }
                    break;
                #endregion

                #region Set
                case SensorMessageType.Set: // sent from or to a sensor when a sensor value should be updated
                    if (sensor != null)
                    {
                        SensorValue sv = new SensorValue(message.NodeID, message.SensorID, DateTime.Now, (SensorValueType)message.SubType, float.Parse(message.Payload.Replace('.', ',')));
                        dbService.Insert(sv);
                        sensor.Values.Add(sv);
                        communicator.Broadcast(new NetworkMessage(NetworkMessageID.SensorValue, JsonConvert.SerializeObject(sv)));
                    }
                    break;
                #endregion

                #region Request
                case SensorMessageType.Request: // requests a variable value (usually from an actuator destined for controller)
                    break;
                #endregion

                #region Internal
                case SensorMessageType.Internal: // special internal message
                    InternalValueType ivt = (InternalValueType)message.SubType;

                    switch (ivt)
                    {
                        case InternalValueType.BatteryLevel: // int, in %
                            if (node != null)
                            {
                                BatteryLevel bl = new BatteryLevel(node.ID, DateTime.Now, byte.Parse(message.Payload));
                                dbService.Insert(bl);
                                node.BatteryLevels.Add(bl);
                                communicator.Broadcast(new NetworkMessage(NetworkMessageID.BatteryLevel, JsonConvert.SerializeObject(bl)));
                            }
                            break;
                        case InternalValueType.Time:
                            gatewayProxy.Send(new SensorMessage(message.NodeID, message.SensorID, SensorMessageType.Internal, false, (byte)InternalValueType.Time, GetTimeForSensors().ToString()));
                            break;
                        case InternalValueType.Version:
                            break;
                        case InternalValueType.IDRequest:
                            GetNextAvailableNodeID();
                            break;
                        case InternalValueType.IDResponse:
                            break;
                        case InternalValueType.InclusionMode:
                            break;
                        case InternalValueType.Config:
                            gatewayProxy.Send(new SensorMessage(message.NodeID, 255, SensorMessageType.Internal, false, (byte)InternalValueType.Config, UnitSystem));
                            break;
                        case InternalValueType.FindParent:
                            break;
                        case InternalValueType.FindParentResponse:
                            break;
                        case InternalValueType.LogMessage:
                            break;
                        case InternalValueType.Children:
                            break;
                        case InternalValueType.SketchName:
                        case InternalValueType.SketchVersion:
                            if (node != null)
                            {
                                if (ivt == InternalValueType.SketchName)
                                    node.SketchName = message.Payload;
                                else
                                    node.SketchVersion = message.Payload;

                                dbService.Update(node);
                                communicator.Broadcast(new NetworkMessage(NetworkMessageID.NodePresentation, JsonConvert.SerializeObject(node)));
                            }
                            break;
                        case InternalValueType.Reboot:
                            break;
                        case InternalValueType.GatewayReady:
                            break;
                    }
                    break;
                #endregion

                #region Stream
                case SensorMessageType.Stream: //used for OTA firmware updates
                    switch ((StreamValueType)message.SubType)
                    {
                        case StreamValueType.FirmwareConfigRequest:
                            //var fwtype = pullWord(payload, 0);
                            //var fwversion = pullWord(payload, 2);
                            //sendFirmwareConfigResponse(sender, fwtype, fwversion, db, gw);
                            break;
                        case StreamValueType.FirmwareConfigResponse:
                            break;
                        case StreamValueType.FirmwareRequest:
                            break;
                        case StreamValueType.FirmwareResponse:
                            //var fwtype = pullWord(payload, 0);
                            //var fwversion = pullWord(payload, 2);
                            //var fwblock = pullWord(payload, 4);
                            //sendFirmwareResponse(sender, fwtype, fwversion, fwblock, db, gw);
                            break;
                        case StreamValueType.Sound:
                            break;
                        case StreamValueType.Image:
                            break;
                    }
                    break;
                #endregion
            }

            //if (node != null && node.Reboot)
            //    gatewayProxy.Send(new Message(node.NodeID, 255, MessageType.Internal, false, (byte)InternalValueType.Reboot, ""));
        }
        public int Delete(Node item)
        {
            foreach (BatteryLevel bl in item.BatteryLevels)
                Delete(bl);

            foreach (Sensor s in item.Sensors)
                Delete(s);

            return con.Delete(NodeDto.FromModel(item));
        }
 public int Update(Node item)
 {
     return con.Update(NodeDto.FromModel(item));
 }
 public int Insert(Node item)
 {
     return con.Insert(NodeDto.FromModel(item), "OR REPLACE");
 }