コード例 #1
0
 private void SaveClientStat(string key, STAT stat, POWERSTAT powerstat)
 {
     if (clientTable.ContainsKey(key))
     {
         DMClient oldClient = clientTable[key];
         oldClient.Stat      = stat;
         oldClient.Powerstat = powerstat;
     }
 }
コード例 #2
0
 public void RemoveClient(DMClient dmclient)
 {
     if (dmclient.DeviceID != null)
     {
         if (clientTable.ContainsKey(dmclient.DeviceID))
         {
             clientTable.Remove(dmclient.DeviceID);
         }
     }
 }
コード例 #3
0
        /// <summary>
        /// 处理异步连接
        /// </summary>
        /// <param name="ar"></param>
        public void AcceptCallback(IAsyncResult ar)
        {
            Socket clientSocket = serverSocket.EndAccept(ar);
            //创建Client
            DMClient dmclient = new DMClient(clientSocket, this);

            //启动client
            dmclient.Start();
            //再继续进行接收连接
            serverSocket.BeginAccept(new AsyncCallback(AcceptCallback), null);
        }
コード例 #4
0
 /// <summary>
 /// 校验是不是已经注册的设备
 /// </summary>
 /// <param name="dmclient"></param>
 /// <returns></returns>
 private bool VerifyClientRegistration(DMClient dmclient)
 {
     if (dmclient.Clientstat == DMClient.CLIENTSTAT.UNREGISTERED)
     {
         dmclient.CloseConnect();
         return(false);
     }
     else
     {
         return(true);
     }
 }
コード例 #5
0
 //public bool CheckAdmin(string username ,string password)
 //{
 //    return (this.LEMSAdmin.Equals(username.Trim()) && this.LEMSAdminPassword.Equals(password.Trim()));
 //}
 /// <summary>
 /// 初始化设备
 /// </summary>
 /// <param name="client"></param>
 /// <returns></returns>
 private bool InitializationDevice(DMClient dmclient)
 {
     JsonDataHandle.JsonDataModel jsonData = JsonDataHandle.ReadJson(saveFile);
     if (jsonData.Sequence == 0 || jsonData.Sequence < this.StartingSequence)
     {
         jsonData.Sequence = this.StartingSequence;
     }
     dmclient.DeviceID = string.Format(this.IDTemplate, jsonData.Sequence.ToString("00"));
     jsonData.Sequence++;
     JsonDataHandle.SaveJson(saveFile, jsonData);
     SendReply(COMMAND.REP_REGISTER, dmclient);
     dmclient.CloseConnect();
     return(true);
 }
コード例 #6
0
        /// <summary>
        /// 发送到设备
        /// </summary>
        /// <param name="deviceID"></param>
        /// <param name="command"></param>
        /// <param name="stat"></param>
        /// <param name="powerStat"></param>
        /// <param name="otherinfo"></param>
        /// <returns></returns>
        public string SendMsgToClient(string deviceID, COMMAND command, STAT stat, POWERSTAT powerStat, string otherinfo = null)
        {
            DMClient c = clientTable[deviceID];

            if (c != null)
            {
                DMProtocol dmProtocol = null;
                string     msg        = null;
                switch (command)
                {
                case COMMAND.ASK:
                    dmProtocol = new DMProtocol(HEADER.XXZL.ToString(), VERSION.V0100.ToString(), (int)command, deviceID, 0, 0, DateTime.Now.ToString("yyyyMMddHHmmss"));
                    msg        = dmProtocol.makeMessage();
                    break;

                case COMMAND.REMIND:
                    string[] info = new string[1];
                    info[0]    = otherinfo;
                    dmProtocol = new DMProtocol(HEADER.XXZL.ToString(), VERSION.V0100.ToString(), (int)command, deviceID, (int)stat, (int)powerStat, DateTime.Now.ToString("yyyyMMddHHmmss"), info);
                    msg        = dmProtocol.makeMessage();
                    break;

                case COMMAND.SETUP:
                    dmProtocol = new DMProtocol(HEADER.XXZL.ToString(), VERSION.V0100.ToString(), (int)command, deviceID, (int)stat, (int)powerStat, DateTime.Now.ToString("yyyyMMddHHmmss"));
                    msg        = dmProtocol.makeMessage();
                    break;

                case COMMAND.UPDATE:
                    dmProtocol = new DMProtocol(HEADER.XXZL.ToString(), VERSION.V0100.ToString(), (int)command, deviceID, 0, 0, DateTime.Now.ToString("yyyyMMddHHmmss"));
                    msg        = dmProtocol.makeMessage();
                    break;
                }
                //try
                //{
                //    c.Send(msg);
                //    return "Success";

                //}
                //catch (Exception ex)
                //{
                //    Console.WriteLine(ex.Message);
                //    return "Err:ex.Message";
                //}
                return(c.SendAndReceive(msg));
            }
            else
            {
                return(GetErrMessageToLIMS(deviceID, "Device is offline", (int)COMMAND.ERR_CONCET));
            }
        }
