MemoryTableDataSocket.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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. JArray jArray = new JArray(
  162. arr[0].Pointid, //0
  163. arr[0].Status, //1
  164. arr[0].CarNo, //2
  165. arr[0].Weight, //3
  166. arr[0].WeightStatus, //4
  167. arr[0].StopStatus, //5
  168. arr[0].Datetime, //6
  169. arr[0].LicType, //7
  170. // 20221011 By BourneCao
  171. // 增加双摄像头支持
  172. //PlateNoList.Count == 0 ? "" : PlateNoList[PlateNoList.Count - 1].CarNo, //8
  173. PlateNoList.Count == 0 ? "" : PlateNoList[0].CarNo, //8
  174. RfidList.Count == 0 ? "" : RfidList[RfidList.Count - 1].CarNo, //9
  175. arr[0].WeightSave, //10
  176. arr[0].FinishTime, //11
  177. arr[0].Entry, //12
  178. arr[0].Exit, //13
  179. // 20221011 By BourneCao
  180. // 增加双摄像头支持
  181. PlateNoList.Count <= 1 ? "" : PlateNoList[1].CarNo //14
  182. );
  183. double mainWgt = 0;
  184. double viceWgt = 0;
  185. if (WgtList.Count > 1)
  186. {
  187. foreach (var it in WgtList)
  188. {
  189. if (it.Key.EndsWith("WeightMain"))
  190. {
  191. //jArray.Add(it.Value); //14
  192. mainWgt = it.Value;
  193. }
  194. else if (it.Key.EndsWith("WeightVice"))
  195. {
  196. //jArray.Add(it.Value); //15
  197. viceWgt = it.Value;
  198. }
  199. }
  200. jArray.Add(mainWgt); //14
  201. jArray.Add(viceWgt); //15
  202. }
  203. else
  204. {
  205. jArray.Add(arr[0].Weight);
  206. }
  207. try
  208. {
  209. WriteLog(PointIdKey + "重量采集:" + "计量点:" + arr[0].Pointid + "车号:" + arr[0].CarNo + "重量状态(0:重量稳定; 1:重量不稳定; 2:空磅):" + arr[0].WeightStatus + "重量" + arr[0].Weight);
  210. }
  211. catch (Exception)
  212. {
  213. WriteLog(PointIdKey + "写日志异常");
  214. }
  215. return jArray;
  216. }
  217. WriteLog(PointIdKey + "空值");
  218. return null;
  219. }
  220. catch (Exception exp)
  221. {
  222. ChangeCln();
  223. WriteLog(PointIdKey + "_TrackTable:" + exp.Message + exp.StackTrace + "空值");
  224. return null;
  225. }
  226. }
  227. #endregion 计量主程序使用
  228. public List<JArray> TrackTable(params string[] PointIdKeys)
  229. {
  230. try
  231. {
  232. List<Track> arr = (List<Track>)cln.CallRpc<Track>("Track.Table:select", new object[] { });//读
  233. if (arr != null && arr.Count > 0)
  234. {
  235. List<JArray> lj = new List<JArray>();
  236. foreach (Track tk in arr)
  237. {
  238. foreach (string strPointKey in PointIdKeys)
  239. {
  240. if (tk.Pointid == strPointKey)
  241. {
  242. //静态衡
  243. Dictionary<string, double> WgtList = tk.WeightList == null ? new Dictionary<string, double>() : tk.WeightList;
  244. JArray jArray = new JArray(
  245. tk.Pointid, //0 pointid 计量点 string CAR11,CAR12,CAR13等计量点编号
  246. tk.Weight, //1 重量 number
  247. tk.WeightStatus, //2 重量状态 number 0:重量稳定; 1:重量不稳定; 2:空磅
  248. tk.Datetime //3 上称时间 uint 当前时间的距离1970.1.1.08:00时间的秒数; 秒
  249. );
  250. double mainWgt = 0;
  251. double viceWgt = 0;
  252. if (WgtList.Count > 1)
  253. {
  254. foreach (var it in WgtList)
  255. {
  256. if (it.Key.EndsWith("WeightMain"))
  257. {
  258. mainWgt = it.Value;
  259. }
  260. else if (it.Key.EndsWith("WeightVice"))
  261. {
  262. viceWgt = it.Value;
  263. }
  264. }
  265. jArray.Add(mainWgt); //4
  266. jArray.Add(viceWgt); //5
  267. }
  268. else
  269. {
  270. jArray.Add(tk.Weight);
  271. }
  272. lj.Add(jArray);
  273. }
  274. }
  275. }
  276. return lj.Count > 0 ? lj : null;
  277. }
  278. WriteLog(string.Format("计量点[{0}]没有对应的采集信息:", string.Join("", PointIdKeys)));
  279. return null;
  280. }
  281. catch (Exception exp)
  282. {
  283. ChangeCln();
  284. WriteLog("調用TrackTable异常:" + exp.Message + exp.StackTrace + "空值");
  285. return null;
  286. }
  287. }
  288. /// <summary>
  289. /// 查询List的数据
  290. /// </summary>
  291. /// <param name="PointIdKeys"></param>
  292. /// <returns></returns>
  293. public List<JArray> TrackTableList(params string[] PointIdKeys)
  294. {
  295. try
  296. {
  297. List<Track> arr = (List<Track>)cln.CallRpc<Track>("Track.Table:select", new object[] { });//读
  298. if (arr != null && arr.Count > 0)
  299. {
  300. List<JArray> lj = new List<JArray>();
  301. foreach (Track tk in arr)
  302. {
  303. foreach (string strPointKey in PointIdKeys)
  304. {
  305. if (tk.Pointid == strPointKey)
  306. {
  307. List<CarInfo> PlateNoList = tk.PlateNoList == null ? new List<CarInfo>() : tk.PlateNoList;
  308. List<CarInfo> RfidList = tk.RfidList == null ? new List<CarInfo>() : tk.RfidList;
  309. JArray jArray = new JArray(
  310. tk.Pointid, //0
  311. tk.Status, //1
  312. tk.CarNo, //2
  313. tk.Weight, //3
  314. tk.WeightStatus, //4
  315. tk.StopStatus, //5
  316. tk.Datetime, //6
  317. tk.LicType, //7
  318. PlateNoList.Count == 0 ? "" : PlateNoList[PlateNoList.Count - 1].CarNo, //8
  319. RfidList.Count == 0 ? "" : RfidList[RfidList.Count - 1].CarNo, //9
  320. tk.WeightSave, //10
  321. tk.FinishTime, //11
  322. tk.Entry, //12
  323. tk.Exit); //13
  324. try
  325. {
  326. switch (strPointKey.ToUpper())
  327. {
  328. case "CAR40":
  329. WriteLogSwicth("优钢热送", strPointKey + "优钢热送:" + "计量点:" + tk.Pointid + ";重量状态(0:重量稳定; 1:重量不稳定; 2:空磅):" + tk.WeightStatus + " ;重量" + tk.Weight);
  330. break;
  331. case "CAR41":
  332. WriteLogSwicth("高棒热送", strPointKey + "高棒热送:" + "计量点:" + tk.Pointid + ";重量状态(0:重量稳定; 1:重量不稳定; 2:空磅):" + tk.WeightStatus + " ;重量" + tk.Weight);
  333. break;
  334. case "CAR42":
  335. WriteLogSwicth("普棒热送", strPointKey + "普棒热送:" + "计量点:" + tk.Pointid + ";重量状态(0:重量稳定; 1:重量不稳定; 2:空磅):" + tk.WeightStatus + " ;重量" + tk.Weight);
  336. break;
  337. case "CAR43":
  338. WriteLogSwicth("板材热送", strPointKey + "板材热送:" + "计量点:" + tk.Pointid + ";重量状态(0:重量稳定; 1:重量不稳定; 2:空磅):" + tk.WeightStatus + " ;重量" + tk.Weight);
  339. break;
  340. case "CAR44":
  341. WriteLogSwicth("高线热送", strPointKey + "高线热送:" + "计量点:" + tk.Pointid + ";重量状态(0:重量稳定; 1:重量不稳定; 2:空磅):" + tk.WeightStatus + " ;重量" + tk.Weight);
  342. break;
  343. default:
  344. break;
  345. }
  346. }
  347. catch (Exception)
  348. {
  349. WriteLog(strPointKey + "写日志异常");
  350. }
  351. lj.Add(jArray);
  352. }
  353. }
  354. }
  355. return lj.Count > 0 ? lj : null;
  356. }
  357. WriteLog(string.Format("计量点[{0}]没有对应的采集信息:", string.Join("", PointIdKeys)));
  358. return null;
  359. }
  360. catch (Exception exp)
  361. {
  362. ChangeCln();
  363. WriteLog("調用TrackTable异常:" + exp.Message + exp.StackTrace + "空值");
  364. return null;
  365. }
  366. }
  367. /// <summary>
  368. /// 按照计量点的输出日志
  369. /// </summary>
  370. /// <param name="strPointName"></param>
  371. /// <param name="str"></param>
  372. private void WriteLogSwicth(string strPointName, string str)
  373. {
  374. try
  375. {
  376. string m_szRunPath;
  377. m_szRunPath = PbCache.path;
  378. if (Directory.Exists(m_szRunPath + "\\log") == false)
  379. {
  380. Directory.CreateDirectory(m_szRunPath + "\\log");
  381. }
  382. string strDate = DateTime.Now.ToString("yyyyMMdd");
  383. string strPathFile = m_szRunPath + "\\log\\" + strDate;
  384. if (!Directory.Exists(strPathFile))//如果不存在就创建file文件夹
  385. {
  386. Directory.CreateDirectory(strPathFile);
  387. }
  388. TextWriter tw = new StreamWriter(strPathFile + "\\重量采集日志_" + strPointName + strDate + ".log", true);
  389. tw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  390. tw.WriteLine(str);
  391. tw.WriteLine("\r\n");
  392. tw.Close();
  393. }
  394. catch { }
  395. }
  396. private void WriteLog(string str)
  397. {
  398. try
  399. {
  400. string m_szRunPath;
  401. m_szRunPath = PbCache.path;
  402. if (Directory.Exists(m_szRunPath + "\\log") == false)
  403. {
  404. Directory.CreateDirectory(m_szRunPath + "\\log");
  405. }
  406. string strDate = DateTime.Now.ToString("yyyyMMdd");
  407. string strPathFile = m_szRunPath + "\\log\\" + strDate;
  408. if (!Directory.Exists(strPathFile))//如果不存在就创建file文件夹
  409. {
  410. Directory.CreateDirectory(strPathFile);
  411. }
  412. TextWriter tw = new StreamWriter(strPathFile + "\\重量采集日志_" + strDate + ".log", true);
  413. tw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  414. tw.WriteLine(str);
  415. tw.WriteLine("\r\n");
  416. tw.Close();
  417. }
  418. catch { }
  419. }
  420. private void ChangeCln()
  421. {
  422. // 自动切换主备数采平台
  423. if(PbCache.icoredbType == "icoredb2")
  424. {
  425. PbCache.icoredbType = "icoredb1";
  426. try
  427. {
  428. cln = new CoredbClient(AppConfigCache.icoredbTcp);
  429. }
  430. catch { }
  431. }
  432. else
  433. {
  434. PbCache.icoredbType = "icoredb2";
  435. try
  436. {
  437. cln = new CoredbClient(AppConfigCache.icoredbTcp2);
  438. }
  439. catch { }
  440. }
  441. WriteLog("MemoryTableData:" + cln);
  442. }
  443. }
  444. }