public static string CreateToken(string requestUrl, CreateToken createToken, out string errorMsg)
    {
        errorMsg = string.Empty;

        RequestDataResult requestDataResult = new RequestDataResult();

        string data          = JsonConvert.SerializeObject(createToken);
        string response_data = string.Empty;

        byte[] byteDataParams = UTF8Encoding.UTF8.GetBytes(data.ToString());

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUrl);

        request.Method        = "POST";
        request.ContentType   = "application/json";
        request.ContentLength = byteDataParams.Length;
        Stream stDataParams = request.GetRequestStream();

        stDataParams.Write(byteDataParams, 0, byteDataParams.Length);
        stDataParams.Close();

        try
        {
            HttpWebResponse webResponse    = (HttpWebResponse)request.GetResponse();
            Stream          webStream      = webResponse.GetResponseStream();
            StreamReader    responseReader = new StreamReader(webStream, System.Text.Encoding.UTF8);
            requestDataResult = JsonConvert.DeserializeObject <RequestDataResult>(responseReader.ReadToEnd());
            Console.Out.WriteLine(requestDataResult.Data);
            responseReader.Close();
        }
        catch (WebException e)
        {
            using (WebResponse response = e.Response)
            {
                HttpWebResponse httpResponse = (HttpWebResponse)response;

                using (Stream errorData = response.GetResponseStream())
                    using (StreamReader errorReader = new StreamReader(errorData))
                    {
                        string errorText = errorReader.ReadToEnd();
                        Console.WriteLine("--------Error---------");
                        Console.WriteLine("Error code: {0}", httpResponse.StatusCode);
                        Console.WriteLine("Error Msg: {0}", errorText);

                        ErrorResult errorResult = JsonConvert.DeserializeObject <ErrorResult>(errorText);
                        errorMsg = errorResult.Error.Detail;
                    }
            }

            requestDataResult.Data = string.Empty;
        }

        return(requestDataResult.Data);
    }
