LineConnect.cs 8.5 KB
using OnlineStore.Common;
using OnlineStore.LoadCSVLibrary;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace OnlineStore.DeviceLibrary
{
    public class LineConnect
    {
        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";
        public static string cmd_checStartIn = "cmd_checStartIn";

        public static    InOutPosInfo DoorPosInfo = null;//料仓门口的料信息
        private int seq = 1;
        private int ClientKeepSecond = 10;
        private TcpClient client = null; 

        private int StoreId = 0;
        private string CID = "";

        public LineConnect (int sId,string cid)
        {
            this.StoreId = sId;
            this.CID = cid;
        }

        public 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("[" + CID + "]未配置流水线地址,不需要连接");
                    return;
                }
                client = new TcpClient();

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

        public void StopConnect()
        {
            if (client != null)
            {
                client.close();
            }
        }

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

        public 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("["+CID+"]SendHeart 出错:" + ex.ToString());
            }
        }
        private DateTime LastOutStoreEndTime = DateTime.Now;
        public void OutStoreEnd(StoreSendBean store)
        {
            if (client == null)
            {
                return;
            }
            try
            { 
                LastOutStoreEndTime = DateTime.Now;
                store.Cmd = cmd_outend;
                string msg = ToParamStr(store);
                LogUtil.info("[" + CID + "]OutStoreEnd:" + msg);
                client.send(msg);
            }
            catch (Exception ex)
            {
                LogUtil.error("[" + CID + "]OutStoreEnd 出错:" + ex.ToString());
            }
        }
        private string ToParamStr(StoreSendBean store)
        {
            return JsonHelper.SerializeObject(store) + "\r";
        }

        private 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("[" + CID + "]收到流水线消息:" + message);
                        InOutPosInfo inout = new InOutPosInfo(reviceInfo.WareCode, "", reviceInfo.PlateH, reviceInfo.PlateW, reviceInfo.IsNg, reviceInfo.PosId,reviceInfo.rfid); 
                        string logName = "收到流水线命令【 " + inout.ToStr() + "】:";

                        if (DoorPosInfo != null)
                        {
                            LogUtil.error(logName + ",清理DoorPosInfo:" + DoorPosInfo.ToStr());
                            DoorPosInfo = null;
                        }

                        //直接存入缓存 
                        LogUtil.error(logName + "缓存到DoorPosInfo中");
                        DoorPosInfo = inout;
                    }
                    else if (cmd.Equals(cmd_updateDebug))
                    {
                        int isDebug = reviceInfo.IsDebug;

                        LogUtil.info("[" + CID + "]收到流水线更改调试状态=" + isDebug);
                        EquipManager.Equip.IsDebug = isDebug.Equals(1) ? true : false;
                        ConfigAppSettings.SaveValue(Setting_Init.IsInDebug, isDebug);
                        LogUtil.info("[" + CID + "]切换调试状态= " + isDebug + ";");
                    }
                    else if (cmd.Equals(cmd_checStartIn))
                    {
                        LogUtil.info("[" + CID + "]收到流水线验证消息:" + message); 
                    } 
                }
            }
            catch (Exception ex)
            {
                LogUtil.error("[" + CID + "]处理流水线消息【" + message + "】出错:" + ex.ToString());
            }
        }
        private bool CanOutStore = false;
        public DateTime LastUpdateTime = new DateTime(0);
        public bool IsConnect()
        {
            if (client == null)
            {
                return false;
            }
            if (client.IsRun() && client.IsConnected())
            {
                return true;
            }
            return false;
        }

        //public bool CanStartOut()
        //{
        //    TimeSpan span = DateTime.Now - LastUpdateTime;
        //    if (span.TotalSeconds < ClientKeepSecond && CanOutStore)
        //    {
        //        return true;
        //    }
        //    return false;
        //}
        public bool CanReSend()
        {
            TimeSpan span = DateTime.Now - LastOutStoreEndTime;
            if (span.TotalSeconds > 8)
            {
                return true;
            }
            return false;
        }


        public StoreSendBean GetBean(int ss, int runs, int doorHasTray, int alarmType)
        {
            StoreSendBean bean = new StoreSendBean(ss, runs, doorHasTray, alarmType);
            bean.StoreId = StoreId;
            bean.Cid = CID;

            bean.Seq = nextSeq();
            bean.WaitInStoreList = new List<string>( );
            if (DoorPosInfo != null)
            {
                bean.WaitInStoreList.Add(DoorPosInfo.ShelfPosId);
            }
            return bean; 
        }
    }
    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 bool IsNg = false;
    }
    public class StoreSendBean
    {
        public StoreSendBean(int ss, int runs, int doorHasTray, int alarmType)
        {
            this.StoreId = 0;
            this.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.Seq = 0;
            this.WaitInStoreList = new List<string>();
            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>();


    }
}