using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;

namespace TSDome
{
    partial class AlarmPush : Form
    {
        IntPtr hAlarmRecvServer=IntPtr.Zero;
        private AlarmSDK.PNPC_F_MPI_MON_AS_CALLBACK_AlarmData m_AlarmData;
        public AlarmPush()
        {
            m_AlarmData = new AlarmSDK.PNPC_F_MPI_MON_AS_CALLBACK_AlarmData(NPC_F_MPI_MON_AS_CALLBACK_AlarmData);
            InitializeComponent();
            if(OnAlarmPushDemo()==0)
            {
                 if (hAlarmRecvServer!=IntPtr.Zero)
                {
                    //不需要调用注销函数，断开连接即可 No need to call the logout function, you can disconnect

                    //销毁监控客户端对象 Destroy monitoring client object
                    AlarmSDK.NPC_F_MPI_MON_AS_StopAlarmRecvServer(
                        hAlarmRecvServer		//[IN]客户端句柄 Client handle
                        );
                    hAlarmRecvServer =IntPtr.Zero;
                     MessageBox.Show("NPC_F_MPI_MON_AS_StopAlarmRecvServer OK!\n");
                }
            }
        }
        public void DeleteAlarm()
        {
            if (hAlarmRecvServer != IntPtr.Zero)
            {
                //不需要调用注销函数，断开连接即可 No need to call the logout function, you can disconnect

                //销毁监控客户端对象 Destroy monitoring client object
                AlarmSDK.NPC_F_MPI_MON_AS_StopAlarmRecvServer(
                    hAlarmRecvServer		//[IN]客户端句柄  Client handle
                    );
                hAlarmRecvServer = IntPtr.Zero;
                //MessageBox.Show("NPC_F_MPI_MON_AS_StopAlarmRecvServer OK!\n");
            }
        }
        //报警信息输出回调函数定义 Alarm information output callback function definition
        public void	NPC_F_MPI_MON_AS_CALLBACK_AlarmData( IntPtr in_pUserData,ref AlarmSDK.NPC_S_MPI_MON_PUSH_DEV_ALARM in_pDevAlarm)
        {
            string Time=in_pDevAlarm.tAlarmTime.usYear.ToString()+"."+in_pDevAlarm.tAlarmTime.usMonth.ToString()+"."
                + ((int)in_pDevAlarm.tAlarmTime.ucDay).ToString() + "." + ((int)in_pDevAlarm.tAlarmTime.ucHour).ToString() + "." + ((int)in_pDevAlarm.tAlarmTime.ucMinute).ToString() + "." + ((int)in_pDevAlarm.tAlarmTime.ucSecond).ToString();
            string AlarmType="";
            switch(in_pDevAlarm.iAlarmType)
            {
                case AlarmSDK.NPC_D_MON_ALARM_EVENT_ALL:				//所有报警事件 All alarm events
                    AlarmType="ALL";
                    break;
                case AlarmSDK.NPC_D_MON_ALARM_EVENT_FAULT:				//设备故障报警 Equipment failure alarm
                    AlarmType="FAULT";
                    break;
                case AlarmSDK.NPC_D_MON_ALARM_EVENT_MOTION:				//移动侦测报警 Motion detection alarm
                    AlarmType="MOTION";
                    break;
                case AlarmSDK.NPC_D_MON_ALARM_EVENT_VIDEO_BLIND:				//视频遮挡报警 Video occlusion alarm
                    AlarmType="VIDEO_BLIND";
                    break;
                case AlarmSDK.NPC_D_MON_ALARM_EVENT_VIDEO_LOSS:				//视频丢失报警 Video loss alarm
                    AlarmType="VIDEO_LOSS";
                    break;
                case AlarmSDK.NPC_D_MON_ALARM_EVENT_PROBE:				//探头报警（外部输入报警） Probe alarm (external input alarm)
                    AlarmType="PROBE";
                    break;
                case AlarmSDK.NPC_D_MON_ALARM_EVENT_PIR:				//人体感应报警 Human body induction alarm
                    AlarmType="PIR";
                    break;
                case AlarmSDK.NPC_D_MON_ALARM_EVENT_DOORBELL:				//门铃报警 Doorbell alarm
                    AlarmType="DOORBELL";
                    break;
                case AlarmSDK.NPC_D_MON_ALARM_EVENT_HDD_ERROR:			//硬盘错误 Hard disk error
                    AlarmType="HDD_ERROR";
                    break;
                case AlarmSDK.NPC_D_MON_ALARM_EVENT_HDD_FULL:				//硬盘满 Hard disk full
                    AlarmType="HDD_FULL";
                    break;
                case AlarmSDK.NPC_D_MON_ALARM_EVENT_SMART_IPC_CROSS_LINE:				//越线侦测报警 Cross-line detection alarm
                    AlarmType="SMART_IPC_CROSS_LINE";
                    break;
                case AlarmSDK.NPC_D_MON_ALARM_EVENT_SMART_IPC_AREA_INTRUSION:				//区域入侵报警 Regional intrusion alarm
                    AlarmType = "SMART_IPC_AREA_INTRUSION"; 
                    break;
                case AlarmSDK.NPC_D_MON_ALARM_EVENT_SMART_IPC_AREA_IN:				//区域进入报警 Zone entry alarm
                    AlarmType = "SMART_IPC_AREA_IN";
                    break;
                case AlarmSDK.NPC_D_MON_ALARM_EVENT_SMART_IPC_AREA_OUT:				//区域离开报警 Zone departure alarm
                    AlarmType = "SMART_IPC_AREA_OUT";
                    break;
                case AlarmSDK.NPC_D_MON_ALARM_EVENT_SMART_IPC_OBJECT_FORGET:				//物品遗留报警 Item legacy alarm
                    AlarmType = "SMART_IPC_OBJECT_FORGET";
                    break;
                case AlarmSDK.NPC_D_MON_ALARM_EVENT_SMART_IPC_OBJECT_PICKUP:				//物品拾取报警 Item picking alarm
                    AlarmType = "SMART_IPC_OBJECT_PICKUP";
                    break;
            }
            insertItem(new string(in_pDevAlarm.sDevUmid), in_pDevAlarm.iDevChNo.ToString(),Time,AlarmType);
        }
        private delegate void SetPos(string DevUmid, string DevChNo, string Time, string AlarmType);
        private void insertItem(string DevUmid, string DevChNo, string Time, string AlarmType)
        {
            if (this.InvokeRequired)
            {
                SetPos setpos = new SetPos(insertItem);
                this.Invoke(setpos, new object[] { DevUmid, DevChNo, Time, AlarmType });
            }
            else
            {
                if (FindUMID(DevUmid) < 0)
                {
                    return;
                }
                listViewAlarm.Items.Insert(0, new ListViewItem(new string[] {DevUmid, DevChNo, Time, AlarmType}));
            }
        }
        //报警客户端DEMO Alarm client DEMO
        private int OnAlarmPushDemo()
        {
            hAlarmRecvServer = AlarmSDK.NPC_F_MPI_MON_AS_StartAlarmRecvServer(
                    "123.207.228.156".ToCharArray(),				//[IN]服务器地址 server address
                    8300,							//[IN]服务器端口号，如8300 Server port number
                    "alarm_admin".ToCharArray(),					//[IN]用户ID User ID
                    31,								//[IN]客户端类型，参见NPC_D_MON_CLIENT_TYPE_*定义 Client type
                    "00-11-22-33-44-55".ToCharArray(),			//[IN]客户端ID（或客户端标识，如IMSI等） Client ID
                    "Alarm CMS".ToCharArray(),					//[IN]客户端使用者 Client user
                    "English".ToCharArray(),					//[IN]客户端语言，如：英文：English，简体中文：SimpChinese Client language
                    "100010009".ToCharArray()						//[IN]客户端定制标识 Client custom logo
                    );

                if (hAlarmRecvServer==IntPtr.Zero)
                {
                     MessageBox.Show("NPC_F_MPI_MON_AS_StartAlarmRecvServer fail!\r\n");
                   return 0;
                }

                //MessageBox.Show("NPC_F_MPI_MON_AS_StartAlarmRecvServer success \n");

                //设置报警信息回调 Set alarm information callback
                if (AlarmSDK.NPC_F_MPI_MON_AS_SetAlarmCallback(
                        hAlarmRecvServer,		//[IN]报警消息接收服务句柄器地址 Alarm message receiving service handle address
                        m_AlarmData,						//[IN]回调函数 Callback
                        IntPtr.Zero			//[IN]用户数据 User data
                    )==0)
                {
                     MessageBox.Show("NPC_F_MPI_MON_AS_SetAlarmCallback fail!\r\n");
                    return 0;
                }
                // MessageBox.Show("NPC_F_MPI_MON_AS_SetAlarmCallback success \n");
                while (true)
                {
                    int iState = 0;
                    AlarmSDK.NPC_F_MPI_MON_AS_QuerySrvConnState(hAlarmRecvServer, ref iState);
                    if (iState == 2)
                    {
                        //MessageBox.Show("Server connected!\n");  
                        return 1;
                    }
                }
            return 0;
        }