コード例 #7
0
        /// <summary>
        /// 给LIMS发消息
        /// </summary>
        /// <param name="deviceId"></param>
        /// <param name="cardData"></param>
        /// <param name="stat"></param>
        /// <param name="powerstat"></param>
        /// <param name="info"></param>
        /// <param name="infoTypeID">消息类型,可以继承设备消息类型</param>
        /// <returns></returns>
        public string SendMsgToLIMS(string deviceId, string cardData, int stat, int powerstat, string info, int infoTypeID)
        {
            //制作一个读卡后30秒内不再相应读卡
            if (infoTypeID == (int)COMMAND.READCARD)
            {
                if (clientTable.ContainsKey(deviceId))
                {
                    DMClient oldClient = clientTable[deviceId];
                    //现在距上次读卡必须超过30秒。低于30秒则直接返回。
                    if (DateTime.Now < oldClient.LastCardReadingTime.AddSeconds(30))
                    {
                        return("The card reading time is too close.");
                    }
                    else
                    {
                        //超过30秒后更新之
                        oldClient.LastCardReadingTime = DateTime.Now;
                    }
                }
                else
                {
                    return("When reading the card, the device is no longer online.");
                }
            }
            LIMSAPIData data = new LIMSAPIData();

            data.CardData     = cardData;
            data.DeviceID     = deviceId;
            data.DeviceInfo   = info;
            data.DeviceStatID = stat;
            data.InfoTypeID   = infoTypeID;
            data.PowerStatID  = powerstat;
            data.Sign         = MakeLIMSAPIDataSign(data, LIMSAppKey);

            string json = JsonSerializer.Serialize(data);

            RESTClient restClient = new RESTClient(LIMSServerURL, LIMSAppKey);

            try
            {
                return(restClient.Post(json, "api/event/api/UpdateDevice"));
            }
            catch (Exception ex)
            {
                log.Error(ex.Message);
                return(ex.Message);
            }
            //还未填入api地址
        }
コード例 #8
0
        /// <summary>
        /// 为了LIMS同步调用设备发送命令准备的方法
        /// </summary>
        /// <param name="deviceId"></param>
        /// <param name="cardData"></param>
        /// <param name="stat"></param>
        /// <param name="powerstat"></param>
        /// <param name="info"></param>
        /// <param name="infoTypeID"></param>
        public void ReplyMsgToLIMS(string deviceId, string cardData, int stat, int powerstat, string info, int infoTypeID)
        {
            LIMSAPIData data = new LIMSAPIData();

            data.CardData     = cardData;
            data.DeviceID     = deviceId;
            data.DeviceInfo   = info;
            data.DeviceStatID = stat;
            data.InfoTypeID   = infoTypeID;
            data.PowerStatID  = powerstat;
            data.Sign         = MakeLIMSAPIDataSign(data, LIMSAppKey);

            string   json     = JsonSerializer.Serialize(data);
            DMClient dmClient = clientTable[deviceId];

            dmClient.SetReplayMessgaeToLIMS(json);
        }
コード例 #9
0
        public void SendReply(COMMAND command, DMClient dmclient)
        {
            DMProtocol dMProtocol;

            switch (command)
            {
            case COMMAND.REP_REGISTER:
                dMProtocol = new DMProtocol(HEADER.XXZL.ToString(), VERSION.V0100.ToString(), (int)COMMAND.REP_REGISTER, dmclient.DeviceID, (int)dmclient.Stat, (int)dmclient.Powerstat, DateTime.Now.ToString("yyyyMMddHHmmss"));
                dmclient.Send(dMProtocol.makeMessage());
                break;

            case COMMAND.REP_READCARD:
                dMProtocol = new DMProtocol(HEADER.XXZL.ToString(), VERSION.V0100.ToString(), (int)COMMAND.REP_READCARD, dmclient.DeviceID, (int)dmclient.Stat, (int)dmclient.Powerstat, DateTime.Now.ToString("yyyyMMddHHmmss"));
                dmclient.Send(dMProtocol.makeMessage());
                break;
            }
        }
コード例 #10
0
        /// <summary>
        /// 获取个别设备信息
        /// </summary>
        /// <param name="DeviceID"></param>
        /// <returns></returns>
        public DeviceInformation GetClientInfo(string deviceID)
        {
            DMClient c = clientTable[deviceID];

            if (c != null)
            {
                DeviceInformation deviceInformation = new DeviceInformation();
                deviceInformation.DeviceID   = c.DeviceID;
                deviceInformation.DeviceStat = (int)c.Stat;
                deviceInformation.IP         = c.RemoteIP;
                deviceInformation.PowerStat  = (int)c.Powerstat;
                return(deviceInformation);
            }
            else
            {
                return(null);
            }
        }
