Ejemplo n.º 1
0
        private void SaveHeart(BoxModel model)
        {
            var errorPosition = string.Empty;

            for (int i = 0; i < model.State.Length; i++)
            {
                byte state = model.State[i];
                if (state == 0x05)
                {
                    errorPosition += i + 1 + ",";
                }
            }
            errorPosition = errorPosition.TrimEnd(',');
            var setErrorSql = string.Empty;

            if (!string.IsNullOrEmpty(errorPosition))
            {
                setErrorSql = string.Format(@"  update WP_箱子表 set BoxStatus = 1 where 库位id in(
  select top 1 id from WP_库位表 where 箱子MAC = '{0}') and 位置 in({1})", model.FormatMac, errorPosition);
                LogHelper.WriteLog(DateTime.Now.ToString("HH:mm:ss") + "异常开箱MAC:" + model.FormatMac + ";异常位置:" + errorPosition);
            }

            LogHelper.SaveHeart(model.FormatMac, model.Command);
            var sql = string.Format(@"
declare @cou int
select @cou =count(id)  from WP_设备心跳记录表 where  mac='{0}'
if @cou =0
begin
insert into WP_设备心跳记录表(mac,command) values('{0}','{1}')
end
else
begin
update WP_设备心跳记录表 set  command = '{1}',createtime = getdate() where mac = '{0}'
UPDATE [WP_库位表] SET [状态] = 1,修改时间 = getdate(),离线时长 = 0,版本号='{3}' WHERE 箱子MAC = '{0}'
update WP_BarCode set Version = '{3}' where BarCode ='{0}'
end 
{2}
", model.FormatMac, model.Command, setErrorSql, Converts.GetTPandMac(model.Placeholder));

            try
            {
                var b = DbHelperSQL.ExecuteSql(sql);
            }
            catch (Exception ex2)
            {
                LogHelper.WriteLog(DateTime.Now.ToString("HH:mm:ss") + ex2.Message);
            }
        }
Ejemplo n.º 2
0
 //EF0332110B150E1C3401383631383533303332303036363033030303000300000000000000130000000000000500060B03FA
 public static byte[] ToCommand(BoxModel model)
 {
     LogHelper.WriteLog(DateTime.Now.ToString("HH:mm:ss") + "需要拼接的命令:" + JsonConvert.SerializeObject(model));
     byte[] macByte = Encoding.UTF8.GetBytes(model.FormatMac);
     return(TcpClientHelper.GetSendMulty(macByte, model.State));
 }
Ejemplo n.º 3
0
        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;
            }
        }
Ejemplo n.º 4
0
        protected override BoxRequestInfo ProcessMatchedRequest(byte[] buffer, int offset, int length, bool toBeCopied)
        {
            var boxModel = new BoxModel();
            var headByte = new byte[2];
            var command  = buffer.CloneRange(offset, 50);

            if (!CheckDataGram(command))
            {
                return(new BoxRequestInfo(new BoxModel()));
            }
            boxModel.Head   = ByteToHexString(buffer.CloneRange(offset, 2));//开始标识的解析,2个字节
            boxModel.Lenght = BitConverter.ToUInt16(buffer, offset + 2);

            if (boxModel.Head.Equals("EF02"))
            {
                boxModel.Type      = 2;
                boxModel.OpenType  = buffer[offset + 3];
                boxModel.Mac       = Converts.GetTPandMac(buffer.CloneRange(offset + 4, 15));
                boxModel.FormatMac = FormatMac(boxModel.Mac);
                boxModel.Command   = Converts.GetTPandMac(command);
                boxModel.State     = buffer.CloneRange(offset + 19, 12);
                var oderCount = int.Parse(Encoding.UTF8.GetString(buffer.CloneRange(offset + 46, 2)));
                var orderNo   = buffer.CloneRange(offset + 31, oderCount);
                LogHelper.WriteLog("命令过来的type类型:" + (int)boxModel.OpenType);
                switch ((Enum.OpenType)((int)boxModel.OpenType))
                {
                case Enum.OpenType.订单开箱:
                    boxModel.OrderNo = "S" + Encoding.UTF8.GetString(orderNo);
                    break;

                case Enum.OpenType.补货开箱:
                    boxModel.OrderNo = "B" + Encoding.UTF8.GetString(orderNo);
                    break;

                case Enum.OpenType.开箱检查:
                    boxModel.OrderNo = "J" + Encoding.UTF8.GetString(orderNo);
                    break;

                case Enum.OpenType.测试开箱:
                    boxModel.OrderNo = "C" + Encoding.UTF8.GetString(orderNo);
                    break;
                }
            }
            else if (boxModel.Head.Equals("EF03"))
            {
                boxModel.Type        = buffer[offset + 9];
                boxModel.Time        = Converts.GetTPandMac(buffer.CloneRange(offset + 3, 6));
                boxModel.Mac         = Converts.GetTPandMac(buffer.CloneRange(offset + 10, 15));
                boxModel.FormatMac   = FormatMac(Converts.GetTPandMac(buffer.CloneRange(offset + 10, 15)));
                boxModel.State       = buffer.CloneRange(offset + 25, 12);
                boxModel.Signal      = buffer.CloneRange(offset + 37, 1);
                boxModel.Placeholder = buffer.CloneRange(offset + 47, 1);
                boxModel.Check       = buffer.CloneRange(offset + 48, 2);
                boxModel.Command     = Converts.GetTPandMac(command);
            }
            //三代箱子
            else if (boxModel.Head.Equals("FF02"))
            {
                boxModel.Type      = 2;
                boxModel.OpenType  = buffer[offset + 3];
                boxModel.Mac       = Converts.GetTPandMac(buffer.CloneRange(offset + 4, 15));
                boxModel.FormatMac = FormatMac(boxModel.Mac);
                boxModel.Command   = Converts.GetTPandMac(command);
                boxModel.State     = buffer.CloneRange(offset + 19, 12);
                var oderCount = int.Parse(Encoding.UTF8.GetString(buffer.CloneRange(offset + 47, 1)));
                var orderNo   = buffer.CloneRange(offset + 31, oderCount);
                LogHelper.WriteLog("命令过来的type类型:" + (int)boxModel.OpenType);
                boxModel.OrderNo = Encoding.UTF8.GetString(orderNo);
            }
            return(new BoxRequestInfo(boxModel));
        }