using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace TSDome
{
    class AlarmSDK
    {
        //报警事件定义 Alarm event definition
        public const int NPC_D_MON_ALARM_EVENT_ALL = 0;				//所有报警事件 All alarm eventsAll alarm events
        public const int NPC_D_MON_ALARM_EVENT_FAULT = 1;				//设备故障报警 Equipment failure alarm
        public const int NPC_D_MON_ALARM_EVENT_MOTION = 2;				//移动侦测报警 Motion detection alarm
        public const int NPC_D_MON_ALARM_EVENT_VIDEO_BLIND = 3;				//视频遮挡报警 Video occlusion alarm
        public const int NPC_D_MON_ALARM_EVENT_VIDEO_LOSS = 4;				//视频丢失报警 Video loss alarm
        public const int NPC_D_MON_ALARM_EVENT_PROBE = 5;				//探头报警（外部输入报警） Probe alarm (external input alarm)
        public const int NPC_D_MON_ALARM_EVENT_PIR = 6;				//人体感应报警 Human body induction alarm
        public const int NPC_D_MON_ALARM_EVENT_DOORBELL = 7;				//门铃报警 Doorbell alarm
        public const int NPC_D_MON_ALARM_EVENT_HDD_ERROR = 8;			//硬盘错误 Hard disk error
        public const int NPC_D_MON_ALARM_EVENT_HDD_FULL = 9;				//硬盘满 Hard disk full
        public const int NPC_D_MON_ALARM_EVENT_SMART_IPC_CROSS_LINE = 10;				//越线侦测报警 Cross-line detection alarm
        public const int NPC_D_MON_ALARM_EVENT_SMART_IPC_AREA_INTRUSION = 11;				//区域入侵报警 Regional intrusion alarm
        public const int NPC_D_MON_ALARM_EVENT_SMART_IPC_AREA_IN = 12;				//区域进入报警 Zone entry alarm
        public const int NPC_D_MON_ALARM_EVENT_SMART_IPC_AREA_OUT = 13;				//区域离开报警 Zone departure alarm
        public const int NPC_D_MON_ALARM_EVENT_SMART_IPC_OBJECT_FORGET = 14;				//物品遗留报警 Item legacy alarm
        public const int NPC_D_MON_ALARM_EVENT_SMART_IPC_OBJECT_PICKUP = 15;				//物品拾取报警 Item picking alarm

        public const int NPC_D_MON_DEVID_BUFSIZE = 32;				//设备ID缓冲区大小（如摄像机ID或DVRID）Device ID buffer size (such as camera ID or DVRID)

       public struct NPC_S_TD_DATE_TIME
        {
	        public ushort 									usYear;						//年 year
	        public ushort 									usMonth;					//月 month
	        public byte									ucDay;						//日 day
	        public byte									ucHour;						//时 hour
	        public byte									ucMinute;					//分minute
	        public byte									ucSecond;					//秒 second
	        public uint										dwMinsecond;				//毫秒 ms
        }

        public struct NPC_S_MPI_MON_PUSH_DEV_ALARM
        {
            [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst=32, ArraySubType = UnmanagedType.I1)]
            public char[] sAlarmId;									//报警ID，设备端唯一，如果为空，则设备端为老设备 Alarm ID, unique on the device side. If it is empty, the device is the old device.
	        [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst=32, ArraySubType = UnmanagedType.I1)]
            public char[] sDevUmid;									//设备UMID，如果为空，则表示是其它方式推送的报警，需要参考sDevId参数 The device UMID, if it is empty, indicates that it is an alarm pushed by other methods. You need to refer to the sDevId parameter.
            public int iDevChNo;										//设备通道号 Device channel number
            [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst=NPC_D_MON_DEVID_BUFSIZE, ArraySubType = UnmanagedType.I1)]
            public char[] sDevId;				//设备ID，即摄像机ID Device ID, ie camera ID
            [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst=128, ArraySubType = UnmanagedType.I1)]
            public char[] sDevName;									//设备名称，即镜头名称 Device name, the name of the lens
            public int iAlarmType;										//报警类型，参见NPC_D_MON_ALARM_TYPE_*定义 Alarm type, see NPC_D_MON_ALARM_TYPE_* definition
            public NPC_S_TD_DATE_TIME tAlarmTime;										//报警时间 Alarm time
            public int iAlarmState;									//报警状态，0：停止（离线），1：开始（在线），2：设备故障，3：硬盘满 Alarm status, 0: stop (offline), 1: start (online), 2: device failure, 3: hard drive full
	        [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst=128, ArraySubType = UnmanagedType.I1)]
            public char[] sAlarmInfo;								//报警信息 Alarm information
            public uint dwParam1;										//扩展参数1 Extended parameter1
            public uint dwParam2;										//扩展参数2 Extended parameter2

        };



        /*
//-------------------------------------------------------------------------------------------
        //启动报警消息接收服务 Start alarm message receiving service
        [DllImport(@"NpcMpiMonTsp.dll")]
        public static extern IntPtr NPC_F_MPI_MON_AS_StartAlarmRecvServer(
            char[] in_pServerAddr,				//[IN]服务器地址 server address
            ushort in_usServerPort,			//[IN]服务器端口号，如8300 Server port number, such as 8300
            char[] in_pUserId,					//[IN]用户ID User ID
            int in_iClientType,				//[IN]客户端类型，参见NPC_D_MON_CLIENT_TYPE_*定义 Client type, see NPC_D_MON_CLIENT_TYPE_* definition
            char[] in_pClientId,				//[IN]客户端ID（或客户端标识，如IMSI等）Client ID (or client ID, such as IMSI, etc.)
            char[] in_pClientOwner,			//[IN]客户端使用者 Client user
            char[] in_pClientLanguage,			//[IN]客户端语言，如：英文：English，简体中文：SimpChinese Client language, such as: English: English, Simplified Chinese: SimpChinese
            char[] in_pClientCustomFlag		//[IN]客户端定制标识 Client custom logo
	    );

        //停止报警消息接收服务 Stop alarm message receiving service
        [DllImport(@"NpcMpiMonTsp.dll")]
        public static extern void	NPC_F_MPI_MON_AS_StopAlarmRecvServer(
        IntPtr in_hAlarmRecvServer			//[IN]报警消息接收服务句柄 Alarm message receiving service handle
	    );

        //设置报警信息回调 Set alarm information callback
        //报警信息输出回调函数定义 Alarm information output callback function definition
        [UnmanagedFunctionPointer(CallingConvention.Cdecl)] 
        public delegate void PNPC_F_MPI_MON_AS_CALLBACK_AlarmData( IntPtr in_pUserData,ref NPC_S_MPI_MON_PUSH_DEV_ALARM in_pDevAlarm);
        [DllImport(@"NpcMpiMonTsp.dll")]
        public static extern uint NPC_F_MPI_MON_AS_SetAlarmCallback(
        IntPtr in_hAlarmRecvServer,		//[IN]报警消息接收服务句柄器地址 Alarm message receiving service handle address
        PNPC_F_MPI_MON_AS_CALLBACK_AlarmData in_pAlarmCallback,			//[IN]回调函数 Callback
        IntPtr in_pUserData				//[IN]用户数据	User data
	    );
        */

        //查询服务器连接状态 Query server connection status
        [DllImport(@"NpcMpiMonTsp.dll")]
        public static extern uint	NPC_F_MPI_MON_AS_QuerySrvConnState(
        IntPtr in_hAlarmRecvServer,		//[IN]Alarm message receiving service handle
        ref int out_pSrvConnState			//[OUT]服务器连接状态，0：未连接，1：正在连接，2：已连接 Report server connection status, 0: not connected, 1: connected, 2: connected
	    );

        //查询最优服务器(0=成功，其他失败) Query the optimal server (0=success, other failures)
        [DllImport(@"NpcMpiMonTsp.dll")]
        public static extern int	NPC_F_MPI_MON_DNP_GetBestSrv(
        char[] in_pServerAddr,				//[IN]服务器IP地址或域名 Server IP address or domain name
        ushort in_usServerPort,			//[IN]服务器端口号 Server port number
        ref char[] out_pBestSrvIp,				//[OUT]Optimal server IP
        ref int out_pBestSrvPotr			//[OUT]Optimal server port
	    );

