MemoryTableDataSocket.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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. WriteLog("MemoryTableData:" + cln);
  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. WriteLog("MemoryTableData:" + cln);
  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. WriteLog("读采集数据异常错误:" + ex.Message);
  65. return null;
  66. }
  67. }
  68. /// <summary>
  69. /// 写入控制表(道闸,仪表,红外,LED等)
  70. /// </summary>
  71. /// <param name="sPoint">计量点</param>
  72. /// <param name="OptionType">操作类型</param>
  73. /// <param name="value">被写入的信息</param>
  74. /// <param name="strUserID">用户编号</param>
  75. /// <param name="errorInfo">异常信息接收</param>
  76. /// <returns></returns>
  77. public bool writeWarnInfo(string sPoint, string OptionType, string value, string strUserID, out string errorInfo)
  78. {
  79. try
  80. {
  81. string strPntid = sPoint.Replace("CAR", "HMI") + OptionType;
  82. cln.CallRpc<int>("Control:doCommand", new object[] { strPntid, value, strUserID });
  83. errorInfo = "";
  84. return true;
  85. }
  86. catch (Exception ex)
  87. {
  88. WriteLog("写入信息到内存表异常!" + ex.Message);
  89. errorInfo = "写入信息到内存表异常!" + ex.Message;
  90. return false;
  91. }
  92. }
  93. #endregion 监控界面使用
  94. #region 计量主程序使用
  95. /// <summary>
  96. /// 读取Live表 某个字段信息
  97. /// </summary>
  98. /// <param name="value"></param>
  99. /// <param name="PointIdKey">CAR11</param>
  100. /// <param name="TagName">PowerStatus</param>
  101. /// <returns>JArray arr[0].Tagname, arr[0].Value, arr[0].DateTime, arr[0].Source</returns>
  102. public JArray LiveTable(string PointIdKey, string TagName)
  103. {
  104. try
  105. {
  106. List<Live> arr = (List<Live>)cln.CallRpc<Live>("Live.Table:select", new object[] { PointIdKey + TagName });//读
  107. if (arr != null && arr.Count > 0)
  108. {
  109. JArray jArray = new JArray(arr[0].Tagname, arr[0].Value, arr[0].DateTime, arr[0].Source);
  110. return jArray;
  111. }
  112. return null;
  113. }
  114. catch (Exception exp)
  115. {
  116. WriteLog("LiveTable:" + exp.Message + exp.StackTrace);
  117. return null;
  118. }
  119. }
  120. /// <summary>
  121. /// 读取实时表,一个计量点只有一条数据(重量,稳定,红外,车号等信息)
  122. /// </summary>
  123. /// <param name="value"></param>
  124. /// <param name="PointIdKey">计量点编号</param>
  125. /// <returns>
  126. /// MemoryTableDataSocket MemoClass = new MemoryTableDataSocket(strPntNo);
  127. /// JArray aryValue = MemoClass.TrackTable(strPntNo);//Track表数据每个计量点的数据
  128. /// string pointId = aryValue[0]; string carNo = aryValue[1];
  129. /// 对照如下:
  130. /// 0 pointid 计量点 string CAR11,CAR12,CAR13等计量点编号
  131. /// 1 status 车辆状态 number 0:等待车辆 1:车辆上称 2:重量不稳定 3:重量稳定 4:计量完成 5:下称
  132. /// 2 carno 车号 string 鄂B23955
  133. /// 3 weight 重量 number
  134. /// 4 weightStatus 重量状态 number 0:重量稳定; 1:重量不稳定; 2:空磅
  135. /// 5 parkStatus 停车状态 number 0:正常(头尾都未压线) 1:车头压线 2:车尾压线 3:两头压线; 记录在重量稳定后生成
  136. /// 6 datetime 上称时间 uint 当前时间的距离1970.1.1.08:00时间的秒数; 秒
  137. /// 7 LicType 车牌类型 number 0:车牌抓拍; 1:RFID 识别
  138. /// 8 WeightSave 计量保存重量 uint
  139. /// 9 FinishTime 过磅完成时间 uint 秒
  140. /// 10 entry 入口 string East = 东边, West = 西边
  141. /// 11 exit 出口 string East = 东边, West = 西边
  142. /// 12 platenoList 抓拍车牌信息 array "{direct, plateno} direct:string 方向 plateno: string 车牌/电子车牌"
  143. /// 13 rfidList RFID识别车牌信息 array "{direct, plateno} direct:string 方向plateno: string 车牌/电子车牌"
  144. /// </returns>
  145. public JArray TrackTable(string PointIdKey)
  146. {
  147. try
  148. {
  149. Log.GetInstance().WriteLog(36, "开始读取数采实时库 车号:" + PbCache.lockCarNo + ";重量:" + PbCache.collect.weight + ";时间:" + DateTime.Now.ToLongTimeString());
  150. List<Track> arr = (List<Track>)cln.CallRpc<Track>("Track.Table:select", new object[] { PointIdKey });//读
  151. Log.GetInstance().WriteLog(36, "完成读取数采实时库 车号:" + PbCache.lockCarNo + ";重量:" + PbCache.collect.weight + ";时间:" + DateTime.Now.ToLongTimeString());
  152. if (arr != null && arr.Count > 0)
  153. {
  154. List<CarInfo> PlateNoList = arr[0].PlateNoList == null ? new List<CarInfo>() : arr[0].PlateNoList;
  155. List<CarInfo> RfidList = arr[0].RfidList == null ? new List<CarInfo>() : arr[0].RfidList;
  156. //静态衡
  157. Dictionary<string, double> WgtList = arr[0].WeightList == null ? new Dictionary<string, double>() : arr[0].WeightList;
  158. JArray jArray = new JArray(
  159. arr[0].Pointid, //0
  160. arr[0].Status, //1
  161. arr[0].CarNo, //2
  162. arr[0].Weight, //3
  163. arr[0].WeightStatus, //4
  164. arr[0].StopStatus, //5
  165. arr[0].Datetime, //6
  166. arr[0].LicType, //7
  167. PlateNoList.Count == 0 ? "" : PlateNoList[PlateNoList.Count - 1].CarNo, //8
  168. RfidList.Count == 0 ? "" : RfidList[RfidList.Count - 1].CarNo, //9
  169. arr[0].WeightSave, //10
  170. arr[0].FinishTime, //11
  171. arr[0].Entry, //12
  172. arr[0].Exit); //13
  173. double mainWgt = 0;
  174. double viceWgt = 0;
  175. if (WgtList.Count > 1)
  176. {
  177. foreach (var it in WgtList)
  178. {
  179. if (it.Key.EndsWith("WeightMain"))
  180. {
  181. //jArray.Add(it.Value); //14
  182. mainWgt = it.Value;
  183. }
  184. else if (it.Key.EndsWith("WeightVice"))
  185. {
  186. //jArray.Add(it.Value); //15
  187. viceWgt = it.Value;
  188. }
  189. }
  190. jArray.Add(mainWgt); //14
  191. jArray.Add(viceWgt); //15
  192. }
  193. else
  194. {
  195. jArray.Add(arr[0].Weight);
  196. }
  197. try
  198. {
  199. WriteLog(PointIdKey + "重量采集:" + "计量点:" + arr[0].Pointid + "车号:" + arr[0].CarNo + "重量状态(0:重量稳定; 1:重量不稳定; 2:空磅):" + arr[0].WeightStatus + "重量" + arr[0].Weight);
  200. }
  201. catch (Exception)
  202. {
  203. WriteLog(PointIdKey + "写日志异常");
  204. }
  205. return jArray;
  206. }
  207. WriteLog(PointIdKey + "空值");
  208. return null;
  209. }
  210. catch (Exception exp)
  211. {
  212. try
  213. {
  214. cln = new CoredbClient(AppConfigCache.icoredbTcp);
  215. }
  216. catch { }
  217. WriteLog(PointIdKey + "_TrackTable:" + exp.Message + exp.StackTrace + "空值");
  218. return null;
  219. }
  220. }
  221. #endregion 计量主程序使用
  222. public List<JArray> TrackTable(params string[] PointIdKeys)
  223. {
  224. try
  225. {
  226. List<Track> arr = (List<Track>)cln.CallRpc<Track>("Track.Table:select", new object[] { });//读
  227. if (arr != null && arr.Count > 0)
  228. {
  229. List<JArray> lj = new List<JArray>();
  230. foreach (Track tk in arr)
  231. {
  232. foreach (string strPointKey in PointIdKeys)
  233. {
  234. if (tk.Pointid == strPointKey)
  235. {
  236. //静态衡
  237. Dictionary<string, double> WgtList = tk.WeightList == null ? new Dictionary<string, double>() : tk.WeightList;
  238. JArray jArray = new JArray(
  239. tk.Pointid, //0 pointid 计量点 string CAR11,CAR12,CAR13等计量点编号
  240. tk.Weight, //1 重量 number
  241. tk.WeightStatus, //2 重量状态 number 0:重量稳定; 1:重量不稳定; 2:空磅
  242. tk.Datetime //3 上称时间 uint 当前时间的距离1970.1.1.08:00时间的秒数; 秒
  243. );
  244. double mainWgt = 0;
  245. double viceWgt = 0;
  246. if (WgtList.Count > 1)
  247. {
  248. foreach (var it in WgtList)
  249. {
  250. if (it.Key.EndsWith("WeightMain"))
  251. {
  252. mainWgt = it.Value;
  253. }
  254. else if (it.Key.EndsWith("WeightVice"))
  255. {
  256. viceWgt = it.Value;
  257. }
  258. }
  259. jArray.Add(mainWgt); //4
  260. jArray.Add(viceWgt); //5
  261. }
  262. else
  263. {
  264. jArray.Add(tk.Weight);
  265. }
  266. lj.Add(jArray);
  267. }
  268. }
  269. }
  270. return lj.Count > 0 ? lj : null;
  271. }
  272. WriteLog(string.Format("计量点[{0}]没有对应的采集信息:", string.Join("", PointIdKeys)));
  273. return null;
  274. }
  275. catch (Exception exp)
  276. {
  277. try
  278. {
  279. cln = new CoredbClient(AppConfigCache.icoredbTcp);
  280. }
  281. catch { }
  282. WriteLog("調用TrackTable异常:" + exp.Message + exp.StackTrace + "空值");
  283. return null;
  284. }
  285. }
  286. /// <summary>
  287. /// 查询List的数据
  288. /// </summary>
  289. /// <param name="PointIdKeys"></param>
  290. /// <returns></returns>
  291. public List<JArray> TrackTableList(params string[] PointIdKeys)
  292. {
  293. try
  294. {
  295. List<Track> arr = (List<Track>)cln.CallRpc<Track>("Track.Table:select", new object[] { });//读
  296. if (arr != null && arr.Count > 0)
  297. {
  298. List<JArray> lj = new List<JArray>();
  299. foreach (Track tk in arr)
  300. {
  301. foreach (string strPointKey in PointIdKeys)
  302. {
  303. if (tk.Pointid == strPointKey)
  304. {
  305. List<CarInfo> PlateNoList = tk.PlateNoList == null ? new List<CarInfo>() : tk.PlateNoList;
  306. List<CarInfo> RfidList = tk.RfidList == null ? new List<CarInfo>() : tk.RfidList;
  307. JArray jArray = new JArray(
  308. tk.Pointid, //0
  309. tk.Status, //1
  310. tk.CarNo, //2
  311. tk.Weight, //3
  312. tk.WeightStatus, //4
  313. tk.StopStatus, //5
  314. tk.Datetime, //6
  315. tk.LicType, //7
  316. PlateNoList.Count == 0 ? "" : PlateNoList[PlateNoList.Count - 1].CarNo, //8
  317. RfidList.Count == 0 ? "" : RfidList[RfidList.Count - 1].CarNo, //9
  318. tk.WeightSave, //10
  319. tk.FinishTime, //11
  320. tk.Entry, //12
  321. tk.Exit); //13
  322. try
  323. {
  324. switch (strPointKey.ToUpper())
  325. {
  326. case "CAR40":
  327. WriteLogSwicth("优钢热送", strPointKey + "优钢热送:" + "计量点:" + tk.Pointid + ";重量状态(0:重量稳定; 1:重量不稳定; 2:空磅):" + tk.WeightStatus + " ;重量" + tk.Weight);
  328. break;
  329. case "CAR41":
  330. WriteLogSwicth("高棒热送", strPointKey + "高棒热送:" + "计量点:" + tk.Pointid + ";重量状态(0:重量稳定; 1:重量不稳定; 2:空磅):" + tk.WeightStatus + " ;重量" + tk.Weight);
  331. break;
  332. case "CAR42":
  333. WriteLogSwicth("普棒热送", strPointKey + "普棒热送:" + "计量点:" + tk.Pointid + ";重量状态(0:重量稳定; 1:重量不稳定; 2:空磅):" + tk.WeightStatus + " ;重量" + tk.Weight);
  334. break;
  335. case "CAR43":
  336. WriteLogSwicth("板材热送", strPointKey + "板材热送:" + "计量点:" + tk.Pointid + ";重量状态(0:重量稳定; 1:重量不稳定; 2:空磅):" + tk.WeightStatus + " ;重量" + tk.Weight);
  337. break;
  338. case "CAR44":
  339. WriteLogSwicth("高线热送", strPointKey + "高线热送:" + "计量点:" + tk.Pointid + ";重量状态(0:重量稳定; 1:重量不稳定; 2:空磅):" + tk.WeightStatus + " ;重量" + tk.Weight);
  340. break;
  341. default:
  342. break;
  343. }
  344. }
  345. catch (Exception)
  346. {
  347. WriteLog(strPointKey + "写日志异常");
  348. }
  349. lj.Add(jArray);
  350. }
  351. }
  352. }
  353. return lj.Count > 0 ? lj : null;
  354. }
  355. WriteLog(string.Format("计量点[{0}]没有对应的采集信息:", string.Join("", PointIdKeys)));
  356. return null;
  357. }
  358. catch (Exception exp)
  359. {
  360. try
  361. {
  362. cln = new CoredbClient(AppConfigCache.icoredbTcp);
  363. }
  364. catch { }
  365. WriteLog("調用TrackTable异常:" + exp.Message + exp.StackTrace + "空值");
  366. return null;
  367. }
  368. }
  369. /// <summary>
  370. /// 按照计量点的输出日志
  371. /// </summary>
  372. /// <param name="strPointName"></param>
  373. /// <param name="str"></param>
  374. private void WriteLogSwicth(string strPointName, string str)
  375. {
  376. try
  377. {
  378. string m_szRunPath;
  379. m_szRunPath = PbCache.path;
  380. if (Directory.Exists(m_szRunPath + "\\log") == false)
  381. {
  382. Directory.CreateDirectory(m_szRunPath + "\\log");
  383. }
  384. string strDate = DateTime.Now.ToString("yyyyMMdd");
  385. string strPathFile = m_szRunPath + "\\log\\" + strDate;
  386. if (!Directory.Exists(strPathFile))//如果不存在就创建file文件夹
  387. {
  388. Directory.CreateDirectory(strPathFile);
  389. }
  390. TextWriter tw = new StreamWriter(strPathFile + "\\重量采集日志_" + strPointName + strDate + ".log", true);
  391. tw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  392. tw.WriteLine(str);
  393. tw.WriteLine("\r\n");
  394. tw.Close();
  395. }
  396. catch { }
  397. }
  398. private void WriteLog(string str)
  399. {
  400. try
  401. {
  402. string m_szRunPath;
  403. m_szRunPath = PbCache.path;
  404. if (Directory.Exists(m_szRunPath + "\\log") == false)
  405. {
  406. Directory.CreateDirectory(m_szRunPath + "\\log");
  407. }
  408. string strDate = DateTime.Now.ToString("yyyyMMdd");
  409. string strPathFile = m_szRunPath + "\\log\\" + strDate;
  410. if (!Directory.Exists(strPathFile))//如果不存在就创建file文件夹
  411. {
  412. Directory.CreateDirectory(strPathFile);
  413. }
  414. TextWriter tw = new StreamWriter(strPathFile + "\\重量采集日志_" + strDate + ".log", true);
  415. tw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  416. tw.WriteLine(str);
  417. tw.WriteLine("\r\n");
  418. tw.Close();
  419. }
  420. catch { }
  421. }
  422. }
  423. }