VisionHelper.cs 4.0 KB
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.Web.Management;
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)
        {
            if(ConfigHelper.Config.Get("Func_ReelStatusCheck", false))
            {
                return true;
            }
            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);
                        using (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";
        }
    }
}