using NVRCsharpDemo;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using Common;
using System.Configuration;
using com.hnshituo.core.webapp.vo;
using System.Resources;
using System.Security.Cryptography;
using TrainVideoDataDispose.Properties;

namespace TrainVideoDataDispose
{
    public partial class text : Form
    {
        public int m_lUserID = -1;
        public int m_lGetFaceParamCfgHandle = -1;

        private CHCNetSDK.REALDATACALLBACK RealData = null;
        public CHCNetSDK.NET_DVR_DEVICEINFO_V30 DeviceInfo;
        public CHCNetSDK.NET_DVR_DEVICEINFO_V40 tDeviceInfo;
        public CHCNetSDK.NET_DVR_IPPARACFG_V40 m_struIpParaCfgV40;
        public CHCNetSDK.NET_DVR_STREAM_MODE m_struStreamMode;
        public CHCNetSDK.NET_DVR_IPCHANINFO m_struChanInfo;
        public CHCNetSDK.NET_DVR_PU_STREAM_URL m_struStreamURL;
        public CHCNetSDK.NET_DVR_IPCHANINFO_V40 m_struChanInfoV40; 
        public CHCNetSDK.NET_DVR_USER_LOGIN_INFO pLoginInfo;
        public CHCNetSDK.LOGINRESULTCALLBACK LoginCallBack; 
        public CHCNetSDK.RemoteConfigCallback remoteConfigCallback; 
        //private PlayCtrl.DECCBFUN m_fDisplayFun = null;
        public delegate void MyDebugInfo(string str);
        private MetetBaseRailwayAiweightService metetBaseRailwayAiweightService = new MetetBaseRailwayAiweightService();

        private Label labelLogin;
        public text()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                //string strUrl = "http://10.111.67.23/ISAPI/System/deviceInfo";
                string strUrl = "http://10.111.67.23/ISAPI/Traffic/ContentMgmt/dataOperation";
                WebClient client = new WebClient();

                int interval = int.Parse(ConfigurationManager.AppSettings["interval"].ToString());

                string startTime = DateTime.Now.ToString("yyyy-MM-dd") + "Y" + DateTime.Now.ToString("HH:mm:ss") + "Z";
                string endTime = DateTime.Now.AddMinutes(interval).ToString("yyyy-MM-dd") + "Y" + DateTime.Now.AddMinutes(interval).ToString("HH:mm:ss") + "Z";
                string searchID =  System.Guid.NewGuid().ToString();
                // 设置用户名和密码
                client.Credentials = new NetworkCredential("admin", "Admin12345");
                string requestXmls = string.Format(@"<?xml version:'1.0' encoding='utf-8'?>
                                        <DataOperation>
                                            <operationType>search</operationType>
                                            <searchCond>
                                                <searchID>{0}</searchID>
                                                <timeSpanList>
                                                    <timeSpan>
                                                        <startTime>{1}</startTime>
                                                        <endTime>{2}</endTime>
                                                    </timeSpan>
                                                </timeSpanList>
                                                <criteria>
                                                    <dataType>0</dataType>
                                                    <channel/>
                                                    <plateType/>
                                                    <plateColor/>
                                                    <direction/>
                                                    <plate/>
                                                    <speedMin/>
                                                    <speedMax/>
                                                    <vehicleType/>
                                                    <vehicleColor/>
                                                    <laneNo/>
                                                    <surveilType>0</surveilType>
                                                    <romoteHost/>
                                                    <analysised>true</analysised>
                                                    <sendFlag/>
                                                </criteria>
                                                <searchResultPosition>0</searchResultPosition>
                                                <maxResults>999</maxResults>
                                            </searchCond>
                                        </DataOperation>", searchID,startTime,endTime);
                byte[] sendData = Encoding.UTF8.GetBytes(requestXmls);
                byte[] responseData = client.UploadData(strUrl,"POST", sendData);
                string strResponseData = Encoding.UTF8.GetString(responseData);

                XmlToDataSet(strResponseData);

                XmlDocument xmlDoc = new XmlDocument();//初始化一个实例
                xmlDoc.LoadXml(strResponseData);//读取文件

                List<MetetBaseRailwayAiweight> metetBaseRailwayAiweights = new List<MetetBaseRailwayAiweight>();
                XmlNodeList s = xmlDoc.DocumentElement.GetElementsByTagName("matchElement");
                foreach (XmlElement node in s)
                {
                    MetetBaseRailwayAiweight metetBaseRailwayAiweight = new MetetBaseRailwayAiweight();
                    int stratIndex = node.GetElementsByTagName("plate")[0].InnerText.Length - 7;
                    metetBaseRailwayAiweight.railwayNo = node.GetElementsByTagName("plate")[0].InnerText.Substring(stratIndex, 7);
                    metetBaseRailwayAiweight.railwayType = node.GetElementsByTagName("plate")[0].InnerText.Substring(0, stratIndex);
                    metetBaseRailwayAiweight.meterWeight = node.GetElementsByTagName("selfWeight")[0].InnerText;
                    metetBaseRailwayAiweight.railwaySpeed = node.GetElementsByTagName("speed")[0].InnerText;
                    metetBaseRailwayAiweights.Add(metetBaseRailwayAiweight);
                }
                if (metetBaseRailwayAiweights.Count > 0)
                {
                    RESTfulResult<List<MetetBaseRailwayAiweight>> result  = metetBaseRailwayAiweightService.add(metetBaseRailwayAiweights);
                    if (result.Succeed)
                    {
                        // 输出接收的消息
                        this.textinfo.Text = "成功";
                    }
                }
            }
            catch (Exception ex)
            {
                this.textinfo.Text = ex.Message;
            }

            return;
        }

        #region Xml To DataSet
        public static DataSet XmlToDataSet(string xmlString)
        {
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.LoadXml(xmlString);
            StringReader stream = null;
            XmlTextReader reader = null;
            try
            {
                DataSet xmlDS = new DataSet();
                stream = new StringReader(xmldoc.InnerXml);
                reader = new XmlTextReader(stream);
                xmlDS.ReadXml(reader);
                reader.Close();
                return xmlDS;
            }
            catch (System.Exception ex)
            {
                reader.Close();
                throw ex;
            }
        }
        #endregion

    }
}
