MemoryTableDataSocket_Belt.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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. /// <summary>
  10. /// 皮带仪表类型
  11. /// </summary>
  12. public enum StrapMeasType
  13. {
  14. SA600 = 0,
  15. ICS300 = 1,
  16. XR6000 = 2,
  17. }
  18. public class MemoryTableDataSocket_Belt
  19. {
  20. private CoredbClient cln = null;
  21. public MemoryTableDataSocket_Belt()
  22. {
  23. try
  24. {
  25. cln = new CoredbClient(AppConfigCache.icoredbTcp);
  26. }
  27. catch (Exception ex)
  28. {
  29. WriteLog("MemoryTableData:" + cln);
  30. }
  31. }
  32. public MemoryTableDataSocket_Belt(string sPoint, string serverUrl)
  33. { //""tarantool://guest@192.1.3.54:20"+计量点编号
  34. try
  35. {
  36. cln = new CoredbClient(serverUrl);// + sPoint.Substring(3, 2));//计量网
  37. }
  38. catch (Exception ex)
  39. {
  40. WriteLog("MemoryTableData:" + cln);
  41. }
  42. }
  43. #region 监控界面使用
  44. /// <summary>
  45. /// 根据计量点取采集的值
  46. /// CAR11PlcStauts PLC控制器状态 "0:初始值 数采服务器每隔2秒向PLC写入值1234;PLC会每隔2S不停用值4321覆盖"
  47. /// CAR11MesWeight 上位机仪表重量 129160
  48. /// HMI11LightWest HMI下发前红绿灯命令 "远控模式下:1、前红绿灯为绿灯;2、前红绿灯为红灯;"
  49. /// CAR11LightWest 前红绿灯 "1、前红绿灯为绿灯;2、前红绿灯为红灯;"
  50. /// CAR11LightEast 后红绿灯 "1、前红绿灯为绿灯;2、前红绿灯为红灯;"
  51. /// HMI11CaptureWest HMI控制前抓拍触发信号 "0、不操作;1、HMI下发西边抓拍单元抓拍信号;"
  52. /// HMI11CaptureEast HMI控制后抓拍触发信号 "0、不操作;1、HMI下发东边抓拍单元抓拍信号;"
  53. /// HMI11ClearZero HMI下发仪表清零指令 1:HMI对仪表清零
  54. /// CAR11StaticStatus 车辆检测 "0、东西方向激光均无报警; 1、过磅时,西边激光报警;2、过磅时,东边激光报警;3、过磅时,东西两边激光均报警"
  55. /// CAR11InfraredWest 前车量检测状态 "1、前激光报警;2、前激光无报警;"
  56. /// CAR11InfraredEast 后车量检测状态 "1、后激光报警;2、后激光无报警;"
  57. /// CAR11ZeroWeightStatus 汽车空磅状态 0、其它,1、零点报警,红绿灯变红,
  58. /// HMI11SwitchLight 红绿灯远控/自动 "1、远方切换开关处于自动控制位置;2、远方切换开关处于远方控制位置;远方切换方开关位于界面上"
  59. /// CAR11SwitchLight PLC红绿灯控制模式状态 "PLC有三种控制模式:1、自动、2、远控;3、检修。 优先权从高到低:检修>远控>自动。PLC默认为自动控制,设2个切换开关,2个在远方操作人员在界面上,可完成远控/自动切换。一个开关在现场PLC控制箱中,可完成检修/自动切换。"
  60. /// CAR11VlDirection 左红外光栅检测 1、光栅报警;2、光栅无报警
  61. /// CAR11VlPLCWeight 右红外光栅检测 1、光栅报警;2、光栅无报警
  62. /// </summary>
  63. public List<LiveData> getWarnInfo(string sPoint)
  64. {
  65. try
  66. {
  67. List<LiveData> ltn = (List<LiveData>)cln.CallRpc<LiveData>("LiveData:readByPointId", new object[] { sPoint });
  68. return ltn;
  69. }
  70. catch (Exception ex)
  71. {
  72. WriteLog("读采集数据异常错误:" + ex.Message);
  73. return null;
  74. }
  75. }
  76. public List<LiveData> getWarnInfo_Belt(string sPoint)
  77. {
  78. try
  79. {
  80. List<LiveData> ltn = (List<LiveData>)cln.CallRpc<LiveData>("LiveData:readByPointId", new object[] { sPoint });
  81. return ltn;
  82. }
  83. catch (Exception ex)
  84. {
  85. WriteLog("读采集数据异常错误:" + ex.Message);
  86. return null;
  87. }
  88. }
  89. /// <summary>
  90. /// 写入控制表(道闸,仪表,红外,LED等)
  91. /// </summary>
  92. /// <param name="sPoint">计量点</param>
  93. /// <param name="OptionType">操作类型</param>
  94. /// <param name="value">被写入的信息</param>
  95. /// <param name="strUserID">用户编号</param>
  96. /// <param name="errorInfo">异常信息接收</param>
  97. /// <returns></returns>
  98. public bool writeWarnInfo(string sPoint, string OptionType, string value, string strUserID, out string errorInfo)
  99. {
  100. try
  101. {
  102. string strPntid = sPoint.Replace("CAR", "HMI") + OptionType;
  103. cln.CallRpc<int>("Control:doCommand", new object[] { strPntid, value, strUserID });
  104. errorInfo = "";
  105. return true;
  106. }
  107. catch (Exception ex)
  108. {
  109. WriteLog("写入信息到内存表异常!" + ex.Message);
  110. errorInfo = "写入信息到内存表异常!" + ex.Message;
  111. return false;
  112. }
  113. }
  114. #endregion 监控界面使用
  115. #region 计量主程序使用
  116. /// <summary>
  117. /// 读取Live表 某个字段信息
  118. /// </summary>
  119. /// <param name="value"></param>
  120. /// <param name="PointIdKey">CAR11</param>
  121. /// <param name="TagName">PowerStatus</param>
  122. /// <returns>JArray arr[0].Tagname, arr[0].Value, arr[0].DateTime, arr[0].Source</returns>
  123. public JArray LiveTable(string PointIdKey, string TagName)
  124. {
  125. try
  126. {
  127. List<Live> arr = (List<Live>)cln.CallRpc<Live>("Live.Table:select", new object[] { PointIdKey + TagName });//读
  128. if (arr != null && arr.Count > 0)
  129. {
  130. JArray jArray = new JArray(arr[0].Tagname, arr[0].Value, arr[0].DateTime, arr[0].Source);
  131. return jArray;
  132. }
  133. return null;
  134. }
  135. catch (Exception exp)
  136. {
  137. WriteLog("LiveTable:" + exp.Message + exp.StackTrace);
  138. return null;
  139. }
  140. }
  141. /// <summary>
  142. /// 读取实时表,一个计量点只有一条数据(重量,稳定,红外,车号等信息)
  143. /// </summary>
  144. /// <param name="value"></param>
  145. /// <param name="PointIdKey">计量点编号</param>
  146. /// <returns>
  147. /// MemoryTableDataSocket MemoClass = new MemoryTableDataSocket(strPntNo);
  148. /// JArray aryValue = MemoClass.TrackTable(strPntNo);//Track表数据每个计量点的数据
  149. /// string pointId = aryValue[0]; string carNo = aryValue[1];
  150. /// 对照如下:
  151. /// 0 pointid 计量点 string CAR11,CAR12,CAR13等计量点编号
  152. /// 1 status 车辆状态 number 0:等待车辆 1:车辆上称 2:重量不稳定 3:重量稳定 4:计量完成 5:下称
  153. /// 2 carno 车号 string 鄂B23955
  154. /// 3 weight 重量 number
  155. /// 4 weightStatus 重量状态 number 0:重量稳定; 1:重量不稳定; 2:空磅
  156. /// 5 parkStatus 停车状态 number 0:正常(头尾都未压线) 1:车头压线 2:车尾压线 3:两头压线; 记录在重量稳定后生成
  157. /// 6 datetime 上称时间 uint 当前时间的距离1970.1.1.08:00时间的秒数; 秒
  158. /// 7 LicType 车牌类型 number 0:车牌抓拍; 1:RFID 识别
  159. /// 8 WeightSave 计量保存重量 uint
  160. /// 9 FinishTime 过磅完成时间 uint 秒
  161. /// 10 entry 入口 string East = 东边, West = 西边
  162. /// 11 exit 出口 string East = 东边, West = 西边
  163. /// 12 platenoList 抓拍车牌信息 array "{direct, plateno} direct:string 方向 plateno: string 车牌/电子车牌"
  164. /// 13 rfidList RFID识别车牌信息 array "{direct, plateno} direct:string 方向plateno: string 车牌/电子车牌"
  165. /// </returns>
  166. public JArray TrackTable(string PointIdKey)
  167. {
  168. try
  169. {
  170. List<Track> arr = (List<Track>)cln.CallRpc<Track>("Track.Table:select", new object[] { PointIdKey });//读
  171. if (arr != null && arr.Count > 0)
  172. {
  173. List<CarInfo> PlateNoList = arr[0].PlateNoList == null ? new List<CarInfo>() : arr[0].PlateNoList;
  174. List<CarInfo> RfidList = arr[0].RfidList == null ? new List<CarInfo>() : arr[0].RfidList;
  175. //静态衡
  176. Dictionary<string, double> WgtList = arr[0].WeightList == null ? new Dictionary<string, double>() : arr[0].WeightList;
  177. JArray jArray = new JArray(
  178. arr[0].Pointid, //0
  179. arr[0].Status, //1
  180. arr[0].CarNo, //2
  181. arr[0].Weight, //3
  182. arr[0].WeightStatus, //4
  183. arr[0].StopStatus, //5
  184. arr[0].Datetime, //6
  185. arr[0].LicType, //7
  186. PlateNoList.Count == 0 ? "" : PlateNoList[PlateNoList.Count - 1].CarNo, //8
  187. RfidList.Count == 0 ? "" : RfidList[RfidList.Count - 1].CarNo, //9
  188. arr[0].WeightSave, //10
  189. arr[0].FinishTime, //11
  190. arr[0].Entry, //12
  191. arr[0].Exit); //13
  192. double mainWgt = 0;
  193. double viceWgt = 0;
  194. if (WgtList.Count > 1)
  195. {
  196. foreach (var it in WgtList)
  197. {
  198. if (it.Key.EndsWith("WeightMain"))
  199. {
  200. //jArray.Add(it.Value); //14
  201. mainWgt = it.Value;
  202. }
  203. else if (it.Key.EndsWith("WeightVice"))
  204. {
  205. //jArray.Add(it.Value); //15
  206. viceWgt = it.Value;
  207. }
  208. }
  209. jArray.Add(mainWgt); //14
  210. jArray.Add(viceWgt); //15
  211. }
  212. else
  213. {
  214. jArray.Add(arr[0].Weight);
  215. }
  216. try
  217. {
  218. WriteLog(PointIdKey + "重量采集:" + "计量点:" + arr[0].Pointid + "车号:" + arr[0].CarNo + "重量状态(0:重量稳定; 1:重量不稳定; 2:空磅):" + arr[0].WeightStatus + "重量" + arr[0].Weight);
  219. }
  220. catch (Exception)
  221. {
  222. WriteLog(PointIdKey + "写日志异常");
  223. }
  224. return jArray;
  225. }
  226. WriteLog(PointIdKey + "空值");
  227. return null;
  228. }
  229. catch (Exception exp)
  230. {
  231. try
  232. {
  233. cln = new CoredbClient(AppConfigCache.icoredbTcp);
  234. }
  235. catch { }
  236. WriteLog(PointIdKey + "_TrackTable:" + exp.Message + exp.StackTrace + "空值");
  237. return null;
  238. }
  239. }
  240. #endregion 计量主程序使用
  241. //测试
  242. private void find()
  243. {
  244. getBeltDataInfo("Belt01PLC" + "B01Flow", "Flow", "jl1/Belt/BEL01/BEL01Weight/B01Flow");
  245. }
  246. //皮带秤写入平台
  247. public int getBeltDataInfo(string tagname, object value, string source)
  248. {
  249. try
  250. {
  251. List<int> ltn = (List<int>)cln.CallRpc<LiveData>("LiveData:writeTag", new object[] { tagname, value, source });
  252. return ltn[0];
  253. }
  254. catch (Exception ex)
  255. {
  256. WriteLog("读采集数据异常错误:" + ex.Message);
  257. return 500;
  258. }
  259. }
  260. /// <summary>
  261. /// 按照计量点的输出日志
  262. /// </summary>
  263. /// <param name="strPointName"></param>
  264. /// <param name="str"></param>
  265. private void WriteLogSwicth(string strPointName, string str)
  266. {
  267. try
  268. {
  269. string m_szRunPath;
  270. m_szRunPath = PbCache.path;
  271. if (Directory.Exists(m_szRunPath + "\\log") == false)
  272. {
  273. Directory.CreateDirectory(m_szRunPath + "\\log");
  274. }
  275. string strDate = DateTime.Now.ToString("yyyyMMdd");
  276. string strPathFile = m_szRunPath + "\\log\\" + strDate;
  277. if (!Directory.Exists(strPathFile))//如果不存在就创建file文件夹
  278. {
  279. Directory.CreateDirectory(strPathFile);
  280. }
  281. TextWriter tw = new StreamWriter(strPathFile + "\\重量采集日志_" + strPointName + strDate + ".log", true);
  282. tw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  283. tw.WriteLine(str);
  284. tw.WriteLine("\r\n");
  285. tw.Close();
  286. }
  287. catch { }
  288. }
  289. private void WriteLog(string str)
  290. {
  291. try
  292. {
  293. string m_szRunPath;
  294. m_szRunPath = PbCache.path;
  295. if (Directory.Exists(m_szRunPath + "\\log") == false)
  296. {
  297. Directory.CreateDirectory(m_szRunPath + "\\log");
  298. }
  299. string strDate = DateTime.Now.ToString("yyyyMMdd");
  300. string strPathFile = m_szRunPath + "\\log\\" + strDate;
  301. if (!Directory.Exists(strPathFile))//如果不存在就创建file文件夹
  302. {
  303. Directory.CreateDirectory(strPathFile);
  304. }
  305. TextWriter tw = new StreamWriter(strPathFile + "\\重量采集日志_" + strDate + ".log", true);
  306. tw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  307. tw.WriteLine(str);
  308. tw.WriteLine("\r\n");
  309. tw.Close();
  310. }
  311. catch { }
  312. }
  313. }
  314. }