Пример #1
0
 void _rpc_HomegearError(RPCController sender, long level, string message)
 {
     HomegearError?.Invoke(this, level, message);
 }
Пример #2
0
        private void ProcessPacket(byte[] packet)
        {
            if (packet == null || packet.Length < 8)
            {
                return;
            }

            if ((packet[3] & 1) == 0) //Request
            {
                string             methodName = "";
                List <RPCVariable> parameters = _rpcDecoder.DecodeRequest(packet, ref methodName);
                RPCVariable        response   = new RPCVariable(RPCVariableType.rpcVoid);
                if (methodName == "")
                {
                    response = RPCVariable.CreateError(-1,
                                                       "Packet is not well formed: " + BitConverter.ToString(packet));
                }
                else if (methodName == "system.listMethods")
                {
                    response = new RPCVariable(RPCVariableType.rpcArray);
                    response.ArrayValue.Add(new RPCVariable("system.multicall"));
                    response.ArrayValue.Add(new RPCVariable("error"));
                    response.ArrayValue.Add(new RPCVariable("event"));
                    response.ArrayValue.Add(new RPCVariable("newDevices"));
                    response.ArrayValue.Add(new RPCVariable("updateDevice"));
                    response.ArrayValue.Add(new RPCVariable("deleteDevices"));
                    response.ArrayValue.Add(new RPCVariable("newEvent"));
                    response.ArrayValue.Add(new RPCVariable("deleteEvent"));
                    response.ArrayValue.Add(new RPCVariable("updateEvent"));
                }
                else if (methodName == "system.multicall" && parameters.Any())
                {
                    foreach (RPCVariable method in parameters[0].ArrayValue)
                    {
                        if (method.Type != RPCVariableType.rpcStruct || method.StructValue.Count() != 2)
                        {
                            continue;
                        }

                        if (method.StructValue["methodName"].StringValue != "event")
                        {
                            continue;
                        }

                        List <RPCVariable> eventParams = method.StructValue["params"].ArrayValue;
                        if (eventParams.Count() != 5 || eventParams[0].Type != RPCVariableType.rpcString ||
                            eventParams[1].Type != RPCVariableType.rpcInteger ||
                            eventParams[2].Type != RPCVariableType.rpcInteger ||
                            eventParams[3].Type != RPCVariableType.rpcString)
                        {
                            continue;
                        }

                        RPCEvent?.Invoke(this, eventParams[1].IntegerValue, eventParams[2].IntegerValue, eventParams[3].StringValue, eventParams[4], eventParams[0].StringValue);
                    }
                }
                else if (methodName == "error" && parameters.Count() == 3 &&
                         parameters[1].Type == RPCVariableType.rpcInteger &&
                         parameters[2].Type == RPCVariableType.rpcString)
                {
                    HomegearError?.Invoke(this, parameters[1].IntegerValue, parameters[2].StringValue);
                }
                else if (methodName == "newDevices")
                {
                    NewDevices?.Invoke(this);
                }
                else if (methodName == "deleteDevices")
                {
                    DevicesDeleted?.Invoke(this);
                }
                else if (methodName == "updateDevice")
                {
                    if (parameters.Count == 4 && parameters[0].Type == RPCVariableType.rpcString &&
                        parameters[1].Type == RPCVariableType.rpcInteger &&
                        parameters[2].Type == RPCVariableType.rpcInteger &&
                        parameters[3].Type == RPCVariableType.rpcInteger)
                    {
                        UpdateDevice?.Invoke(this, parameters[1].IntegerValue, parameters[2].IntegerValue,
                                             parameters[3].IntegerValue);
                    }
                }
                else if (methodName == "newEvent")
                {
                    if (parameters.Count == 2 && parameters[1].Type == RPCVariableType.rpcStruct)
                    {
                        string id       = "";
                        long   type     = -1;
                        long   peerId   = 0;
                        long   channel  = -1;
                        string variable = "";
                        if (parameters[1].StructValue.ContainsKey("ID"))
                        {
                            id = parameters[1].StructValue["ID"].StringValue;
                        }

                        if (parameters[1].StructValue.ContainsKey("TYPE"))
                        {
                            type = parameters[1].StructValue["TYPE"].IntegerValue;
                        }

                        if (parameters[1].StructValue.ContainsKey("PEERID"))
                        {
                            peerId = parameters[1].StructValue["PEERID"].IntegerValue;
                        }

                        if (parameters[1].StructValue.ContainsKey("PEERCHANNEL"))
                        {
                            channel = parameters[1].StructValue["PEERCHANNEL"].IntegerValue;
                        }

                        if (parameters[1].StructValue.ContainsKey("VARIABLE"))
                        {
                            variable = parameters[1].StructValue["VARIABLE"].StringValue;
                        }

                        NewEvent?.Invoke(this, id, type, peerId, channel, variable);
                    }
                }
                else if (methodName == "deleteEvent")
                {
                    if (parameters.Count == 6)
                    {
                        EventDeleted?.Invoke(this, parameters[1].StringValue, parameters[2].IntegerValue,
                                             parameters[3].IntegerValue, parameters[4].IntegerValue, parameters[5].StringValue);
                    }
                }
                else if (methodName == "updateEvent")
                {
                    if (parameters.Count == 6)
                    {
                        UpdateEvent?.Invoke(this, parameters[1].StringValue, parameters[2].IntegerValue,
                                            parameters[3].IntegerValue, parameters[4].IntegerValue, parameters[5].StringValue);
                    }
                }
                byte[] responsePacket = _rpcEncoder.EncodeResponse(response).ToArray();
                SendPacket(responsePacket);
            }
            else //Response
            {
                _rpcResponse = _rpcDecoder.DecodeResponse(packet);
                _responseReceived.Set();
            }
        }