Commit b4d8baf5 张东亮

添加监控相机配置

1 个父辈 0d25c6ad
...@@ -34,10 +34,12 @@ public class HIKCamera ...@@ -34,10 +34,12 @@ public class HIKCamera
public event EventHandler<Bitmap> camera_event; public event EventHandler<Bitmap> camera_event;
public string DeviceName = "monitor1"; public string DeviceName = "monitor1";
public PictureBox picture = new PictureBox(); public PictureBox picture = new PictureBox();
public bool LoadCameraConfig(string CameraID, out string msg,object preview=null) bool enableCamera = ConfigHelper.Config.Get("Func_EnableCam", true);
public bool LoadCameraConfig(string CameraID, out string msg, object preview = null)
{ {
Name = CameraID; Name = CameraID;
msg = ""; msg = "";
if (!enableCamera) return true;
string path = $".\\Config\\{CameraID}.json"; string path = $".\\Config\\{CameraID}.json";
if (!File.Exists(path)) if (!File.Exists(path))
{ {
...@@ -65,12 +67,12 @@ public class HIKCamera ...@@ -65,12 +67,12 @@ public class HIKCamera
return false; return false;
} }
//PictureBox p1 = new PictureBox(); //PictureBox p1 = new PictureBox();
if(preview==null) if (preview == null)
{ {
camera.PreviewImage(DeviceName, picture.Handle); camera.PreviewImage(DeviceName, picture.Handle);
} }
else else
{ {
camera.PreviewImage(DeviceName, (IntPtr)preview); camera.PreviewImage(DeviceName, (IntPtr)preview);
} }
camera.Open(DeviceName); camera.Open(DeviceName);
...@@ -89,7 +91,8 @@ public class HIKCamera ...@@ -89,7 +91,8 @@ public class HIKCamera
} }
int errortimes = 0; int errortimes = 0;
bool camerathreadrun = true; bool camerathreadrun = true;
public void stopCamera() { public void stopCamera()
{
camerathreadrun = false; camerathreadrun = false;
} }
void startCamera() void startCamera()
...@@ -119,7 +122,7 @@ public class HIKCamera ...@@ -119,7 +122,7 @@ public class HIKCamera
camera.Close(DeviceName); camera.Close(DeviceName);
Thread.Sleep(1000); Thread.Sleep(1000);
camera.Open(DeviceName); camera.Open(DeviceName);
errortimes=0; errortimes = 0;
errorsleeptime = errorsleeptime * 2; errorsleeptime = errorsleeptime * 2;
LogUtil.error(Name + $"相机错误次数过多,重新打开,{errortimes}"); LogUtil.error(Name + $"相机错误次数过多,重新打开,{errortimes}");
} }
...@@ -144,11 +147,11 @@ public class HIKCamera ...@@ -144,11 +147,11 @@ public class HIKCamera
} }
void saveTest(Bitmap bitmap) void saveTest(Bitmap bitmap)
{ {
if(!Directory.Exists(".\\TestImg")) if (!Directory.Exists(".\\TestImg"))
{ {
Directory.CreateDirectory(".\\TestImg"); Directory.CreateDirectory(".\\TestImg");
} }
bitmap.Save(Path.Combine(".\\TestImg\\",$"{DateTime.Now.ToString("yyyyMMddhhmmssfff")}.bmp")); bitmap.Save(Path.Combine(".\\TestImg\\", $"{DateTime.Now.ToString("yyyyMMddhhmmssfff")}.bmp"));
} }
public void CameraGrabOne(string filename) public void CameraGrabOne(string filename)
{ {
...@@ -173,7 +176,8 @@ public class HIKCamera ...@@ -173,7 +176,8 @@ public class HIKCamera
public string GetFixtureStateFilename(string PositionNum, string WareNumber, StoreMoveType storeMoveType, FixtureState fixtureState) public string GetFixtureStateFilename(string PositionNum, string WareNumber, StoreMoveType storeMoveType, FixtureState fixtureState)
{ {
Path.GetInvalidFileNameChars().ToList().ForEach((ix) => { WareNumber = WareNumber.Replace(ix.ToString(), ""); }); Path.GetInvalidFileNameChars().ToList().ForEach((ix) => { WareNumber = WareNumber.Replace(ix.ToString(), ""); });
if (WareNumber.Length > 150) { if (WareNumber.Length > 150)
{
WareNumber = ""; WareNumber = "";
} }
if (string.IsNullOrEmpty(WareNumber)) if (string.IsNullOrEmpty(WareNumber))
......
...@@ -12,33 +12,15 @@ namespace DeviceLibrary ...@@ -12,33 +12,15 @@ namespace DeviceLibrary
{ {
public class IPCameraHelper public class IPCameraHelper
{ {
//"E:\\Codes\\CSharp-Workspace\\MyProject\\WindowsService\\IPCamService\\bin\\Debug\\IPCamService.exe" static bool Func_EnableTriggerIPCam = ConfigHelper.Config.Get("Func_EnableTriggerIPCam", false);
static string appPath = AppDomain.CurrentDomain.BaseDirectory;
static string serviceFilePath = ConfigHelper.Config.Get("IPCamService_FilePath", $"{appPath}IPCamService\\IPCamService.exe");
static string serviceName = ConfigHelper.Config.Get("IPCamService_ServiceName", "IPCamService");
/// <summary>
/// 安装服务
/// </summary>
public static void InstallService()
{
if (!IsServiceExisted(serviceName))
{
InstallService(serviceFilePath);
LogUtil.info("安装监控相机服务");
ServiceStart(serviceName);
LogUtil.info("启动监控相机服务");
}
else
{
ServiceStart(serviceName);
LogUtil.info("启动监控相机服务");
}
}
static string baseDir = ConfigHelper.Config.Get("IPCameraService_HttpServer", "http://localhost:8088"); static string baseDir = ConfigHelper.Config.Get("IPCameraService_HttpServer", "http://localhost:8088");
static string cameName= ConfigHelper.Config.Get("IPCameraService_CamName", "cam1"); static string cameName = ConfigHelper.Config.Get("IPCameraService_CamName", "cam1");
public static void StartRecord(string fileName="") public static void StartRecord(string fileName = "")
{ {
Task.Factory.StartNew(delegate { if (!Func_EnableTriggerIPCam) return;
Task.Factory.StartNew(delegate
{
string url = $"{baseDir}/cam/startRecord?camName={cameName}&filename={fileName}"; string url = $"{baseDir}/cam/startRecord?camName={cameName}&filename={fileName}";
string res = HttpHelper.Get(url); string res = HttpHelper.Get(url);
LogUtil.info($"开始记录视频:{fileName},{res}"); LogUtil.info($"开始记录视频:{fileName},{res}");
...@@ -47,6 +29,7 @@ namespace DeviceLibrary ...@@ -47,6 +29,7 @@ namespace DeviceLibrary
} }
public static void StopRecord() public static void StopRecord()
{ {
if (!Func_EnableTriggerIPCam) return;
Task.Factory.StartNew(delegate Task.Factory.StartNew(delegate
{ {
string url = $"{baseDir}/cam/stopRecord?camName={cameName}"; string url = $"{baseDir}/cam/stopRecord?camName={cameName}";
...@@ -54,79 +37,6 @@ namespace DeviceLibrary ...@@ -54,79 +37,6 @@ namespace DeviceLibrary
LogUtil.info($"停止记录视频:{res}"); LogUtil.info($"停止记录视频:{res}");
}); });
} }
//判断服务是否存在
static bool IsServiceExisted(string serviceName)
{
ServiceController[] services = ServiceController.GetServices();
foreach (ServiceController sc in services)
{
if (sc.ServiceName.ToLower() == serviceName.ToLower())
{
return true;
}
}
return false;
}
//安装服务
static void InstallService(string serviceFilePath)
{
try
{
using (AssemblyInstaller installer = new AssemblyInstaller())
{
installer.UseNewContext = true;
installer.Path = serviceFilePath;
IDictionary savedState = new Hashtable();
installer.Install(savedState);
installer.Commit(savedState);
}
}catch (Exception ex)
{
LogUtil.error("安装监控相机服务失败",ex);
}
}
//卸载服务
static void UninstallService(string serviceFilePath)
{
using (AssemblyInstaller installer = new AssemblyInstaller())
{
installer.UseNewContext = true;
installer.Path = serviceFilePath;
installer.Uninstall(null);
}
}
//启动服务
static void ServiceStart(string serviceName)
{
try
{
using (ServiceController control = new ServiceController(serviceName))
{
if (control.Status == ServiceControllerStatus.Stopped)
{
control.Start();
}
}
}catch (Exception ex)
{
LogUtil.error($"启动监控相机服务失败",ex);
}
}
//停止服务
static void ServiceStop(string serviceName)
{
using (ServiceController control = new ServiceController(serviceName))
{
if (control.Status == ServiceControllerStatus.Running)
{
control.Stop();
}
}
}
} }
} }
...@@ -190,7 +190,6 @@ namespace DeviceLibrary ...@@ -190,7 +190,6 @@ namespace DeviceLibrary
ConfigHelper.Config.Get("CamTestReel_Ability", false); ConfigHelper.Config.Get("CamTestReel_Ability", false);
ConfigHelper.Config.Set("CamTestReel_debug", false); ConfigHelper.Config.Set("CamTestReel_debug", false);
ConfigHelper.Config.Get("Device_1315_ReelHeight_Compensation", 0); ConfigHelper.Config.Get("Device_1315_ReelHeight_Compensation", 0);
IPCameraHelper.InstallService();
} }
private void Crc_LanguageChangeEvent(object sender, EventArgs e) private void Crc_LanguageChangeEvent(object sender, EventArgs e)
......
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!