xiongzhu 5 лет назад
Родитель
Сommit
ce3146f171
8 измененных файлов с 281 добавлено и 39 удалено
  1. 2 0
      DeviceCenter.csproj
  2. 106 0
      model/AcsDevice.cs
  3. 26 0
      model/AcsEvent.cs
  4. 20 0
      model/EventUploadReq.cs
  5. 18 2
      utils/AppUtil.cs
  6. 46 2
      utils/http.cs
  7. 27 22
      views/MainWindow.xaml
  8. 36 13
      views/MainWindow.xaml.cs

+ 2 - 0
DeviceCenter.csproj

@@ -128,12 +128,14 @@
     <Compile Include="AskDate.xaml.cs">
       <DependentUpon>AskDate.xaml</DependentUpon>
     </Compile>
+    <Compile Include="model\AcsEvent.cs" />
     <Compile Include="model\Area.cs" />
     <Compile Include="model\Building.cs" />
     <Compile Include="model\CarCamDevice.cs" />
     <Compile Include="Converter.cs" />
     <Compile Include="model\Config.cs" />
     <Compile Include="enums\DeviceEnums.cs" />
+    <Compile Include="model\EventUploadReq.cs" />
     <Compile Include="model\Floor.cs" />
     <Compile Include="model\GetAreasResponse.cs" />
     <Compile Include="model\GetBuildingResponse.cs" />

+ 106 - 0
model/AcsDevice.cs

@@ -4,6 +4,7 @@ using System;
 using System.Collections.Generic;
 using System.IO;
 using System.Runtime.InteropServices;
+using System.Threading;
 using System.Threading.Tasks;
 using System.Windows;
 
@@ -363,5 +364,110 @@ namespace DeviceCenter
 
             return sContents;
         }
