Exemplo n.º 1
0
            /// <summary>
            /// 收到POST消息处理
            /// </summary>
            /// <param name="request">request消息结构</param>
            /// <param name="response">response消息结构</param>
            public override void onPost(HttpListenerRequest request, HttpListenerResponse response)
            {
                Console.WriteLine("POST:" + request.Url);

                String ip   = request.RemoteEndPoint.Address.ToString();
                int    port = request.RemoteEndPoint.Port;

                Log.WriteDebug("收到POST消息:IP=" + ip + ";Port=" + port);

                try
                {
                    //空消息
                    if (!request.HasEntityBody)
                    {
                        Log.WriteDebug("收到POST消息,消息内容为空!");
                        if (request.Cookies.Count > 0)
                        {
                            foreach (Cookie cookie in request.Cookies)
                            {
                                response.AppendCookie(cookie);
                            }
                        }

                        byte[] xml = RecvEmptyMsgHandle(ip, port);
                        if (xml.Length <= 0)
                        {
                            response.ContentLength64 = 0; //回复空post
                            Log.WriteDebug("收到POST消息,消息内容为空!回复空消息");
                        }
                        else
                        {
                            response.OutputStream.Write(xml, 0, xml.Length);
                            Log.WriteDebug("收到POST消息,消息内容为空!回复消息:\n" +
                                           System.Text.Encoding.Default.GetString(xml));
                        }
                        //MessageBox.Show("yyyy:" + ip + ":" + port + "  " + getSnForSnIpPortNode(ip,port));
                    }
                    else
                    {
                        //接收POST参数
                        Stream stream = request.InputStream;
                        System.IO.StreamReader reader = new System.IO.StreamReader(stream, Encoding.UTF8);
                        //String body = reader.ReadToEnd();
                        XmlParameterStruct parameterStruct = new XmlParameterStruct();

                        string msg = reader.ReadToEnd();
                        Log.WriteDebug("收到AP消息。消息内容:\n" + msg);
                        parameterStruct = new XmlHandle().HandleRecvApMsg(msg);

                        Log.WriteDebug(string.Format("收到[{0}]POST消息,消息Method={1}!",
                                                     parameterStruct.xmlInform.SN, parameterStruct.Method));

                        if (request.Cookies.Count > 0)
                        {
                            foreach (Cookie cookie in request.Cookies)
                            {
                                response.AppendCookie(cookie);
                            }
                        }
                        else
                        {
                            Random rand   = new Random(DateTime.Now.Millisecond);
                            Cookie cookie = new Cookie("JSESSIONID", string.Format("{0}{1}{2}{3}",
                                                                                   rand.Next().ToString(), rand.Next().ToString(),
                                                                                   rand.Next().ToString(), rand.Next().ToString()));
                            cookie.Path = "/";
                            response.AppendCookie(cookie);
                            //response.AppendCookie(new Cookie("sn", parameterStruct.xmlInform.SN));
                        }

                        if (String.Compare(parameterStruct.Method, RPCMethod.Inform, true) == 0)
                        {
                            ApConnHmsInfo connInfo = new ApConnHmsInfo(parameterStruct.xmlInform.SN);
                            connInfo.Ip        = request.RemoteEndPoint.Address.ToString();
                            connInfo.Port      = request.RemoteEndPoint.Port;
                            connInfo.EventCode = parameterStruct.xmlInform.EventCode;

                            Log.WriteDebug("收到Inform消息,SN = " + connInfo.Sn);
                            if (!string.IsNullOrEmpty(connInfo.Sn))
                            {
                                if (myDB.deviceinfo_record_exist(connInfo.Sn) != 1)
                                {
                                    Log.WriteError(string.Format("设备({0})未开户,不回复消息。", connInfo.Sn));
                                    return;
                                }
                            }
                            else
                            {
                                Log.WriteError("未读到设备SN号。");
                                return;
                            }

                            foreach (string eventcode in connInfo.EventCode)
                            {
                                if (string.IsNullOrEmpty(eventcode))
                                {
                                    break;
                                }
                                Log.WriteDebug("消息EventCode = " + eventcode);
                            }
                            GlobalParameter.apConnHmsList.add(connInfo);
                            Log.WriteDebug("设置" + parameterStruct.xmlInform.SN + "为上线状态!");
                            if (!myDB.SetconnHSToOnLine(parameterStruct.xmlInform.SN))
                            {
                                Log.WriteError("设置" + parameterStruct.xmlInform.SN + "为上线状态失败!");
                            }
                            if (XmlHandle.GetEventInList(connInfo.EventCode, InformEventCode.BOOT))
                            {
                                Log.WriteDebug("设置AP(" + parameterStruct.xmlInform.SN + ")的告警为历史告警。");
                                int ret = myDB.alarminfo_record_set_2_history(parameterStruct.xmlInform.SN);
                                if (ret != 0)
                                {
                                    Log.WriteError(string.Format("更新AP的告警为历史告警出错,出错原因:({0}){1}。"
                                                                 , ret, myDB.get_rtv_str(ret)));
                                }

                                Log.WriteDebug("更新AP(" + parameterStruct.xmlInform.SN +
                                               ")反向连接地址为:" + parameterStruct.xmlInform.ConnectionRequestURL);
                                int re = myDB.apconninfo_record_update(parameterStruct.xmlInform.SN,
                                                                       parameterStruct.xmlInform.ConnectionRequestURL,
                                                                       GlobalParameter.ConnectionRequestUsername,
                                                                       GlobalParameter.ConnectionRequestPassWd);
                                if (re != 0)
                                {
                                    Log.WriteError(string.Format("更新AP反向连接地址出错,出错原因:({0}){1}。"
                                                                 , ret, myDB.get_rtv_str(ret)));
                                }

                                Log.WriteDebug("更新AP(" + parameterStruct.xmlInform.SN +
                                               ")的IP地址为:" + connInfo.Ip);
                                strDevice deviceInfo = new strDevice();
                                deviceInfo.ipAddr = connInfo.Ip;

                                int errCode = myDB.deviceinfo_record_update(parameterStruct.xmlInform.SN, deviceInfo);
                                if (0 != errCode)
                                {
                                    Log.WriteError(string.Format("更新AP的IP地址出错,出错原因:({0}){1}。"
                                                                 , errCode, myDB.get_rtv_str(errCode)));
                                }
                            }
                            if (XmlHandle.GetEventInList(connInfo.EventCode, InformEventCode.M_Reboot))
                            {
                                string str = String.Format("修改SN({0}),任务类型({1}),任务状态({2})!",
                                                           connInfo.Sn, TaskType.RebootTask, TaskStatus.ReponseOk);
                                Log.WriteDebug(str);
                                if (!myDB.SetApTaskStatusBySN(connInfo.Sn, TaskType.RebootTask,
                                                              TaskStatus.ReponseOk))
                                {
                                    Log.WriteError(str);
                                }
                            }
                            if (XmlHandle.GetEventInList(connInfo.EventCode, InformEventCode.VALUE_CHANGE))
                            {
                                ChangeApDeviceInfo(parameterStruct);
                            }
                            //if (XmlHandle.GetEventInList(connInfo.EventCode, InformEventCode.PERIODIC))
                            //{
                            //    CheckParameterList(connInfo.Sn, parameterStruct.parameterNode);
                            //}
                            byte[] res = XmlHandle.CreateInformResponseXmlFile();// Encoding.UTF8.GetBytes("OK");
                            response.OutputStream.Write(res, 0, res.Length);
                            Log.WriteDebug("收到Inform消息,回复消息:\n" + System.Text.Encoding.Default.GetString(res));
                        }
                        else
                        {
                            ApConnHmsInfo connInfo = GlobalParameter.apConnHmsList.getSnForconnList(ip, port);
                            if (connInfo != null)
                            {
                                if (!string.IsNullOrEmpty(connInfo.Sn))
                                {
                                    if (myDB.deviceinfo_record_exist(connInfo.Sn) != 1)
                                    {
                                        Log.WriteError(string.Format("设备({0})未开户,不回复消息。", connInfo.Sn));
                                        return;
                                    }
                                }
                                else
                                {
                                    Log.WriteError("未读到设备SN号。");
                                    return;
                                }

                                if (String.Compare(parameterStruct.Method, RPCMethod.GetParameterValuesResponse, true) == 0)
                                {
                                    //去掉周期性查询
                                    if (String.Compare(parameterStruct.ID, PeriodicGetValue, true) != 0)
                                    {
                                        string str = String.Format("修改SN({0}),任务ID({1}),任务状态({2})!",
                                                                   connInfo.Sn, parameterStruct.ID, TaskStatus.ReponseOk);
                                        Log.WriteDebug(str);
                                        if (!myDB.SetStatusBySnId(parameterStruct.ID, connInfo.Sn,
                                                                  TaskStatus.ReponseOk))
                                        {
                                            Log.WriteError(str);
                                        }
                                    }
                                    Log.WriteDebug("更改数据库中AP的状态或参数值...");
                                    CheckParameterList(connInfo.Sn, parameterStruct.parameterNode);

                                    SaveParameterList(connInfo.Sn, parameterStruct.ID, parameterStruct.parameterNode);
                                }
                                else if (String.Compare(parameterStruct.Method, RPCMethod.SetParameterValuesResponse, true) == 0)
                                {
                                    //去掉1Boot的回复。
                                    if (String.Compare(parameterStruct.ID, XmlHandle.SetParameterValueFor1Boot, true) != 0)
                                    {
                                        string str = String.Format("修改SN({0}),任务ID({1}),任务状态({2})!",
                                                                   connInfo.Sn, parameterStruct.ID, TaskStatus.ReponseOk);
                                        Log.WriteDebug(str);
                                        if (!myDB.SetStatusBySnId(parameterStruct.ID, connInfo.Sn,
                                                                  TaskStatus.ReponseOk))
                                        {
                                            Log.WriteError(str);
                                        }

                                        //周期日志上传回复,保存Key
                                        string taskType = parameterStruct.ID.Substring(0, TaskType.GetLogTask.ToString().Length);
                                        if (taskType.Equals(TaskType.GetLogTask.ToString()))
                                        {
                                            if (GlobalParameter.AutonomousTransferKey.ContainsKey(connInfo.Sn))
                                            {
                                                GlobalParameter.AutonomousTransferKey.Remove(connInfo.Sn);
                                            }
                                            GlobalParameter.AutonomousTransferKey.Add(connInfo.Sn, parameterStruct.ID);
                                        }
                                    }
                                }
                                else if (String.Compare(parameterStruct.Method, RPCMethod.TransferComplete, true) == 0)
                                {
                                    TaskStatus status = TaskStatus.TaskNull;
                                    if (parameterStruct.transferComplete.FaultCode == 0)  //AP返回成功
                                    {
                                        status = TaskStatus.ReponseOk;
                                        //设置LOG上传成功标志
                                        string taskType = parameterStruct.transferComplete.CommandKey.Substring(0, TaskType.GetLogTask.ToString().Length);
                                        if (taskType.Equals(TaskType.GetLogTask.ToString()))
                                        {
                                            string tmpstr = String.Format("修改SN({0}),任务ID({1}),AP LOG表状态为可用!",
                                                                          connInfo.Sn, parameterStruct.transferComplete.CommandKey);
                                            Log.WriteDebug(tmpstr);
                                            if (0 != myDB.aploginfo_record_update(
                                                    connInfo.Sn, parameterStruct.transferComplete.CommandKey, 1))
                                            {
                                                Log.WriteError(tmpstr);
                                            }
                                        }
                                    }
                                    else
                                    {
                                        status = TaskStatus.ReponseFail;
                                    }
                                    string str = String.Format("修改SN({0}),任务ID({1}),任务状态({2})!",
                                                               connInfo.Sn, parameterStruct.transferComplete.CommandKey, status);
                                    Log.WriteDebug(str);
                                    if (!myDB.SetStatusBySnId(parameterStruct.transferComplete.CommandKey,
                                                              connInfo.Sn, status))
                                    {
                                        Log.WriteError(str);
                                    }

                                    byte[] res = XmlHandle.CreateTransferCompleteResponseXmlFile();// Encoding.UTF8.GetBytes("OK");
                                    response.OutputStream.Write(res, 0, res.Length);
                                    Log.WriteDebug("收到TransferComplete消息,回复消息:\n" + System.Text.Encoding.Default.GetString(res));
                                    return;
                                }
                                else if (String.Compare(parameterStruct.Method, RPCMethod.AutonomousTransferComplete, true) == 0)
                                {
                                    string CommandKey = "";
                                    if (String.IsNullOrEmpty(parameterStruct.autoTransferComplete.CommandKey))
                                    {
                                        if (GlobalParameter.AutonomousTransferKey.ContainsKey(connInfo.Sn))
                                        {
                                            CommandKey = GlobalParameter.AutonomousTransferKey[connInfo.Sn];
                                        }
                                    }

                                    if (!string.IsNullOrEmpty(CommandKey) &&
                                        parameterStruct.autoTransferComplete.FaultCode == 0)  //AP返回成功
                                    {
                                        //设置LOG上传成功标志
                                        string taskType = CommandKey.Substring(0, TaskType.GetLogTask.ToString().Length);
                                        if (taskType.Equals(TaskType.GetLogTask.ToString()))
                                        {
                                            int    ret    = 0;
                                            string tmpstr = String.Format("修改SN({0}),任务ID({1}),AP LOG表状态为可用!",
                                                                          connInfo.Sn, CommandKey);
                                            Log.WriteDebug(tmpstr);

                                            //周期上传时,只保存最后一次的log
                                            if (1 == myDB.aploginfo_record_exist(connInfo.Sn, CommandKey))
                                            {
                                                ret = myDB.aploginfo_record_delete(connInfo.Sn, CommandKey);
                                                if (0 != ret)
                                                {
                                                    Log.WriteError(tmpstr + "出错。无法删除已有记录。错误原因:" + myDB.get_rtv_str(ret));
                                                }
                                            }

                                            if (ret == 0)
                                            {
                                                ret = myDB.aploginfo_record_insert(
                                                    connInfo.Sn, CommandKey, DateTime.Now.ToString(),
                                                    parameterStruct.autoTransferComplete.TargetFileName,
                                                    Convert.ToUInt32(parameterStruct.autoTransferComplete.FileSize), "None");
                                                if (0 != ret)
                                                {
                                                    Log.WriteError(tmpstr + "出错。错误原因:" + myDB.get_rtv_str(ret));
                                                }
                                                else
                                                {
                                                    ret = myDB.aploginfo_record_update(connInfo.Sn, CommandKey, 1);
                                                    if (0 != ret)
                                                    {
                                                        Log.WriteError(tmpstr + "出错。错误原因:" + myDB.get_rtv_str(ret));
                                                    }
                                                }
                                            }
                                        }
                                    }

                                    byte[] res = XmlHandle.CreateAutonomousTransferCompleteResponseXmlFile();// Encoding.UTF8.GetBytes("OK");
                                    response.OutputStream.Write(res, 0, res.Length);
                                    Log.WriteDebug("收到AutonomousTransferComplete消息,回复消息:\n" + System.Text.Encoding.Default.GetString(res));
                                    return;
                                }
                                else if (String.Compare(parameterStruct.Method, RPCMethod.RebootResponse, true) == 0)
                                {
                                    string str = String.Format("修改SN({0}),任务ID({1}),任务状态({2})!",
                                                               connInfo.Sn, parameterStruct.ID, TaskStatus.SendTask);
                                    Log.WriteDebug(str);
                                    if (!myDB.SetStatusBySnId(parameterStruct.ID, connInfo.Sn,
                                                              TaskStatus.SendTask))
                                    {
                                        Log.WriteError(str);
                                    }
                                }
                                else if (String.Compare(parameterStruct.Method, RPCMethod.Fault, true) == 0)
                                {
                                    string str = String.Format("修改SN({0}),任务ID({1}),任务状态({2})!",
                                                               connInfo.Sn, parameterStruct.ID, TaskStatus.ReponseFail);
                                    Log.WriteDebug(str);
                                    if (!myDB.SetStatusBySnId(parameterStruct.ID, connInfo.Sn,
                                                              TaskStatus.ReponseFail))
                                    {
                                        Log.WriteError(str);
                                    }
                                }
                            }

                            //如果有其它任务,下发其它任务,否则回复空
                            byte[] xml = NextSendMsgHandle(ip, port);
                            if (xml.Length <= 0)
                            {
                                response.ContentLength64 = 0; //回复空post
                            }
                            else
                            {
                                response.OutputStream.Write(xml, 0, xml.Length);
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    Log.WriteError("处理收到的消息(" + ip + ":" + port + ")出错。");
                    Log.WriteError("出错原因:" + e.Message.ToString());
                }
            }
Exemplo n.º 2
0
            /// <summary>
            /// 收到空消息处理过程
            /// </summary>
            /// <param name="ip">发送消息AP的IP</param>
            /// <param name="port">发送消息AP的端口</param>
            /// <returns>如果有下条任务,返回下条任务XML,否则返回空</returns>
            private byte[] RecvEmptyMsgHandle(String ip, int port)
            {
                byte[] xml;

                ApConnHmsInfo connInfo = GlobalParameter.apConnHmsList.getSnForconnList(ip, port);

                if (connInfo == null)
                {
                    Log.WriteError("上线列表中找不到该AP,回复空消息。");
                    xml = Encoding.UTF8.GetBytes(""); //回复空post
                }
                else
                {
                    if (XmlHandle.GetEventInList(connInfo.EventCode, InformEventCode.BOOT))
                    {
                        List <XmlParameter>   inList = new List <XmlParameter>();
                        System.Data.DataTable dt     = new System.Data.DataTable();
                        if (0 == myDB.inform_1boot_record_entity_get(ref dt))
                        {
                            for (int i = 0; i < dt.Rows.Count; i++)
                            {
                                XmlParameter xmlParameter = new XmlParameter(dt.Rows[i][0].ToString(),
                                                                             dt.Rows[i][1].ToString());
                                inList.Add(xmlParameter);
                            }
                        }

                        xml = XmlHandle.Create_1BOOT_InformResponse(inList);
                        //xml = XmlHandle.CreateRebootXmlFile("reboot1233");
                    }
                    else if (XmlHandle.GetEventInList(connInfo.EventCode, InformEventCode.PERIODIC))
                    {
                        //发送获取AP状态的节点
                        String[] nameList = new string[4];
                        nameList[0] = "Services.FAPService.1.CellConfig.LTE.RAN.RF.X_VENDOR_PHY_CELL_ID";
                        nameList[1] = "Services.FAPService.1.CellConfig.LTE.RAN.RF.EARFCNDL";
                        nameList[2] = "DeviceInfo.X_VENDOR_ENODEB_STATUS.X_VENDOR_CELL_STATUS";
                        nameList[3] = "Services.FAPService.1.CellConfig.LTE.EPC.TAC";
                        xml         = XmlHandle.CreateGetParameterValuesXmlFile(PeriodicGetValue, nameList);
                    }
                    else
                    {
                        TaskType taskType = TaskType.TaskNull;
                        string   taskId   = "";
                        Log.WriteDebug("获取SN(" + connInfo.Sn + ")的任务。");
                        string upgradeTimer = "";
                        xml = myDB.GetTaskBySN(ref taskType, ref taskId, ref upgradeTimer, connInfo.Sn);
                        //定时升级任务
                        if (taskType == TaskType.UpgradTask)
                        {
                            DateTime nowTime = DateTime.Now;
                            DateTime runTime = Convert.ToDateTime(upgradeTimer);
                            if (DateTime.Compare(nowTime, runTime) < 0)
                            {
                                Log.WriteError(string.Format("设备({0})升级任务[{1}]的字时间为{2},目前未到时间。暂不升级。",
                                                             connInfo.Sn, taskId, upgradeTimer));
                                return(Encoding.UTF8.GetBytes("")); //回复空post
                            }
                        }

                        if ((xml != null) && (taskType != TaskType.TaskNull) && (false == string.IsNullOrEmpty(taskId)))
                        {
                            if (!myDB.SetApTaskStatusBySN(connInfo.Sn, taskType, TaskStatus.SendTask))
                            {
                                string str = String.Format("修改SN({0}),任务类型({1}),任务状态({2})!",
                                                           connInfo.Sn, taskType, TaskStatus.SendTask);
                                Log.WriteError(str);
                            }
                        }
                        else
                        {
                            xml = Encoding.UTF8.GetBytes(""); //回复空post
                        }
                    }
                }

                return(xml);
            }