コード例 #1
0
        internal static byte[] Pack(PortControlRequest portControlRequest)
        {
            List <byte> sendBuffer = new List <byte>();

            sendBuffer.AddRange(BitConverter.GetBytes(portControlRequest.cmn));
            sendBuffer.AddRange(BitConverter.GetBytes(portControlRequest.msgId));
            sendBuffer.AddRange(BitConverter.GetBytes(portControlRequest.msgLen));
            sendBuffer.AddRange(BitConverter.GetBytes(portControlRequest.sockId));
            sendBuffer.AddRange(BitConverter.GetBytes(portControlRequest.onOff));

            return(sendBuffer.ToArray());
        }
コード例 #2
0
ファイル: HttpServer.cs プロジェクト: yanhan-dev/SmartHome
        /// <summary>
        /// 串口控制继电器 数据发送
        /// </summary>
        /// <param name="portControlRequest"></param>
        /// <returns></returns>
        private async void ProcessPortControlRequest(PortControlRequest portControlRequest, System.Net.Http.HttpListenerResponse response)
        {
            bool isSend = false;

            byte[] buffer = PortControlRequest.Pack(portControlRequest);

            //清除缓存
            DataTemp.httpControlResponse = null;

            isSend = serialPortInput.SendMessage(buffer);
            if (!isSend)
            {
                Console.WriteLine("串口控制数据发送失败");
                return;
            }
            else
            {
                Console.WriteLine($"串口发送: + {BitConverter.ToString(buffer)}");

                int i = 0;
                for (i = 0; i < 3; i++)
                {
                    if (DataTemp.portControlResponse == null)
                    {
                        //串口没回应, 等待1秒
                        Thread.Sleep(1000);
                    }
                    else
                    {
                        //收到控制数据
                        if (!DataTemp.CmnSockID.ContainsKey(DataTemp.portControlResponse.Cmn))
                        {
                            Console.WriteLine($"cmn{DataTemp.portControlResponse.Cmn}不存在, 丢弃数据");
                        }
                        else
                        {
                            if (DataTemp.portControlResponse.Success == 0)
                            {
                                //控制成功
                                Console.WriteLine("控制成功");
                                DataTemp.httpControlResponse = new HttpControlResponse()
                                {
                                    Success = "1"
                                };
                            }
                            else
                            {
                                //控制失败
                                Console.WriteLine("控制失败");
                                DataTemp.httpControlResponse = new HttpControlResponse()
                                {
                                    Success = "0"
                                };
                            }
                            await response.WriteContentAsync(JsonConvert.SerializeObject(DataTemp.httpControlResponse));
                        }
                        DataTemp.portControlResponse = null;
                        break;
                    }
                }
                if (i == 2)
                {
                    Console.WriteLine("串口没在规定时间回应控制数据");
                    return;
                }
            }
        }
コード例 #3
0
ファイル: HttpServer.cs プロジェクト: yanhan-dev/SmartHome
        /// <summary>
        /// Http继电器控制请求
        /// </summary>
        /// <param name="postData"></param>
        /// <param name="response"></param>
        private async void ProcessControl(IDictionary <string, string> postData, System.Net.Http.HttpListenerResponse response)
        {
            ResponseModel responseModel = new ResponseModel();

            //POST数据中是否有'data'这个Key
            if (null == postData || !postData.ContainsKey("data"))
            {
                responseModel.Status        = ResultStatus.Error;
                responseModel.ResultMessage = "GetData No 'data' Key";
                Console.WriteLine("GetData No 'data' Key");
                await response.WriteContentAsync(JsonConvert.SerializeObject(responseModel));

                return;
            }

            //data Key 对应的 Value 是否有值
            if (string.IsNullOrWhiteSpace(postData["data"]))
            {
                responseModel.Status        = ResultStatus.Error;
                responseModel.ResultMessage = "GetData POST 'data' Key No Value";
                Console.WriteLine("GetData POST 'data' Key No Value");
                await response.WriteContentAsync(JsonConvert.SerializeObject(responseModel));

                return;
            }
            ////
            HttpControlRequest httpControlRequest;

            try
            {
                httpControlRequest = JsonConvert.DeserializeObject <HttpControlRequest>(postData["data"]);
            }
            catch (Exception ex)
            {
                responseModel.Status        = ResultStatus.Error;
                responseModel.ResultMessage = $"GetData Json Err: {ex.Message}";
                Console.WriteLine($"GetData Json Err: {ex.Message}");
                await response.WriteContentAsync(JsonConvert.SerializeObject(responseModel));

                return;
            }

            //token校验
            if (!Common.Tokens.ContainsValue(httpControlRequest.Token))
            {
                Console.WriteLine("token不存在");
                responseModel.Status        = ResultStatus.Error;
                responseModel.ResultMessage = "token不存在";
                await response.WriteContentAsync(JsonConvert.SerializeObject(responseModel));

                return;
            }



            PortControlRequest portControlRequest = new PortControlRequest();

            portControlRequest.cmn    = Common.GetCmn();
            portControlRequest.sockId = httpControlRequest.SockID;
            portControlRequest.onOff  = httpControlRequest.OnOff;

            DataTemp.CmnSockID.Add(portControlRequest.cmn, portControlRequest.sockId);

            ProcessPortControlRequest(portControlRequest, response);
        }