internal static byte[] Pack(PortEnvRequest portEnvRequest) { List <byte> sendBuffer = new List <byte>(); sendBuffer.AddRange(BitConverter.GetBytes(portEnvRequest.cmn)); sendBuffer.AddRange(BitConverter.GetBytes(portEnvRequest.msgId)); sendBuffer.AddRange(BitConverter.GetBytes(portEnvRequest.msgLen)); sendBuffer.AddRange(BitConverter.GetBytes(portEnvRequest.reqId)); return(sendBuffer.ToArray()); }
/// <summary> /// 串口环境数据请求 /// </summary> /// <param name="portEnvRequest"></param> /// <returns></returns> private bool ProcessPortEnvRequest(PortEnvRequest portEnvRequest) { bool isSend = false; byte[] buffer = PortEnvRequest.Pack(portEnvRequest); isSend = serialPortInput.SendMessage(buffer); if (!isSend) { Console.WriteLine("串口数据发送失败"); return(false); } else { Console.WriteLine($"串口发送: + {BitConverter.ToString(buffer)}"); return(true); } }
/// <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秒之内的, 无需向串口请求新的数据"); } }