frmHotDeliveryBelt.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. using com.hnshituo.core.webapp.vo;
  2. using Common;
  3. using Common.vo.pb;
  4. using Infragistics.Win.UltraWinGrid;
  5. using JC_MeasuringSystem;
  6. using MeterModuleLibrary;
  7. using MeterPlugInLibrary;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Data;
  11. using System.Linq;
  12. using System.Net;
  13. using System.Threading;
  14. using System.Windows.Forms;
  15. namespace StorageMeterSystem
  16. {
  17. public partial class frmHotDeliveryBelt : Form
  18. {
  19. private HotDeliveryDataCollectionControl collection = new HotDeliveryDataCollectionControl(); //实时表数据采集
  20. public frmHotDeliveryBelt()
  21. {
  22. InitializeComponent();
  23. }
  24. private Log lg = Log.GetInstance();
  25. private BaseDbCls bd = new BaseDbCls();
  26. private string sVoice = "";
  27. private Thread thVoice = null;
  28. private VoicePlay vp = new VoicePlay();
  29. private string userId = "", userName = "";
  30. private List<string> strPntNoList = new List<string>();
  31. private CoreAppUser userInfo = null;
  32. private string strIpAddress = "";
  33. private string[] strPntNoCollect = null; //所有的计量点
  34. private DbHelper db = new DbHelper();
  35. private MeterBaseScalePointService monitorService = new MeterBaseScalePointService();
  36. private frmHotDeliveryBeltWarn fm = null;//预警界面
  37. public bool bMsgOpen = false; //是否打开了语音提醒
  38. private void frmMonitor_Load(object sender, EventArgs e)
  39. {
  40. PbCacheMonitor.frmMsgClosingTime = DateTime.Now;
  41. //timer2.Start();
  42. userId = ((ST_MainForm)(this.MdiParent)).UserID;
  43. userName = ((ST_MainForm)(this.MdiParent)).UserName;
  44. userInfo = ((ST_MainForm)(this.MdiParent)).AppUser;
  45. #region 载入计量点的信息
  46. bd.setBaseDbScalePointHot();
  47. bd.getDbBaseScaleHot();
  48. setGridData();
  49. strIpAddress = GetIP();
  50. foreach (UltraGridRow ugr in ultraGridPW.Rows)
  51. {
  52. strPntNoList.Add(ugr.Cells["collectionCode"].Text.Trim());//添加所有的计量点
  53. if (ugr.Cells["monitorUserNo"].Text.Trim() != "")
  54. {
  55. ugr.Cells["UNCK"].Value = true;
  56. }
  57. /*
  58. if (ugr.Cells["monitorUserNo"].Text.Trim() != "" && ugr.Cells["monitorUserNo"].Text.Trim() == userName && ugr.Cells["monitorUserIP"].Text.Trim() == strIpAddress)
  59. {
  60. ugr.Cells["UNCK"].Value = true;
  61. ucHotDeliveryBelt uc = new ucHotDeliveryBelt();
  62. //uc.strCollectNo = ugr.Cells["collectionCode"].Text.Trim();
  63. uc.Name = "uc" + ugr.Cells["scalePointNo"].Text;
  64. uc.sUserId = userId;
  65. uc.sUserName = userName;
  66. uc.sIpAddress = strIpAddress;
  67. uc.sPointNo = ugr.Cells["baseSpotNo"].Text;
  68. uc.sPointName = ugr.Cells["baseSpotName"].Text;
  69. //uc.ucMonitoringEvent += new EventHandler(ucEvent);
  70. //uc.evoice += new EventVoice(setsVoice);
  71. //****************************************注意下面这个**********************
  72. // uc.moxaip = dtPointInfo.Rows[k]["MOXAIP"].ToString().Trim();
  73. uc.openWarnMsgBelt += new ucHotDeliveryBelt.EventOpenWarnMsgBelt(OpenMsg);
  74. //uc.rowPointInfo = null;
  75. uc.BorderStyle = BorderStyle.FixedSingle;
  76. uc.db = db;
  77. flPanel.Controls.Add(uc);
  78. }
  79. else
  80. {
  81. ugr.Cells["UNCK"].Value = false;
  82. }
  83. */
  84. }
  85. strPntNoCollect = strPntNoList.ToArray();//加载所有的计量点,不管有没有接管
  86. collection.EventHotDeliveryDataCollectionArgs += new EventHotDeliveryDataCollection(weightCollect);//数据采集解析
  87. //collection.Start(new string[5] { "CAR40", "CAR41", "CAR42", "CAR43", "CAR44" });//数据采集启动
  88. collection.Start(strPntNoCollect);//数据采集启动
  89. ClsControlPack.RefreshAndAutoSize(this.ultraGridPW);
  90. #endregion 载入计量点的信息
  91. //thVoice.Start();
  92. }
  93. /// <summary>
  94. /// 计量点信息
  95. /// </summary>
  96. public void setGridData()
  97. {
  98. DataTable dt = dtJGPointInfo.Clone();
  99. if (PbCacheMonitor.ltMeterBaseScalePoint != null && PbCacheMonitor.ltMeterBaseScalePoint.Count > 0)
  100. {
  101. foreach (MeterBaseScalePoint mwm in PbCacheMonitor.ltMeterBaseScalePoint)
  102. {
  103. DataRow dr = dt.NewRow();
  104. dr["scalePointNo"] = mwm.scalePointNo;
  105. dr["baseSpotNo"] = mwm.baseSpotNo;
  106. dr["baseSpotName"] = mwm.baseSpotName;
  107. dr["monitorUserNo"] = mwm.monitorUserNo;
  108. dr["collectionCode"] = mwm.collectionCode;
  109. dr["monitorUserIP"] = mwm.monitorUserIP;
  110. dt.Rows.Add(dr);
  111. }
  112. }
  113. ClsControlPack.CopyDataToDatatable(ref dt, ref this.dtJGPointInfo, true);
  114. ClsControlPack.RefreshAndAutoSize(this.ultraGridPW);
  115. }
  116. private void VoiceOption(ucHotDeliveryBelt um, bool bMainUm)
  117. {
  118. if (PbCacheMonitor.ltMeterBaseScalePoint == null || PbCacheMonitor.ltMeterBaseScalePoint.Count == 0)
  119. {
  120. MessageBox.Show("获取计量点信息异常,请稍后再试");
  121. return;
  122. }
  123. MeterBaseScalePoint monitor = PbCacheMonitor.ltMeterBaseScalePoint.Where(s => s.baseSpotNo == um.sPointNo).FirstOrDefault();
  124. if (monitor == null)
  125. {
  126. MessageBox.Show("获取当前计量点信息异常,请稍后再试");
  127. return;
  128. }
  129. }
  130. /// <summary>
  131. /// 修改数据到接管表
  132. /// </summary>
  133. private void UpdateCarijeg(MeterBaseScalePoint monitor)
  134. {
  135. RESTfulResult<string> rms = monitorService.doUpdateWf(monitor);
  136. if (!rms.Succeed)
  137. {
  138. lg.WriteLog(4, "写入数据失败");
  139. }
  140. }
  141. /// <summary>
  142. /// 预报支数报警信息,从用户控件中回调主界面方法
  143. /// </summary>
  144. /// <param name="strWarnInfo"></param>
  145. private void OpenMsg(string strWarnInfo)
  146. {
  147. try
  148. {
  149. if (!bMsgOpen)
  150. {
  151. if (PbCacheMonitor.frmMsgClosingTime == null)
  152. {
  153. bMsgOpen = true;
  154. fm = frmHotDeliveryBeltWarn.CreateInstrance(this);
  155. fm.TopMost = true;
  156. fm.setLbTxt(strWarnInfo);
  157. fm.Show();
  158. }
  159. else
  160. {
  161. TimeSpan secondSpan = new TimeSpan(DateTime.Now.Ticks - PbCacheMonitor.frmMsgClosingTime.Value.Ticks);
  162. if (secondSpan.TotalSeconds > 5)
  163. {
  164. bMsgOpen = true;
  165. fm = frmHotDeliveryBeltWarn.CreateInstrance(this);
  166. fm.TopMost = true;
  167. fm.setLbTxt(strWarnInfo);
  168. fm.Show();
  169. }
  170. }
  171. }
  172. /*
  173. if (!string.IsNullOrEmpty(openPointNo) && fm != null)
  174. {
  175. fm.CloseFrm();
  176. }
  177. */
  178. }
  179. catch { }
  180. }
  181. /// <summary>
  182. /// 重量采集
  183. /// </summary>
  184. /// <param name="o"></param>
  185. /// <param name="e"></param>
  186. private void weightCollect(object o, List<CollectModel> e)
  187. {
  188. try
  189. {
  190. //采集的重量赋值
  191. if (e != null)
  192. {
  193. foreach (var item in e)
  194. {
  195. foreach (MeterBaseScalePoint mwm in PbCacheMonitor.ltMeterBaseScalePoint)
  196. {
  197. if (mwm.collectionCode.Equals(item.pointid))
  198. {
  199. mwm.weight = item.weight;
  200. mwm.weightStatus = item.weightStatus;
  201. if (mwm.weight < 200)
  202. {
  203. //零点值,重量清零时,零点值也清零;
  204. mwm.weightZero = mwm.weight;
  205. }
  206. }
  207. //将采集的重量给监控界面赋值
  208. foreach (Control cn in flPanel.Controls)
  209. {
  210. if (cn.Name.Contains("uc"))
  211. {
  212. ucHotDeliveryBelt ucCar = cn as ucHotDeliveryBelt;
  213. if (ucCar.sPointNo == mwm.baseSpotNo)
  214. {
  215. ucCar.rowPointInfo = mwm;
  216. }
  217. }
  218. }
  219. }
  220. }
  221. }
  222. //foreach (MeterBaseScalePoint mon in PbCacheMonitor.ltMeterBaseScalePoint)
  223. //{
  224. // foreach (Control cn in flPanel.Controls)
  225. // {
  226. // if (cn.Name.Contains("uc"))
  227. // {
  228. // ucHotDeliveryBelt ucCar = cn as ucHotDeliveryBelt;
  229. // if (ucCar.sPointNo == mon.baseSpotNo)
  230. // {
  231. // ucCar.rowPointInfo = mon;
  232. // }
  233. // }
  234. // }
  235. //}
  236. }
  237. catch (Exception)
  238. {
  239. throw;
  240. }
  241. }
  242. /// <summary>
  243. /// 重点!!!,已取消,直接从采集信息中拿值
  244. /// 从主界面向用户控件中插入信息
  245. /// </summary>
  246. /// <param name="sender"></param>
  247. /// <param name="e"></param>
  248. private void timer2_Tick(object sender, EventArgs e)
  249. {
  250. try
  251. {
  252. //if (bd.getDb())
  253. if (PbCacheMonitor.ltMeterBaseScalePoint != null)
  254. {
  255. foreach (MeterBaseScalePoint mon in PbCacheMonitor.ltMeterBaseScalePoint)
  256. {
  257. foreach (Control cn in flPanel.Controls)
  258. {
  259. if (cn.Name.Contains("uc"))
  260. {
  261. ucHotDeliveryBelt ucCar = cn as ucHotDeliveryBelt;
  262. if (ucCar.sPointNo == mon.baseSpotNo)
  263. {
  264. ucCar.rowPointInfo = mon;
  265. }
  266. }
  267. }
  268. }
  269. }
  270. else
  271. {
  272. lg.WriteLog(4, "未找到实时监控数据");
  273. }
  274. //*/
  275. }
  276. catch (Exception ex)
  277. {
  278. lg.WriteLog(4, "数据库连接异常");
  279. }
  280. }
  281. private void ultraGridPW_CellChange(object sender, CellEventArgs e)
  282. {
  283. ultraGridPW.UpdateData();
  284. UltraGridRow ugr = ultraGridPW.ActiveRow;
  285. if (ugr != null)
  286. {
  287. bd.getDbBaseScaleHot();
  288. if (ugr.Cells["UNCK"].Value.ToString().ToLower() == "false")
  289. {
  290. //重新查询一下是否已经接管了;
  291. MeterBaseScalePoint mwm = PbCacheMonitor.ltMeterBaseScalePoint.Where(s => s.scalePointNo == ugr.Cells["scalePointNo"].Text.Trim()).FirstOrDefault();
  292. //先查一次是否接管了
  293. if (mwm != null && !string.IsNullOrEmpty(mwm.monitorUserNo) && (!mwm.monitorUserNo.Equals(userName) || !mwm.monitorUserIP.Equals(strIpAddress)))
  294. {
  295. ugr.Cells["UNCK"].Value = true;
  296. MessageBox.Show("当前计量点正在被账号【" + mwm.monitorUserNo + "】接管,接管IP【" + strIpAddress + "】,不允许重复接管!");
  297. setGridData();
  298. foreach (UltraGridRow ugrRow in ultraGridPW.Rows)
  299. {
  300. if (ugrRow.Cells["monitorUserNo"].Text.Trim() != "")
  301. {
  302. ugrRow.Cells["UNCK"].Value = true;
  303. }
  304. else
  305. {
  306. ugrRow.Cells["UNCK"].Value = false;
  307. }
  308. }
  309. return;
  310. }
  311. flPanel.Controls.RemoveByKey("uc" + ugr.Cells["scalePointNo"].Text);
  312. RESTfulResult<string> rm = monitorService.doUpdateWf(new MeterBaseScalePoint { scalePointNo = ugr.Cells["scalePointNo"].Text.Trim(), monitorUserNo = "", monitorUserIP = "" });
  313. if (!rm.Succeed)
  314. {
  315. lg.WriteLog(4, "写入取消接管信息失败");
  316. }
  317. }
  318. else
  319. {
  320. MeterBaseScalePoint mwm = PbCacheMonitor.ltMeterBaseScalePoint.Where(s => s.scalePointNo == ugr.Cells["scalePointNo"].Text.Trim()).FirstOrDefault();
  321. //先查一次是否接管了
  322. if (mwm != null && !string.IsNullOrEmpty(mwm.monitorUserNo))
  323. {
  324. ugr.Cells["UNCK"].Value = false;
  325. MessageBox.Show("当前计量点已被账号【" + mwm.monitorUserNo + "】接管,接管IP地址【" + mwm.monitorUserIP + "】");
  326. setGridData();
  327. foreach (UltraGridRow ugrRow in ultraGridPW.Rows)
  328. {
  329. if (ugrRow.Cells["monitorUserNo"].Text.Trim() != "")
  330. {
  331. ugrRow.Cells["UNCK"].Value = true;
  332. }
  333. else
  334. {
  335. ugrRow.Cells["UNCK"].Value = false;
  336. }
  337. }
  338. return;
  339. }
  340. ucHotDeliveryBelt uc = new ucHotDeliveryBelt();//点击数据行进行接管
  341. //uc.strCollectNo = ugr.Cells["collectionCode"].Text.Trim();
  342. uc.Name = "uc" + ugr.Cells["scalePointNo"].Text;
  343. uc.sUserId = userId;
  344. uc.sUserName = userName;
  345. uc.sIpAddress = strIpAddress;
  346. uc.sPointNo = ugr.Cells["baseSpotNo"].Text;
  347. uc.sPointName = ugr.Cells["baseSpotName"].Text;
  348. uc.appUser = userInfo;
  349. //uc.ucMonitoringEvent += new EventHandler(ucEvent);
  350. //uc.evoice += new EventVoice(setsVoice);
  351. //****************************************注意下面这个**********************
  352. // uc.moxaip = dtPointInfo.Rows[k]["MOXAIP"].ToString().Trim();
  353. uc.openWarnMsgBelt += new ucHotDeliveryBelt.EventOpenWarnMsgBelt(OpenMsg);
  354. uc.rowPointInfo = null;
  355. uc.BorderStyle = BorderStyle.FixedSingle;
  356. uc.db = db;
  357. flPanel.Controls.Add(uc);
  358. RESTfulResult<string> rm = monitorService.doUpdateWf(new MeterBaseScalePoint { scalePointNo = ugr.Cells["scalePointNo"].Text.Trim(), monitorUserNo = userName, monitorUserIP = strIpAddress });
  359. if (!rm.Succeed)
  360. {
  361. lg.WriteLog(4, "写入接管信息失败");
  362. }
  363. }
  364. }
  365. //点击后,重新查询一遍数据
  366. bd.getDbBaseScaleHot();
  367. setGridData();
  368. foreach (UltraGridRow ugrRow in ultraGridPW.Rows)
  369. {
  370. if (ugrRow.Cells["monitorUserNo"].Text.Trim() != "")
  371. {
  372. ugrRow.Cells["UNCK"].Value = true;
  373. }
  374. }
  375. }
  376. /// <summary>
  377. /// 获取IP地址
  378. /// </summary>
  379. /// <returns></returns>
  380. public string GetIP()
  381. {
  382. try
  383. {
  384. IPHostEntry ipHost = Dns.Resolve(Dns.GetHostName());
  385. IPAddress ipAddr = ipHost.AddressList[0];
  386. return ipAddr.ToString();
  387. }
  388. catch (Exception ex)
  389. {
  390. Random rd = new Random();
  391. MessageBox.Show("GetIP方法异常,请关闭界面并稍后打开,或联系管理员!\r\n异常原因:\r\n!" + ex);
  392. lg.WriteLog(13, "GetIP方法异常:" + ex.Message);
  393. return rd.Next(99999, 999999) + "";
  394. }
  395. }
  396. /// <summary>
  397. /// 界面关闭方法:还原接管状态
  398. /// </summary>
  399. /// <param name="sender"></param>
  400. /// <param name="e"></param>
  401. private void frmMonitor_FormClosing(object sender, FormClosingEventArgs e)
  402. {
  403. try
  404. {
  405. collection.Stop();
  406. //timer2.Stop();
  407. //取消接管所有已接管的计量点
  408. foreach (UltraGridRow ugr in ultraGridPW.Rows)
  409. {
  410. if (ugr.Cells["monitorUserNo"].Text.Trim() != "" && ugr.Cells["monitorUserNo"].Text.Trim() == userName)
  411. {
  412. if (ugr.Cells["UNCK"].Value.ToString().ToUpper() == "TRUE")
  413. {
  414. UpdateCarijeg(new MeterBaseScalePoint { scalePointNo = ugr.Cells["scalePointNo"].Text.Trim(), monitorUserNo = "", monitorUserIP = "" });
  415. }
  416. }
  417. }
  418. if (thVoice != null) thVoice.Abort();
  419. }
  420. catch (Exception ex)
  421. {
  422. lg.WriteLog(4, "关闭热送磅计量异常:" + ex.Message);
  423. }
  424. finally
  425. {
  426. }
  427. }
  428. }
  429. }