Пример #1
0
        public virtual bool MessageRequestHandler(byte[] receivedMessage)
        {
//Console.WriteLine("\n   _z_ [" + this.NodeId + "]  " + (this.DeviceHandler != null ? this.DeviceHandler.ToString() : "!" + this.GenericClass.ToString()));
//Console.WriteLine("   >>> " + zp.ByteArrayToString(receivedMessage) + "\n");
            // Do nothing
            //if (this.NodeId != 1 && (this.TypeId == null || this.DeviceHandler == null))
            //{
            //    TimeSpan ts = new TimeSpan(DateTime.Now.Ticks - _lastmanufacturerget.Ticks);
            //    if (ts.TotalSeconds > 10)
            //    {
            //        _lastmanufacturerget = DateTime.Now;
            //        ManufacturerSpecific_Get();
            //    }
            //}
            //
            bool handled        = false;
            int  message_length = receivedMessage.Length;

            //
            if (this.DeviceHandler != null && this.DeviceHandler.HandleRawMessageRequest(receivedMessage))
            {
                handled = true;
            }
            //
            if (!handled && message_length > 8)
            {
                byte command_length = receivedMessage[6];
                byte command_class  = receivedMessage[7];
                byte command_type   = receivedMessage[8]; // is this the Payload length in bytes? or is it the command type?
                //
                switch (command_class)
                {
////////////////////////////////////////////////////////////////////////////////////////////////////////////
                case (byte)CommandClass.COMMAND_CLASS_BASIC:
                case (byte)CommandClass.COMMAND_CLASS_ALARM:
                case (byte)CommandClass.COMMAND_CLASS_SENSOR_BINARY:
                case (byte)CommandClass.COMMAND_CLASS_SENSOR_ALARM:
                case (byte)CommandClass.COMMAND_CLASS_SENSOR_MULTILEVEL:
                case (byte)CommandClass.COMMAND_CLASS_SWITCH_BINARY:
                case (byte)CommandClass.COMMAND_CLASS_SWITCH_MULTILEVEL:
                case (byte)CommandClass.COMMAND_CLASS_SCENE_ACTIVATION:
////////////////////////////////////////////////////////////////////////////////////////////////////////////

                    //if (command_type == (byte)Command.COMMAND_BASIC_REPORT || command_type == (byte)Command.COMMAND_BASIC_GET) // command_type should be PayLoad length !?!?
                    //{
                    if (this.DeviceHandler != null)
                    {
                        handled = this.DeviceHandler.HandleBasicReport(receivedMessage);
                    }
                    //}
                    break;

////////////////////////////////////////////////////////////////////////////////////////////////////////////
                case (byte)CommandClass.COMMAND_CLASS_COONFIGURATION:
////////////////////////////////////////////////////////////////////////////////////////////////////////////
//                        01 0B 00 04 00 2B 05 70 06 02 01 01 AA

                    if (message_length > 11 && command_type == (byte)Command.COMMAND_CONFIG_REPORT)     // CONFIGURATION PARAMETER REPORT  0x06
                    {
                        byte paramid  = receivedMessage[9];
                        byte paramlen = receivedMessage[10];
                        //
                        if (!_nodeconfigparamslen.ContainsKey(paramid))
                        {
                            _nodeconfigparamslen.Add(paramid, paramlen);
                        }
                        else
                        {
                            // this shouldn't change on read... but you never know! =)
                            _nodeconfigparamslen[paramid] = paramlen;
                        }
                        //
                        byte[] bval = new byte[4];
                        // extract bytes value
                        Array.Copy(receivedMessage, 11, bval, 4 - (int)paramlen, (int)paramlen);
                        uint paramval = bval[0];
                        Array.Reverse(bval);
                        paramval = BitConverter.ToUInt32(bval, 0);
                        // convert it to uint
                        //
                        _raiseUpdateParameterEvent(this, paramid, ParameterType.PARAMETER_CONFIG, paramval);
                        //
                        handled = true;
                    }

                    break;

////////////////////////////////////////////////////////////////////////////////////////////////////////////
                case (byte)CommandClass.COMMAND_CLASS_ASSOCIATION:
////////////////////////////////////////////////////////////////////////////////////////////////////////////
                    //01 0C 00 04 00 2B 06 85 03 01 04 00 01 58

                    if (message_length > 12 && command_type == (byte)Command.COMMAND_ASSOCIATION_REPORT)     // ASSOCIATION REPORT 0x03
                    {
                        byte   groupid    = receivedMessage[9];
                        byte   maxassoc   = receivedMessage[10];
                        byte   numassoc   = receivedMessage[11]; // it is always zero ?!?
                        byte   nodeassoc  = 0;
                        string assocnodes = "";
                        if (receivedMessage.Length > 13)
                        {
                            for (int a = 12; a < receivedMessage.Length - 1; a++)
                            {
                                assocnodes += receivedMessage[a] + ",";
                            }
                        }
                        assocnodes = assocnodes.TrimEnd(',');
                        //
                        //_raiseUpdateParameterEvent(this, 0, ParameterType.PARAMETER_ASSOC, groupid);
                        _raiseUpdateParameterEvent(this, 1, ParameterType.PARAMETER_ASSOC, maxassoc);
                        _raiseUpdateParameterEvent(this, 2, ParameterType.PARAMETER_ASSOC, numassoc);
                        _raiseUpdateParameterEvent(this, 3, ParameterType.PARAMETER_ASSOC, groupid + ":" + assocnodes);
                        //
                        handled = true;
                    }

                    break;

////////////////////////////////////////////////////////////////////////////////////////////////////////////
                case (byte)CommandClass.COMMAND_CLASS_WAKE_UP:
////////////////////////////////////////////////////////////////////////////////////////////////////////////
                    //01 0C 00 04 00 2B 06 84 06 00 01 68 65 54

                    if (message_length > 11 && command_type == (byte)Command.COMMAND_WAKEUP_REPORT)     // WAKE UP REPORT 0x06
                    {
                        uint interval = ((uint)receivedMessage[9]) << 16;
                        interval |= (((uint)receivedMessage[10]) << 8);
                        interval |= (uint)receivedMessage[11];
                        //
                        _raiseUpdateParameterEvent(this, 0, ParameterType.PARAMETER_WAKEUP_INTERVAL, interval);
                        //
                        handled = true;
                    }
                    // 0x01, 0x08, 0x00, 0x04, 0x00, 0x06, 0x02, 0x84, 0x07, 0x74
                    else if (message_length > 7 && command_type == (byte)Command.COMMAND_WAKEUP_NOTIFICATION)     // AWAKE NOTIFICATION 0x07
                    {
                        _raiseUpdateParameterEvent(this, 0, ParameterType.PARAMETER_WAKEUP_NOTIFY, 1);
                        //
                        handled = true;
                    }

                    break;

////////////////////////////////////////////////////////////////////////////////////////////////////////////
                case (byte)CommandClass.COMMAND_CLASS_BATTERY:
////////////////////////////////////////////////////////////////////////////////////////////////////////////
                    // 01 0F 00 04 00 2B 09 80 03 64 31 05 01 2A 03 0B 26
                    if (message_length > 7 && /*command_length == (byte)Command.COMMAND_BASIC_REPORT && */ command_type == 0x03)     // Battery Report
                    {
                        _raiseUpdateParameterEvent(this, 0, ParameterType.PARAMETER_BATTERY, receivedMessage[9]);
                        //
                        handled = true;
                    }

                    break;

////////////////////////////////////////////////////////////////////////////////////////////////////////////
                case (byte)CommandClass.COMMAND_CLASS_MULTIINSTANCE:
                case (byte)CommandClass.COMMAND_CLASS_METER:
////////////////////////////////////////////////////////////////////////////////////////////////////////////

                    if (message_length > 10)
                    {
                        if (this.DeviceHandler != null)
                        {
                            handled = this.DeviceHandler.HandleMultiInstanceReport(receivedMessage);
                        }
                    }

                    break;

////////////////////////////////////////////////////////////////////////////////////////////////////////////
                case (byte)CommandClass.COMMAND_CLASS_MANUFACTURER_SPECIFIC:
////////////////////////////////////////////////////////////////////////////////////////////////////////////
                    if (message_length > 14)
                    {
                        byte[] manufacturerid = new byte[2] {
                            receivedMessage[9], receivedMessage[10]
                        };
                        byte[] typeid = new byte[2] {
                            receivedMessage[11], receivedMessage[12]
                        };
                        byte[] productid = new byte[2] {
                            receivedMessage[13], receivedMessage[14]
                        };

                        this.ManufacturerId = zp.ByteArrayToString(manufacturerid).Replace(" ", "");
                        this.TypeId         = zp.ByteArrayToString(typeid).Replace(" ", "");
                        this.ProductId      = zp.ByteArrayToString(productid).Replace(" ", "");

                        ManufacturerSpecific manufacturerspecs = new ManufacturerSpecific()
                        {
                            TypeId         = zp.ByteArrayToString(typeid).Replace(" ", ""),
                            ProductId      = zp.ByteArrayToString(productid).Replace(" ", ""),
                            ManufacturerId = zp.ByteArrayToString(manufacturerid).Replace(" ", "")
                        };
                        //
                        _deviceHandlerCheck(manufacturerspecs);
                        //
                        if (ManufacturerSpecificResponse != null)
                        {
                            try
                            {
                                ManufacturerSpecificResponse(this, new ManufacturerSpecificResponseEventArg()
                                {
                                    NodeId = this.NodeId,
                                    ManufacturerSpecific = manufacturerspecs
                                });
                            }
                            catch (Exception ex) {
                                Console.WriteLine("ZWaveLib: Error during ManufacturerSpecificResponse callback, " + ex.Message + "\n" + ex.StackTrace);
                            }
                        }
                        //
                        handled = true;
                        //
//Console.WriteLine (" ########################################################################################################### ");
//this.SendMessage (new byte[] { 0x01, 0x09, 0x00, 0x13, 0x13, this.NodeId, 0x31, 0x01, 0x25, 0x40, 0xa1 });
                    }

                    break;
                }
            }
            //
            if (!handled && message_length > 3)
            {
                //if (receivedMessage[3] == 0x13)
                //{
                //    // Check the callback id
                //    if (callbackIds.ContainsKey(receivedMessage[4]))
                //    {
                //        MessagingCompleted(receivedMessage[4]);
                //    }
                //    return true;
                //}
                //else
                if (receivedMessage[3] != 0x13)
                {
                    bool log = true;
                    if (message_length > 7 && /* cmd_class */ receivedMessage[7] == (byte)CommandClass.COMMAND_CLASS_MANUFACTURER_SPECIFIC)
                    {
                        log = false;
                    }
                    if (log)
                    {
                        Console.WriteLine("ZWaveLib UNHANDLED message: " + zp.ByteArrayToString(receivedMessage));
                    }
                }
            }

            return(false);
        }