        public string AssemblyTitle
        {
            get
            {
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyTitleAttribute), false);
                if (attributes.Length > 0)
                {
                    AssemblyTitleAttribute titleAttribute = (AssemblyTitleAttribute)attributes[0];
                    if (titleAttribute.Title != "")
                    {
                        return titleAttribute.Title;
                    }
                }
                return System.IO.Path.GetFileNameWithoutExtension(Assembly.GetExecutingAssembly().CodeBase);
            }
        }

        public string AssemblyVersion
        {
            get
            {
                return Assembly.GetExecutingAssembly().GetName().Version.ToString();
            }
        }

        public string AssemblyDescription
        {
            get
            {
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyDescriptionAttribute), false);
                if (attributes.Length == 0)
                {
                    return "";
                }
                return ((AssemblyDescriptionAttribute)attributes[0]).Description;
            }
        }

        public string AssemblyProduct
        {
            get
            {
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyProductAttribute), false);
                if (attributes.Length == 0)
                {
                    return "";
                }
                return ((AssemblyProductAttribute)attributes[0]).Product;
            }
        }

        public string AssemblyCopyright
        {
            get
            {
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false);
                if (attributes.Length == 0)
                {
                    return "";
                }
                return ((AssemblyCopyrightAttribute)attributes[0]).Copyright;
            }
        }

        public string AssemblyCompany
        {
            get
            {
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCompanyAttribute), false);
                if (attributes.Length == 0)
                {
                    return "";
                }
                return ((AssemblyCompanyAttribute)attributes[0]).Company;
            }
        }
        private int FindUMID(string strUMID)
        {
            int n=-1;
            int ncount = listViewUMID.Items.Count;
            strUMID = strUMID.Trim("\0".ToCharArray()); 
            for(int i=0;i<ncount;i++)
            {
                if(strUMID==listViewUMID.Items[i].Text)
                {
                    n = i;
                    break;
                }
            }
            return n;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if(textBoxUMID.Text!="")
            {
                if (FindUMID(textBoxUMID.Text) == -1)
                {
                    listViewUMID.Items.Add(new ListViewItem(new string[] { textBoxUMID.Text }));
                }
            }
        }

        private void btnDec_Click(object sender, EventArgs e)
        {
            if (textBoxUMID.Text != "")
            {
                int nRow=FindUMID(textBoxUMID.Text);
                if (nRow!= -1)
                {
                    listViewUMID.Items.RemoveAt(nRow);
                }
            } 
        }

        protected override void OnFormClosing(FormClosingEventArgs e) 
        { 
            this.Hide(); 
            e.Cancel = true; 
        }

        private void listViewUMID_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}
