Exemple #1
0
 public static async Task WriteJsonResponse(string message, System.Net.Http.HttpListenerResponse response, bool noCache = false)
 {
     response.Headers.ContentEncoding.Add("utf-8");
     response.Headers.ContentType.Add("application/json");
     if (noCache)
     {
         response.Headers.Add("Cache-Control", "no-cache, no-store, must-revalidate");
         response.Headers.Add("Pragma", "no-cache"); // HTTP 1.0.
         response.Headers.Add("Expires", "0");       // Proxies.
     }
     await response.WriteContentAsync(message);
 }
Exemple #2
0
        /// <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;
                }
            }
        }
Exemple #3
0
        /// <summary>
        /// 处理登出
        /// </summary>
        /// <param name="postData"></param>
        /// <param name="response"></param>
        private async void ProcessLogOut(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 = "LogOut No Post Data";
                Console.WriteLine("LogOut No Post Data");
                await response.WriteContentAsync(JsonConvert.SerializeObject(responseModel));

                return;
            }

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

                return;
            }

            User user;

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

                return;
            }

            //用户密码是否正确
            if (!(Common.User.ContainsKey(user.UID) && Common.User[user.UID] == user.PWD))
            {
                responseModel.Status        = ResultStatus.Error;
                responseModel.ResultMessage = "LogOut UserName Or Password Error";
                Console.WriteLine("LogOut UserName Or Password Error");
                await response.WriteContentAsync(JsonConvert.SerializeObject(responseModel));

                return;
            }


            //登出
            responseModel.Status = ResultStatus.Success;
            Console.WriteLine($"{user.UID} LogOut Success");

            if (Common.Tokens.ContainsKey(user.UID))
            {
                Common.Tokens.Remove(user.UID);
                responseModel.ResultMessage = $"{user.UID} 登出成功";
            }
            else
            {
                responseModel.ResultMessage = "用户已登出, 无需重复提交";
            }

            await response.WriteContentAsync(JsonConvert.SerializeObject(responseModel));
        }
Exemple #4
0
        /// <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);
        }
Exemple #5
0
        /// <summary>
        /// Http获取环境数据请求
        /// </summary>
        /// <param name="postData"></param>
        /// <param name="response"></param>
        private async void ProcessGetData(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;
            }

            HttpEnvRequest httpEnvRequest;

            try
            {
                httpEnvRequest = JsonConvert.DeserializeObject <HttpEnvRequest>(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(httpEnvRequest.Token))
            {
                Console.WriteLine("token不存在");
                responseModel.Status        = ResultStatus.Error;
                responseModel.ResultMessage = "token不存在";
                await response.WriteContentAsync(JsonConvert.SerializeObject(responseModel));

                return;
            }

            //校验成功, 返回数据
            HttpEnvResponse httpEnvResponse = HttpEnvResponse.Pack(DataTemp.EnvData);

            responseModel.Status        = ResultStatus.Success;
            responseModel.ResultMessage = "环境数据";
            responseModel.Data          = httpEnvResponse;
            await response.WriteContentAsync(JsonConvert.SerializeObject(responseModel));

            //为了提高性能, 每一次返回的都是上次获取的环境数据
            if (DateTime.Now - DataTemp.DateTime > new TimeSpan(0, 0, 4))
            {
                Console.WriteLine("DataTemp.DateTimes数据为5秒之前的, 向串口请求新的数据");

                PortEnvRequest portEnvRequest = new PortEnvRequest();
                portEnvRequest.cmn = Common.GetCmn();

                ProcessPortEnvRequest(portEnvRequest);
            }
            else
            {
                Console.WriteLine("DataTemp.DateTimes数据为5秒之内的, 无需向串口请求新的数据");
            }
        }
Exemple #6
0
        /// <summary>
        /// Http用户登录
        /// </summary>
        /// <param name="postData"></param>
        /// <param name="response"></param>
        private async void ProcessLogin(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 = "Login Post No 'data' Key";
                Console.WriteLine("Login Post No 'data' Key");
                await response.WriteContentAsync(JsonConvert.SerializeObject(responseModel));

                return;
            }

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

                return;
            }

            User user;

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

                return;
            }

            //用户密码是否正确
            if (!(Common.User.ContainsKey(user.UID) && Common.User[user.UID] == user.PWD))
            {
                responseModel.Status        = ResultStatus.Error;
                responseModel.ResultMessage = "Login Username Or Password Error";
                Console.WriteLine("Login Username Or Password Error");
                await response.WriteContentAsync(JsonConvert.SerializeObject(responseModel));

                return;
            }


            //登录成功
            string token = Common.MD5Encrypt64();

            responseModel.Status        = ResultStatus.Success;
            responseModel.ResultMessage = "Login Success";
            responseModel.Data          = $"{{'token':'{token}'}}";
            Console.WriteLine("Login Success");

            //如果UID对应的token已存在, 更新token
            if (Common.Tokens.ContainsKey(user.UID))
            {
                Common.Tokens[user.UID] = token;
            }
            //否则根据UID新建token
            else
            {
                Common.Tokens.Add(user.UID, token);
            }

            await response.WriteContentAsync(JsonConvert.SerializeObject(responseModel));
        }
Exemple #7
0
 public static async Task WriteResponce(string message, System.Net.Http.HttpListenerResponse response)
 {
     response.Headers.ContentEncoding.Add("utf-8");
     response.Headers.ContentType.Add("text/html;charset=utf-8");
     await response.WriteContentAsync(message);
 }