MemoryTableDataSocket.cs 22 KB

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