private void LogReceivePacket(ZigBeeCoordinatorContext ctx, ZigBeeDevice device, ZigBeeCommunicationsController.ZigBeeReceivePacket packet)
        {
            FiveVoltPacket fiveVoltPacket = FiveVoltPacket.Decode(packet.ReceiveData);

            if (fiveVoltPacket is InvalidFileVoltPacket)
            {
                InvalidFileVoltPacket invalidPacket = (InvalidFileVoltPacket)fiveVoltPacket;
                _log.Error("Invalid five volt packet. Reason \"" + invalidPacket.Reason + "\"");
            }
            else if (fiveVoltPacket is CapabilityFiveVoltPacket)
            {
                CapabilityFiveVoltPacket capabilityFiveVoltPacket = (CapabilityFiveVoltPacket)fiveVoltPacket;
                LogCapabilityFiveVoltPacket(ctx, device, capabilityFiveVoltPacket);
            }
            else if (fiveVoltPacket is CapabilityDataFiveVoltPacket)
            {
                CapabilityDataFiveVoltPacket capabilityDataFiveVoltPacket = (CapabilityDataFiveVoltPacket)fiveVoltPacket;
                LogCapabilityDataFiveVoltPacket(ctx, device, capabilityDataFiveVoltPacket);
            }
            else if (fiveVoltPacket is MessageFiveVoltPacket)
            {
                MessageFiveVoltPacket messageFiveVoltPacket = (MessageFiveVoltPacket)fiveVoltPacket;
                LogMessageFiveVoltPacket(ctx, device, messageFiveVoltPacket);
            }
            else
            {
                _log.Error("Unhandled five volt packet type [" + fiveVoltPacket.GetType() + "]");
            }
        }
        public override void ProcessData(ZigBeeCoordinatorContext ctx, ZigBeeDevice device, byte[] data)
        {
            int? userId = data[0];
            float weight = BitConverter.ToInt16(data, 1) / 10.0f;

            // user not specified
            if (userId == 0)
            {
                userId = FindUserBasedOnHistory(ctx, weight);
            }

            if (userId == null)
            {
                _log.Error("Could not find user based on weight '" + weight + "'");
                return;
            }

            _log.Info("Weight received for user [" + userId + "]: " + weight);

            WirelessScaleData wirelessScaleData = new WirelessScaleData
            {
                UserId = userId.Value,
                Value = weight,
                ZigBeeDevice = device,
                ReceivedDateTime = DateTime.Now
            };
            ctx.DbCtx.WirelessScaleDatas.AddObject(wirelessScaleData);
            ctx.DbCtx.SaveChanges();
        }
Example #3
0
        private void LogCapabilityDataFiveVoltPacket(ZigBeeCoordinatorContext ctx, ZigBeeDevice device, CapabilityDataFiveVoltPacket packet)
        {
            Capability capability = Capability.Finder.FindByIdOrDefault(ctx, packet.Capability);
            if (capability == null)
            {
                _log.Error("Could not find capability with id [" + packet.Capability + "]");
                return;
            }

            capability.ProcessData(ctx, device, packet.Data);
        }
Example #4
0
        public ZigBeeDevice FindByAddressOrDefault(ZigBeeCoordinatorContext ctx, byte[] address)
        {
            List <ZigBeeDevice> devices      = ctx.DbCtx.ZigBeeDevices.ToList();
            ZigBeeDevice        zigBeeDevice = devices.FirstOrDefault(d => address.EqualsAllItems(d.DeviceAddress));

            if (zigBeeDevice != null)
            {
                return(zigBeeDevice);
            }
            return(null);
        }
        private void LogCapabilityDataFiveVoltPacket(ZigBeeCoordinatorContext ctx, ZigBeeDevice device, CapabilityDataFiveVoltPacket packet)
        {
            Capability capability = Capability.Finder.FindByIdOrDefault(ctx, packet.Capability);

            if (capability == null)
            {
                _log.Error("Could not find capability with id [" + packet.Capability + "]");
                return;
            }

            capability.ProcessData(ctx, device, packet.Data);
        }
Example #6
0
        private void LogCapabilityFiveVoltPacket(ZigBeeCoordinatorContext ctx, ZigBeeDevice device, CapabilityFiveVoltPacket capabilityFiveVoltPacket)
        {
            Capability capability = Capability.Finder.FindByIdOrDefault(ctx, capabilityFiveVoltPacket.Capability);
            if (capability == null)
            {
                _log.Error("Could not find capability with id [" + capabilityFiveVoltPacket.Capability + "]");
                return;
            }

            _log.Info("New capability found [" + capability.Name + "] at address [" + device.DeviceAddressString + "]");
            device.Capabilities.Add(capability);
        }
        private void LogCapabilityFiveVoltPacket(ZigBeeCoordinatorContext ctx, ZigBeeDevice device, CapabilityFiveVoltPacket capabilityFiveVoltPacket)
        {
            Capability capability = Capability.Finder.FindByIdOrDefault(ctx, capabilityFiveVoltPacket.Capability);

            if (capability == null)
            {
                _log.Error("Could not find capability with id [" + capabilityFiveVoltPacket.Capability + "]");
                return;
            }

            _log.Info("New capability found [" + capability.Name + "] at address [" + device.DeviceAddressString + "]");
            device.Capabilities.Add(capability);
        }