//----------------------------------------------------------------------------------------
        //创建设备网络端口映射服务 Create a device network port mapping service
        [DllImport(@"NpcMpiMonTsp.dll")]
        public static extern IntPtr	NPC_F_MPI_MON_DNP_CreateDevNetPortServer(
        char[] in_pServerAddr,				//[IN]服务器IP地址或域名 Server IP address or domain name
        ushort in_usServerPort,			//[IN]服务器端口号 Server port number
        char[] in_pAuthId,					//[IN]认证ID Authentication ID
        char[] in_pAuthPwd,                //[IN]认证密码 Authentication password
        int in_bIfConnSpecSrv      //[IN]是否连接指定服 1 Whether to connect to the specified service 1
	    );

        //销毁设备网络端口映射服务 Destroy device network port mapping service
        [DllImport(@"NpcMpiMonTsp.dll")]
        public static extern void	NPC_F_MPI_MON_DNP_DestroyDevNetPortServer(
        IntPtr in_hDevNetPort				//[IN]设备网络端口 Device network port
	    );
        //----------------------------------------------------------------------------------------
//检测服务器连接状态，返回：0：未连接，1：正在连接，2：已连接，返回9表示错误
//Check the server connection status, return: 0: not connected, 1: is connecting, 2: connected, returning 9 indicates error
        [DllImport(@"NpcMpiMonTsp.dll")]
        public static extern int	NPC_F_MPI_MON_DNP_CheckSrvConnState(
        IntPtr in_hDevNetPort				//[IN]Device network port
	    );
        //添加映射端口（连接指定通道），返回值：0：成功，其它失败
