private void SetOrderNo(BoxSession session, byte[] states) { try { LogHelper.WriteLog(DateTime.Now.ToString("HH:mm:ss") + "allsessions:" + JsonConvert.SerializeObject(boxServer.GetAllSessions().Select(o => o.CustomId).ToList())); var sessions = boxServer.GetSessions(o => o.CustomId.Equals(session.CustomId) && o.CustomType == 1); LogHelper.WriteLog(DateTime.Now.ToString("HH:mm:ss") + "sessions:" + JsonConvert.SerializeObject(sessions.Select(o => o.CustomId).ToList())); if (sessions.Count() != 0) { var orderNo = sessions.First().OrderNo; LogHelper.WriteLog(DateTime.Now.ToString("HH:mm:ss") + "orderNo:" + orderNo); var errorPosition = string.Empty; for (int i = 0; i < states.Length; i++) { byte state = states[i]; if (state != 0x00) { errorPosition += i + 1 + ","; } } var suffix = orderNo.Substring(0, 1); switch (suffix) { case "S": UpdateOrder(errorPosition, orderNo); break; case "B": UpdateBack(errorPosition, orderNo); break; case "J": break; case "C": break; default: LogHelper.WriteLog(DateTime.Now.ToString("HH:mm:ss") + "三版本:"); ThreeUpdateOrder(errorPosition, orderNo); break; } } } catch (Exception ex) { LogHelper.WriteLog(DateTime.Now.ToString("HH:mm:ss") + ex.Message); } }
private void BoxProtocolServer_NewRequestReceived(BoxSession session, BoxRequestInfo requestInfo) { //LogHelper.WriteLog(DateTime.Now.ToString("HH:mm:ss") + "所有的mac:" + JsonConvert.SerializeObject(boxServer.GetAllSessions().Select(o => new { o.CustomId, o.CustomType, o.SessionID }).ToList())); LogHelper.WriteLog(DateTime.Now.ToString("HH:mm:ss") + "当前session的ID:" + session.SessionID); //如果头命令不是EF03则关闭当前的session if (requestInfo.Body.Head.Contains("EF")) { session.CustomId = requestInfo.Body.Mac; session.Mac = requestInfo.Body.FormatMac; switch ((类型)requestInfo.Body.Type) { case 类型.微信: session.CustomType = 2; LogHelper.WriteLog(DateTime.Now.ToString("HH:mm:ss") + "微信传过来的命令:" + requestInfo.Body.Command); var command = BoxModel.ToCommand(requestInfo.Body); LogHelper.WriteLog(DateTime.Now.ToString("HH:mm:ss") + "微信端口命令:" + Converts.GetTPandMac(command)); if (!OpenBoxByMac(session.CustomId, command, requestInfo.Body.OrderNo, 1)) { LogHelper.WriteLog(DateTime.Now.ToString("HH:mm:ss") + "已经开箱失败:"); //需要发送byte数组的命令,返回微信处理 //session.Send(JsonConvert.SerializeObject(new ResponseResult() { Status = false, ErrorCode = 01, Message ="箱子未连接"})); } LogHelper.WriteLog(DateTime.Now.ToString("HH:mm:ss") + "已经开箱成功:"); break; case 类型.心跳: session.CustomType = 1; var macDt = DbHelperSQL.GetDataTableBySQL(string.Format("select top 1 * from WP_库位表 where 箱子MAC ='{0}'", session.Mac)); //如果没有查到信息,那么说明箱子在三代上 if (macDt.Rows.Count == 0) { var cache = (List <OnlineBox>)CacheHelper.GetCache("Boxes"); if (cache != null) { var mac = cache.FirstOrDefault(o => o.mac.Equals(session.Mac)); LogHelper.WriteLog(DateTime.Now.ToString("HH:mm:ss") + ";mac:" + mac); if (mac != null) { //如果之前是离线的,需要通知管理后台 if (!mac.online) { var requestUrl = string.Format("{0}test/online?mac={1}", Constant.YunApi, mac.mac); var response = JsonConvert.DeserializeObject <BuyResponse>(Utils.HttpGet(requestUrl)); LogHelper.WriteLog(DateTime.Now.ToString("HH:mm:ss") + ";heartResponse:" + response); if (!response.operationStatus.Equals("SUCCESS")) { LogHelper.WriteLog(DateTime.Now.ToString("HH:mm:ss") + ";在线通知接口请求失败"); } } mac.online = true; mac.lineTime = DateTime.Now; CacheHelper.SetCache("Boxes", cache); } } } else { //处理并存储心跳信息 SaveHeart(requestInfo.Body); LogHelper.WriteLog(DateTime.Now.ToString("HH:mm:ss") + "心跳命令:" + requestInfo.Body.Command); //判断是否有异常开箱的情况,记录并反馈 ShowLog(txtLog, requestInfo.Body.ToString()); } break; case 类型.开箱: session.CustomType = 1; LogHelper.WriteLog(DateTime.Now.ToString("HH:mm:ss") + "开箱回复命令:" + requestInfo.Body.Command); //发送微信反馈 SetOrderNo(session, requestInfo.Body.State); ShowLog(txtLog, requestInfo.Body.ToString()); break; default: session.CustomType = 1; break; } } //三代微信过来的信息 else if (requestInfo.Body.Head.Contains("FF")) { session.CustomId = requestInfo.Body.Mac; switch ((类型)requestInfo.Body.Type) { case 类型.微信: session.CustomType = 2; LogHelper.WriteLog(DateTime.Now.ToString("HH:mm:ss") + "微信传过来的命令:" + requestInfo.Body.Command); var command = BoxModel.ToCommand(requestInfo.Body); LogHelper.WriteLog(DateTime.Now.ToString("HH:mm:ss") + "微信端口命令:" + Converts.GetTPandMac(command)); if (!OpenBoxByMac(session.CustomId, command, requestInfo.Body.OrderNo, 1)) { LogHelper.WriteLog(DateTime.Now.ToString("HH:mm:ss") + "已经开箱失败:"); //需要发送byte数组的命令,返回微信处理 //session.Send(JsonConvert.SerializeObject(new ResponseResult() { Status = false, ErrorCode = 01, Message ="箱子未连接"})); } LogHelper.WriteLog(DateTime.Now.ToString("HH:mm:ss") + "开箱命令发送成功:"); break; default: session.CustomType = 1; break; } } else { session.Close(); return; } }
static void BoxProtocolServer_NewSessionConnected(BoxSession session) { }