geboshi_V1/LeatherProject/LeatherApp/Config.cs

559 lines
24 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using LeatherApp.Utils;
using Newtonsoft.Json.Linq;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace LeatherApp
{
public class Config
{
public static string appBasePath;
public static Models.User loginUser;
//对应文件DefectItemList.json
//public static JArray defectItemList;
//
public static JArray colorNameList;//, materialNameList;
//
public static JArray materialNameList;//, materialNameList;
//CMDProcess目录下的命令JSON文件
public static Dictionary<CMDName, JObject> CMDProcess;
/// <summary>
/// 剩余N米提示(M)
/// </summary>
public static int residueWarnningLen = 20;
/// <summary>
/// 定单N米提示(M)
/// </summary>
public static int orderWarnningLen = 20;
//瑕疵暂停跳过几张图
public static int defectPauseSkipPhotoCount=5;
//DB
//有些服务器防火墙有问题需要加上 min pool size=1 避免认为是恶意请求
//如果用到bulkCopy需要加 AllowLoadLocalInfile=true
//public static string dbMysqlCon = "server=localhost;Database=ProductionDB;Uid=root;Pwd=123456;";
public static string DBConStr = "server=localhost;Database=LeatherDB;Uid=root;Pwd=123456; AllowLoadLocalInfile=true";
public static string ErpDBConStr = "server=localhost;Database=LeatherDB;Uid=root;Pwd=123456; AllowLoadLocalInfile=true";
public static string ErpSql = "select top 1 a,b,c from tableA where code=@code order by id desc";
//使用oracle数据库
public static string ErpDBType = "sqlServer";
//远程服务器连接字段
public static string ServerDBConStr = "server=localhost;Database=LeatherDB;Uid=root;Pwd=123456; AllowLoadLocalInfile=true";
//LOG
public static string LogPath;
//DEV
public static bool StopCodeScanner = false;
public static bool StopIO = false;
public static bool StopPLC=true;
public static bool StopLight = true;
public static string PlcIPAdrees;
public static short PlcRackN=0, PlcSolt=1;
public static string Carmer1ConfigFilePath,Carmer2ConfigFilePath;
public static string IOCard_DeviceNum = "PCI-1730,BID#0";
public static bool ScannerReversalX = false;
public static bool ScannerReversalY = false;
public static string Light_PortName;//COM端口号
public static LightDevNameEnum Light_Name = LightDevNameEnum.;
public static CamerDevNameEnum Camer_Name = CamerDevNameEnum.;
public static string CeHouIP;
public static int CeHouPort;
public static int CeHouCnt;
public static double JinShuTanCeDis;
public static string CustomerName;
public static string ThicknessNames;
public static string JM_PortName;//COM端口号
//LIB
public static string model_path = "";//./models/best_0805_bs8.onnx
//public static string labels_path = "./models/hexin.names";
public static int cm2px_x = 1;
public static int cm2px_y = 1;
//边缘孔洞宽(门幅计算包含,识别库去除)
public static int MarginHoleWidth = 120;
public static int MiddleSuperposition;
public static int expand_pixel=50;
public static string ImagePath;
public static bool IsSaveAllImage, IsSaveDefectSourceImage, IsSaveDefectCutImage;
//材质Material 1
public static string[] SuedeList1 = new string[0];
//材质Material 2
public static string[] SuedeList2 = new string[0];
//材质Material 3
public static string[] SuedeList3 = new string[0];
//材质Material 4
public static string[] SuedeList4 = new string[0];
//材质Material 5
public static string[] SuedeList5 = new string[0];
//材质Material 6
public static string[] SuedeList6 = new string[0];
//忽略的等级判断项目
public static string[] SkipLabelGrade = new string[0];
//云端
public static string cloud_ip, cloud_username, cloud_password, CloudThisName;
public static int cloud_port, cloud_open;
//传统算法
public static bool OpenHalconDefect;
public static string HalconParamPath;
//开启蜂鸣
public static bool OpenBeep;
public static int BeepTime;
//开启检出就报警
public static bool OpenWarnBeep;
//边补偿
public static int L_offset, R_offset;
public static double EdgeOffset;//幅宽补偿
//开启金属检测
public static bool OpenJinShuJianCe;
//开启厚度记录
public static bool OpenHouDuJiLu;
public static double DataOffset1; //数据1补偿
public static double DataOffset2; //数据2补偿
public static double DataOffset3; //数据3补偿
//开启分卷功能
public static bool OpenFenJuan;
public static double CutDis; //裁切位置
//开启计米停车
public static bool OpenJMStop;
public static double StopLookDis;
//图片自动删除
public static bool OpenClearPic;
public static int ClearDays;
//远程服务地址
public static string ServerIP;
//计米器显示补偿
public static double JMOffset;
//降等原因
public static Dictionary<string, string> DownGradeReason;
//开启超速报警
public static bool OpenOverSpeed;
//
public static void LoadCloudConfig()
{
if (string.IsNullOrWhiteSpace(appBasePath))
appBasePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
IniFile ini = new IniFile(appBasePath + "\\CloudConfig.ini");
cloud_ip = ini.ReadString("Cloud", "cloud_ip", "");
cloud_port = ini.ReadInt("Cloud", "cloud_port", 0);
cloud_open = ini.ReadInt("Cloud", "cloud_open", 0);
cloud_username = ini.ReadString("Cloud", "cloud_username", "");
cloud_password = ini.ReadString("Cloud", "cloud_password", "");
CloudThisName = ini.ReadString("Cloud", "CloudThisName", "");
}
public static void LoadAllConfig()
{
if (string.IsNullOrWhiteSpace(appBasePath))
appBasePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
DownGradeReason = LoadDownGradeReasonInfo();
LoadDefectItemList();
LoadColorNameList();
LoadMaterialNameList();
//----
string lsCmdPath = appBasePath + "\\CMDProcess\\";
if (!Directory.Exists(lsCmdPath))
Directory.CreateDirectory(lsCmdPath);
string TempPicPath = appBasePath + "\\TempPic\\";
if (!Directory.Exists(TempPicPath))
Directory.CreateDirectory(TempPicPath);
string fileName;
CMDProcess = new Dictionary<CMDName, JObject>();
foreach (int item in Enum.GetValues(typeof(CMDName)))//item为key值
{
//string name = Enum.GetName(typeof(CMDName), item);//获取名称
fileName = lsCmdPath + item + ".json";
if (File.Exists(fileName))
CMDProcess.Add((CMDName)item, JObject.Parse(File.ReadAllText(fileName)));
//else
// CMDProcess.Add((CMDName)item, null);
}
// ----
IniFile ini = new IniFile(appBasePath + "\\SysConfig.ini");
//[DB]
DBConStr = ini.ReadString("DB", "DBConStr", "");
ErpDBType = ini.ReadString("DB", "ErpDBType", "SqlServer");
if (string.IsNullOrEmpty(ErpDBType))
ErpDBType = "SqlServer";
//[Erp]
ErpDBConStr = ini.ReadString("ErpDB", "ErpDBConStr", "");
ErpSql = ini.ReadString("ErpDB", "ErpSql", "");
ServerDBConStr = ini.ReadString("ErpDB", "ServerDBConStr", "");
//[LOG]
LogPath = ini.ReadString("LOG", "LogPath", "");
//[CONTROL]
defectPauseSkipPhotoCount = ini.ReadInt("CONTROL", "DefectPauseSkipPhotoCount", 0);
//[DEV]
StopCodeScanner = ini.ReadBool("DEV", "StopCodeScanner", false);
StopIO = ini.ReadBool("DEV", "StopIO", false);
StopPLC = ini.ReadBool("DEV", "StopPLC", false);
PlcIPAdrees = ini.ReadString("DEV", "PlcIPAdrees", "");
PlcRackN = ini.ReadShort("DEV", "PlcRackN", PlcRackN);
PlcSolt = ini.ReadShort("DEV", "PlcSolt", PlcSolt);
StopLight = ini.ReadBool("DEV", "StopLight", false);
Carmer1ConfigFilePath = ini.ReadString("DEV", "Carmer1ConfigFilePath", "");
Carmer2ConfigFilePath = ini.ReadString("DEV", "Carmer2ConfigFilePath", "");
IOCard_DeviceNum = ini.ReadString("DEV", "IOCard_DeviceNum", "");
Light_Name = (LightDevNameEnum)ini.ReadInt("DEV", "Light_Name");
Light_PortName = ini.ReadString("DEV", "Light_PortName", "");
Camer_Name = (CamerDevNameEnum)ini.ReadInt("DEV", "Camer_Name");
ScannerReversalX = ini.ReadBool("DEV", "ScannerReversalX", false);
ScannerReversalY = ini.ReadBool("DEV", "ScannerReversalY", false);
JM_PortName = ini.ReadString("DEV", "JM_PortName", "");
//StopCeHou = ini.ReadBool("DEV", "StopCeHou", true);
CeHouIP = ini.ReadString("DEV", "CeHouIP", "");
CeHouPort = ini.ReadInt("DEV", "CeHouPort");
CeHouCnt = ini.ReadInt("DEV", "CeHouCnt");
JinShuTanCeDis = ini.ReadDouble("DEV", "JinShuTanCeDis");
residueWarnningLen = ini.ReadInt("DEV", "residueWarnningLen", 20);
orderWarnningLen = ini.ReadInt("DEV", "orderWarnningLen", 0);
ThicknessNames = ini.ReadString("DEV", "ThicknessNames", "数据1,数据2数据3");
if (ThicknessNames.Split(',').Length < 3)
ThicknessNames = "数据1,数据2,数据3";
//[LIB]
model_path = ini.ReadString("LIB", "model_path", "");
//labels_path = ini.ReadString("LIB", "labels_path", "");
cm2px_x = ini.ReadInt("LIB", "cm2px_x", 1);
cm2px_y = ini.ReadInt("LIB", "cm2px_y", 1);
MarginHoleWidth = ini.ReadInt("LIB", "MarginHoleWidth", 0);
MiddleSuperposition = ini.ReadInt("LIB", "MiddleSuperposition", 0);
expand_pixel=ini.ReadInt("LIB", "expand_pixel", 50);
ImagePath = ini.ReadString("LIB", "ImagePath", "");
IsSaveAllImage = ini.ReadBool("LIB", "IsSaveAllImage", false);
IsSaveDefectSourceImage = ini.ReadBool("LIB", "IsSaveDefectSourceImage", false);
IsSaveDefectCutImage = ini.ReadBool("LIB", "IsSaveDefectCutImage", false);
//
string lsTmp = ini.ReadString("Material", "SuedeList", "").Trim();
if (!string.IsNullOrWhiteSpace(lsTmp))
SuedeList1 = lsTmp.Split(new char[] { ',', ';' });
else
SuedeList1 = new string[1] { "" };
lsTmp = ini.ReadString("Material", "SuedeList2", "").Trim();
if (!string.IsNullOrWhiteSpace(lsTmp))
SuedeList2 = lsTmp.Split(new char[] { ',', ';' });
else
SuedeList2 = new string[1] { "" };
lsTmp = ini.ReadString("Material", "SuedeList3", "").Trim();
if (!string.IsNullOrWhiteSpace(lsTmp))
SuedeList3 = lsTmp.Split(new char[] { ',', ';' });
else
SuedeList3 = new string[1] { "" };
lsTmp = ini.ReadString("Material", "SuedeList4", "").Trim();
if (!string.IsNullOrWhiteSpace(lsTmp))
SuedeList4 = lsTmp.Split(new char[] { ',', ';' });
else
SuedeList4 = new string[1] { "" };
lsTmp = ini.ReadString("Material", "SuedeList5", "").Trim();
if (!string.IsNullOrWhiteSpace(lsTmp))
SuedeList5 = lsTmp.Split(new char[] { ',', ';' });
else
SuedeList5 = new string[1] { "" };
lsTmp = ini.ReadString("Material", "SuedeList6", "").Trim();
if (!string.IsNullOrWhiteSpace(lsTmp))
SuedeList6 = lsTmp.Split(new char[] { ',', ';' });
else
SuedeList6 = new string[1] { "" };
lsTmp = ini.ReadString("SkipLabelGrade", "LabelGradeList", "").Trim();
if (!string.IsNullOrWhiteSpace(lsTmp))
SkipLabelGrade = lsTmp.Split(new char[] { ',', ';' });
else
SkipLabelGrade = new string[1] { "" };
//传统参数
OpenHalconDefect = ini.ReadBool("Halcon", "OpenHalconDefect");
lsTmp = ini.ReadString("Halcon", "HalconParamPath", "").Trim();
if (!string.IsNullOrWhiteSpace(lsTmp))
HalconParamPath = lsTmp;
else
HalconParamPath = appBasePath;
//蜂鸣
OpenBeep = ini.ReadBool("Beep", "OpenBeep");
OpenWarnBeep = ini.ReadBool("Beep", "OpenWarnBeep");
BeepTime = ini.ReadInt("Beep", "BeepTime", 3000);
L_offset = ini.ReadInt("BOffset", "L_offset", 0);
R_offset = ini.ReadInt("BOffset", "R_offset", 0);
EdgeOffset = ini.ReadDouble("BOffset", "EdgeOffset", 0);
CustomerName = ini.ReadString("Customer", "CustomerName", "").Trim();
//开启金属检测
OpenJinShuJianCe = ini.ReadBool("Fun", "OpenJinShuJianCe");
OpenHouDuJiLu = ini.ReadBool("Fun", "OpenHouDuJiLu");
OpenFenJuan = ini.ReadBool("Fun", "OpenFenJuan");
CutDis = ini.ReadDouble("Fun", "CutDis");
OpenJMStop = ini.ReadBool("Fun", "OpenJMStop");
StopLookDis = ini.ReadDouble("Fun", "StopLookDis");
OpenClearPic = ini.ReadBool("Fun", "OpenClearPic");
ClearDays = ini.ReadInt("Fun", "ClearDays");
OpenOverSpeed = ini.ReadBool("Fun", "OpenOverSpeed");
DataOffset1 = ini.ReadDouble("HouDuOffset", "DataOffset1", 0);
DataOffset2 = ini.ReadDouble("HouDuOffset", "DataOffset2", 0);
DataOffset3 = ini.ReadDouble("HouDuOffset", "DataOffset3", 0);
ServerIP = ini.ReadString("ServerVD", "ServerIP", "172.16.21.210");
JMOffset = ini.ReadDouble("Fun", "JMOffset", 0);
}
public static Dictionary<string, string> LoadDownGradeReasonInfo()
{
Dictionary<string, string> data = new Dictionary<string, string>();
if (string.IsNullOrWhiteSpace(appBasePath))
appBasePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
string configPath = appBasePath + "\\降等原因.csv";
if (!File.Exists(configPath))
return data;
try
{
string lsTmp = File.ReadAllText(configPath, Encoding.Default);
var line = lsTmp.Split("\r\n");
int index = 0;
foreach (var item in line)
{
if (index++ == 0) //忽略第一行标题
continue;
try
{
if(item.IndexOf("\t") >=0)
data.Add(item.Split("\t")[0], item.Split("\t")[1]);
else if (item.IndexOf(",") >= 0)
data.Add(item.Split(",")[0], item.Split(",")[1]);
}
catch {
;
}
}
}
catch { }
return data;
}
#region defectItemList <=> DefectItemList.json
public static JArray LoadDefectItemList()
{
JArray defectItemList = new JArray();
if (string.IsNullOrWhiteSpace(appBasePath))
appBasePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
string configPath = appBasePath + "\\DefectItemList.json";
string lsTmp = File.ReadAllText(configPath);
defectItemList = JArray.Parse(lsTmp);
JObject obj = new JObject
{
{ "id", 100 },
{ "code", "JSYC" },
{ "name", "金属" },
{ "color", "Red" }
};
defectItemList.Add(obj);
return defectItemList;
}
private static string Pre_modelName;
private static JArray defectlabelList;
//根据模型加载label
public static bool LoadModelDefectItemList(string modelName, out JArray defectItemList)
{
bool ret = true;
if (string.IsNullOrWhiteSpace(appBasePath))
appBasePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
if (string.IsNullOrEmpty(modelName))
modelName = "";
string configPath = appBasePath + $"\\{modelName.Replace(".trt","")}.json";
if (!File.Exists(configPath))
{
configPath = appBasePath + "\\DefectItemList.json";
ret = false;
}
if (Pre_modelName == configPath)
{
defectItemList = defectlabelList;
}
else
{
Pre_modelName = configPath;
string lsTmp = File.ReadAllText(configPath);
defectItemList = JArray.Parse(lsTmp);
defectlabelList = JArray.Parse(lsTmp);
if (OpenJinShuJianCe)
{
JObject obj = new JObject
{
{ "id", 100 },
{ "code", "JSYC" },
{ "name", "金属" },
{ "color", "Red" }
};
defectItemList.Add(obj);
defectlabelList.Add(obj);
}
}
return ret;
}
public static void SaveDefectItemList(JArray newValue)
{
if (string.IsNullOrWhiteSpace(appBasePath))
appBasePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
string configPath = appBasePath + "\\DefectItemList.json";
File.WriteAllText(configPath, newValue.ToString());
}
public static JObject getDefectItem(string modelName, string code)
{
try
{
JArray defectItemList;
LoadModelDefectItemList(modelName, out defectItemList);
if (code == "ng")
{
JObject obj = new JObject();
obj.Add("id", 0);
obj.Add("code", "ng");
obj.Add("name", "NG");
obj.Add("color", "Red");
return obj;
}
var item = defectItemList.FirstOrDefault(m => m.Value<string>("code") == code);
if (item == null)
return null;
return (JObject)item;
}
catch { return null; }
}
public static JObject getDefectItem(string modelName, int id)
{
try
{
JArray defectItemList;
LoadModelDefectItemList(modelName, out defectItemList);
if (id == -99)
{
JObject obj = new JObject();
obj.Add("id", 0);
obj.Add("code", "ng");
obj.Add("name", "NG");
obj.Add("color", "Red");
return obj;
}
var item = defectItemList.FirstOrDefault(m => m.Value<int>("id") == id);
if (item == null)
return null;
return (JObject)item;
}
catch { return null; }
}
public static string getDefectCode(string modelName, int id)
{
try
{
JArray defectItemList;
LoadModelDefectItemList(modelName, out defectItemList);
var item = defectItemList.FirstOrDefault(m => m.Value<int>("id") == id);
if (item == null)
return "";
return ((JObject)item).Value<string>("code");
}
catch { return ""; }
}
public static string getDefectName(string modelName, string code)
{
try {
JArray defectItemList;
LoadModelDefectItemList(modelName, out defectItemList);
var item = defectItemList.FirstOrDefault(m => m.Value<string>("code") == code);
if (item == null)
return "";
return ((JObject)item).Value<string>("name");
}
catch { return ""; }
}
public static string getDefectCode(string modelName, string name)
{
try {
JArray defectItemList;
LoadModelDefectItemList(modelName, out defectItemList);
var item = defectItemList.FirstOrDefault(m => m.Value<string>("name") == name);
if (item == null)
return "";
return ((JObject)item).Value<string>("code");
}
catch { return ""; }
}
#endregion
#region colorNameList <=> ColorNameList.json
public static void LoadColorNameList()
{
if (string.IsNullOrWhiteSpace(appBasePath))
appBasePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
string configPath = appBasePath + "\\ColorNameList.json";
string lsTmp = File.ReadAllText(configPath);
colorNameList = JArray.Parse(lsTmp);
}
public static JObject getColorItem(int code)
{
var item = Config.colorNameList.FirstOrDefault(m => m.Value<int>("code") == code);
if (item == null)
return null;
return (JObject)item;
}
public static void LoadMaterialNameList()
{
if (string.IsNullOrWhiteSpace(appBasePath))
appBasePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
string configPath = appBasePath + "\\MaterialName.json";
string lsTmp = File.ReadAllText(configPath);
materialNameList = JArray.Parse(lsTmp);
}
//public static JObject getMaterialItem(string code)
//{
// var item = Config.materialNameList.FirstOrDefault(m => m.Value<string>("code") == code);
// if (item == null)
// return null;
// return (JObject)item;
//}
#endregion
}
}