using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using NetSDKCS;
namespace BrakeMachine.Brack
{
public class BrackMgr
{
private static fDisConnectCallBack m_DisConnectCallBack;
private static fHaveReConnectCallBack m_ReConnectCallBack;
private static fAnalyzerDataCallBack m_AnalyzerDataCallBack;
private static IntPtr m_LoginID = IntPtr.Zero;
private static NET_DEVICEINFO_Ex m_DeviceInfo;
private static Int64 m_ID = 1;
private static IntPtr m_EventID = IntPtr.Zero;
private static int passage = 1;//通道
private const int m_WaitTime = 5000;
#region 连接
///
/// 连接
///
///
public static void Connect()
{
IntelligentTrafficDemo();//加载准备事件
Login();//登录
subscribe(); //
}
private static void IntelligentTrafficDemo()
{
m_AnalyzerDataCallBack = new fAnalyzerDataCallBack(AnalyzerDataCallBack);
try
{
NETClient.Init(m_DisConnectCallBack, IntPtr.Zero, null);
NETClient.SetAutoReconnect(m_ReConnectCallBack, IntPtr.Zero);
}
catch (Exception ex)
{
Process.GetCurrentProcess().Kill();
}
}
private static int AnalyzerDataCallBack(IntPtr lAnalyzerHandle, uint dwEventType, IntPtr pEventInfo, IntPtr pBuffer, uint dwBufSize, IntPtr dwUser, int nSequence, IntPtr reserved)
{
EM_EVENT_IVS_TYPE type = (EM_EVENT_IVS_TYPE)dwEventType;
switch (type)
{
case EM_EVENT_IVS_TYPE.TRAFFICJUNCTION:
{
NET_DEV_EVENT_TRAFFICJUNCTION_INFO info = (NET_DEV_EVENT_TRAFFICJUNCTION_INFO)Marshal.PtrToStructure(pEventInfo, typeof(NET_DEV_EVENT_TRAFFICJUNCTION_INFO));
EventInfo eventInfo = new EventInfo();
eventInfo.ID = m_ID.ToString();
eventInfo.Time = info.UTC.ToString();
eventInfo.Type = "junction(卡口)";
eventInfo.GroupID = info.stuFileInfo.nGroupId.ToString();
eventInfo.Index = info.stuFileInfo.bIndex.ToString();
eventInfo.Count = info.stuFileInfo.bCount.ToString();
eventInfo.PlateNumber = info.stTrafficCar.szPlateNumber;
eventInfo.PlateColor = info.stTrafficCar.szPlateColor;
eventInfo.PlateType = info.stTrafficCar.szPlateType;
eventInfo.VehicleColor = info.stTrafficCar.szVehicleColor;
eventInfo.VehicleSize = GetVehicleSize((EM_VehicleSizeType)info.stTrafficCar.nVehicleSize);
eventInfo.VehicleType = System.Text.Encoding.Default.GetString(info.stuVehicle.szObjectSubType);
eventInfo.LaneNumber = info.stTrafficCar.nLane.ToString();
eventInfo.Address = Marshal.PtrToStringAnsi(info.stTrafficCar.szDeviceAddress);
eventInfo.FileLenth = info.stuObject.stPicInfo.dwFileLenth;
eventInfo.OffSet = info.stuObject.stPicInfo.dwOffSet;
eventInfo.BoundingBox = info.stuObject.BoundingBox;
if (IntPtr.Zero != pBuffer && dwBufSize > 0)
{
eventInfo.Buffer = new byte[dwBufSize];
Marshal.Copy(pBuffer, eventInfo.Buffer, 0, (int)dwBufSize);
}
//this.BeginInvoke((Action)UpdateUI, eventInfo);
m_ID++;
break;
}
case EM_EVENT_IVS_TYPE.TRAFFIC_MANUALSNAP:
{
NET_DEV_EVENT_TRAFFIC_MANUALSNAP_INFO info = (NET_DEV_EVENT_TRAFFIC_MANUALSNAP_INFO)Marshal.PtrToStructure(pEventInfo, typeof(NET_DEV_EVENT_TRAFFIC_MANUALSNAP_INFO));
EventInfo eventInfo = new EventInfo();
eventInfo.ID = m_ID.ToString();
eventInfo.Time = info.UTC.ToString();
eventInfo.Type = "manual snap(手动抓拍)";
eventInfo.GroupID = info.stuFileInfo.nGroupId.ToString();
eventInfo.Index = info.stuFileInfo.bIndex.ToString();
eventInfo.Count = info.stuFileInfo.bCount.ToString();
eventInfo.PlateNumber = info.stTrafficCar.szPlateNumber;
eventInfo.PlateColor = info.stTrafficCar.szPlateColor;
eventInfo.PlateType = info.stTrafficCar.szPlateType;
eventInfo.VehicleColor = info.stTrafficCar.szVehicleColor;
eventInfo.VehicleSize = GetVehicleSize((EM_VehicleSizeType)info.stTrafficCar.nVehicleSize);
eventInfo.VehicleType = System.Text.Encoding.Default.GetString(info.stuVehicle.szObjectSubType);
eventInfo.LaneNumber = info.stTrafficCar.nLane.ToString();
eventInfo.Address = Marshal.PtrToStringAnsi(info.stTrafficCar.szDeviceAddress);
eventInfo.FileLenth = info.stuObject.stPicInfo.dwFileLenth;
eventInfo.OffSet = info.stuObject.stPicInfo.dwOffSet;
eventInfo.BoundingBox = info.stuObject.BoundingBox;
if (IntPtr.Zero != pBuffer && dwBufSize > 0)
{
eventInfo.Buffer = new byte[dwBufSize];
Marshal.Copy(pBuffer, eventInfo.Buffer, 0, (int)dwBufSize);
}
//this.BeginInvoke((Action)UpdateUI, eventInfo);
m_ID++;
break;
}
default:
Console.WriteLine(dwEventType.ToString("X"));
break;
}
return 0;
}
private static bool Login()
{
if (IntPtr.Zero == m_LoginID)
{
ushort port = 37777;
string ip = "";
string name = "";
string pwd = "";
m_DeviceInfo = new NET_DEVICEINFO_Ex();
m_LoginID = NETClient.LoginWithHighLevelSecurity(ip, port, name, pwd, EM_LOGIN_SPAC_CAP_TYPE.TCP, IntPtr.Zero, ref m_DeviceInfo);
if (IntPtr.Zero == m_LoginID)
{
return false;
}
return true;//登录成功
}
else
{
bool result = NETClient.Logout(m_LoginID);
if (!result)
{
return false;
}
m_LoginID = IntPtr.Zero;
return true;//登录成功
}
}
#endregion
#region 订阅
///
/// 订阅
///
private static void subscribe()
{
if (IntPtr.Zero == m_EventID)
{
m_ID = 1;
m_EventID = NETClient.RealLoadPicture(m_LoginID, passage, (uint)EM_EVENT_IVS_TYPE.ALL, true, m_AnalyzerDataCallBack, m_LoginID, IntPtr.Zero);
if (IntPtr.Zero == m_EventID)
{
return;
}
}
else
{
bool ret = NETClient.StopLoadPic(m_EventID);
if (!ret)
{
return;
}
m_EventID = IntPtr.Zero;
}
}
#endregion
#region 手动抓拍
public static void Manualsnap()
{
NET_MANUAL_SNAP_PARAMETER par = new NET_MANUAL_SNAP_PARAMETER();
par.byReserved = new byte[60];
par.nChannel = passage;
par.bySequence = "";
IntPtr parPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(NET_MANUAL_SNAP_PARAMETER)));
Marshal.StructureToPtr(par, parPtr, true);
bool ret = NETClient.ControlDevice(m_LoginID, EM_CtrlType.MANUAL_SNAP, parPtr, m_WaitTime);
Marshal.FreeHGlobal(parPtr);
if (!ret)
{
return;
}
}
#endregion
private static string GetVehicleSize(EM_VehicleSizeType nVehicleSize)
{
string result = "UnKnow(未知)";
switch (nVehicleSize)
{
case EM_VehicleSizeType.UnKnow:
break;
case EM_VehicleSizeType.Light_Duty:
result = "Light-duty(小型车)";
break;
case EM_VehicleSizeType.Medium:
result = "Medium(中型车)";
break;
case EM_VehicleSizeType.Oversize:
result = "Oversize(大型车)";
break;
case EM_VehicleSizeType.Minisize:
result = "Minisize(微型车)";
break;
case EM_VehicleSizeType.Largesize:
result = "Largesize(长车)";
break;
default:
break;
}
return result;
}
}
}