LineConnect.cs 9.1 KB
using OnlineStore.Common;
using System;
using System.Collections.Generic;
using System.Linq; 
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace OnlineStore.DeviceLibrary
{
    public class LineConnect
    {
        private static int ClientKeepSecond = 10;
        private static TcpClient client = null;
        public static  List<string> WaitInStoreList = new List<string>();
        public static void StartConnect()
        {
            Task.Factory.StartNew(delegate
            {
                string lineServer = ConfigAppSettings.GetValue(Setting_Init.LineServerIp);
                int linePort = ConfigAppSettings.GetIntValue(Setting_Init.LineServerPort);
                if (lineServer.Equals(""))
                {
                    LogUtil.error("未配置流水线地址,不需要连接");
                    return;
                }
                client = new TcpClient();

                bool result = client.StartConnect(lineServer, linePort, HandlerMsg, 2000);
            });
        }

        public static void StopConnect()
        {
            if (client != null)
            {
                client.close();
            }
        }
        private static int seq = 1;

        public static int nextSeq()
        {
            if (seq.Equals(Int32.MaxValue))
            {
                LogUtil.info("seq当前值:" + seq + ",重置seq=0");
                seq = 0;
            }
            Interlocked.Increment(ref seq);
            return seq;
        }

        public static void SendHeart(StoreSendBean store)
        {
            if (client == null)
            {
                return;
            }
            try
            {
                store.Cmd = cmd_heart; 
                string heartMsg = ToParamStr(store);
                
                client.send(heartMsg);
            } catch (Exception ex)
            {
                LogUtil.error("SendHeart 出错:" + ex.ToString());
            }
        }
        private static  DateTime LastOutStoreEndTime = DateTime.Now;
        public static void OutStoreEnd(StoreSendBean store)
        {
            if (client == null)
            {
                return;
            }
            try
            {
                LastOutStoreEndTime = DateTime.Now;
                store.Cmd = cmd_outend; 
                string msg = ToParamStr(store);
                LogUtil.info("OutStoreEnd:" + msg);
                client.send(msg);
            }
            catch (Exception ex)
            {
                LogUtil.error("OutStoreEnd 出错:" + ex.ToString());
            }
        }
        private static string ToParamStr(StoreSendBean store)
        {
            return JsonHelper.SerializeObject(store)+"\r";
        }
         
        private static void HandlerMsg(string message)
        {
            try
            {
                message = message.Replace("\r", "");
                StoreReviceBean reviceInfo = JsonHelper.DeserializeJsonToObject<StoreReviceBean>(message);
                //string[] msgArray = message.Split(cmd_spilt);
                if (reviceInfo == null)
                {
                    LogUtil.debug("收到消息:" + message + ",解析失败");
                }
                else
                {
                    string cmd = reviceInfo.Cmd; 
                    CanOutStore = reviceInfo.CanOutStore.Equals(1);
                    LastUpdateTime = DateTime.Now;
                    if (cmd.Equals(cmd_startIn))
                    {
                        LogUtil.info("收到流水线入库消息:" + message);
                        StoreManager.Store.ReviceLineInStoreCMD(reviceInfo.PosId, reviceInfo.PlateH, reviceInfo.PlateW, reviceInfo.WareCode,reviceInfo.rfid);
                    }
                    else if (cmd.Equals(cmd_updateDebug))
                    {
                        int isDebug = reviceInfo.IsDebug;

                        LogUtil.info("收到流水线更改调试状态=" + isDebug);
                        StoreManager.Store.IsDebug = isDebug.Equals(1) ? true : false;
                        ConfigAppSettings.SaveValue(Setting_Init.IsInDebug, isDebug);
                        LogUtil.info("切换调试状态= " + isDebug + ";");
                    }
                    else if (cmd.Equals(cmd_checStartIn))
                    {
                        LogUtil.info("收到流水线入库库位验证消息:" + message);
                        //if (LineConnect.WaitInStoreList.Contains(reviceInfo.PosId))
                        //{
                        //    LogUtil.error(" LineConnect.WaitInStoreList已存在库位【" + reviceInfo.PosId + "】先移除在验证");
                        //    LineConnect.WaitInStoreList.Remove(reviceInfo.PosId);
                        //}

                        bool result = StoreManager.Store.ReviceLineCheckInStoreCMD(reviceInfo.PosId, reviceInfo.PlateH, reviceInfo.PlateW, reviceInfo.WareCode,reviceInfo.rfid);
                        if (result && (!WaitInStoreList.Contains(reviceInfo.PosId)))
                        {
                            LineConnect.WaitInStoreList.Add(reviceInfo.PosId);
                        }
                    }

                }
            }
            catch (Exception ex)
            {
                LogUtil.error("处理流水线消息【"+ message +"】出错:" +ex.ToString() );
            }
        } 
        public static char cmd_spilt = ';';
        public  static string cmd_heart = "heart";
        private static string cmd_outend = "outend";
        private static string cmd_startIn = "starIn";
        private static string cmd_updateDebug = "updateDebug";
        private static bool CanOutStore = false;
        public static DateTime LastUpdateTime = new DateTime(0);
        public static string cmd_checStartIn = "cmd_checStartIn";
        public static bool IsConnect()
        { 
            if (client == null)
            {
                return false;
            }
            if (client.IsRun()&& client.IsConnected())
            {
                return true;
            }
            return false ;
        }

        public static bool CanStartOut()
        {
            TimeSpan span = DateTime.Now - LastUpdateTime;
            if (span.TotalSeconds < ClientKeepSecond && CanOutStore)
            {
                return true;
            }
            return false;
        }
        /// <summary>
        /// 是否可以重发消息
        /// </summary>
        /// <returns></returns>
        public  static bool CanReSend()
        {
            TimeSpan span = DateTime.Now - LastOutStoreEndTime;
            if (span.TotalSeconds > 8)
            {
                return true;
            }
            return false;
        }
    }
    public class StoreReviceBean
    {
        public string Cmd = "";
        public int CanOutStore = 0;
        public string PosId = "";
        public int PlateH =0;
        public int PlateW = 0;
        public string WareCode = "";
        public int IsDebug = 0;
        public string rfid = "";
    }
    public class StoreSendBean
    {
        public StoreSendBean(int id, string cid, int ss, int runs, int doorHasTray, int alarmType)
        {
            this.StoreId = id;
            this.Cid = cid;
            this.SStatus = ss;
            this.SRunStatus = runs;
            this.DoorHasTray = doorHasTray;
            this.AlarmType = alarmType;
            this.Seq = LineConnect.nextSeq();
            this.WaitInStoreList = new List<string>(LineConnect.WaitInStoreList);
            this.data = new Dictionary<string, string>();
          //  WaitInStoreList.Add("1#AC1_1_1");

        }
        public string Cmd =LineConnect. cmd_heart;
        public int StoreId = 0;
        public string Cid = "";
        public int Seq = 0;
        public int SStatus = 0;
        public int SRunStatus = 0;
        public int DoorHasTray = 0;
        public int AlarmType = 0;

        public List<string> WaitInStoreList = new List<string>();
        public Dictionary<string, string> data = new Dictionary<string, string>();

        //public string PosId = "";
        //public string PlateH = "0";
        //public string PlateW = "0";
        ///// <summary>
        ///// urgentReel: true 表示紧急料,需要出到料串上
        ///// </summary>
        //public bool urgentReel = false;
        ///// <summary>
        ///// cutReel: true 表示分盘料,需要出到料串上
        ///// </summary>
        //public bool cutReel = false;
        ///// <summary>
        ///// smallReel: true  小料(7x8),放置到小料架上
        ///// </summary>
        //public bool smallReel = false;

        ///// <summary>
        ///// rfid: 分配的料架RFID
        ///// </summary>
        //public string rfid = "";
        ///// <summary>
        ///// rfidLoc: 料架的架位,值为 - 1时,可以自由分配皮带线, 
        ///// 小料时,架位为1 - 46优先走1 / 2号皮带线,47 - 92优先走3 / 4号皮带线, 
        ///// 70,71,72时只能分配到3 / 4号皮带线; 
        ///// 大料时,架位1 - 6优先走1 / 2号皮带线, 7 - 12优先走3/ 4号皮带线
        ///// </summary>
        //public int rfidLoc = 0;

    }
}