MemoryTableDataSocket.cs 22 KB

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