示例#2
0
        protected override void OnCompletedInternal(ActionCompletedUnit ou)
        {
            var result = ou.Action.Result;

            if (result is RequestNodeInfoResult)
            {
                RequestNodeInfoResult res = (RequestNodeInfoResult)result;
                if (res)
                {
                    if (_nodeInfo.Result)
                    {
                        _nodeInfoSecondAttempt.Token.SetCancelled();
                    }
                    else if (_nodeInfoSecondAttempt.Result)
                    {
                        isFirstFailed = true;
                        SpecificResult.RequestNodeInfo = _nodeInfoSecondAttempt.SpecificResult;
                    }

                    if ((res.CommandClasses == null || !res.CommandClasses.Contains(COMMAND_CLASS_MULTI_CHANNEL_V4.ID)) &&
                        (res.SecureCommandClasses == null || !res.SecureCommandClasses.Contains(COMMAND_CLASS_MULTI_CHANNEL_V4.ID)))
                    {
                        _requestEndPoints.Token.SetCancelled();
                        _requestEndPointsCapabilities.Token.SetCancelled();
                    }
                }
                else if (isFirstFailed)
                {
                    _requestEndPoints.Token.SetCancelled();
                    _requestEndPointsCapabilities.Token.SetCancelled();
                }
            }
            else if (result is RequestDataResult)
            {
                RequestDataResult res = (RequestDataResult)result;
                if (res && res.Command != null && res.Command.Length > 1 && res.Command[0] == COMMAND_CLASS_MULTI_CHANNEL_V4.ID)
                {
                    if (res.Command[1] == COMMAND_CLASS_MULTI_CHANNEL_V4.MULTI_CHANNEL_END_POINT_REPORT.ID)
                    {
                        COMMAND_CLASS_MULTI_CHANNEL_V4.MULTI_CHANNEL_END_POINT_REPORT epReport = res.Command;
                        _isIdentical = epReport.properties1.identical == 0x01;
                        _endPoints   = epReport.properties2.individualEndPoints;
                        var count = _isIdentical ? 1 : _endPoints;
                        if ((count > 0 && count < 0xFF))
                        {
                            var tmpList = new List <ActionBase>();
                            for (int i = 0; i < count; i++)
                            {
                                COMMAND_CLASS_MULTI_CHANNEL_V4.MULTI_CHANNEL_CAPABILITY_GET cGet = new COMMAND_CLASS_MULTI_CHANNEL_V4.MULTI_CHANNEL_CAPABILITY_GET();
                                cGet.properties1.res      = 0x00;
                                cGet.properties1.endPoint = (byte)(i + 1);
                                var requestCapability = new RequestDataOperation(0, res.NodeId,
                                                                                 cGet, _txOptions,
                                                                                 new COMMAND_CLASS_MULTI_CHANNEL_V4.MULTI_CHANNEL_CAPABILITY_REPORT(), 2, 5000);
                                tmpList.Add(requestCapability);

                                if (_network != null && _network.HasSecurityScheme(res.NodeId, SecuritySchemeSet.ALL))
                                {
                                    byte[] data = null;
                                    if (_network.HasSecurityScheme(res.NodeId, SecuritySchemeSet.ALLS2))
                                    {
                                        data = new COMMAND_CLASS_SECURITY_2.SECURITY_2_COMMANDS_SUPPORTED_GET();
                                    }
                                    else if (_network.HasSecurityScheme(res.NodeId, SecuritySchemes.S0))
                                    {
                                        data = new COMMAND_CLASS_SECURITY.SECURITY_COMMANDS_SUPPORTED_GET();
                                    }

                                    COMMAND_CLASS_MULTI_CHANNEL_V4.MULTI_CHANNEL_CMD_ENCAP multiChannelCmd = new COMMAND_CLASS_MULTI_CHANNEL_V4.MULTI_CHANNEL_CMD_ENCAP();
                                    multiChannelCmd.commandClass = data[0];
                                    multiChannelCmd.command      = data[1];
                                    multiChannelCmd.parameter    = new List <byte>();
                                    for (int j = 2; j < data.Length; j++)
                                    {
                                        multiChannelCmd.parameter.Add(data[j]);
                                    }
                                    multiChannelCmd.properties1.res                 = 0;
                                    multiChannelCmd.properties1.sourceEndPoint      = 0;
                                    multiChannelCmd.properties2.bitAddress          = 0;
                                    multiChannelCmd.properties2.destinationEndPoint = (byte)(i + 1);
                                    data = multiChannelCmd;

                                    var requestSupported = new RequestDataOperation(0, res.NodeId,
                                                                                    multiChannelCmd, _txOptions,
                                                                                    new COMMAND_CLASS_MULTI_CHANNEL_V4.MULTI_CHANNEL_CMD_ENCAP(), 2, 5000);
                                    tmpList.Add(requestSupported);
                                }
                            }

                            _requestEndPointsCapabilities.AddActions(tmpList.ToArray());
                        }
                        SpecificResult.RequestEndPoints = res;
                    }
                }
            }
            else if (result.InnerResults != null && result.InnerResults.Count > 0)
            {
                SpecificResult.RequestEndPointCapabilities = new List <RequestDataResult>();
                foreach (var item in result.InnerResults)
                {
                    if (item is RequestDataResult)
                    {
                        RequestDataResult res = (RequestDataResult)item;
                        if (res && res.Command != null && res.Command.Length > 1 && res.Command[0] == COMMAND_CLASS_MULTI_CHANNEL_V4.ID)
                        {
                            if (res.Command[1] == COMMAND_CLASS_MULTI_CHANNEL_V4.MULTI_CHANNEL_CAPABILITY_REPORT.ID)
                            {
                                COMMAND_CLASS_MULTI_CHANNEL_V4.MULTI_CHANNEL_CAPABILITY_REPORT cmd = res.Command;
                                if (_isIdentical)
                                {
                                    SpecificResult.RequestEndPointCapabilities.Clear();
                                    for (int i = 0; i < _endPoints; i++)
                                    {
                                        SpecificResult.RequestEndPointCapabilities.Add(res);
                                    }
                                }
                                else
                                {
                                    SpecificResult.RequestEndPointCapabilities.Add(res);
                                }
                            }
                            else if (res.Command[1] == COMMAND_CLASS_MULTI_CHANNEL_V4.MULTI_CHANNEL_CMD_ENCAP.ID)
                            {
                                try
                                {
                                    byte[] secureCC = null;
                                    COMMAND_CLASS_MULTI_CHANNEL_V4.MULTI_CHANNEL_CMD_ENCAP cmd = res.Command;
                                    if (cmd.commandClass == COMMAND_CLASS_SECURITY.ID && cmd.command == COMMAND_CLASS_SECURITY.SECURITY_COMMANDS_SUPPORTED_REPORT.ID)
                                    {
                                        COMMAND_CLASS_SECURITY.SECURITY_COMMANDS_SUPPORTED_REPORT supportedS0 = new[] { cmd.commandClass, cmd.command }.Concat(cmd.parameter).ToArray();
                                        secureCC = supportedS0.commandClassSupport.TakeWhile(x => x != 0xEF).ToArray();
                                    }
                                    else if (cmd.commandClass == COMMAND_CLASS_SECURITY_2.ID && cmd.command == COMMAND_CLASS_SECURITY_2.SECURITY_2_COMMANDS_SUPPORTED_REPORT.ID)
                                    {
                                        COMMAND_CLASS_SECURITY_2.SECURITY_2_COMMANDS_SUPPORTED_REPORT supportedS2 = new[] { cmd.commandClass, cmd.command }.Concat(cmd.parameter).ToArray();
                                        secureCC = supportedS2.commandClass.TakeWhile(x => x != 0xEF).ToArray();
                                    }
                                    if (_isIdentical)
                                    {
                                        for (int i = 0; i < _endPoints; i++)
                                        {
                                            _network.SetSecureCommandClasses(new NodeTag(NodeId, (byte)(i + 1)), secureCC);
                                        }
                                    }
                                    else
                                    {
                                        _network.SetSecureCommandClasses(new NodeTag(NodeId, cmd.properties1.sourceEndPoint), secureCC);
                                    }
                                }
                                catch
                                { }
                            }
                        }
                    }
                }
            }
        }