VisionHelper.cs
3.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
using Common;
using OnlineStore.Common;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.Eventing.Reader;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace DeviceLibrary
{
public class VisionHelper
{
static string visionUrl = ConfigHelper.Config.Get("VisionUrl", "http://localhost:8089/vision/eyem");
public static bool ReelStatusCheck(string configName)
{
string cameraName = ConfigHelper.Config.Get("MonitorCamName", "cam1");
string url = visionUrl + $"/reelStatusCheck/cam?camName={cameraName}&configName={configName.Replace("#", "%23")}";
try
{
CheckAndRunServer();
string json = HttpHelper.Get(url);
Result result = JsonHelper.DeserializeJsonToObject<Result>(json);
if (result == null)
{
LogUtil.info($"reelStatusCheck 无反馈,重试");
Thread.Sleep(1000);
json = HttpHelper.Get(url);
result = JsonHelper.DeserializeJsonToObject<Result>(json);
}
if (result != null && result.code == 0)
{
LogUtil.info($"{configName} 料盘放置正常");
return true;
}
else
{
LogUtil.info($"{configName} 料盘放置异常");
return false;
}
}
catch (Exception ex)
{
LogUtil.error($"ReelStatusCheck {configName}", ex);
}
return false;
}
static Process EyemFeatureSet = new Process();
static int webclienttimeout = 30000;
public static void CheckAndRunServer()
{
try
{
lock (EyemFeatureSet)
{
Process[] processesByName = Process.GetProcessesByName("EyemFeatureSet");
if (processesByName.Length != 0)
{
return;
}
string text = "Modules\\EyemFeatureSet\\EyemFeatureSet.exe";
if (!File.Exists(text))
{
throw new Exception("找不到算法服务器文件");
}
EyemFeatureSet = ProcessUtil.StartProcess("EyemFeatureSet", Application.StartupPath + "\\Modules\\EyemFeatureSet\\");
int num = 5;
while (num > 0)
{
num--;
Thread.Sleep(1000);
MyWebClient myWebClient = new MyWebClient(webclienttimeout);
string text2 = myWebClient.DownloadString(visionUrl + "/alive");
if (text2.Trim() == "\"1\"")
{
return;
}
}
throw new Exception("算法服务器文件打开失败");
}
}catch(Exception ex)
{
LogUtil.error("CheckAndRunServer EyemFeatureSet.exe", ex);
}
}
public class Result
{
/// <summary>
/// 状态码,0为正常
/// </summary>
public int code { get; set; } = 0;
/// <summary>
/// 返回数据
/// </summary>
public Dictionary<string, string> data { get; set; }
/// <summary>
/// 提示信息
/// </summary>
public string msg { get; set; } = "ok";
}
}
}