//失败码：
//7：等待登录服务器超时
//8：查找空闲TCP监听端口失败
//9：参数错误
//600001：解析连接参数失败
//600002：创建对象失败
//600004：创建映射端口失败
//600009：参数错误
//630001：协议错误或系统调用失败
//630002：连接设备失败
//630004：分配资源失败
//630006：访问服务器失败（访问超时或连接断开或服务器操作失败）
//630009：其它原因失败
//630101：用户名错误
//630102：密码错误
//630104：正在连接服务器
//630105：未连接服务器
//630111：权限不够
//630112：设备离线
        // Add a mapping port (connected to the specified channel), return value: 0: success, other failures
//Failure code:
//7: Waiting for the login server to time out
//8: Failed to find idle TCP listening port
//9: Parameter error
//600001: Failed to parse connection parameters
//600002: Failed to create object
//600004: Failed to create mapping port
//600009: Parameter error
//630001: Protocol error or system call failed
//630002: Connection device failed
//630004: Failed to allocate resources
//630006: Access to the server failed (access timeout or connection lost or server operation failed)
//630009: Other reasons failed
//630101: Username error
//630102: Password error
//630104: Connecting to the server
//630105: The server is not connected
//630111: Permission is not enough
//630112: Device offline
        [DllImport(@"NpcMpiMonTsp.dll")]
        public static extern int	NPC_F_MPI_MON_DNP_AddPortByChNo(
        IntPtr in_hDevNetPort,				//[IN]设备网络端口 Device network port
	    char[]							in_pUmid,					//[IN]UMID
        int in_iDestChNo,				//[IN]目标通道号，0为缺省通道号 Target channel number, 0 is the default channel number
        ref ushort inout_pLocalPort,			//[INOUT]本地端口号，如果值为0，则由系统分配端口号 Local port number, if the value is 0, the port number is assigned by the system
        int in_iTimeout			//[IN]超时时长（毫秒）2000 Timeout period (ms) 2000
	    );
        //删除映射端口 Delete mapped port
        [DllImport(@"NpcMpiMonTsp.dll")]
        public static extern void	NPC_F_MPI_MON_DNP_DelPort(
        IntPtr in_hDevNetPort,				//[IN]设备网络端口 Device network port
        ushort in_usLocalPort				//[IN]本地端口号 Local port number
	    );
    }
}