コード例 #11
0
 public void AddClient(string key, DMClient dmclient)
 {
     if (clientTable.ContainsKey(key))
     {
         DMClient oldClient = clientTable[key];
         //修改了可访问性 实现了本身的Equals
         //if (!oldClient.clientSocket.Equals(dmclient.clientSocket))
         if (!oldClient.Equals(dmclient))
         {
             oldClient.CloseConnect(key);
         }
         else
         {
             SaveClientStat(key, dmclient.Stat, dmclient.Powerstat);
         }
         return;
     }
     dmclient.SetRegistered();
     clientTable.Add(key, dmclient);
     //var log = LogManager.GetLogger(Startup.repository.Name, typeof(DMServerMainThread));
     //log.Info("Device registration :"+ key);
 }
コード例 #12
0
ファイル: DMClient.cs プロジェクト: rexfa/LEMServer
 public bool Equals(DMClient client)
 {
     return(this.clientSocket.Equals(client.clientSocket));
 }
コード例 #13
0
        /// <summary>
        /// 暂时废弃
        /// </summary>
        /// <param name="ar"></param>
        //public static void ReadCallback(IAsyncResult ar)
        //{
        //    StateObject state = (StateObject)ar.AsyncState;
        //    Socket clientSocket = state.workSocket;
        //    int count=0;
        //    try
        //    {
        //        //count = clientSocket.EndReceive(ar);
        //        //string msg = Encoding.UTF8.GetString(state.buffer, 0, count);
        //        //Console.WriteLine(msg);
        //        try
        //        {
        //            //导入数据处理器
        //            ProtocolProcessor.DeviceInformationProcessing(ar);
        //        }
        //        catch (Exception ex)
        //        {
        //            Console.WriteLine(ex.Message);
        //        }
        //    }
        //    catch (System.Net.Sockets.SocketException se)
        //    {
        //        Console.WriteLine(se.Message);
        //    }

        //    if (count > 0)
        //    {
        //        state.stringBuilder.Append(Encoding.ASCII.GetString(state.buffer, 0, count));
        //        clientSocket.BeginReceive(state.buffer, 0, StateObject.BUFFER_SIZE, 0,
        //                                 new AsyncCallback(ReadCallback), state);
        //    }

        //    else
        //    {
        //        if (state.stringBuilder.Length > 1)
        //        {
        //            //All of the data has been read, so displays it to the console
        //            string strContent;
        //            strContent = state.stringBuilder.ToString();
        //            Console.WriteLine(String.Format("Read {0} byte from socket" + "data = {1} ", strContent.Length, strContent));

        //        }
        //        //长连接,不再主动断开
        //        //clientSocket.Close();
        //    }

        //}
        /// <summary>
        /// 处理设备发来的消息消息
        /// </summary>
        /// <param name="dMProtocol"></param>
        /// <param name="dmclient"></param>
        public void HandleRequest(DMSCRProtocol dMProtocol, DMClient dmclient)
        {
            if (dMProtocol.Header == HEADER.SXZL)
            {
                switch (dMProtocol.Command)
                {
                //注册时判断是否是初始化服务,如果符合标准进行设备初始化,实际上是赋予设备新ID
                case COMMAND.REGISTER:
                    dmclient.DeviceID  = dMProtocol.DeviceID;
                    dmclient.Stat      = dMProtocol.Stat;
                    dmclient.Powerstat = dMProtocol.Powerstat;
                    //if (IsProvideInitialization && dmclient.RemoteIP.Equals(this.PremiseDeviceIP) && dmclient.DeviceID.Equals("SZ000001"))
                    if (IsProvideInitialization && dmclient.DeviceID.Equals("SZ000001"))
                    {
                        InitializationDevice(dmclient);
                    }
                    else
                    {
                        AddClient(dMProtocol.DeviceID, dmclient);
                        SendReply(COMMAND.REP_REGISTER, dmclient);
                    }
                    break;

                case COMMAND.READCARD:
                    dmclient.Stat      = dMProtocol.Stat;
                    dmclient.Powerstat = dMProtocol.Powerstat;
                    if (VerifyClientRegistration(dmclient))
                    {
                        SendReply(COMMAND.REP_READCARD, dmclient);
                        /* 发送到LIMS服务器 */
                        string cardresult = SendMsgToLIMS(dMProtocol.DeviceID, dMProtocol.OtherInfo[0], dMProtocol.StatInt, dMProtocol.PowerStatInt, "", dMProtocol.CommandInt);
                        Console.WriteLine("Card Result : " + cardresult);

                        SaveClientStat(dMProtocol.DeviceID, dMProtocol.Stat, dMProtocol.Powerstat);
                    }
                    break;

                case COMMAND.REP_ASK:
                    //log.Info("REP_ASK");
                    ReplyMsgToLIMS(dMProtocol.DeviceID, "", dMProtocol.StatInt, dMProtocol.PowerStatInt, "", dMProtocol.CommandInt);
                    SaveClientStat(dMProtocol.DeviceID, dMProtocol.Stat, dMProtocol.Powerstat);
                    break;

                case COMMAND.REP_REMIND:
                    //log.Info("REP_REMIND");
                    ReplyMsgToLIMS(dMProtocol.DeviceID, "", dMProtocol.StatInt, dMProtocol.PowerStatInt, "", dMProtocol.CommandInt);
                    SaveClientStat(dMProtocol.DeviceID, dMProtocol.Stat, dMProtocol.Powerstat);
                    break;

                case COMMAND.REP_UPDATE:
                    //log.Info("REP_UPDATE");
                    ReplyMsgToLIMS(dMProtocol.DeviceID, "", dMProtocol.StatInt, dMProtocol.PowerStatInt, "", dMProtocol.CommandInt);
                    SaveClientStat(dMProtocol.DeviceID, dMProtocol.Stat, dMProtocol.Powerstat);
                    break;

                case COMMAND.REP_SETUP:
                    //log.Info("REP_SETUP");
                    ;                       ReplyMsgToLIMS(dMProtocol.DeviceID, "", dMProtocol.StatInt, dMProtocol.PowerStatInt, "", dMProtocol.CommandInt);
                    SaveClientStat(dMProtocol.DeviceID, dMProtocol.Stat, dMProtocol.Powerstat);
                    break;

                case COMMAND.SCR_BOOKING:
                    if (VerifyClientRegistration(dmclient))
                    {
                        scrServerHandle.HandleServerMessage(dMProtocol.DeviceID, dMProtocol.Command, dmclient, (DMSCRProtocol)dMProtocol);
                    }
                    break;

                case COMMAND.SCR_BOOK_CALENDAR:
                    if (VerifyClientRegistration(dmclient))
                    {
                        scrServerHandle.HandleServerMessage(dMProtocol.DeviceID, dMProtocol.Command, dmclient, (DMSCRProtocol)dMProtocol);
                    }
                    break;

                case COMMAND.SCR_BOOK_DATE:
                    if (VerifyClientRegistration(dmclient))
                    {
                        scrServerHandle.HandleServerMessage(dMProtocol.DeviceID, dMProtocol.Command, dmclient, (DMSCRProtocol)dMProtocol);
                    }
                    break;

                case COMMAND.SCR_BOOK_INFO:
                    if (VerifyClientRegistration(dmclient))
                    {
                        scrServerHandle.HandleServerMessage(dMProtocol.DeviceID, dMProtocol.Command, dmclient, (DMSCRProtocol)dMProtocol);
                    }
                    break;

                //仪器信息
                case COMMAND.SCR_DEV_INFO:
                    if (VerifyClientRegistration(dmclient))
                    {
                        scrServerHandle.HandleServerMessage(dMProtocol.DeviceID, dMProtocol.Command, dmclient, (DMSCRProtocol)dMProtocol);
                    }
                    break;

                case COMMAND.SCR_STATE_BOOK:    //屏幕设备这个也是登录用
                    // start 暂时等对屏幕设备登陆用
                    dmclient.DeviceID  = dMProtocol.DeviceID;
                    dmclient.Stat      = dMProtocol.Stat;
                    dmclient.Powerstat = dMProtocol.Powerstat;
                    AddClient(dMProtocol.DeviceID, dmclient);
                    // end 暂时等对屏幕设备登陆用
                    scrServerHandle.HandleServerMessage(dMProtocol.DeviceID, dMProtocol.Command, dmclient, (DMSCRProtocol)dMProtocol);

                    break;

                case COMMAND.SCR_HELP:
                    if (VerifyClientRegistration(dmclient))
                    {
                        scrServerHandle.HandleServerMessage(dMProtocol.DeviceID, dMProtocol.Command, dmclient, (DMSCRProtocol)dMProtocol);
                    }
                    break;

                case COMMAND.SCR_USERINFO:
                    if (VerifyClientRegistration(dmclient))
                    {
                        scrServerHandle.HandleServerMessage(dMProtocol.DeviceID, dMProtocol.Command, dmclient, (DMSCRProtocol)dMProtocol);
                    }
                    break;
                }
            }
            else
            {
                Console.WriteLine("客户端消息头不合法。");
            }
        }