Пример #1
0
        public void Start(string configpath)
        {
            _config            = configpath;
            Device             = new HikVision();
            Device.LastRunTime = DateTime.Now.Ticks / 10000;
            Device.Initialilize();
            NVRInfos = Device.NVRInfos;
            Config setting = new Config(_config)
            {
                GroupName = "app"
            };
            int port = setting.GetValue("http", "port", 6001);
            ///生成录像包装路径
            string dir = setting.GetValue("http", "dir", AppDomain.CurrentDomain.BaseDirectory);
            ///vms 下载录像零时保存地址
            string temp = setting.GetValue("video", "temp", AppDomain.CurrentDomain.BaseDirectory);
            ///包装录像包装地址
            string path = setting.GetValue("video", "path", AppDomain.CurrentDomain.BaseDirectory);
            string url  = setting.GetValue("http", "url", "http://127.0.0.1");

            _server          = new HttpServer();
            _server.TempPath = Temp = temp;
            _server.Url      = url;
            _server.StartListen(port);
            _server.NVRInfos         = NVRInfos;
            _server.Strap            = this;
            _server.Path             = path;
            _server.VirtualDirectory = dir;
        }
Пример #2
0
        public override bool Initialilize()
        {
            LastRunTime      = DateTime.Now.Ticks / 10000;
            struIpParaCfgV40 = new CHCNetSDK.NET_DVR_IPPARACFG_V40();
            struPicCfgV40    = new CHCNetSDK.NET_DVR_PICCFG_V40();
            m_struShowStrCfg = new CHCNetSDK.NET_DVR_SHOWSTRING_V30();
            Console.WriteLine("开始初始化");
            heartbeat.Interval = 60 * 1000 * 3;
            heartbeat.Elapsed -= Heartbeat_Elapsed;
            heartbeat.Elapsed += Heartbeat_Elapsed;
            heartbeat.Start();
            if (!Directory.Exists(AppDomain.CurrentDomain.BaseDirectory + "SdkLog\\"))
            {
                Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory + "SdkLog\\");
            }
            Config config = new Config(configpath)
            {
                GroupName = "service"
            };

            string[] host = config.GetSectionValues();
            if (NVRInfos == null)
            {
                CHCNetSDK.NET_DVR_SetLogToFile(0, AppDomain.CurrentDomain.BaseDirectory + "SdkLog\\", true);
                bool init = CHCNetSDK.NET_DVR_Init();
                if (!init)
                {
                    _log.Error("初始化失败" + CHCNetSDK.NET_DVR_GetLastError());
                    ErrorMsg = "初始化失败";
                    return(false);
                }

                bool retime = CHCNetSDK.NET_DVR_SetConnectTime(3000, 500);
                if (!retime)
                {
                    _log.Error("连接超时设置" + CHCNetSDK.NET_DVR_GetLastError());
                    ErrorMsg = "连接超时设置";
                }
                bool re = CHCNetSDK.NET_DVR_SetReconnect(3000, 1);
                if (!re)
                {
                    _log.Error("重连设置" + CHCNetSDK.NET_DVR_GetLastError());
                    ErrorMsg = "重连设置";
                }

                NVRInfos = new List <NVRInfo>();
            }
            for (int x = 0; x < host.Length; x++)
            {
                string name = host[x];
                config.Section = name;
                string ip       = config.GetValue("host", "ip", "192.168.2.1");
                int    port     = config.GetValue("host", "port", 8000);
                string username = config.GetValue("host", "username", "admin");
                string password = config.GetValue("host", "password", "ywwy2016");
                _log.Error(name + "." + x);
                NVRInfo nvr = NVRInfos.FirstOrDefault(o => o.Host.DVRIPAddress == ip);

                if (nvr == null)
                {
                    nvr = new NVRInfo()
                    {
                        Host = new NVRHostHiKv()
                        {
                            DVRIPAddress = ip, DVRPassword = password, DVRPort = port, DVRUserName = username, LUserId = -1
                        }, IPCs = new List <IPC>()
                    };
                }
                CHCNetSDK.NET_DVR_DEVICEINFO_V30 DeviceInfo = new CHCNetSDK.NET_DVR_DEVICEINFO_V30();
                int _login = -1;
                if (nvr.Host.LUserId > -1)
                {
                    _login = nvr.Host.LUserId;
                }
                else
                {
                    _login = CHCNetSDK.NET_DVR_Login_V30(ip, port, username, password, ref DeviceInfo);
                    Console.WriteLine(x + "登录" + _login);
                }

                if (_login >= 0)
                {
                    nvr.DeviceInfo   = DeviceInfo;
                    nvr.Host.LUserId = _login;
                    uint dwSize = (uint)Marshal.SizeOf(struIpParaCfgV40);

                    IntPtr ptrIpParaCfgV40 = Marshal.AllocHGlobal((Int32)dwSize);
                    Marshal.StructureToPtr(struIpParaCfgV40, ptrIpParaCfgV40, true);

                    uint dwReturn = 0;
                    int  iGroupNo = 0;
                    #region IPC通道
                    if (DeviceInfo.byIPChanNum > 0)
                    {
                        if (!CHCNetSDK.NET_DVR_GetDVRConfig(_login, CHCNetSDK.NET_DVR_GET_IPPARACFG_V40, iGroupNo, ptrIpParaCfgV40, dwSize, ref dwReturn))
                        {
                            ErrorMsg = "获取IPC失败." + CHCNetSDK.NET_DVR_GetLastError();
                            Console.WriteLine(ErrorMsg);
                            _log.Error(ErrorMsg);
                            Marshal.FreeHGlobal(ptrIpParaCfgV40);
                        }
                        else
                        {
                            struIpParaCfgV40 = (CHCNetSDK.NET_DVR_IPPARACFG_V40)Marshal.PtrToStructure(ptrIpParaCfgV40, typeof(CHCNetSDK.NET_DVR_IPPARACFG_V40));
                            CHCNetSDK.NET_DVR_IPDEVINFO_V31[] ipdevinfos = struIpParaCfgV40.struIPDevInfo;
                            #region 获取通道信息
                            for (int m = 0; m < ipdevinfos.Length; m++)
                            {
                                CHCNetSDK.NET_DVR_GET_STREAM_UNION unionGetStream = struIpParaCfgV40.struStreamMode[m].uGetStream;
                                uint   unSize      = (uint)Marshal.SizeOf(unionGetStream);
                                IntPtr ptrChanInfo = Marshal.AllocHGlobal((Int32)unSize);
                                Marshal.StructureToPtr(unionGetStream, ptrChanInfo, true);
                                CHCNetSDK.NET_DVR_IPCHANINFO struChanInfo = (CHCNetSDK.NET_DVR_IPCHANINFO)Marshal.PtrToStructure(ptrChanInfo, typeof(CHCNetSDK.NET_DVR_IPCHANINFO));
                                IPC ipc = new IPC()
                                {
                                    AdminPort = ipdevinfos[m].wDVRPort.ToString(), ChannelIndex = m + (int)struIpParaCfgV40.dwStartDChan, ChannelIp = ipdevinfos[m].struIP.sIpV4, ChannelName = "IPCamera" + m, Status = ipdevinfos[m].byEnable, UserName = Encoding.UTF8.GetString(ipdevinfos[m].sUserName).Trim('\0')
                                };
                                #region 获取通道名称
                                if (ipc.Status == 1)
                                {
                                    Int32  nSize     = Marshal.SizeOf(struPicCfgV40);
                                    IntPtr ptrPicCfg = Marshal.AllocHGlobal(nSize);
                                    Marshal.StructureToPtr(struPicCfgV40, ptrPicCfg, true);
                                    if (!CHCNetSDK.NET_DVR_GetDVRConfig(_login, CHCNetSDK.NET_DVR_GET_PICCFG_V40, ipc.ChannelIndex, ptrPicCfg, (UInt32)nSize, ref dwReturn))
                                    {
                                        ipc.ErrorMsg = CHCNetSDK.NET_DVR_GetLastError().ToString();
                                        ipc.Status   = 0;
                                        IPC oldipc = nvr.IPCs.FirstOrDefault(o => o.ChannelIp == ipc.ChannelIp);
                                        if (oldipc != null)
                                        {
                                            nvr.IPCs.Remove(oldipc);
                                        }
                                        nvr.IPCs.Add(ipc);
                                    }
                                    else
                                    {
                                        struPicCfgV40 = (CHCNetSDK.NET_DVR_PICCFG_V40)Marshal.PtrToStructure(ptrPicCfg, typeof(CHCNetSDK.NET_DVR_PICCFG_V40));
                                        if (struPicCfgV40.sChanName != null)
                                        {
                                            ipc.ChannelNumber = System.Text.Encoding.GetEncoding("GBK").GetString(struPicCfgV40.sChanName).Trim('\0');
                                        }
                                        ipc.Status = struChanInfo.byEnable;
                                        _log.Info("2:" + ipc.ChannelIndex + "." + ipc.ChannelIp + "." + struChanInfo.byEnable + "." + ipc.ChannelNumber);
                                        Console.WriteLine(ipc.ChannelIndex + "." + ipc.ChannelIp + "." + struChanInfo.byEnable + "." + ipc.ChannelNumber);
                                        IPC oldipc = nvr.IPCs.FirstOrDefault(o => o.ChannelIp == ipc.ChannelIp);
                                        if (oldipc != null)
                                        {
                                            nvr.IPCs.Remove(oldipc);
                                        }
                                        nvr.IPCs.Add(ipc);
                                    }
                                    Marshal.FreeHGlobal(ptrPicCfg);
                                }
                                #endregion
                                Marshal.FreeHGlobal(ptrChanInfo);
                            }
                            #endregion
                        }

                        NVRInfo oldnvr = NVRInfos.FirstOrDefault(o => o.Host.DVRIPAddress == ip);
                        if (nvr != null)
                        {
                            NVRInfos.Remove(oldnvr);
                        }
                        NVRInfos.Add(nvr);
                    }
                    #endregion
                    #region 其他通道
                    else
                    {
                        for (int i = 0; i < DeviceInfo.byChanNum; i++)
                        {
                            dwReturn = 0;

                            Int32  nSize     = Marshal.SizeOf(struPicCfgV40);
                            IntPtr ptrPicCfg = Marshal.AllocHGlobal(nSize);
                            Marshal.StructureToPtr(struPicCfgV40, ptrPicCfg, true);
                            int chancelid = i + DeviceInfo.byStartChan;
                            if (!CHCNetSDK.NET_DVR_GetDVRConfig(_login, CHCNetSDK.NET_DVR_GET_PICCFG_V40, chancelid, ptrPicCfg, (UInt32)nSize, ref dwReturn))
                            {
                                ErrorMsg = CHCNetSDK.NET_DVR_GetLastError().ToString();
                            }
                            else
                            {
                                struPicCfgV40 = (CHCNetSDK.NET_DVR_PICCFG_V40)Marshal.PtrToStructure(ptrPicCfg, typeof(CHCNetSDK.NET_DVR_PICCFG_V40));
                                IPC ipc = new IPC()
                                {
                                    AdminPort = "", ChannelIndex = chancelid, ChannelIp = nvr.Host.DVRIPAddress, ChannelName = "IPCamera" + i, Status = 1, ChannelNumber = System.Text.Encoding.GetEncoding("GBK").GetString(struPicCfgV40.sChanName).Trim('\0')
                                };

                                IPC oldipc = nvr.IPCs.FirstOrDefault(o => o.ChannelIp == ipc.ChannelIp);
                                if (oldipc != null)
                                {
                                    nvr.IPCs.Remove(oldipc);
                                }
                                nvr.IPCs.Add(ipc);
                            }
                            NVRInfo oldnvr = NVRInfos.FirstOrDefault(o => o.Host.DVRIPAddress == ip);
                            if (nvr != null)
                            {
                                NVRInfos.Remove(oldnvr);
                            }
                            NVRInfos.Add(nvr);
                            Marshal.FreeHGlobal(ptrPicCfg);
                        }
                    }
                    #endregion

                    Marshal.FreeHGlobal(ptrIpParaCfgV40);
                }
                else
                {
                    NVRInfo nvrerr = new NVRInfo()
                    {
                        Host = new NVRHostHiKv()
                        {
                            DVRIPAddress = ip, DVRPassword = password, DVRPort = port, DVRUserName = username, LUserId = -1
                        }, IPCs = new List <IPC>()
                    };

                    ErrorMsg             = "登录失败." + CHCNetSDK.NET_DVR_GetLastError();
                    nvrerr.Host.ErrorMsg = ErrorMsg;
                    Console.WriteLine(ErrorMsg);
                    _log.Error(ErrorMsg);
                    NVRInfo oldnvr = NVRInfos.FirstOrDefault(o => o.Host.DVRIPAddress == ip);
                    if (nvr != null)
                    {
                        NVRInfos.Remove(oldnvr);
                    }
                    NVRInfos.Add(nvrerr);
                }
            }
            GC.Collect();
            GC.WaitForPendingFinalizers();
            Console.WriteLine("完成初始化");

            return(true);
        }