Ejemplo n.º 1
0
 /// <summary>
 /// 主动下发校时信息(定时任务)
 /// </summary>
 /// <param name="code"></param>
 public static void SendCheckTimerInfo(NewEnergySession session, string code)
 {
     //68 0C 0C 68 5C 12 12 00 08 09 17 10 12 13 23 28 90 16
     char[]   a      = code.ToCharArray();
     string[] strArr = ExplainUtils.str2StrArr(code);
     byte[]   msg    = new byte[18];
     msg[0] = 0x68;
     msg[1] = 0x0C;
     msg[2] = 0x0C;
     msg[3] = 0x68;
     msg[4] = 0x5C;
     msg[5] = ExplainUtils.strToToHexByte(strArr[0])[0];
     msg[6] = ExplainUtils.strToToHexByte(strArr[1])[0];
     msg[7] = ExplainUtils.strToToHexByte(strArr[2])[0];
     msg[8] = ExplainUtils.strToToHexByte(strArr[3])[0];
     msg[9] = 0x09;
     System.Timers.Timer timer = new System.Timers.Timer();
     timer.Interval = int.Parse(ConfigurationManager.AppSettings["interval"]);
     timer.Enabled  = true;
     timer.Elapsed += (obj, e) => {
         DateTime now     = DateTime.Now;
         string[] timeArr = now.ToString("yy-MM-dd-HH-mm-ss").Split('-');
         msg[10] = ExplainUtils.string2Bcd(timeArr[0])[0];
         msg[11] = ExplainUtils.string2Bcd(timeArr[1])[0];
         msg[12] = ExplainUtils.string2Bcd(timeArr[2])[0];
         msg[13] = ExplainUtils.string2Bcd(timeArr[3])[0];
         msg[14] = ExplainUtils.string2Bcd(timeArr[4])[0];
         msg[15] = ExplainUtils.string2Bcd(timeArr[5])[0];
         int    index  = Array.LastIndexOf(msg, (byte)0x68);
         int    len    = msg.Length - 2 - index;
         byte[] newArr = new byte[len];
         Buffer.BlockCopy(msg, index, newArr, 0, len);
         msg[msg.Length - 2] = ExplainUtils.makeCheckSum(newArr);
         msg[17]             = 0x16;
         String str = BitConverter.ToString(msg).Replace("-", " ");
         //Console.WriteLine("{0} 定时任务 >> 主动下发校时:{1}", DateTime.Now, str);
         logger.Info("<<" + code + ">>timer start,send checking time info to<<" + session.RemoteEndPoint + ">>:" + str);
         session.Send(msg, 0, msg.Length);
     };
     timer.Start();
     if (!timers.ContainsKey(code))
     {
         timers.TryAdd(code, timer);
     }
     else
     {
         timers[code] = timer;
     }
 }
Ejemplo n.º 2
0
        /// <summary>
        /// 下发校时消息
        /// </summary>
        /// <param name="sendMsg"></param>
        /// <param name="session"></param>
        /// <param name="typeIndex"></param>
        /// <param name="index"></param>
        public static void SendCheckTimerInfo(byte[] sendMsg, NewEnergySession session, int typeIndex, int index)
        {
            DateTime now = DateTime.Now;

            string[] timeArr = now.ToString("yy-MM-dd-HH-mm-ss").Split('-');
            sendMsg[typeIndex + 1] = ExplainUtils.string2Bcd(timeArr[0])[0];
            sendMsg[typeIndex + 2] = ExplainUtils.string2Bcd(timeArr[1])[0];
            sendMsg[typeIndex + 3] = ExplainUtils.string2Bcd(timeArr[2])[0];
            sendMsg[typeIndex + 4] = ExplainUtils.string2Bcd(timeArr[3])[0];
            sendMsg[typeIndex + 5] = ExplainUtils.string2Bcd(timeArr[4])[0];
            sendMsg[typeIndex + 6] = ExplainUtils.string2Bcd(timeArr[5])[0];
            int len = sendMsg.Length - 2 - index;

            byte[] newArr = new byte[len];
            Buffer.BlockCopy(sendMsg, index, newArr, 0, len);
            sendMsg[sendMsg.Length - 2] = ExplainUtils.makeCheckSum(newArr);
            String reply = BitConverter.ToString(sendMsg).Replace("-", " ");

            logger.Info("checking time reply<<" + session.RemoteEndPoint + ">>: " + reply);
            Console.WriteLine("checking time reply<<{0}>>: {1}", session.RemoteEndPoint, reply);
            session.Send(sendMsg, 0, sendMsg.Length);//回复客户端
        }
