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

namespace OnlineStore.DeviceLibrary
{
    public class LineConnect
    {
        private static TcpClient client = null;

        public static void StartConnect()
        {
            string lineServer = ConfigAppSettings.GetValue(Setting_Init.LineServerIp);
            int linePort = ConfigAppSettings.GetIntValue(Setting_Init.LineServerPort);
            if (lineServer.Equals(""))
            {
                return;
            }
            client = new TcpClient();
            bool result = client.connect(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(int id,string cid,int ss,int runs,string posid)
        {
            if (client == null)
            {
                return;
            }
            try
            {
                string heartMsg = cmd_heart + cmd_spilt + id + cmd_spilt + cid + cmd_spilt + nextSeq() + cmd_spilt + ss + cmd_spilt + runs + cmd_spilt + posid + cmd_spilt+"\r";
                client.send(heartMsg);
            }catch(Exception ex)
            {
                LogUtil.error("SendHeart 出错:"+ex.ToString());
            }
        }
        public static void OutStoreEnd(int id, string cid, int ss, int runs, string posid, string plateH, string plateW)
        {
            if (client == null)
            {
                return;
            }
            try
            {
                string heartMsg = cmd_outend + cmd_spilt + id + cmd_spilt + cid + cmd_spilt + nextSeq() + cmd_spilt + ss + cmd_spilt + runs + cmd_spilt + posid + cmd_spilt;
                string msg = heartMsg + posid + cmd_spilt + plateH + cmd_spilt + plateW + cmd_spilt + "\r";
                client.send(msg );
            }
            catch (Exception ex)
            {
                LogUtil.error("OutStoreEnd 出错:" + ex.ToString());
            }
        }
        private static void HandlerMsg(string message)
        {
            try
            {
                string[] msgArray = message.Split(cmd_spilt);
                if (msgArray.Length > 4)
                {
                    string cmd = msgArray[0];
                    if (cmd.Equals(cmd_startIn))
                    {
                        string posId = msgArray[1];
                        string plateH = msgArray[2];
                        string plateW = msgArray[3];
                        string code = msgArray[4];
                        StoreManager.Store.ReviceLineInStoreCMD(posId, plateH, plateW, code);

                    }
                }
            }
            catch (Exception ex)
            {
            }
        }
        public static char cmd_spilt = ';';
        private static string cmd_heart = "heart";
        private static string cmd_outend = "outend";
        private static string cmd_startIn = "starIn";

        public static bool IsConnect()
        {
            if (client == null)
            {
                return false;
            }
            if (client.IsConnected())
            {
                return true;
            }
            return false ;
        }
    }
}