Beispiel #1
0
            /// <summary>
            /// 修改AP状态(4 Value Change 时调用)
            /// </summary>
            /// <param name="parameterStruct"> inform 消息传过来的参数</param>
            /// <returns></returns>
            private Boolean ChangeApDeviceInfo(XmlParameterStruct parameterStruct)
            {
                StructAlarm[] structAlarm = new StructAlarm[AlarmMaxNum];
                Boolean       isAlarm     = false;

                CheckParameterList(parameterStruct.xmlInform.SN, parameterStruct.xmlInform.ValueChange.valueChangeNode);
                foreach (XmlParameter x in parameterStruct.xmlInform.ValueChange.valueChangeNode)
                {
                    if (GetAlarmStruct(x.name, x.value, ref structAlarm))
                    {
                        isAlarm = true;
                    }
                }

                if (isAlarm)
                {
                    ChangeApDeviceStatus(parameterStruct.xmlInform.SN, structAlarm);
                }

                return(true);
            }
Beispiel #2
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());
                }
            }