Example #8
0
        public void ProcessData(ZigBeeCoordinatorContext ctx, ZigBeeDevice device, byte[] data)
        {
            _log.Debug("Processing data for capability [" + CapabilityId + ": " + Name + "], from [" + device.DeviceAddressString + "], data [" + data.ToHexString() + "]");

            if (CapabilityHandler == null)
            {
                _log.Error("Unknown capability id [" + CapabilityId + "]");
            }
            else
            {
                CapabilityHandler.ProcessData(ctx, device, data);
            }
        }
Example #9
0
        public void ProcessData(ZigBeeCoordinatorContext ctx, ZigBeeDevice device, byte[] data)
        {
            _log.Debug("Processing data for capability [" + CapabilityId + ": " + Name + "], from [" + device.DeviceAddressString + "], data [" + data.ToHexString() + "]");

            if (CapabilityHandler == null)
            {
                _log.Error("Unknown capability id [" + CapabilityId + "]");
            }
            else
            {
                CapabilityHandler.ProcessData(ctx, device, data);
            }
        }
Example #10
0
 private void LogMessageFiveVoltPacket(ZigBeeCoordinatorContext ctx, ZigBeeDevice device, MessageFiveVoltPacket packet)
 {
     string message = "Message from [" + device.NetworkAddress.ToHexString() + "]: " + packet.Message;
     switch (packet.Level)
     {
         case MessageLevel.Debug: _log.Debug(message); break;
         case MessageLevel.Info: _log.Info(message); break;
         case MessageLevel.Warning: _log.Warn(message); break;
         case MessageLevel.Error: _log.Error(message); break;
         default:
             _log.Error(message + " (Invalid message level: " + packet.Level + ")");
             break;
     }
 }
        private void FixupZigBeeDevice(ZigBeeDevice previousValue)
        {
            if (previousValue != null && previousValue.ZigBeeDeviceLogs.Contains(this))
            {
                previousValue.ZigBeeDeviceLogs.Remove(this);
            }

            if (ZigBeeDevice != null)
            {
                if (!ZigBeeDevice.ZigBeeDeviceLogs.Contains(this))
                {
                    ZigBeeDevice.ZigBeeDeviceLogs.Add(this);
                }
                if (ZigBeeDeviceId != ZigBeeDevice.ZigBeeDeviceId)
                {
                    ZigBeeDeviceId = ZigBeeDevice.ZigBeeDeviceId;
                }
            }
        }
Example #12
0
        private void LogMessageFiveVoltPacket(ZigBeeCoordinatorContext ctx, ZigBeeDevice device, MessageFiveVoltPacket packet)
        {
            string message = "Message from [" + device.NetworkAddress.ToHexString() + "]: " + packet.Message;

            switch (packet.Level)
            {
            case MessageLevel.Debug: _log.Debug(message); break;

            case MessageLevel.Info: _log.Info(message); break;

            case MessageLevel.Warning: _log.Warn(message); break;

            case MessageLevel.Error: _log.Error(message); break;

            default:
                _log.Error(message + " (Invalid message level: " + packet.Level + ")");
                break;
            }
        }
Example #13
0
        public void LogPacket(ZigBeeCoordinatorContext ctx, ZigBeeCommunicationsController.Packet packet)
        {
            ZigBeeDevice device = ZigBeeDevice.Finder.FindAddOrUpdate(ctx, packet.Address, packet.NetworkAddress);

            device.LastPacketDateTime = DateTime.Now;

            ZigBeeDeviceLog log = new ZigBeeDeviceLog
            {
                DateTime     = packet.ReceivedDataTime,
                PacketData   = packet.PacketData,
                ZigBeeDevice = device
            };

            ctx.DbCtx.ZigBeeDeviceLogs.AddObject(log);
            ctx.DbCtx.SaveChanges();

            if (packet is ZigBeeCommunicationsController.ZigBeeReceivePacket)
            {
                LogReceivePacket(ctx, device, (ZigBeeCommunicationsController.ZigBeeReceivePacket)packet);
            }
        }