Ejemplo n.º 3
0
        static void protocolServer_NewRequestReceived(NewEnergySession session, NewEnergyRequestInfo requestInfo)
        {
            bool checkTimeDone = false;

            byte[] sendMsg = ExplainUtils.HexSpaceStringToByteArray(requestInfo.Body.all);
            String content = BitConverter.ToString(sendMsg).Replace("-", " ");

            Console.WriteLine("<<{0}>>Received == {1}", session.RemoteEndPoint, content);
            logger.Info("<<" + session.RemoteEndPoint + ">>Received:" + content);
            //68 31 00 31 00 68 C9 12 12 08 00 00 02 70 00 00 01 00 68 16
            int    index          = ExplainUtils.GetSecondIndexFromByteArr(sendMsg, (byte)0x68); //第二个68的索引
            int    indexOfControl = index + 1;                                                   //控制字的索引
            int    typeIndex;                                                                    //帧类型索引
            string code;                                                                         //地址码
            int    codeIndex;                                                                    //地址码索引
            int    len;                                                                          //要校验的长度:有C9控制字则从C9开始,否则从第二个68开始到校验和前一位
            int    checkIndex;                                                                   //校验的起始位

            if (sendMsg[indexOfControl] == 0xC9)
            {
                typeIndex = indexOfControl + 5;
                codeIndex = indexOfControl + 1;
                //有C9控制字的地址码高低位互换
                code = sendMsg[codeIndex + 1].ToString("X2") + sendMsg[codeIndex].ToString("X2")
                       + sendMsg[codeIndex + 3].ToString("X2") + sendMsg[codeIndex + 2].ToString("X2");
                len        = sendMsg.Length - 2 - indexOfControl;
                checkIndex = indexOfControl;
            }
            else
            {
                typeIndex = index + 6;
                codeIndex = index + 2;
                code      = code = sendMsg[codeIndex].ToString("X2") + sendMsg[codeIndex + 1].ToString("X2")
                                   + sendMsg[codeIndex + 2].ToString("X2") + sendMsg[codeIndex + 3].ToString("X2");
                len        = sendMsg.Length - 2 - index;
                checkIndex = index;
            }

            var sessions = MsgHandler.sessions;

            if (!sessions.ContainsKey(code))
            {
                sessions.TryAdd(code, session);
            }
            else
            {
                sessions[code].Close();
                sessions[code] = session;
            }
            byte type = sendMsg[typeIndex]; //帧类型

            if (type == 0x00)               //登录、心跳、失电
            {
                string markCode = sendMsg[sendMsg.Length - 4].ToString("X2") + sendMsg[sendMsg.Length - 3].ToString("X2");
                string markName = "unknown frame";
                if ("0100".Equals(markCode))
                {
                    markName = "login";
                    if (!checkTimeDone)
                    {
                        MsgHandler.SendCheckTimerInfo(session, code);
                    }
                }
                else if ("0400".Equals(markCode))
                {
                    markName = "heartbeat";
                }
                else if ("0010".Equals(markCode))
                {
                    markName = "power-lossing";
                }
                sendMsg[indexOfControl]     = 0x00;
                sendMsg[indexOfControl + 6] = 0x00;
                byte[] newArr = new byte[len];
                Buffer.BlockCopy(sendMsg, checkIndex, newArr, 0, len);
                sendMsg[sendMsg.Length - 2] = ExplainUtils.makeCheckSum(newArr);//计算校验码

                String reply = BitConverter.ToString(sendMsg).Replace("-", " ");
                Console.WriteLine("{0} reply:{1}", markName, reply);
                logger.Info(markName + " reply: " + reply);
                session.Send(sendMsg, 0, sendMsg.Length); //回复客户端
            }
            else if (type == 0x09)                        //校时
            {
                byte controlCode = sendMsg[indexOfControl];
                if (controlCode == 0xDC)//校时成功
                {
                    checkTimeDone = true;
                    if (MsgHandler.timers.ContainsKey(code))
                    {
                        System.Timers.Timer timer = MsgHandler.timers[code];
                        timer.Enabled = false;
                        timer.Stop();
                        MsgHandler.timers.TryRemove(code, out timer);
                    }
                    Console.WriteLine("<<{0}>><<{1}>>check time done,sotpped the timer.", code, session.RemoteEndPoint);
                    logger.Info("<<" + code + ">><<" + session.RemoteEndPoint + ">>check time done,sotpped the timer.");
                }
                else//校时回复
                {
                    MsgHandler.SendCheckTimerInfo(sendMsg, session, typeIndex, checkIndex);
                }
            }
            else if (type == 0x01)  //采集频率回复
            {
                //MsgHandler.SendFrequencyForCollection();//测试
                Console.WriteLine("reply from client<<" + session.RemoteEndPoint + ">> : set collection frequency done.");
                logger.Info("reply from client<<" + session.RemoteEndPoint + ">> : set collection frequency done.");
            }
            else if (type == 0x03)  //初始化成功
            {
                Console.WriteLine("reply from client<<" + session.RemoteEndPoint + ">> : init done.");
                logger.Info("reply from client<<" + session.RemoteEndPoint + ">> : init done.");
            }
            else if (type == 0x04)  //重启成功
            {
                Console.WriteLine("reply from client<<" + session.RemoteEndPoint + ">> : restart done.");
                logger.Info("reply from client<<" + session.RemoteEndPoint + ">> : restart done.");
            }
            else if (type == 0x52) //下发倾角仪回复
            {
                Console.WriteLine("reply from client<<" + session.RemoteEndPoint + ">> : received inclinometer configuration.");
                logger.Info("reply from client<<" + session.RemoteEndPoint + ">> : received inclinometer configuration.");
            }
            else if (type == 0x53)  //取消倾角仪回复
            {
                //同下发,只是帧类型不同
                Console.WriteLine("reply from client<<" + session.RemoteEndPoint + ">> : cancelled inclinometer.");
                logger.Info("reply from client<<" + session.RemoteEndPoint + ">> : cancelled inclinometer.");
            }
            else if (type == 0x39)  //倾角仪数据上报
            {
                MsgHandler.ParseInclinometerMsg(sendMsg);
            }
            else if (type == 0x15)  //倾角数据采集无响应
            {
                //68 0A 0A 68 5B 12 12 00 08 15 02 01 01 D4 DC 16
                //68 0A 0A 68 DB 12 12 00 08 15 02 01 01 D4 5C 16
                byte   controlCode = sendMsg[4];
                byte[] arr         = new byte[] { controlCode, 0x80 };
                sendMsg[4] = ExplainUtils.makeCheckSum(arr);
                byte[] newArr = new byte[len];
                Buffer.BlockCopy(sendMsg, checkIndex, newArr, 0, len);
                sendMsg[14] = ExplainUtils.makeCheckSum(newArr);
                String reply = BitConverter.ToString(sendMsg).Replace("-", " ");
                Console.WriteLine("reply from client<<" + session.RemoteEndPoint + ">> : no response from inclinometer >> reply:" + reply);
                logger.Info("reply from client<<" + session.RemoteEndPoint + ">> : no response from inclinometer >> reply:" + reply);
                session.Send(sendMsg, 0, sendMsg.Length);//回复客户端
            }
        }
Ejemplo n.º 4
0
 static void protocolServer_NewSessionConnected(NewEnergySession session)
 {
     Console.WriteLine("{0} Session:<<{1}>><<{2}>> connected,session count >> {3}", DateTime.Now, session.RemoteEndPoint, session.SessionID, appServer.SessionCount);
     logger.Info("Session:<<" + session.RemoteEndPoint + ">><<" + session.SessionID + ">> connected,session count >> " + appServer.SessionCount);
 }
Ejemplo n.º 5
0
 /// <summary>
 /// 会话关闭事件
 /// </summary>
 /// <param name="session"></param>
 /// <param name="reason"></param>
 static void protocolServer_SessionClosed(NewEnergySession session, CloseReason reason)
 {
     Console.WriteLine("Client <<{0}>><<{1}>> disconnected,Online session >> {2},Reason:{3}", session.RemoteEndPoint, session.SessionID, appServer.SessionCount, reason);
     logger.Warn("Client【" + session.RemoteEndPoint + "】disconnected == Num:" + appServer.SessionCount.ToString() + ",Reason:" + reason.GetType() + " " + reason.ToString());
     session.Close();
 }