+
+        public void getEvent(int userId, DateTime start, DateTime end)
+        {
+            CHCNetSDK.NET_DVR_ACS_EVENT_COND struCond = new CHCNetSDK.NET_DVR_ACS_EVENT_COND();
+            struCond.Init();
+            struCond.dwSize = (uint)Marshal.SizeOf(struCond);
+
+            struCond.dwMajor = CHCNetSDK.MAJOR_EVENT;
+
+            struCond.dwMinor = CHCNetSDK.MINOR_FACE_VERIFY_PASS;
+
+            DateTime date = DateTime.Now.AddDays(-1);
+            struCond.struStartTime.dwYear = start.Year;
+            struCond.struStartTime.dwMonth = start.Month;
+            struCond.struStartTime.dwDay = start.Day;
+            struCond.struStartTime.dwHour = start.Hour;
+            struCond.struStartTime.dwMinute = start.Minute;
+            struCond.struStartTime.dwSecond = start.Second;
+
+            struCond.struEndTime.dwYear = end.Year;
+            struCond.struEndTime.dwMonth = end.Month;
+            struCond.struEndTime.dwDay = end.Day;
+            struCond.struEndTime.dwHour = end.Hour;
+            struCond.struEndTime.dwMinute = end.Minute;
+            struCond.struEndTime.dwSecond = end.Second;
+
+            struCond.byPicEnable = 0;
+            struCond.szMonitorID = "";
+            struCond.wInductiveEventType = 65535;
+
+            //if (!StrToByteArray(ref struCond.byCardNo, textBoxCardNo.Text))
+            //{
+            //    return;
+            //}
+
+            //if (!StrToByteArray(ref struCond.byName, textBoxName.Text))
+            //{
+            //    return;
+            //}
+            //struCond.dwBeginSerialNo = 0;
+            //struCond.dwEndSerialNo = 0;
+
+            uint dwSize = struCond.dwSize;
+            IntPtr ptrCond = Marshal.AllocHGlobal((int)dwSize);
+            Marshal.StructureToPtr(struCond, ptrCond, false);
+            int m_lGetAcsEventHandle = CHCNetSDK.NET_DVR_StartRemoteConfig(userId, CHCNetSDK.NET_DVR_GET_ACS_EVENT, ptrCond, (int)dwSize, null, IntPtr.Zero);
+            if (-1 == m_lGetAcsEventHandle)
+            {
+                Marshal.FreeHGlobal(ptrCond);
+                log.Error($"NET_DVR_StartRemoteConfig FAIL, ERROR CODE {CHCNetSDK.NET_DVR_GetLastError()}");
+                return;
+            }
+
+            Thread m_pDisplayListThread = new Thread(() =>
+            {
+                List<AcsEvent> events = ProcessEvent(m_lGetAcsEventHandle);
+                EventUploadReq req = new EventUploadReq(ip, start.ToString("yyyy-MM-dd HH:mm:ss"), end.ToString("yyyy-MM-dd HH:mm:ss"), events);
+            });
+            m_pDisplayListThread.Start();
+            Marshal.FreeHGlobal(ptrCond);
+        }
+
+        public List<AcsEvent> ProcessEvent(int m_lGetAcsEventHandle)
+        {
+            int dwStatus = 0;
+            Boolean Flag = true;
+            CHCNetSDK.NET_DVR_ACS_EVENT_CFG struCFG = new CHCNetSDK.NET_DVR_ACS_EVENT_CFG();
+            struCFG.dwSize = (uint)Marshal.SizeOf(struCFG);
+            int dwOutBuffSize = (int)struCFG.dwSize;
+            struCFG.init();
+            List<AcsEvent> eventList = new List<AcsEvent>();
+            while (Flag)
+            {
+                dwStatus = CHCNetSDK.NET_DVR_GetNextRemoteConfig(m_lGetAcsEventHandle, ref struCFG, dwOutBuffSize);
+                switch (dwStatus)
+                {
+                    case CHCNetSDK.NET_SDK_GET_NEXT_STATUS_SUCCESS://成功读取到数据,处理完本次数据后需调用next
+                        int employNo = (int)struCFG.struAcsEventInfo.dwEmployeeNo;
+                        string cardNo = System.Text.Encoding.UTF8.GetString(struCFG.struAcsEventInfo.byCardNo).TrimEnd('\0');
+                        string time = $"{struCFG.struTime.dwYear:D4}-{struCFG.struTime.dwMonth:D2}-{struCFG.struTime.dwDay:D2} {struCFG.struTime.dwHour:D2}:{struCFG.struTime.dwMinute:D2}:{struCFG.struTime.dwSecond:D2}";
+                        log.Info($"receive acs event employNo:{employNo} cardNo:{cardNo} time:{time}");
+                        eventList.Add(new AcsEvent(ip, direction.ToString(), employNo.ToString(), cardNo, time));
+                        break;
+                    case CHCNetSDK.NET_SDK_GET_NEXT_STATUS_NEED_WAIT:
+                        Thread.Sleep(200);
+                        break;
+                    case CHCNetSDK.NET_SDK_GET_NEXT_STATUS_FAILED:
+                        CHCNetSDK.NET_DVR_StopRemoteConfig(m_lGetAcsEventHandle);
+                        log.Error($"NET_SDK_GET_NEXT_STATUS_FAILED {CHCNetSDK.NET_DVR_GetLastError()}");
+                        Flag = false;
+                        break;
+                    case CHCNetSDK.NET_SDK_GET_NEXT_STATUS_FINISH:
+                        CHCNetSDK.NET_DVR_StopRemoteConfig(m_lGetAcsEventHandle);
+                        Flag = false;
+                        break;
+                    default:
+                        log.Info($"NET_SDK_GET_NEXT_STATUS_UNKOWN {CHCNetSDK.NET_DVR_GetLastError()}");
+                        Flag = false;
+                        CHCNetSDK.NET_DVR_StopRemoteConfig(m_lGetAcsEventHandle);
+                        break;
+                }
+            }
+
+            return eventList;
+        }
     }
 }

+ 26 - 0
model/AcsEvent.cs

