MemoryTableDataSocket.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. using iCore.Rtdb;
  2. using iCore.Rtdb.RtdbTable;
  3. using Newtonsoft.Json.Linq;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. namespace Common
  8. {
  9. public class MemoryTableDataSocket
  10. {
  11. private CoredbClient cln = null;
  12. //[Obsolete]
  13. public MemoryTableDataSocket(string sPoint)
  14. { //""tarantool://guest@192.1.3.54:20"+计量点编号
  15. try
  16. {
  17. cln = new CoredbClient(AppConfigCache.icoredbTcp);// + sPoint.Substring(3, 2));//计量网
  18. }
  19. catch (Exception ex)
  20. {
  21. ChangeCln();
  22. }
  23. }
  24. public MemoryTableDataSocket(string sPoint, string serverUrl)
  25. { //""tarantool://guest@192.1.3.54:20"+计量点编号
  26. try
  27. {
  28. cln = new CoredbClient(serverUrl);// + sPoint.Substring(3, 2));//计量网
  29. }
  30. catch (Exception ex)
  31. {
  32. ChangeCln();
  33. }
  34. }
  35. #region 监控界面使用
  36. /// <summary>
  37. /// 根据计量点取采集的值
  38. /// CAR11PlcStauts PLC控制器状态 "0:初始值 数采服务器每隔2秒向PLC写入值1234;PLC会每隔2S不停用值4321覆盖"
  39. /// CAR11MesWeight 上位机仪表重量 129160
  40. /// HMI11LightWest HMI下发前红绿灯命令 "远控模式下:1、前红绿灯为绿灯;2、前红绿灯为红灯;"
  41. /// CAR11LightWest 前红绿灯 "1、前红绿灯为绿灯;2、前红绿灯为红灯;"
  42. /// CAR11LightEast 后红绿灯 "1、前红绿灯为绿灯;2、前红绿灯为红灯;"
  43. /// HMI11CaptureWest HMI控制前抓拍触发信号 "0、不操作;1、HMI下发西边抓拍单元抓拍信号;"
  44. /// HMI11CaptureEast HMI控制后抓拍触发信号 "0、不操作;1、HMI下发东边抓拍单元抓拍信号;"
  45. /// HMI11ClearZero HMI下发仪表清零指令 1:HMI对仪表清零
  46. /// CAR11StaticStatus 车辆检测 "0、东西方向激光均无报警; 1、过磅时,西边激光报警;2、过磅时,东边激光报警;3、过磅时,东西两边激光均报警"
  47. /// CAR11InfraredWest 前车量检测状态 "1、前激光报警;2、前激光无报警;"
  48. /// CAR11InfraredEast 后车量检测状态 "1、后激光报警;2、后激光无报警;"
  49. /// CAR11ZeroWeightStatus 汽车空磅状态 0、其它,1、零点报警,红绿灯变红,
  50. /// HMI11SwitchLight 红绿灯远控/自动 "1、远方切换开关处于自动控制位置;2、远方切换开关处于远方控制位置;远方切换方开关位于界面上"
  51. /// CAR11SwitchLight PLC红绿灯控制模式状态 "PLC有三种控制模式:1、自动、2、远控;3、检修。 优先权从高到低:检修>远控>自动。PLC默认为自动控制,设2个切换开关,2个在远方操作人员在界面上,可完成远控/自动切换。一个开关在现场PLC控制箱中,可完成检修/自动切换。"
  52. /// CAR11VlDirection 左红外光栅检测 1、光栅报警;2、光栅无报警
  53. /// CAR11VlPLCWeight 右红外光栅检测 1、光栅报警;2、光栅无报警
  54. /// </summary>
  55. public List<LiveData> getWarnInfo(string sPoint)
  56. {
  57. try
  58. {
  59. List<LiveData> ltn = (List<LiveData>)cln.CallRpc<LiveData>("LiveData:readByPointId", new object[] { sPoint });
  60. return ltn;
  61. }
  62. catch (Exception ex)
  63. {
  64. ChangeCln();
  65. WriteLog("读采集数据异常错误:" + ex.Message);
  66. return null;
  67. }
  68. }
  69. /// <summary>
  70. /// 写入控制表(道闸,仪表,红外,LED等)
  71. /// </summary>
  72. /// <param name="sPoint">计量点</param>
  73. /// <param name="OptionType">操作类型</param>
  74. /// <param name="value">被写入的信息</param>
  75. /// <param name="strUserID">用户编号</param>
  76. /// <param name="errorInfo">异常信息接收</param>
  77. /// <returns></returns>
  78. public bool writeWarnInfo(string sPoint, string OptionType, string value, string strUserID, out string errorInfo)
  79. {
  80. try
  81. {
  82. string strPntid = sPoint.Replace("CAR", "HMI") + OptionType;
  83. cln.CallRpc<int>("Control:doCommand", new object[] { strPntid, value, strUserID });
  84. errorInfo = "";
  85. return true;
  86. }
  87. catch (Exception ex)
  88. {
  89. ChangeCln();
  90. WriteLog("写入信息到内存表异常!" + ex.Message);
  91. errorInfo = "写入信息到内存表异常!" + ex.Message;
  92. return false;
  93. }
  94. }
  95. #endregion 监控界面使用
  96. #region 计量主程序使用
  97. /// <summary>
  98. /// 读取Live表 某个字段信息
  99. /// </summary>
  100. /// <param name="value"></param>
  101. /// <param name="PointIdKey">CAR11</param>
  102. /// <param name="TagName">PowerStatus</param>
  103. /// <returns>JArray arr[0].Tagname, arr[0].Value, arr[0].DateTime, arr[0].Source</returns>
  104. public JArray LiveTable(string PointIdKey, string TagName)
  105. {
  106. try
  107. {
  108. List<Live> arr = (List<Live>)cln.CallRpc<Live>("Live.Table:select", new object[] { PointIdKey + TagName });//读
  109. if (arr != null && arr.Count > 0)
  110. {
  111. JArray jArray = new JArray(arr[0].Tagname, arr[0].Value, arr[0].DateTime, arr[0].Source);
  112. return jArray;
  113. }
  114. return null;
  115. }
  116. catch (Exception exp)
  117. {
  118. ChangeCln();
  119. WriteLog("LiveTable:" + exp.Message + exp.StackTrace);
  120. return null;
  121. }
  122. }
  123. /// <summary>
  124. /// 读取实时表,一个计量点只有一条数据(重量,稳定,红外,车号等信息)
  125. /// </summary>
  126. /// <param name="value"></param>
  127. /// <param name="PointIdKey">计量点编号</param>
  128. /// <returns>
  129. /// MemoryTableDataSocket MemoClass = new MemoryTableDataSocket(strPntNo);
  130. /// JArray aryValue = MemoClass.TrackTable(strPntNo);//Track表数据每个计量点的数据
  131. /// string pointId = aryValue[0]; string carNo = aryValue[1];
  132. /// 对照如下:
  133. /// 0 pointid 计量点 string CAR11,CAR12,CAR13等计量点编号
  134. /// 1 status 车辆状态 number 0:等待车辆 1:车辆上称 2:重量不稳定 3:重量稳定 4:计量完成 5:下称
  135. /// 2 carno 车号 string 鄂B23955
  136. /// 3 weight 重量 number
  137. /// 4 weightStatus 重量状态 number 0:重量稳定; 1:重量不稳定; 2:空磅
  138. /// 5 parkStatus 停车状态 number 0:正常(头尾都未压线) 1:车头压线 2:车尾压线 3:两头压线; 记录在重量稳定后生成
  139. /// 6 datetime 上称时间 uint 当前时间的距离1970.1.1.08:00时间的秒数; 秒
  140. /// 7 LicType 车牌类型 number 0:车牌抓拍; 1:RFID 识别
  141. /// 8 WeightSave 计量保存重量 uint
  142. /// 9 FinishTime 过磅完成时间 uint 秒
  143. /// 10 entry 入口 string East = 东边, West = 西边
  144. /// 11 exit 出口 string East = 东边, West = 西边
  145. /// 12 platenoList 抓拍车牌信息 array "{direct, plateno} direct:string 方向 plateno: string 车牌/电子车牌"
  146. /// 13 rfidList RFID识别车牌信息 array "{direct, plateno} direct:string 方向plateno: string 车牌/电子车牌"
  147. /// </returns>
  148. public JArray TrackTable(string PointIdKey)
  149. {
  150. try
  151. {
  152. //Log.GetInstance().WriteLog(36, "开始读取数采实时库 车号:" + PbCache.lockCarNo + ";重量:" + PbCache.collect.weight + ";时间:" + DateTime.Now.ToLongTimeString());
  153. List<Track> arr = (List<Track>)cln.CallRpc<Track>("Track.Table:select", new object[] { PointIdKey });//读
  154. //Log.GetInstance().WriteLog(36, "完成读取数采实时库 车号:" + PbCache.lockCarNo + ";重量:" + PbCache.collect.weight + ";时间:" + DateTime.Now.ToLongTimeString());
  155. if (arr != null && arr.Count > 0)
  156. {
  157. List<CarInfo> PlateNoList = arr[0].PlateNoList == null ? new List<CarInfo>() : arr[0].PlateNoList;
  158. List<CarInfo> RfidList = arr[0].RfidList == null ? new List<CarInfo>() : arr[0].RfidList;
  159. //静态衡
  160. Dictionary<string, double> WgtList = arr[0].WeightList == null ? new Dictionary<string, double>() : arr[0].WeightList;
  161. string CarNo1 = "";
  162. string CarNo2 = "";
  163. if (PlateNoList.Count == 0)
  164. {
  165. }
  166. else if(PlateNoList.Count == 1)
  167. {
  168. if(PlateNoList[0].Direct == "West")
  169. {
  170. CarNo1 = PlateNoList[0].CarNo;
  171. }
  172. else if(PlateNoList[0].Direct == "East")
  173. {
  174. CarNo2 = PlateNoList[0].CarNo;
  175. }
  176. }
  177. else if (PlateNoList.Count == 2)
  178. {
  179. if (PlateNoList[0].Direct == "West")
  180. {
  181. CarNo1 = PlateNoList[0].CarNo;
  182. }
  183. else if (PlateNoList[0].Direct == "East")
  184. {
  185. CarNo2 = PlateNoList[0].CarNo;
  186. }
  187. if (PlateNoList[1].Direct == "West")
  188. {
  189. CarNo1 = PlateNoList[1].CarNo;
  190. }
  191. else if (PlateNoList[1].Direct == "East")
  192. {
  193. CarNo2 = PlateNoList[1].CarNo;
  194. }
  195. }
  196. JArray jArray = new JArray(
  197. arr[0].Pointid, //0
  198. arr[0].Status, //1
  199. arr[0].CarNo, //2
  200. //"甘B32619",
  201. arr[0].Weight, //3
  202. arr[0].WeightStatus, //4
  203. arr[0].StopStatus, //5
  204. arr[0].Datetime, //6
  205. arr[0].LicType, //7
  206. // 20221011 By BourneCao
  207. // 增加双摄像头支持
  208. //PlateNoList.Count == 0 ? "" : PlateNoList[PlateNoList.Count - 1].CarNo, //8
  209. CarNo1, //8
  210. //"甘B32619",
  211. RfidList.Count == 0 ? "" : RfidList[RfidList.Count - 1].CarNo, //9
  212. arr[0].WeightSave, //10
  213. arr[0].FinishTime, //11
  214. arr[0].Entry, //12
  215. arr[0].Exit, //13
  216. // 20221011 By BourneCao
  217. // 增加双摄像头支持
  218. CarNo2 //14
  219. //"甘B32639"
  220. );
  221. double mainWgt = 0;
  222. double viceWgt = 0;
  223. if (WgtList.Count > 1)
  224. {
  225. foreach (var it in WgtList)
  226. {
  227. if (it.Key.EndsWith("WeightMain"))
  228. {
  229. //jArray.Add(it.Value); //14
  230. mainWgt = it.Value;
  231. }
  232. else if (it.Key.EndsWith("WeightVice"))
  233. {
  234. //jArray.Add(it.Value); //15
  235. viceWgt = it.Value;
  236. }
  237. }
  238. jArray.Add(mainWgt); //14
  239. jArray.Add(viceWgt); //15
  240. }
  241. else
  242. {
  243. jArray.Add(arr[0].Weight);
  244. }
  245. try
  246. {
  247. WriteLog(PointIdKey + "重量采集:" + "计量点:" + arr[0].Pointid + "车号:" + arr[0].CarNo + "重量状态(0:重量稳定; 1:重量不稳定; 2:空磅):" + arr[0].WeightStatus + "重量" + arr[0].Weight);
  248. }
  249. catch (Exception)
  250. {
  251. WriteLog(PointIdKey + "写日志异常");
  252. }
  253. return jArray;
  254. }
  255. WriteLog(PointIdKey + "空值");
  256. return null;
  257. }
  258. catch (Exception exp)
  259. {
  260. ChangeCln();
  261. WriteLog(PointIdKey + "_TrackTable:" + exp.Message + exp.StackTrace + "空值");
  262. return null;
  263. }
  264. }
  265. #endregion 计量主程序使用
  266. public List<JArray> TrackTable(params string[] PointIdKeys)
  267. {
  268. try
  269. {
  270. List<Track> arr = (List<Track>)cln.CallRpc<Track>("Track.Table:select", new object[] { });//读
  271. if (arr != null && arr.Count > 0)
  272. {
  273. List<JArray> lj = new List<JArray>();
  274. foreach (Track tk in arr)
  275. {
  276. foreach (string strPointKey in PointIdKeys)
  277. {
  278. if (tk.Pointid == strPointKey)
  279. {
  280. //静态衡
  281. Dictionary<string, double> WgtList = tk.WeightList == null ? new Dictionary<string, double>() : tk.WeightList;
  282. JArray jArray = new JArray(
  283. tk.Pointid, //0 pointid 计量点 string CAR11,CAR12,CAR13等计量点编号
  284. tk.Weight, //1 重量 number
  285. tk.WeightStatus, //2 重量状态 number 0:重量稳定; 1:重量不稳定; 2:空磅
  286. tk.Datetime //3 上称时间 uint 当前时间的距离1970.1.1.08:00时间的秒数; 秒
  287. );
  288. double mainWgt = 0;
  289. double viceWgt = 0;
  290. if (WgtList.Count > 1)
  291. {
  292. foreach (var it in WgtList)
  293. {
  294. if (it.Key.EndsWith("WeightMain"))
  295. {
  296. mainWgt = it.Value;
  297. }
  298. else if (it.Key.EndsWith("WeightVice"))
  299. {
  300. viceWgt = it.Value;
  301. }
  302. }
  303. jArray.Add(mainWgt); //4
  304. jArray.Add(viceWgt); //5
  305. }
  306. else
  307. {
  308. jArray.Add(tk.Weight);
  309. }
  310. lj.Add(jArray);
  311. }
  312. }
  313. }
  314. return lj.Count > 0 ? lj : null;
  315. }
  316. WriteLog(string.Format("计量点[{0}]没有对应的采集信息:", string.Join("", PointIdKeys)));
  317. return null;
  318. }
  319. catch (Exception exp)
  320. {
  321. ChangeCln();
  322. WriteLog("調用TrackTable异常:" + exp.Message + exp.StackTrace + "空值");
  323. return null;
  324. }
  325. }
  326. /// <summary>
  327. /// 查询List的数据
  328. /// </summary>
  329. /// <param name="PointIdKeys"></param>
  330. /// <returns></returns>
  331. public List<JArray> TrackTableList(params string[] PointIdKeys)
  332. {
  333. try
  334. {
  335. List<Track> arr = (List<Track>)cln.CallRpc<Track>("Track.Table:select", new object[] { });//读
  336. if (arr != null && arr.Count > 0)
  337. {
  338. List<JArray> lj = new List<JArray>();
  339. foreach (Track tk in arr)
  340. {
  341. foreach (string strPointKey in PointIdKeys)
  342. {
  343. if (tk.Pointid == strPointKey)
  344. {
  345. List<CarInfo> PlateNoList = tk.PlateNoList == null ? new List<CarInfo>() : tk.PlateNoList;
  346. List<CarInfo> RfidList = tk.RfidList == null ? new List<CarInfo>() : tk.RfidList;
  347. JArray jArray = new JArray(
  348. tk.Pointid, //0
  349. tk.Status, //1
  350. tk.CarNo, //2
  351. tk.Weight, //3
  352. tk.WeightStatus, //4
  353. tk.StopStatus, //5
  354. tk.Datetime, //6
  355. tk.LicType, //7
  356. PlateNoList.Count == 0 ? "" : PlateNoList[PlateNoList.Count - 1].CarNo, //8
  357. RfidList.Count == 0 ? "" : RfidList[RfidList.Count - 1].CarNo, //9
  358. tk.WeightSave, //10
  359. tk.FinishTime, //11
  360. tk.Entry, //12
  361. tk.Exit); //13
  362. try
  363. {
  364. switch (strPointKey.ToUpper())
  365. {
  366. case "CAR40":
  367. WriteLogSwicth("优钢热送", strPointKey + "优钢热送:" + "计量点:" + tk.Pointid + ";重量状态(0:重量稳定; 1:重量不稳定; 2:空磅):" + tk.WeightStatus + " ;重量" + tk.Weight);
  368. break;
  369. case "CAR41":
  370. WriteLogSwicth("高棒热送", strPointKey + "高棒热送:" + "计量点:" + tk.Pointid + ";重量状态(0:重量稳定; 1:重量不稳定; 2:空磅):" + tk.WeightStatus + " ;重量" + tk.Weight);
  371. break;
  372. case "CAR42":
  373. WriteLogSwicth("普棒热送", strPointKey + "普棒热送:" + "计量点:" + tk.Pointid + ";重量状态(0:重量稳定; 1:重量不稳定; 2:空磅):" + tk.WeightStatus + " ;重量" + tk.Weight);
  374. break;
  375. case "CAR43":
  376. WriteLogSwicth("板材热送", strPointKey + "板材热送:" + "计量点:" + tk.Pointid + ";重量状态(0:重量稳定; 1:重量不稳定; 2:空磅):" + tk.WeightStatus + " ;重量" + tk.Weight);
  377. break;
  378. case "CAR44":
  379. WriteLogSwicth("高线热送", strPointKey + "高线热送:" + "计量点:" + tk.Pointid + ";重量状态(0:重量稳定; 1:重量不稳定; 2:空磅):" + tk.WeightStatus + " ;重量" + tk.Weight);
  380. break;
  381. default:
  382. break;
  383. }
  384. }
  385. catch (Exception)
  386. {
  387. WriteLog(strPointKey + "写日志异常");
  388. }
  389. lj.Add(jArray);
  390. }
  391. }
  392. }
  393. return lj.Count > 0 ? lj : null;
  394. }
  395. WriteLog(string.Format("计量点[{0}]没有对应的采集信息:", string.Join("", PointIdKeys)));
  396. return null;
  397. }
  398. catch (Exception exp)
  399. {
  400. ChangeCln();
  401. WriteLog("調用TrackTable异常:" + exp.Message + exp.StackTrace + "空值");
  402. return null;
  403. }
  404. }
  405. /// <summary>
  406. /// 按照计量点的输出日志
  407. /// </summary>
  408. /// <param name="strPointName"></param>
  409. /// <param name="str"></param>
  410. private void WriteLogSwicth(string strPointName, string str)
  411. {
  412. try
  413. {
  414. string m_szRunPath;
  415. m_szRunPath = PbCache.path;
  416. if (Directory.Exists(m_szRunPath + "\\log") == false)
  417. {
  418. Directory.CreateDirectory(m_szRunPath + "\\log");
  419. }
  420. string strDate = DateTime.Now.ToString("yyyyMMdd");
  421. string strPathFile = m_szRunPath + "\\log\\" + strDate;
  422. if (!Directory.Exists(strPathFile))//如果不存在就创建file文件夹
  423. {
  424. Directory.CreateDirectory(strPathFile);
  425. }
  426. TextWriter tw = new StreamWriter(strPathFile + "\\重量采集日志_" + strPointName + strDate + ".log", true);
  427. tw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  428. tw.WriteLine(str);
  429. tw.WriteLine("\r\n");
  430. tw.Close();
  431. }
  432. catch { }
  433. }
  434. private void WriteLog(string str)
  435. {
  436. try
  437. {
  438. string m_szRunPath;
  439. m_szRunPath = PbCache.path;
  440. if (Directory.Exists(m_szRunPath + "\\log") == false)
  441. {
  442. Directory.CreateDirectory(m_szRunPath + "\\log");
  443. }
  444. string strDate = DateTime.Now.ToString("yyyyMMdd");
  445. string strPathFile = m_szRunPath + "\\log\\" + strDate;
  446. if (!Directory.Exists(strPathFile))//如果不存在就创建file文件夹
  447. {
  448. Directory.CreateDirectory(strPathFile);
  449. }
  450. TextWriter tw = new StreamWriter(strPathFile + "\\重量采集日志_" + strDate + ".log", true);
  451. tw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  452. tw.WriteLine(str);
  453. tw.WriteLine("\r\n");
  454. tw.Close();
  455. }
  456. catch { }
  457. }
  458. private void ChangeCln()
  459. {
  460. // 自动切换主备数采平台
  461. if(PbCache.icoredbType == "icoredb2")
  462. {
  463. PbCache.icoredbType = "icoredb1";
  464. try
  465. {
  466. cln = new CoredbClient(AppConfigCache.icoredbTcp);
  467. }
  468. catch { }
  469. }
  470. else
  471. {
  472. PbCache.icoredbType = "icoredb2";
  473. try
  474. {
  475. cln = new CoredbClient(AppConfigCache.icoredbTcp2);
  476. }
  477. catch { }
  478. }
  479. WriteLog("MemoryTableData:" + cln);
  480. }
  481. }
  482. }