Ejemplo n.º 1
0
 //定时轮询
 /// <summary>
 /// 在指定时间过后执行指定的表达式
 /// </summary>
 /// <param name="interval">事件之间经过的时间(以毫秒为单位)</param>
 /// <param name="action">要执行的表达式</param>
 private static void SetTimeout(double interval, Action action, string outTradeNo, string key, string machineId)
 {
     if (key == "42") //是否为出货指令
     {
         if (!dicTimers.ContainsKey(outTradeNo))
         {
             dicTimers.Add(outTradeNo, new Timer(interval));
             dicTimers[outTradeNo].Elapsed += delegate(object sender, System.Timers.ElapsedEventArgs e)
             {
                 if (MachineHelper.IsLegalOrder(outTradeNo))
                 {
                     action();
                 }
                 else
                 {
                     dicTimers[outTradeNo].Enabled = false;
                     dicTimers.Remove(outTradeNo);
                 }
             };
         }
         dicTimers[outTradeNo].Enabled   = true;
         dicTimers[outTradeNo].AutoReset = true;
     }
     else //非出货指令发两次
     {
         Timer timer = new Timer(interval);
         timer.Elapsed += delegate(object sender, System.Timers.ElapsedEventArgs e)
         {
             if (!MachineHelper.IsExistPush(machineId, key))// 判断该指令是否存在
             {
                 timer.Enabled = false;
             }
             else
             {
                 timer.Enabled = false;
                 action();
             }
         };
     }
 }
Ejemplo n.º 2
0
        //49(十六进制)开头为后端向socket的标识头
        //webCommandType标识指令的意思(十进制) 10:通知出货 11:一键补货 12:按货道补货 13:修改最大库存 14:现金价格下推
        //totalSize表示需要加密的内容  去除49+webCommandType 48 size(两个字节) chunksum验证 和结尾 EE
        //socketCommand表示向机器下推的指令标识
        public static string GenerateCommand(byte webCommandType, int totalSize, byte socketCommand, List <CommandModel> lstCommandModel)
        {
            byte[] sendByte = new byte[totalSize + 7];              //49+commandType + 48+size+chunk+content+EE
            sendByte[0] = 73;
            sendByte[1] = webCommandType;                           //
            sendByte[2] = 72;
            ByteHelper.IntToTwoByte(totalSize).CopyTo(sendByte, 3); //size
            //sendByte[4] = chunk
            sendByte[6] = socketCommand;
            int i = 0;

            foreach (CommandModel cmdModel in lstCommandModel)
            {
                //byte[] transByte = ByteHelper.strToAscii(cmdModel.Content);
                ByteHelper.strToAscii(cmdModel.Content).CopyTo(sendByte, 7 + i);
                i = i + cmdModel.Size;
            }
            sendByte[5]             = GetChunk(sendByte.Skip(6).Take(totalSize).ToArray());
            sendByte[totalSize + 6] = 238;

            string machineId  = ByteHelper.GenerateRealityData(sendByte.Skip(7).Take(12).ToArray(), "stringval");
            string outTradeNo = string.Empty;
            string key        = ByteHelper.Ten2Hex(socketCommand.ToString());

            if (key == "42") //出货结果通知指令
            {
                outTradeNo = ByteHelper.GenerateRealityData(sendByte.Skip(19).Take(22).ToArray(), "stringval");
            }
            // 发送前加密
            ByteHelper.Encryption(totalSize, sendByte.Skip(6).ToArray()).CopyTo(sendByte, 6);

            serverFullAddr = new IPEndPoint(serverIp, int.Parse(ConfigurationManager.AppSettings["SocketPort"]));//设置IP,端口

            socketSend(sendByte);
            SetTimeout(5000, delegate {
                socketSend(sendByte);
            }, outTradeNo, key, machineId);

            if (key == "42") //出货结果通知指令
            {
                try
                {
                    MachineHelper.CacheOrder(outTradeNo, ByteHelper.byteToHexStr(sendByte.Skip(2).ToArray()));
                }
                catch (Exception ex)
                {
                    MachineHelper.CacheOrder(outTradeNo, ByteHelper.byteToHexStr(sendByte.Skip(2).ToArray()));
                }
            }
            else
            {
                try
                {
                    MachineHelper.CachePush(machineId, key, ByteHelper.byteToHexStr(sendByte.Skip(2).ToArray()));
                }
                catch (Exception ex)
                {
                    MachineHelper.CachePush(machineId, key, ByteHelper.byteToHexStr(sendByte.Skip(2).ToArray()));
                }
            }


            return("");
        }