@@ -0,0 +1,26 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace DeviceCenter
+{
+    public class AcsEvent
+    {
+        public string ip { get; set; }
+        public string direction { get; set; }
+        public string employNo { get; set; }
+        public string cardNo { get; set; }
+        public string time { get; set; }
+
+        public AcsEvent(string ip, string direction, string employNo, string cardNo, string time)
+        {
+            this.ip = ip;
+            this.direction = direction;
+            this.employNo = employNo;
+            this.cardNo = cardNo;
+            this.time = time;
+        }
+    }
+}

+ 20 - 0
model/EventUploadReq.cs

@@ -0,0 +1,20 @@
+using System.Collections.Generic;
+
+namespace DeviceCenter
+{
+    class EventUploadReq
+    {
+        public string ip { get; set; }
+        public string startTime { get; set; }
+        public string endTime { get; set; }
+        public List<AcsEvent> events { get; set; }
+
+        public EventUploadReq(string ip, string startTime, string endTime, List<AcsEvent> events)
+        {
+            this.ip = ip;
+            this.startTime = startTime;
+            this.endTime = endTime;
+            this.events = events;
+        }
+    }
+}

+ 18 - 2
utils/AppUtil.cs

@@ -10,9 +10,25 @@ namespace DeviceCenter
 {
     class AppUtil
     {
+        public static bool isAutoStart()
+        {
+            string strName = AppDomain.CurrentDomain.BaseDirectory + "DeviceCenter.exe";//获取要自动运行的应用程序名
+            string strnewName = strName.Substring(strName.LastIndexOf("\\") + 1);//获取应用程序文件名,不包括路径
+            RegistryKey registry = Registry.LocalMachine.OpenSubKey($"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);//检索指定的子项
+            if (registry != null)
+            {
+                if (strName.Equals(registry.GetValue(strnewName)))
+                {
+                    return true;
+                }
+            }
+
+            return false;
+        }
+
         public static void enableAutoStart()
         {
-            string strName = AppDomain.CurrentDomain.BaseDirectory + "CamTool.exe";//获取要自动运行的应用程序名
+            string strName = AppDomain.CurrentDomain.BaseDirectory + "DeviceCenter.exe";//获取要自动运行的应用程序名
             if (!System.IO.File.Exists(strName))//判断要自动运行的应用程序文件是否存在
                 return;
             string strnewName = strName.Substring(strName.LastIndexOf("\\") + 1);//获取应用程序文件名,不包括路径
@@ -26,7 +42,7 @@ namespace DeviceCenter
 
         public static void disableAutoStart()
         {
-            string strName = AppDomain.CurrentDomain.BaseDirectory + "CamTool.exe";//获取要自动运行的应用程序名
+            string strName = AppDomain.CurrentDomain.BaseDirectory + "DeviceCenter.exe";//获取要自动运行的应用程序名
             if (!System.IO.File.Exists(strName))//判断要取消的应用程序文件是否存在
                 return;
             string strnewName = strName.Substring(strName.LastIndexOf("\\") + 1);///获取应用程序文件名,不包括路径

+ 46 - 2
utils/http.cs

@@ -34,7 +34,7 @@ namespace DeviceCenter.utils
             return _Client;
         }
 
-        public static Promise<T> post<T>(string url, Dictionary<string, string> data  = null)
+        public static Promise<T> post<T>(string url, Dictionary<string, string> data = null)
         {
             return new Promise<T>(async (resolve, reject) =>
             {
@@ -80,6 +80,50 @@ namespace DeviceCenter.utils
             });
         }
 
+        public static Promise<T> postJson<T>(string url, object body)
+        {
+            return new Promise<T>(async (resolve, reject) =>
+            {
+                Config config = Config.getInstance();
+                RestClient client = getClient();
+                client.BaseUrl = new Uri(config.url);
+                var req = new RestRequest(url);
+                req.RequestFormat = DataFormat.Json;
+                if (!string.IsNullOrEmpty(config.token))
+                {
+                    req.AddHeader("Authorization", "Bearer " + config.token);
+                }
+                if (body != null)
+                {
+                    req.AddBody(body);
+                }
+                try
+                {
+                    var res = await getClient().ExecutePostAsync(req);
+                    if (res.StatusCode == HttpStatusCode.OK)
+                    {
+                        if (typeof(T) == typeof(string))
+                        {
+                            resolve((T)Convert.ChangeType(res.Content, typeof(T)));
+                        }
+                        else
+                        {
+                            resolve(JsonConvert.DeserializeObject<T>(res.Content));
+                        }
+                    }
+                    else
+                    {
+                        var resData = JsonConvert.DeserializeObject<Dictionary<string, string>>(res.Content);
+                        reject(new Exception(resData["error"]));
+                    }
+                }
+                catch (Exception e)
+                {
+                    reject(e);
+                }
+            });
+        }
+
         public static Promise<T> upload<T>(string url, string filePath)
         {
             return new Promise<T>(async (resolve, reject) =>
@@ -138,7 +182,7 @@ namespace DeviceCenter.utils
                 {
                     foreach (KeyValuePair<string, string> entry in data)
                     {
-                        if(entry.Key == "query")
+                        if (entry.Key == "query")
                         {
                             req.AddQueryParameter(entry.Key, entry.Value, false);
                         }

+ 27 - 22
views/MainWindow.xaml

@@ -1,49 +1,54 @@
-<Window x:Class="DeviceCenter.MainWindow"
-        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
+<Window x:Class="DeviceCenter.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
-        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
-        xmlns:local="clr-namespace:DeviceCenter"
-        mc:Ignorable="d"
-        xmlns:fa5="http://schemas.fontawesome.com/icons/"
-        Title="设备中心" Height="450" Width="800" ResizeMode="NoResize" Closing="Window_Closing" WindowStartupLocation="CenterScreen" Loaded="Window_Loaded">
+        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:DeviceCenter"
+        mc:Ignorable="d" xmlns:fa5="http://schemas.fontawesome.com/icons/" Title="设备中心" Height="450" Width="800"
+        ResizeMode="NoResize" Closing="Window_Closing" WindowStartupLocation="CenterScreen" Loaded="Window_Loaded">
     <Window.Resources>
-        <local:EnumItemsSource x:Key="deviceTypeConverter" Type="{x:Type local:DeviceType}"/>
-        <local:EnumItemsSource x:Key="directionConverter" Type="{x:Type local:Direction}"/>
-        <local:EnumItemsSource x:Key="deviceStatusConverter" Type="{x:Type local:DeviceStatus}"/>
-        <local:DeviceStatusColorConverter x:Key="deviceStatusColorConverter"/>
+        <local:EnumItemsSource x:Key="deviceTypeConverter" Type="{x:Type local:DeviceType}" />
+        <local:EnumItemsSource x:Key="directionConverter" Type="{x:Type local:Direction}" />
+        <local:EnumItemsSource x:Key="deviceStatusConverter" Type="{x:Type local:DeviceStatus}" />
+        <local:DeviceStatusColorConverter x:Key="deviceStatusColorConverter" />
         <Style x:Key="iconStyle" TargetType="fa5:SvgAwesome">
             <Setter Property="Foreground" Value="#409EFF" />
             <Setter Property="Width" Value="10"></Setter>
         </Style>
     </Window.Resources>
     <DockPanel VerticalAlignment="Stretch" Height="Auto">
-        <StackPanel DockPanel.Dock="Bottom" Height="Auto" Orientation="Horizontal" HorizontalAlignment="Center" Margin="10">
+        <StackPanel DockPanel.Dock="Bottom" Height="Auto" Orientation="Horizontal" HorizontalAlignment="Center"
+                Margin="10">
             <Button Content="上传人员列表" Width="80" Height="25" Name="btn_upload_staff" Click="btn_upload_staff_Click"></Button>
-            <Button Content="添加设备" Width="60" Height="25" Margin="10 0 0 0" Name="btn_add_device" Click="btn_add_device_Click"></Button>
-            <Button Content="删除设备" Width="60" Height="25" Margin="10 0 0 0" Name="btn_del_device" Click="btn_del_device_Click"></Button>
-            <Separator Margin="20 0 10 0" Width="1" Height="14" Style="{StaticResource {x:Static ToolBar.SeparatorStyleKey}}"></Separator>
-            <Button Content="设置开机自启" Width="80" Height="25" Margin="10 0 0 0"></Button>
-            <Button Content="设置定时重启" Width="80" Height="25" Margin="10 0 0 0"></Button>
+            <Button Content="添加设备" Width="60" Height="25" Margin="10 0 0 0" Name="btn_add_device"
+                    Click="btn_add_device_Click"></Button>
+            <Button Content="删除设备" Width="60" Height="25" Margin="10 0 0 0" Name="btn_del_device"
+                    Click="btn_del_device_Click"></Button>
+            <Separator Margin="20 0 10 0" Width="1" Height="14"
+                    Style="{StaticResource {x:Static ToolBar.SeparatorStyleKey}}"></Separator>
             <Button Content="获取事件" Width="80" Height="25" Margin="10 0 0 0" Click="Get_Event_Click"></Button>
         </StackPanel>
-        <ListView x:Name="lv_device" VerticalAlignment="Stretch" VerticalContentAlignment="Stretch" Height="Auto" HorizontalAlignment="Stretch">
+        <ListView x:Name="lv_device" VerticalAlignment="Stretch" VerticalContentAlignment="Stretch" Height="Auto"
+                HorizontalAlignment="Stretch">
             <ListView.View>
                 <GridView>
-                    <GridViewColumn Header="类型" Width="80" DisplayMemberBinding="{Binding type, Converter = {StaticResource deviceTypeConverter}}" />
+                    <GridViewColumn Header="类型" Width="80"
+                            DisplayMemberBinding="{Binding type, Converter = {StaticResource deviceTypeConverter}}" />
                     <GridViewColumn Header="名称" Width="80" DisplayMemberBinding="{Binding name}" />
                     <GridViewColumn Header="IP" Width="120" DisplayMemberBinding="{Binding ip}" />
                     <GridViewColumn Header="状态" Width="100">
                         <GridViewColumn.CellTemplate>
                             <DataTemplate>
                                 <StackPanel Orientation="Horizontal">
-                                    <Ellipse Width="10" Height="10" Fill="{Binding status,Converter = {StaticResource deviceStatusColorConverter}}" Margin="0 0 5 0"></Ellipse>
-                                    <TextBlock Text="{Binding status, Converter= {StaticResource deviceStatusConverter}}"></TextBlock>
+                                    <Ellipse Width="10" Height="10"
+                                            Fill="{Binding status,Converter = {StaticResource deviceStatusColorConverter}}"
+                                            Margin="0 0 5 0"></Ellipse>
+                                    <TextBlock
+                                            Text="{Binding status, Converter= {StaticResource deviceStatusConverter}}"></TextBlock>
                                 </StackPanel>
                             </DataTemplate>
                         </GridViewColumn.CellTemplate>
                     </GridViewColumn>
-                    <GridViewColumn Header="方向" Width="40" DisplayMemberBinding="{Binding direction, Converter = {StaticResource directionConverter}}" />
+                    <GridViewColumn Header="方向" Width="40"
+                            DisplayMemberBinding="{Binding direction, Converter = {StaticResource directionConverter}}" />
                     <GridViewColumn Header="消息" Width="180" DisplayMemberBinding="{Binding message}" />
                 </GridView>
             </ListView.View>

+ 36 - 13
views/MainWindow.xaml.cs

@@ -39,6 +39,7 @@ namespace DeviceCenter
         private System.Windows.Forms.NotifyIcon notifyIcon = null;
         private System.Windows.Forms.ContextMenu contextMenuExit;
         private System.Windows.Forms.MenuItem menuItem1;
+        private System.Windows.Forms.MenuItem menuItem2;
         private System.ComponentModel.IContainer components;
 
         public MainWindow()
@@ -49,18 +50,24 @@ namespace DeviceCenter
             notifyIcon.Click += new EventHandler(notifyIcon_Click);
             notifyIcon.Icon = new Icon("assets\\icon.ico");
 
-            this.components = new System.ComponentModel.Container();
-            this.contextMenuExit = new System.Windows.Forms.ContextMenu();
-            this.menuItem1 = new System.Windows.Forms.MenuItem();
+            components = new System.ComponentModel.Container();
+            contextMenuExit = new System.Windows.Forms.ContextMenu();
+            menuItem1 = new System.Windows.Forms.MenuItem();
+            menuItem2 = new System.Windows.Forms.MenuItem();
 
             // Initialize contextMenuExit
             this.contextMenuExit.MenuItems.AddRange(
-                        new System.Windows.Forms.MenuItem[] { this.menuItem1 });
+                        new System.Windows.Forms.MenuItem[] { this.menuItem1, menuItem2 });
 
             // Initialize menuItem1
             this.menuItem1.Index = 0;
             this.menuItem1.Text = "退出";
             this.menuItem1.Click += new EventHandler(this.menuItem1_Click);
+
+            menuItem2.Index = 1;
+            menuItem2.Text = "开机自启";
+            menuItem2.Click += new EventHandler(this.menuItem2_Click);
+            menuItem2.Checked = AppUtil.isAutoStart();
             notifyIcon.ContextMenu = this.contextMenuExit;
 
             initSdk();
@@ -72,14 +79,14 @@ namespace DeviceCenter
             //dispatcherTimer.Interval = new TimeSpan(0, 30, 0);
             //dispatcherTimer.Start();
 
-            //System.Windows.Threading.DispatcherTimer timer = new System.Windows.Threading.DispatcherTimer();
-            //timer.Tick += (s, e) =>
-            //{
-            //    ((System.Windows.Threading.DispatcherTimer)s).Stop();
-            //    GetAllCard(null, null);
-            //};
-            //timer.Interval = new TimeSpan(0, 0, 5);
-            //timer.Start();
+            System.Windows.Threading.DispatcherTimer timer = new System.Windows.Threading.DispatcherTimer();
+            timer.Tick += (s, e) =>
+            {
+                ((System.Windows.Threading.DispatcherTimer)s).Stop();
+                GetAllCard(null, null);
+            };
+            timer.Interval = new TimeSpan(0, 0, 5);
+            timer.Start();
 
 
             ScheduleJob();
@@ -147,6 +154,21 @@ namespace DeviceCenter
 
         }
 
+        private void menuItem2_Click(object Sender, EventArgs e)
+        {
+            if (AppUtil.isAutoStart())
+            {
+                AppUtil.disableAutoStart();
+                menuItem2.Checked = false;
+            }
+            else
+            {
+                AppUtil.enableAutoStart();
+                menuItem2.Checked = true;
+            }
+
+        }
+
         private async void initSdk()
         {
             await Task.Run(() =>
@@ -356,7 +378,7 @@ namespace DeviceCenter
                 string leaderCard = 1 == struCardCfg.byLeaderCard ? "YES" : "NO";
                 string name = Encoding.UTF8.GetString(struCardCfg.byName);
 
-                //log.Info(string.Format("cardNo:{0} name:{1}", cardNo, name));
+                log.Info($"cardNo:{cardNo} name:{name} departmentNo:{struCardCfg.wDepartmentNo}");
 
                 //var request = new RestRequest("staffInfo/saveStaffInfo", DataFormat.Json);
                 //request.AddParameter("name", name);
@@ -557,6 +579,7 @@ namespace DeviceCenter
                         break;
                 }
             }
+
         }
 
         private void ProcessAcsEvent(ref CHCNetSDK.NET_DVR_ACS_EVENT_CFG struCFG, ref bool flag)