Пример #2
0
        public virtual bool MessageRequestHandler(byte[] receivedMessage)
        {
            //Console.WriteLine("\n   _z_ [" + this.NodeId + "]  " + (this.DeviceHandler != null ? this.DeviceHandler.ToString() : "!" + this.GenericClass.ToString()));
            //Console.WriteLine("   >>> " + zp.ByteArrayToString(receivedMessage) + "\n");
            //
            bool handled       = false;
            int  messageLength = receivedMessage.Length;

            //
            if (this.DeviceHandler != null && this.DeviceHandler.HandleRawMessageRequest(receivedMessage))
            {
                handled = true;
            }
            //
            // Only generic command classes are handled directly in this function
            // other device specific commands are handled by corresponding DeviceHandler
            //
            if (!handled && messageLength > 8)
            {
                //byte commandLength = receivedMessage[6];
                byte commandClass = receivedMessage[7];
                byte commandType  = receivedMessage[8]; // is this the Payload length in bytes? or is it the command type?
                //
                switch (commandClass)
                {
                case (byte)CommandClass.COMMAND_CLASS_BASIC:
                case (byte)CommandClass.COMMAND_CLASS_ALARM:
                case (byte)CommandClass.COMMAND_CLASS_SENSOR_BINARY:
                case (byte)CommandClass.COMMAND_CLASS_SENSOR_ALARM:
                case (byte)CommandClass.COMMAND_CLASS_SENSOR_MULTILEVEL:
                case (byte)CommandClass.COMMAND_CLASS_SWITCH_BINARY:
                case (byte)CommandClass.COMMAND_CLASS_SWITCH_MULTILEVEL:
                case (byte)CommandClass.COMMAND_CLASS_SCENE_ACTIVATION:
                case (byte)CommandClass.COMMAND_CLASS_THERMOSTAT_MODE:
                case (byte)CommandClass.COMMAND_CLASS_THERMOSTAT_FAN_MODE:
                case (byte)CommandClass.COMMAND_CLASS_THERMOSTAT_FAN_STATE:
                case (byte)CommandClass.COMMAND_CLASS_THERMOSTAT_HEATING:
                case (byte)CommandClass.COMMAND_CLASS_THERMOSTAT_OPERATING_STATE:
                case (byte)CommandClass.COMMAND_CLASS_THERMOSTAT_SETBACK:
                case (byte)CommandClass.COMMAND_CLASS_THERMOSTAT_SETPOINT:

                    if (this.DeviceHandler != null)
                    {
                        handled = this.DeviceHandler.HandleBasicReport(receivedMessage);
                    }

                    break;

                case (byte)CommandClass.COMMAND_CLASS_MULTIINSTANCE:
                case (byte)CommandClass.COMMAND_CLASS_METER:

                    if (messageLength > 10)
                    {
                        if (this.DeviceHandler != null)
                        {
                            handled = this.DeviceHandler.HandleMultiInstanceReport(receivedMessage);
                        }
                    }

                    break;

                case (byte)CommandClass.COMMAND_CLASS_CONFIGURATION:

                    if (messageLength > 11 && commandType == (byte)Command.COMMAND_CONFIG_REPORT) // CONFIGURATION PARAMETER REPORT  0x06
                    {
                        byte paramId     = receivedMessage[9];
                        byte paramLength = receivedMessage[10];
                        //
                        if (!nodeConfigParamsLength.ContainsKey(paramId))
                        {
                            nodeConfigParamsLength.Add(paramId, paramLength);
                        }
                        else
                        {
                            // this shouldn't change on read... but you never know! =)
                            nodeConfigParamsLength[paramId] = paramLength;
                        }
                        //
                        byte[] bval = new byte[4];
                        // extract bytes value
                        Array.Copy(receivedMessage, 11, bval, 4 - (int)paramLength, (int)paramLength);
                        uint paramval = bval[0];
                        Array.Reverse(bval);
                        paramval = BitConverter.ToUInt32(bval, 0);
                        // convert it to uint
                        //
                        RaiseUpdateParameterEvent(this, paramId, ParameterType.PARAMETER_CONFIG, paramval);
                        //
                        handled = true;
                    }

                    break;

                case (byte)CommandClass.COMMAND_CLASS_ASSOCIATION:

                    if (messageLength > 12 && commandType == (byte)Command.COMMAND_ASSOCIATION_REPORT) // ASSOCIATION REPORT 0x03
                    {
                        byte   groupId         = receivedMessage[9];
                        byte   maxAssociations = receivedMessage[10];
                        byte   numAssociations = receivedMessage[11]; // it is always zero ?!?
                        string assocNodes      = "";
                        if (receivedMessage.Length > 13)
                        {
                            for (int a = 12; a < receivedMessage.Length - 1; a++)
                            {
                                assocNodes += receivedMessage[a] + ",";
                            }
                        }
                        assocNodes = assocNodes.TrimEnd(',');
                        //
                        //_raiseUpdateParameterEvent(this, 0, ParameterType.PARAMETER_ASSOC, groupid);
                        RaiseUpdateParameterEvent(this, 1, ParameterType.PARAMETER_ASSOC, maxAssociations);
                        RaiseUpdateParameterEvent(this, 2, ParameterType.PARAMETER_ASSOC, numAssociations);
                        RaiseUpdateParameterEvent(
                            this,
                            3,
                            ParameterType.PARAMETER_ASSOC,
                            groupId + ":" + assocNodes
                            );
                        //
                        handled = true;
                    }

                    break;

                case (byte)CommandClass.COMMAND_CLASS_WAKE_UP:

                    if (messageLength > 11 && commandType == (byte)Command.COMMAND_WAKEUP_REPORT) // WAKE UP REPORT 0x06
                    {
                        uint interval = ((uint)receivedMessage[9]) << 16;
                        interval |= (((uint)receivedMessage[10]) << 8);
                        interval |= (uint)receivedMessage[11];
                        //
                        RaiseUpdateParameterEvent(this, 0, ParameterType.PARAMETER_WAKEUP_INTERVAL, interval);
                        //
                        handled = true;
                    }
                    // 0x01, 0x08, 0x00, 0x04, 0x00, 0x06, 0x02, 0x84, 0x07, 0x74
                    else if (messageLength > 7 && commandType == (byte)Command.COMMAND_WAKEUP_NOTIFICATION)     // AWAKE NOTIFICATION 0x07
                    {
                        RaiseUpdateParameterEvent(this, 0, ParameterType.PARAMETER_WAKEUP_NOTIFY, 1);
                        //
                        handled = true;
                    }

                    break;

                case (byte)CommandClass.COMMAND_CLASS_BATTERY:

                    if (messageLength > 7 && /*command_length == (byte)Command.COMMAND_BASIC_REPORT && */ commandType == 0x03) // Battery Report
                    {
                        RaiseUpdateParameterEvent(this, 0, ParameterType.PARAMETER_BATTERY, receivedMessage[9]);
                        //
                        handled = true;
                    }

                    break;

                case (byte)CommandClass.COMMAND_CLASS_HAIL:

                    this.Basic_Get();
                    handled = true;

                    break;

                case (byte)CommandClass.COMMAND_CLASS_MANUFACTURER_SPECIFIC:

                    if (messageLength > 14)
                    {
                        byte[] manufacturerId = new byte[2] {
                            receivedMessage[9], receivedMessage[10]
                        };
                        byte[] typeId = new byte[2] {
                            receivedMessage[11], receivedMessage[12]
                        };
                        byte[] productId = new byte[2] {
                            receivedMessage[13], receivedMessage[14]
                        };

                        this.ManufacturerId = zwavePort.ByteArrayToString(manufacturerId).Replace(" ", "");
                        this.TypeId         = zwavePort.ByteArrayToString(typeId).Replace(" ", "");
                        this.ProductId      = zwavePort.ByteArrayToString(productId).Replace(" ", "");

                        var manufacturerSpecs = new ManufacturerSpecific()
                        {
                            TypeId         = zwavePort.ByteArrayToString(typeId).Replace(" ", ""),
                            ProductId      = zwavePort.ByteArrayToString(productId).Replace(" ", ""),
                            ManufacturerId = zwavePort.ByteArrayToString(manufacturerId).Replace(" ", "")
                        };
                        //
                        CheckDeviceHandler(manufacturerSpecs);
                        //
                        if (ManufacturerSpecificResponse != null)
                        {
                            try
                            {
                                ManufacturerSpecificResponse(this, new ManufacturerSpecificResponseEventArg()
                                {
                                    NodeId = this.NodeId,
                                    ManufacturerSpecific = manufacturerSpecs
                                });
                            }
                            catch (Exception ex)
                            {
                                Console.WriteLine("ZWaveLib: Error during ManufacturerSpecificResponse callback, " + ex.Message + "\n" + ex.StackTrace);
                            }
                        }
                        //
                        handled = true;
                        //
                        //Console.WriteLine (" ########################################################################################################### ");
                        //this.SendMessage (new byte[] { 0x01, 0x09, 0x00, 0x13, 0x13, this.NodeId, 0x31, 0x01, 0x25, 0x40, 0xa1 });
                    }

                    break;
                }
            }
            //
            if (!handled && messageLength > 3)
            {
                if (receivedMessage[3] != 0x13)
                {
                    bool log = true;
                    if (messageLength > 7 && /* cmd_class */ receivedMessage[7] == (byte)CommandClass.COMMAND_CLASS_MANUFACTURER_SPECIFIC)
                    {
                        log = false;
                    }
                    if (log)
                    {
                        Console.WriteLine("ZWaveLib UNHANDLED message: " + zwavePort.ByteArrayToString(receivedMessage));
                    }
                }
            }

            return(false);
        }