Example #14
0
 public ZigBeeDevice FindAddOrUpdate(ZigBeeCoordinatorContext ctx, byte[] address, byte[] networkAddress)
 {
     ZigBeeDevice zigBeeDevice = FindByAddressOrDefault(ctx, address);
     if (zigBeeDevice != null)
     {
         if (!zigBeeDevice.NetworkAddress.EqualsAllItems(networkAddress))
         {
             zigBeeDevice.NetworkAddress = networkAddress;
         }
     }
     else
     {
         zigBeeDevice = new ZigBeeDevice
         {
             CreatedDateTime = DateTime.Now,
             DeviceAddress = address,
             NetworkAddress = networkAddress
         };
         ctx.DbCtx.ZigBeeDevices.AddObject(zigBeeDevice);
         ctx.DbCtx.SaveChanges();
     }
     return zigBeeDevice;
 }
        private void FixupZigBeeDevice(ZigBeeDevice previousValue)
        {
            if (previousValue != null && previousValue.WirelessScaleDatas.Contains(this))
            {
                previousValue.WirelessScaleDatas.Remove(this);
            }

            if (ZigBeeDevice != null)
            {
                if (!ZigBeeDevice.WirelessScaleDatas.Contains(this))
                {
                    ZigBeeDevice.WirelessScaleDatas.Add(this);
                }
                if (ZigBeeDeviceId != ZigBeeDevice.ZigBeeDeviceId)
                {
                    ZigBeeDeviceId = ZigBeeDevice.ZigBeeDeviceId;
                }
            }
            else if (!_settingFK)
            {
                ZigBeeDeviceId = null;
            }
        }
Example #16
0
        public ZigBeeDevice FindAddOrUpdate(ZigBeeCoordinatorContext ctx, byte[] address, byte[] networkAddress)
        {
            ZigBeeDevice zigBeeDevice = FindByAddressOrDefault(ctx, address);

            if (zigBeeDevice != null)
            {
                if (!zigBeeDevice.NetworkAddress.EqualsAllItems(networkAddress))
                {
                    zigBeeDevice.NetworkAddress = networkAddress;
                }
            }
            else
            {
                zigBeeDevice = new ZigBeeDevice
                {
                    CreatedDateTime = DateTime.Now,
                    DeviceAddress   = address,
                    NetworkAddress  = networkAddress
                };
                ctx.DbCtx.ZigBeeDevices.AddObject(zigBeeDevice);
                ctx.DbCtx.SaveChanges();
            }
            return(zigBeeDevice);
        }
        private void FixupZigBeeDevice(ZigBeeDevice previousValue)
        {
            if (previousValue != null && previousValue.WirelessScaleDatas.Contains(this))
            {
                previousValue.WirelessScaleDatas.Remove(this);
            }

            if (ZigBeeDevice != null)
            {
                if (!ZigBeeDevice.WirelessScaleDatas.Contains(this))
                {
                    ZigBeeDevice.WirelessScaleDatas.Add(this);
                }
                if (ZigBeeDeviceId != ZigBeeDevice.ZigBeeDeviceId)
                {
                    ZigBeeDeviceId = ZigBeeDevice.ZigBeeDeviceId;
                }
            }
            else if (!_settingFK)
            {
                ZigBeeDeviceId = null;
            }
        }
Example #18
0
 private void LogReceivePacket(ZigBeeCoordinatorContext ctx, ZigBeeDevice device, ZigBeeCommunicationsController.ZigBeeReceivePacket packet)
 {
     FiveVoltPacket fiveVoltPacket = FiveVoltPacket.Decode(packet.ReceiveData);
     if (fiveVoltPacket is InvalidFileVoltPacket)
     {
         InvalidFileVoltPacket invalidPacket = (InvalidFileVoltPacket)fiveVoltPacket;
         _log.Error("Invalid five volt packet. Reason \"" + invalidPacket.Reason + "\"");
     }
     else if (fiveVoltPacket is CapabilityFiveVoltPacket)
     {
         CapabilityFiveVoltPacket capabilityFiveVoltPacket = (CapabilityFiveVoltPacket)fiveVoltPacket;
         LogCapabilityFiveVoltPacket(ctx, device, capabilityFiveVoltPacket);
     }
     else if (fiveVoltPacket is CapabilityDataFiveVoltPacket)
     {
         CapabilityDataFiveVoltPacket capabilityDataFiveVoltPacket = (CapabilityDataFiveVoltPacket)fiveVoltPacket;
         LogCapabilityDataFiveVoltPacket(ctx, device, capabilityDataFiveVoltPacket);
     }
     else if (fiveVoltPacket is MessageFiveVoltPacket)
     {
         MessageFiveVoltPacket messageFiveVoltPacket = (MessageFiveVoltPacket)fiveVoltPacket;
         LogMessageFiveVoltPacket(ctx, device, messageFiveVoltPacket);
     }
     else
     {
         _log.Error("Unhandled five volt packet type [" + fiveVoltPacket.GetType() + "]");
     }
 }
        private void FixupZigBeeDevice(ZigBeeDevice previousValue)
        {
            if (previousValue != null && previousValue.ZigBeeDeviceLogs.Contains(this))
            {
                previousValue.ZigBeeDeviceLogs.Remove(this);
            }

            if (ZigBeeDevice != null)
            {
                if (!ZigBeeDevice.ZigBeeDeviceLogs.Contains(this))
                {
                    ZigBeeDevice.ZigBeeDeviceLogs.Add(this);
                }
                if (ZigBeeDeviceId != ZigBeeDevice.ZigBeeDeviceId)
                {
                    ZigBeeDeviceId = ZigBeeDevice.ZigBeeDeviceId;
                }
            }
        }
Example #20
0
 public abstract void ProcessData(ZigBeeCoordinatorContext ctx, ZigBeeDevice device, byte[] data);