GetConfig.cs 5.8 KB
using System;
using System.Collections.Generic;
using CameraVisionLib.Model;

namespace Asa.Camera
{
    public partial class VisionLib : IDisposable
    {
        private void GetConfig(string path)
        {
            Common.log.Debug("GetConfig Path=" + path);
            scanParam = new();
            regionLists = new();
            ipCameraInfos = new();
            if (!System.IO.File.Exists(path)) return;

            string json = System.IO.File.ReadAllText(path);
            System.Web.Script.Serialization.JavaScriptSerializer serializer = new();
            Dictionary<string, object> dic = (Dictionary<string, object>)serializer.DeserializeObject(json);
            
            if (dic.TryGetValue("CodeOrder", out object value))
                GetConfig_CodeOrder(value);
            if (dic.TryGetValue("Halcon", out value))
                GetConfig_Halcon(value);
            if (dic.TryGetValue("EyemLib", out value))
                GetConfig_EyemLib(value);
            if (dic.TryGetValue("Region", out value))
                GetConfig_Region(value);
            if (dic.TryGetValue("HIKIPCamera", out value))
                GetConfig_HIKIPCamera(value);
        }

        private void GetConfig_CodeOrder(object param)
        {
            Common.log.Debug("GetConfig_CodeOrder");
            scanParam.CodeOrder = (ScanCodeOrder)Enum.Parse(typeof(ScanCodeOrder), param.ToString());
        }

        private void GetConfig_Halcon(object param)
        {
            Common.log.Debug("GetConfig_Halcon");
            Dictionary<string, object> dict = (Dictionary<string, object>)param;
            if (dict.TryGetValue("CodeType", out object value))
                scanParam.HalconType = (CodeType)Enum.Parse(typeof(CodeType), value.ToString());
            if (dict.TryGetValue("Zoom1DCode", out value))
                scanParam.Zoom1D = Convert.ToSingle(value);
            if (dict.TryGetValue("Zoom2DCode", out value))
                scanParam.Zoom2D = Convert.ToSingle(value);
        }

        private void GetConfig_EyemLib(object param)
        {
            Common.log.Debug("GetConfig_EyemLib");
            Dictionary<string, object> dict = (Dictionary<string, object>)param;
            if (dict.TryGetValue("CodeType", out object value))
                scanParam.EyemType = (CodeType)Enum.Parse(typeof(CodeType), value.ToString());
            if (dict.TryGetValue("BlockSize", out value))
                scanParam.EyemBlockSize = Convert.ToInt32(value);
            if (dict.TryGetValue("RangeC", out value))
                scanParam.EyemRangeC = Convert.ToInt32(value);
            if (dict.TryGetValue("SymbolMin", out value))
                scanParam.EyemSymbolMin = Convert.ToInt32(value);
            if (dict.TryGetValue("SymbolMax", out value))
                scanParam.EyemSymbolMax = Convert.ToInt32(value);
        }

        private void GetConfig_Region(object param)
        {
            Common.log.Debug("GetConfig_Region");
            Dictionary<string, object> dict = (Dictionary<string, object>)param;
            foreach (string cameraKey in dict.Keys)
            {
                List<RegionPlan> planList = new();
                Dictionary<string, object> list = (Dictionary<string, object>)dict[cameraKey];
                foreach (string listKey in list.Keys)
                {
                    List<RegionInfo> infoLists = new();
                    object[] obj = (object[])list[listKey];
                    for (int i = 0; i < obj.Length; i++)
                    {
                        Dictionary<string, object> regionParam = (Dictionary<string, object>)obj[i];
                        string name = "";
                        int x = 0, y = 0, w = 0, h = 0;
                        double r = 0;
                        if (regionParam.TryGetValue("RegionName", out object value))
                             name = value.ToString();
                        if (regionParam.TryGetValue("X", out value))
                            int.TryParse(value.ToString(), out x);
                        if (regionParam.TryGetValue("Y", out value))
                            int.TryParse(value.ToString(), out y);
                        if (regionParam.TryGetValue("Width", out value))
                            int.TryParse(value.ToString(), out w);
                        if (regionParam.TryGetValue("Height", out value))
                            int.TryParse(value.ToString(), out h);
                        if (regionParam.TryGetValue("Ratio", out value))
                            double.TryParse(value.ToString(), out r);
                        infoLists.Add(new RegionInfo(name, x, y, w, h, r));
                    }
                    planList.Add(new RegionPlan(listKey, infoLists));
                }
                regionLists.Add(cameraKey, planList);
            }
        }

        private void GetConfig_HIKIPCamera(object param)
        {
            Common.log.Debug("GetConfig_HIKIPCamera");
            object[] obj = (object[])param;
            for (int i = 0; i < obj.Length; i++)
            {
                Dictionary<string, object> dict = (Dictionary<string, object>)obj[i];
                IPCameraInfo item = new();
                System.Reflection.PropertyInfo[] info = item.GetType().GetProperties();
                for (int j = 0; j < info.Length; j++)
                {
                    if (dict.TryGetValue(info[j].Name, out object value))
                    {
                        if (info[j].PropertyType == typeof(string))
                            info[j].SetValue(item, value.ToString());
                        else if (info[j].PropertyType == typeof(int))
                            info[j].SetValue(item, Convert.ToInt32(value));
                    }
                }
                ipCameraInfos.Add(item);
            }
        }
    }
}