public ZWaveMessage(
            ZWaveMessageType messageType,
            ZWaveFunction function,
            byte nodeId = 0x00,
            ZWaveCommandClass commandClass = 0x00,
            ZWaveCommand command = 0x00)
        {
            this.MessageType = messageType;
            this.Function = function;
            if (nodeId != 0x00)
            {
                this.NodeId = nodeId;
            }

            if (commandClass != 0x00)
            {
                this.CommandClass = commandClass;
            }

            if (command != 0x00)
            {
                this.Command = command;
            }

            Params = new List<byte>();
        }
示例#2
0
        private void _zwavemessagereceived(object sender, ZWaveMessageReceivedEventArgs args)
        {
            int length = args.Message.Length;

            //
            try
            {
                ZWaveMessageHeader zh = (ZWaveMessageHeader)args.Message[0];
                switch (zh)
                {
                case ZWaveMessageHeader.CAN:
                    zp.SendAck();
                    // RESEND
//Console.WriteLine("ZWaveLib: received CAN, resending last message");
//                        zp.ResendLastMessage();
                    break;

                case ZWaveMessageHeader.ACK:
                    zp.SendAck();
                    break;

                case ZWaveMessageHeader.SOF:     // start of zwave frame
                    //
                    // parse frame headers
                    //
                    int msglength              = (int)args.Message[1];
                    ZWaveMessageType  msgtype  = (ZWaveMessageType)args.Message[2];
                    ZWaveCommandClass cmdclass = (args.Message.Length > 3 ? (ZWaveCommandClass)args.Message[3] : 0);
                    //
                    byte sourcenode = 0;
                    //
                    switch (msgtype)
                    {
                    case ZWaveMessageType.REQUEST:
                        zp.SendAck();

                        if (_devices.Count == 0)
                        {
                            break;
                        }

                        byte callbackid = 0;

                        switch (cmdclass)
                        {
                        case ZWaveCommandClass.CMD_NONE:
                            break;

                        case ZWaveCommandClass.CMD_NODE_ADD:

                            callbackid = args.Message[4];
                            if (args.Message[5] == 0x03)             // ADD_NODE_STATUS_ADDING_SLAVE
                            {
                                //
//Console.WriteLine("\n\nADDING NODE SLAVE {0}\n     ->   ", zp.ByteArrayToString(args.Message));
                                //
                                // example response from HSM-100 3in1 sensor
                                // 01 15 00 4A 32 03 2E 0E 04 21 01 60 31 70 84 85 80 72 77 86 EF 20 79
                                //                         bt gt st c1 c2 c3 c4 c5 c6 c7 c8 c9 --------
                                // node supported classes (c1, c2.... cn)
                                //
                                _nodeoperationidcheck = args.Message[6];
                                CreateDevice(_nodeoperationidcheck, 0x00);
                            }
                            else if (args.Message[5] == 0x05)             // ADD_NODE_STATUS_PROTOCOL_DONE
                            {
                                if (_nodeoperationidcheck == args.Message[6])
                                {
//Console.WriteLine("\n\nADDING NODE PROTOCOL DONE {0} {1}\n\n", args.Message[6], callbackid);
                                    Thread.Sleep(500);
                                    GetNodeCapabilities(args.Message[6]);
                                    //Discovery();
                                }
                            }
                            else if (args.Message[5] == 0x07)             // ADD_NODE_STATUS_ADDING_FAIL
                            {
//Console.WriteLine("\n\nADDING NODE FAIL {0}\n\n", args.Message[6]);
                            }
                            break;

                        case ZWaveCommandClass.CMD_NODE_REMOVE:

                            callbackid = args.Message[4];
                            if (args.Message[5] == 0x03)             // REMOVE_NODE_STATUS_REMOVING_SLAVE
                            {
//Console.WriteLine("\n\nREMOVING NODE SLAVE {0}\n\n", args.Message[6]);
                                _nodeoperationidcheck = args.Message[6];
                            }
                            else if (args.Message[5] == 0x06)             // REMOVE_NODE_STATUS_REMOVING_DONE
                            {
                                if (_nodeoperationidcheck == args.Message[6])
                                {
//Console.WriteLine("\n\nREMOVING NODE DONE {0} {1}\n\n", args.Message[6], callbackid);
                                    OnDiscoveryEvent(new DiscoveryEventArgs(args.Message[6], DISCOVERY_STATUS.NODE_REMOVED));             // Send event
                                    RemoveDevice(args.Message[6]);
                                }
                            }
                            else if (args.Message[5] == 0x07)             // REMOVE_NODE_STATUS_REMOVING_FAIL
                            {
//Console.WriteLine("\n\nREMOVING NODE FAIL {0}\n\n", args.Message[6]);
                            }
                            break;

                        case ZWaveCommandClass.CMD_APPLICATION_COMMAND:

                            sourcenode = args.Message[5];
                            ZWaveNode node = _devices.Find(n => n.NodeId == sourcenode);
                            if (node == null)
                            {
                                CreateDevice(sourcenode, 0x00);
                                GetNodeCapabilities(sourcenode);
                            }
                            try
                            {
                                node.MessageRequestHandler(args.Message);
                            }
                            catch (Exception ex)
                            {
//Console.WriteLine("# " + ex.Message + "\n" + ex.StackTrace);
                            }
                            break;

                        case ZWaveCommandClass.CMD_SEND_DATA:

                            byte cid = args.Message[4];
                            if (cid == 0x01)             // SEND DATA OK
                            {
                                // TODO: ... what does that mean?
                            }
                            else if (args.Message[5] == 0x00)
                            {
                                // Messaging complete, remove callbackid
                                zp.PendingMessages.RemoveAll(zm => zm.CallbackId == cid);
                            }
                            else if (args.Message[5] == 0x01)
                            {
                                // TODO: callback error???
                                zp.ResendLastMessage(cid);
                            }
                            break;

                        case ZWaveCommandClass.CMD_NODE_UPDATE_INFO:

                            sourcenode = args.Message[5];
                            int       niflen = (int)args.Message[6];
                            ZWaveNode znode  = _devices.Find(n => n.NodeId == sourcenode);
                            if (znode != null)
                            {
                                byte[] nodeinfo = new byte[niflen - 2];
//Console.WriteLine(ByteArrayToString(args.Message));
                                Array.Copy(args.Message, 7, nodeinfo, 0, niflen - 2);
                                //
                                _raiseUpdateParameterEvent(znode, 0, ParameterType.PARAMETER_NODE_INFO, zp.ByteArrayToString(nodeinfo));
                                _raiseUpdateParameterEvent(znode, 0, ParameterType.PARAMETER_WAKEUP_NOTIFY, "1");
                            }
                            break;

                        default:
                            Console.WriteLine("\nUNHANDLED Z-Wave REQUEST\n     " + zp.ByteArrayToString(args.Message) + "\n");
                            break;
                        }


                        break;

                    case ZWaveMessageType.RESPONSE:


                        switch (cmdclass)
                        {
                        case ZWaveCommandClass.CMD_DISCOVERY_NODES:
                            MessageResponseNodeBitMaskHandler(args.Message);
                            //zp.SendAck();
                            break;

                        case ZWaveCommandClass.CMD_GET_NODE_PROTOCOL_INFO:
                            MessageResponseNodeCapabilityHandler(args.Message);
                            //zp.SendAck();
                            break;

                        case ZWaveCommandClass.CMD_REQUEST_NODE_INFO:
//                                        Console.WriteLine("\nNODE INFO RESPONSE: " + zp.ByteArrayToString(args.Message) + "\n");
                            break;

                        case ZWaveCommandClass.CMD_SEND_DATA:
                            this.ReadyToSend = true;
                            break;

                        default:
                            //if (args.Message.Length > 2 && args.Message[3] != 0x13)
                            Console.WriteLine("\nUNHANDLED Z-Wave RESPONSE\n     " + zp.ByteArrayToString(args.Message) + "\n");
                            break;
                        }


                        break;

                    default:
                        Console.WriteLine("\nUNHANDLED Z-Wave message TYPE\n     " + zp.ByteArrayToString(args.Message) + "\n");
                        break;
                    }



                    //
                    break;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message + "\n" + ex.StackTrace);
            }
        }