Main.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  1. using System;
  2. using DevExpress.XtraBars;
  3. using UAS_DeviceMonitor.DataOperate;
  4. using System.Data;
  5. using System.Text;
  6. using UAS_DeviceMonitor.Entity;
  7. using UAS_DeviceMonitor.PublicMethod;
  8. using DevExpress.XtraGrid.Views.Grid;
  9. using System.Collections.Generic;
  10. using System.Drawing;
  11. using DevExpress.XtraBars.Ribbon;
  12. using System.Threading;
  13. using UAS_DeviceMonitor.CustomerControl.PictureEditWithText;
  14. using System.Windows.Forms;
  15. using DevExpress.XtraEditors.Controls;
  16. using DevExpress.XtraEditors;
  17. namespace UAS_DeviceMonitor
  18. {
  19. public partial class Main : RibbonForm
  20. {
  21. StringBuilder sql = new StringBuilder();
  22. StringBuilder DeviceStatusQuerySQL = new StringBuilder();
  23. DataHelper dh;
  24. ModeBusTCPServer mbt = new ModeBusTCPServer();
  25. List<string> SQL = new List<string>();
  26. #region 初始化代码
  27. public Main()
  28. {
  29. dh = new DataHelper();
  30. SystemInf.dh = dh;
  31. InitializeComponent();
  32. dpc_enableCheckEdit.QueryCheckStateByValue += CheckedEdit_QueryCheckStateByValue;
  33. CommandSetRepositoryItemComboBox1.ParseEditValue += CommandSetRepositoryItemComboBox1_ParseEditValue;
  34. CheckEditCommandSet.QueryCheckStateByValue += CheckedEdit_QueryCheckStateByValue;
  35. PollSettingItemSearchLookUpEdit.ParseEditValue += PollSettingItemSearchLookUpEdit_ParseEditValue;
  36. GridViewPollSetting.RowCellStyle += GridViewPollSetting_RowCellStyle;
  37. #region 添加发送接收编码规则的静态值
  38. ComboBoxData cbd = new ComboBoxData();
  39. cbd.Text = "UTF-8";
  40. cbd.Value = "UTF-8";
  41. ComboBoxData cbd1 = new ComboBoxData();
  42. cbd1.Text = "Hexadecimal";
  43. cbd1.Value = "Hexadecimal";
  44. ComboBoxData cbd2 = new ComboBoxData();
  45. cbd2.Text = "ASCII";
  46. cbd2.Value = "ASCII";
  47. CommandSetRepositoryItemComboBox1.Items.Add(cbd);
  48. CommandSetRepositoryItemComboBox1.Items.Add(cbd1);
  49. CommandSetRepositoryItemComboBox1.Items.Add(cbd2);
  50. #endregion
  51. }
  52. private void CommandSetRepositoryItemComboBox1_ParseEditValue(object sender, ConvertEditValueEventArgs e)
  53. {
  54. string val = "";
  55. if (e.Value != null)
  56. {
  57. val = e.Value.ToString();
  58. }
  59. else
  60. {
  61. val = "FALSE";//默认为不选
  62. }
  63. e.Value = val;
  64. e.Handled = true;
  65. }
  66. private void PollSettingItemSearchLookUpEdit_ParseEditValue(object sender, ConvertEditValueEventArgs e)
  67. {
  68. e.Handled = true;
  69. }
  70. private void Main_Load(object sender, EventArgs e)
  71. {
  72. //设备列表
  73. GridDeviceList.GetDataSQL = "select de_id,de_code,de_name,de_spec,de_indate,de_runstatus,de_address,de_wccode,de_vendcode,de_vendname from device".ToUpper();
  74. ToolPageControlDeviceList.Gridcontrol = GridDeviceList;
  75. //轮询业务
  76. //GridPolling.GetDataSQL = "select 0 CHECKEDCOLUMN,pl_id,pl_code,pl_name,pl_type,pl_dccode,pl_remark from polling".ToUpper();
  77. //GridPolling.ID = "pl_id";
  78. //GridPolling.TableName = "polling";
  79. //GridPolling.InsertSQL = "insert into polling(pl_id,pl_code,pl_name,pl_type,pl_dccode,pl_remark) values(polling_seq.nextval,:pl_code,:pl_name,:pl_type,:pl_dccode,:pl_remark)";
  80. //ButtonDeletePolling.Grid = GridPolling;
  81. //ButtonSavePolling.Grid = GridPolling;
  82. //ButtonAddPolling.Grid = GridPolling;
  83. //轮询配置界面
  84. GridPollingSetting.GetDataSQL = "SELECT '' POLLSETTINGSTATUSCOLUMN,0 CHECKEDCOLUMN,DPC_ID,DPC_DECODE ,DPC_INTERVAL ,DPC_DCCODE ,DPC_FUNCTION , nvl(DPC_ENABLE,0)DPC_ENABLE,DPC_STATUS ,DPC_REMARK FROM DEVICEPOLLINGCONFIG ORDER BY DPC_ID";
  85. GridPollingSetting.ID = "DPC_ID";
  86. GridPollingSetting.TableName = "DEVICEPOLLINGCONFIG";
  87. GridPollingSetting.InsertSQL = "insert into DEVICEPOLLINGCONFIG(DPC_ID,DPC_DECODE ,DPC_INTERVAL ,DPC_DCCODE ,DPC_FUNCTION , DPC_ENABLE,DPC_STATUS ,DPC_REMARK) values(DEVICEPOLLINGCONFIG_seq.nextval,:DPC_DECODE ,:DPC_INTERVAL ,:DPC_DCCODE ,:DPC_FUNCTION , :DPC_ENABLE,:DPC_STATUS ,:DPC_REMARK)";
  88. ButtonSaveCommandSet.Grid = GridPollingSetting;
  89. ButtonNewCommandSet.Grid = GridPollingSetting;
  90. ButtonDeleteCommandSet.Grid = GridPollingSetting;
  91. //指令设置界面
  92. GridCommandSetting.GetDataSQL = "select 0 CHECKEDCOLUMN,dc_id,dc_code,dc_name,dc_value,dc_sendcoding,dc_receivecoding from devicecommand ".ToUpper();
  93. GridCommandSetting.TableName = "devicecommand";
  94. GridCommandSetting.ID = "dc_id";
  95. GridCommandSetting.InsertSQL = "insert into devicecommand(dc_id,dc_code,dc_name,dc_value,dc_debrand,dc_sendcoding,dc_receivecoding) values(devicecommand_seq.nextval,:dc_code,:dc_name,:dc_value,'" + BaseUtil.GetComboxEditValue(Brand) + "',:dc_sendcoding,:dc_receivecoding)";
  96. ButtonSaveCommand.Grid = GridCommandSetting;
  97. ButtonDeleteCommand.Grid = GridCommandSetting;
  98. ButtonNewCommand.Grid = GridCommandSetting;
  99. //设备联网配置界面
  100. GridDeviceNetSetting.GetDataSQL = "SELECT 0 CHECKEDCOLUMN,DNC_ID,DNC_DECODE,DNC_GATEWAY,DNC_UPPERIP,DNC_PORT,DNC_MAC,DNC_IP,DNC_TYPE FROM DEVICENETCONFIG order by DNC_ID";
  101. GridDeviceNetSetting.ID = "dnc_id";
  102. GridDeviceNetSetting.TableName = "DEVICENETCONFIG";
  103. GridDeviceNetSetting.InsertSQL = "insert into DEVICENETCONFIG(DNC_ID,DNC_DECODE,DNC_GATEWAY,DNC_UPPERIP,DNC_PORT,DNC_MAC,DNC_IP,DNC_TYPE) values(DEVICENETCONFIG_SEQ.NEXTVAL,:DNC_DECODE,:DNC_GATEWAY,:DNC_UPPERIP,:DNC_PORT,:DNC_MAC,:DNC_IP,:DNC_TYPE)";
  104. ButtonDeleteNetConfig.Grid = GridDeviceNetSetting;
  105. ButtonAddNetConfig.Grid = GridDeviceNetSetting;
  106. ButtonSaveNetConfig.Grid = GridDeviceNetSetting;
  107. Ptime = new Dictionary<int, PollingTimer>();
  108. }
  109. #endregion
  110. #region 界面通用事件
  111. /// <summary>
  112. /// 选项卡切换不同XPage
  113. /// </summary>
  114. /// <param name="sender"></param>
  115. /// <param name="e"></param>
  116. private void ButtonItem_ItemClick(object sender, ItemClickEventArgs e)
  117. {
  118. HideXPage(e.Item.Tag.ToString());
  119. }
  120. /// <summary>
  121. /// 根据点击按钮的Tag显示对应的XPage
  122. /// </summary>
  123. /// <param name="PageName"></param>
  124. private void HideXPage(string PageName)
  125. {
  126. for (int i = 0; i < MainTabControl.TabPages.Count; i++)
  127. {
  128. if (MainTabControl.TabPages[i].Name == PageName)
  129. MainTabControl.TabPages[i].PageVisible = true;
  130. else
  131. MainTabControl.TabPages[i].PageVisible = false;
  132. }
  133. }
  134. /// <summary>
  135. /// 勾选Grid的CheckBox
  136. /// </summary>
  137. /// <param name="sender"></param>
  138. /// <param name="e"></param>
  139. private void CheckedEdit_QueryCheckStateByValue(object sender, DevExpress.XtraEditors.Controls.QueryCheckStateByValueEventArgs e)
  140. {
  141. string val = "";
  142. if (e.Value != null)
  143. {
  144. val = e.Value.ToString();
  145. }
  146. else
  147. {
  148. val = "FALSE";//默认为不选
  149. }
  150. switch (val.ToUpper())
  151. {
  152. case "TRUE":
  153. case "YES":
  154. case "1":
  155. e.CheckState = System.Windows.Forms.CheckState.Checked;
  156. break;
  157. case "FALSE":
  158. case "NO":
  159. case "0":
  160. e.CheckState = System.Windows.Forms.CheckState.Unchecked;
  161. break;
  162. default:
  163. e.CheckState = System.Windows.Forms.CheckState.Checked;
  164. break;
  165. }
  166. e.Handled = true;
  167. }
  168. #endregion
  169. #region PageCommandSet业务代码(指令设置)
  170. /// <summary>
  171. /// 判断指令编号不能重复
  172. /// </summary>
  173. /// <param name="sender"></param>
  174. /// <param name="e"></param>
  175. private void ButtonSaveCommand_Click(object sender, EventArgs e)
  176. {
  177. GridCommandSetting.InsertSQL = "insert into devicecommand(dc_id,dc_code,dc_name,dc_value,dc_debrand,dc_sendcoding,dc_receivecoding) values(devicecommand_seq.nextval,:dc_code,:dc_name,:dc_value,'" + BaseUtil.GetComboxEditValue(Brand) + "',:dc_sendcoding,:dc_receivecoding)";
  178. ButtonSaveCommand.DoSave();
  179. }
  180. /// <summary>
  181. /// 加载ComBox品牌数据
  182. /// </summary>
  183. /// <param name="sender"></param>
  184. /// <param name="e"></param>
  185. private void PageCommandSet_VisibleChanged(object sender, EventArgs e)
  186. {
  187. if (PageCommandSet.PageVisible)
  188. {
  189. DataTable dt = (DataTable)SystemInf.dh.ExecuteSql("select * from devicebrand", "select");
  190. BaseUtil.FillComBoxEditWidthDataTable(Brand, "db_name", "db_code", dt);
  191. GridCommandSetting.Condition = " where dc_debrand='" + BaseUtil.GetComboxEditValue(Brand) + "' order by dc_id";
  192. }
  193. }
  194. private void Brand_SelectedIndexChanged(object sender, EventArgs e)
  195. {
  196. GridCommandSetting.Condition = " where dc_debrand='" + BaseUtil.GetComboxEditValue(Brand) + "' order by dc_id";
  197. GridCommandSetting.RefreshData();
  198. }
  199. #endregion
  200. #region PagePollingSetting业务代码(轮询配置)
  201. PollingTask pt = new PollingTask();
  202. /// <summary>
  203. /// 绘制轮询配置状态栏
  204. /// </summary>
  205. List<int> PollSettingPaintRowIndex = new List<int>();
  206. private void ButtonStartPolling_Click(object sender, EventArgs e)
  207. {
  208. GridView grid = GridViewPollSetting;
  209. mbt.Open();
  210. for (int i = 0; i < GridPollingSetting.RowCount; i++)
  211. {
  212. Polling pl = new Polling();
  213. pl.Id = int.Parse(GridViewPollSetting.GetRowCellValue(i, "DPC_ID").ToString());
  214. pl.DeviceCode = GridViewPollSetting.GetRowCellValue(i, "DPC_DECODE").ToString();
  215. pl.CommandCode = GridViewPollSetting.GetRowCellValue(i, "DPC_DCCODE").ToString();
  216. pl.Interval = int.Parse(GridViewPollSetting.GetRowCellValue(i, "DPC_INTERVAL").ToString());
  217. pl.SendRadix = GridViewPollSetting.GetRowCellValue(i, "DPC_INTERVAL").ToString();
  218. pl.Enable = GridViewPollSetting.GetRowCellValue(i, "DPC_ENABLE").ToString() != "0";
  219. pl.Dh = new DataHelper();
  220. if (pl.Enable)
  221. {
  222. //插入轮询日志
  223. sql.Clear();
  224. sql.Append("insert into DEVICEPOLLINGLOG(dpg_id,dpg_decode,dpg_starttime,dpg_interval)values");
  225. sql.Append("(DEVICEPOLLINGLOG_seq.nextval,'" + pl.DeviceCode + "',sysdate,'" + pl.Interval + "')");
  226. dh.ExecuteSql(sql.ToString(), "insert");
  227. pt.AddTask(RunTask, pl);
  228. //添加到状态为运行的行
  229. PollSettingPaintRowIndex.Add(i);
  230. GridPollingSetting.Focus();
  231. }
  232. }
  233. }
  234. /// <summary>
  235. /// 绘制轮询状态
  236. /// </summary>
  237. /// <param name="sender"></param>
  238. /// <param name="e"></param>
  239. private void GridViewPollSetting_RowCellStyle(object sender, RowCellStyleEventArgs e)
  240. {
  241. if (e.Column.Name.ToUpper() == "POLLSETTINGSTATUSCOLUMN" && PollSettingPaintRowIndex.Contains(e.RowHandle))
  242. {
  243. e.Appearance.BackColor = Color.Green;
  244. }
  245. }
  246. Dictionary<int, PollingTimer> Ptime;
  247. private void RunTask(object i)
  248. {
  249. PollingTimer timer = new PollingTimer();
  250. Polling pl = (Polling)i;
  251. timer.Polling = i;
  252. timer.Interval = pl.Interval * 5000;
  253. timer.Elapsed += Timer_Tick;
  254. timer.Start();
  255. try
  256. {
  257. if (!Ptime.ContainsKey(pl.Id))
  258. Ptime.Add(pl.Id, timer);
  259. }
  260. catch (Exception)
  261. {
  262. if (!Ptime.ContainsKey(pl.Id))
  263. Ptime.Add(pl.Id, timer);
  264. }
  265. }
  266. /// <summary>
  267. /// 轮询执行的业务
  268. /// </summary>
  269. /// <param name="sender"></param>
  270. /// <param name="e"></param>
  271. private void Timer_Tick(object sender, EventArgs e)
  272. {
  273. PollingTimer timer = (PollingTimer)sender;
  274. Polling pl = (Polling)timer.Polling;
  275. DataHelper dh = pl.Dh;
  276. string Decode = pl.DeviceCode;
  277. string Dccode = pl.CommandCode;
  278. DataTable dt = (DataTable)dh.ExecuteSql("select dnc_ip,dnc_port from DEVICENETCONFIG where dnc_decode='" + Decode + "'", "select");
  279. DataTable dt1 = (DataTable)dh.ExecuteSql("select dc_sendcoding,dc_value,dc_receivecoding from devicecommand where dc_code='" + Dccode + "'", "select");
  280. if (dt.Rows.Count > 0)
  281. {
  282. string IP = dt.Rows[0]["dnc_ip"].ToString() + ":" + dt.Rows[0]["dnc_port"].ToString();
  283. string SendCoding = "";
  284. string ReceiveCoding = "";
  285. string Command = "";
  286. int SendCommandByteSize = 0;
  287. if (dt1.Rows.Count > 0)
  288. {
  289. SendCoding = dt1.Rows[0]["dc_sendcoding"].ToString();
  290. ReceiveCoding = dt1.Rows[0]["dc_receivecoding"].ToString();
  291. Command = dt1.Rows[0]["dc_value"].ToString();
  292. SendCommandByteSize = Encoding.Default.GetBytes(Command.ToCharArray()).Length;
  293. }
  294. mbt.Send(IP, SendCoding, ReceiveCoding, Command);
  295. //处理2秒之内返回的指令
  296. Thread.Sleep(1000);
  297. if (mbt.Returnvalue.ContainsKey(IP))
  298. {
  299. int ReceiveCommandByteSize = Encoding.Default.GetBytes(mbt.Returnvalue[IP].ToCharArray()).Length;
  300. mbt.Returnvalue.Remove(IP);
  301. SQL.Clear();
  302. //更新轮询状态
  303. sql.Clear();
  304. sql.Append("update DEVICEPOLLINGCONFIG set dpc_status='Running' where dpc_decode='" + Decode + "' and dpc_dccode='" + Dccode + "'");
  305. SQL.Add(sql.ToString());
  306. //更新轮询日志状态
  307. sql.Clear();
  308. sql.Append("update DEVICEPOLLINGLOG set dpg_status='Running',dpg_senddatasize=nvl(dpg_senddatasize,0)+" + SendCommandByteSize);
  309. sql.Append(",dpg_receivedatasize=nvl(dpg_receivedatasize,0)+" + ReceiveCommandByteSize + ",dpg_count=nvl(dpg_count,0)+1 ");
  310. sql.Append(" where dpg_id=(select max(dpg_id) from DEVICEPOLLINGLOG where dpg_decode='" + Decode + "')");
  311. SQL.Add(sql.ToString());
  312. dh.ExecuteSQLTran(SQL.ToArray());
  313. }
  314. else
  315. {
  316. SQL.Clear();
  317. //更新轮询状态
  318. sql.Clear();
  319. sql.Append("update DEVICEPOLLINGCONFIG set dpc_status='Stop' where dpc_decode='" + Decode + "' and dpc_dccode='" + Dccode + "'");
  320. SQL.Add(sql.ToString());
  321. //更新轮询日志状态
  322. sql.Clear();
  323. sql.Append("update DEVICEPOLLINGLOG set dpg_status='Running',dpg_senddatasize=nvl(dpg_senddatasize,0)+");
  324. sql.Append(SendCommandByteSize + ",dpg_count=nvl(dpg_count,0)+1 ");
  325. sql.Append("where dpg_id=(select max(dpg_id) from DEVICEPOLLINGLOG where dpg_decode='" + Decode + "')");
  326. SQL.Add(sql.ToString());
  327. dh.ExecuteSQLTran(SQL.ToArray());
  328. }
  329. }
  330. }
  331. /// <summary>
  332. /// 停止全部轮询
  333. /// </summary>
  334. /// <param name="sender"></param>
  335. /// <param name="e"></param>
  336. private void ButtonPausePolling_Click(object sender, EventArgs e)
  337. {
  338. foreach (var item in Ptime)
  339. {
  340. item.Value.Stop();
  341. }
  342. PollSettingPaintRowIndex.Clear();
  343. GridPollingSetting.Focus();
  344. Ptime.Clear();
  345. }
  346. private void GridViewPollSetting_CellValueChanging(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
  347. {
  348. GridViewPollSetting.SetRowCellValue(e.RowHandle, e.Column, e.Value);
  349. if (e.Column.Name.ToUpper() == "DPC_ENABLE")
  350. {
  351. if (GridViewPollSetting.GetRowCellValue(e.RowHandle, "DPC_ENABLE").ToString() == "0")
  352. {
  353. int id = int.Parse(GridViewPollSetting.GetRowCellValue(e.RowHandle, "DPC_ID").ToString());
  354. if (Ptime.ContainsKey(id))
  355. {
  356. Ptime[id].Stop();
  357. Ptime.Remove(id);
  358. PollSettingPaintRowIndex.Remove(e.RowHandle);
  359. }
  360. }
  361. else
  362. {
  363. Polling pl = new Polling();
  364. pl.Id = int.Parse(GridViewPollSetting.GetRowCellValue(e.RowHandle, "DPC_ID").ToString());
  365. pl.DeviceCode = GridViewPollSetting.GetRowCellValue(e.RowHandle, "DPC_DECODE").ToString();
  366. pl.Interval = int.Parse(GridViewPollSetting.GetRowCellValue(e.RowHandle, "DPC_INTERVAL").ToString());
  367. pl.Enable = GridViewPollSetting.GetRowCellValue(e.RowHandle, "DPC_ENABLE").ToString() != "0";
  368. pl.Dh = new DataHelper();
  369. if (pl.Enable)
  370. pt.AddTask(RunTask, pl);
  371. PollSettingPaintRowIndex.Add(e.RowHandle);
  372. }
  373. }
  374. }
  375. /// <summary>
  376. /// 设置GridView多选放大镜
  377. /// </summary>
  378. /// <param name="sender"></param>
  379. /// <param name="e"></param>
  380. private void PagePollingSetting_VisibleChanged(object sender, EventArgs e)
  381. {
  382. if (PagePollingSetting.PageVisible)
  383. {
  384. DataTable dt = (DataTable)SystemInf.dh.ExecuteSql("SELECT DC_CODE,DC_NAME,DC_REMARK FROM DEVICECOMMAND", "select");
  385. PollSettingItemSearchLookUpEdit.DataSource = dt;
  386. PollSettingItemSearchLookUpEdit.DisplayMember = "DC_CODE";
  387. PollSettingItemSearchLookUpEdit.ValueMember = "DC_CODE";
  388. }
  389. }
  390. #endregion
  391. #region 设备查看界面
  392. private void PageDeviceStatus_VisibleChanged(object sender, EventArgs e)
  393. {
  394. if (PageDeviceStatus.PageVisible)
  395. {
  396. DeviceStatusQuerySQL.Clear();
  397. DeviceStatusQuerySQL.Append("select distinct de_code,de_name,dpc_status,dpc_id from device left join DEVICEPOLLINGCONFIG on dpc_decode=de_code order by de_code");
  398. PaintDeviceStatusControl(DeviceStatusQuerySQL.ToString());
  399. //填充筛选条件,车间和线体
  400. DataTable dt = (DataTable)dh.ExecuteSql("select wc_code,wc_name from workcenter", "select");
  401. BaseUtil.FillComBoxEditWidthDataTable(ComboxDeviceStatusWC, "wc_name", "wc_code", dt, true);
  402. }
  403. }
  404. private void CheckEditDeviceStatusEnable_CheckedChanged(object sender, EventArgs e)
  405. {
  406. DeviceStatusQuerySQL.Clear();
  407. if (CheckEditDeviceStatusEnable.Checked)
  408. {
  409. DeviceStatusQuerySQL.Append("select distinct de_code,de_name,dpc_status,dpc_id from device left join DEVICEPOLLINGCONFIG ");
  410. DeviceStatusQuerySQL.Append("on dpc_decode=de_code where nvl(dpc_enable,0)<>0 order by de_code");
  411. }
  412. else
  413. {
  414. DeviceStatusQuerySQL.Append("select distinct de_code,de_name,dpc_status,dpc_id from device left join DEVICEPOLLINGCONFIG ");
  415. DeviceStatusQuerySQL.Append("on dpc_decode=de_code order by de_code");
  416. }
  417. PaintDeviceStatusControl(DeviceStatusQuerySQL.ToString());
  418. }
  419. private void ComboxDeviceStatusWC_SelectedIndexChanged(object sender, EventArgs e)
  420. {
  421. DeviceStatusQuerySQL.Clear();
  422. string WC = BaseUtil.GetComboxEditValue(ComboxDeviceStatusWC);
  423. if (CheckEditDeviceStatusEnable.Checked)
  424. {
  425. DeviceStatusQuerySQL.Append("select distinct de_code,de_name,dpc_status,dpc_id from device left join DEVICEPOLLINGCONFIG ");
  426. DeviceStatusQuerySQL.Append("on dpc_decode=de_code where nvl(dpc_enable,0)<>0 and de_wccode='" + WC + "' order by de_code");
  427. }
  428. else
  429. {
  430. DeviceStatusQuerySQL.Append("select distinct de_code,de_name,dpc_status,dpc_id from device left join DEVICEPOLLINGCONFIG ");
  431. DeviceStatusQuerySQL.Append("on dpc_decode=de_code where de_wccode='" + WC + "' order by de_code");
  432. }
  433. PaintDeviceStatusControl(DeviceStatusQuerySQL.ToString());
  434. sql.Clear();
  435. if (WC == "全部")
  436. sql.Append("select li_code,li_name from line");
  437. else
  438. sql.Append("select li_code,li_name from line where li_wccode='" + WC + "'");
  439. DataTable dt = (DataTable)dh.ExecuteSql(sql.ToString(), "select");
  440. BaseUtil.FillComBoxEditWidthDataTable(ComboxDeviceStatusLC, "li_code", "li_name", dt, true);
  441. }
  442. private void ComboxDeviceStatusLC_SelectedIndexChanged(object sender, EventArgs e)
  443. {
  444. DeviceStatusQuerySQL.Clear();
  445. string WC = BaseUtil.GetComboxEditValue(ComboxDeviceStatusWC);
  446. string LC = BaseUtil.GetComboxEditValue(ComboxDeviceStatusLC);
  447. if (CheckEditDeviceStatusEnable.Checked)
  448. {
  449. DeviceStatusQuerySQL.Append("select distinct de_code,de_name,dpc_status,dpc_id from device left join ");
  450. DeviceStatusQuerySQL.Append("DEVICEPOLLINGCONFIG on dpc_decode=de_code where nvl(dpc_enable,0)<>0");
  451. if (WC != "全部")
  452. DeviceStatusQuerySQL.Append(" and de_wccode='" + WC + "' ");
  453. if (LC != "全部")
  454. DeviceStatusQuerySQL.Append(" and de_linecode='"+LC+"' ");
  455. DeviceStatusQuerySQL.Append(" order by de_code");
  456. }
  457. else
  458. {
  459. DeviceStatusQuerySQL.Append("select distinct de_code,de_name,dpc_status,dpc_id from device left join ");
  460. DeviceStatusQuerySQL.Append("DEVICEPOLLINGCONFIG on dpc_decode=de_code ");
  461. if (WC != "全部")
  462. DeviceStatusQuerySQL.Append(" where de_wccode='" + WC + "' ");
  463. if (LC != "全部")
  464. DeviceStatusQuerySQL.Append(" and de_linecode='" + LC + "' ");
  465. DeviceStatusQuerySQL.Append("order by de_code");
  466. }
  467. PaintDeviceStatusControl(DeviceStatusQuerySQL.ToString());
  468. }
  469. private void PaintDeviceStatusControl(string SQL)
  470. {
  471. DataTable dt = (DataTable)dh.ExecuteSql(SQL, "select");
  472. //界面重新展示时如过数量不一致则重新绘制
  473. if (PanelDeviceStatus.Controls.Count != dt.Rows.Count)
  474. {
  475. PanelDeviceStatus.Controls.Clear();
  476. int CountPerRow = PanelDeviceStatus.Width / 110;
  477. int RightPadding = PanelDeviceStatus.Width % 110;
  478. int Count = dt.Rows.Count;
  479. int RowCount = Count % CountPerRow == 0 ? Count / CountPerRow : Count / CountPerRow + 1;
  480. int LastRowCount = 0;
  481. for (int i = 0; i < PanelDeviceStatus.Controls.Count; i++)
  482. {
  483. PanelDeviceStatus.Controls[i].Dispose();
  484. }
  485. for (int j = 0; j < RowCount; j++)
  486. {
  487. //如果是最后一行则循环尾数
  488. if (j == RowCount - 1)
  489. {
  490. LastRowCount = Count % CountPerRow;
  491. }
  492. for (int i = 0; i < (j == RowCount - 1 ? LastRowCount : CountPerRow); i++)
  493. {
  494. PictureEditWithText pic = new PictureEditWithText();
  495. pic.DeviceName = dt.Rows[j * CountPerRow + i]["de_name"].ToString();
  496. pic.Picedit.ToolTipController = CommonTipController;
  497. pic.Name = dt.Rows[j * CountPerRow + i]["de_code"].ToString() + i + j;
  498. pic.Anchor = AnchorStyles.Left;
  499. pic.Anchor = AnchorStyles.Top;
  500. pic.Picedit.Name = dt.Rows[j * CountPerRow + i]["de_code"].ToString();
  501. pic.Picedit.MouseHover += Pic_MouseHover;
  502. pic.Picedit.Properties.SizeMode = PictureSizeMode.Squeeze;
  503. if (dt.Rows[j * CountPerRow + i]["dpc_status"].ToString() != "Running")
  504. {
  505. pic.Picedit.Image = Properties.Resources.network_offline;
  506. }
  507. else
  508. {
  509. pic.Picedit.Image = Properties.Resources.net_connected;
  510. }
  511. pic.Text = dt.Rows[j * CountPerRow + i]["de_code"].ToString();
  512. pic.Picedit.BorderStyle = BorderStyles.HotFlat;
  513. pic.Location = new Point((10 + RightPadding) / 2 + 110 * i, 10 + j * 150);
  514. PanelDeviceStatus.Controls.Add(pic);
  515. }
  516. }
  517. BaseUtil.CleanMemory();
  518. TimerDeviceStatus.Start();
  519. }
  520. }
  521. private void Pic_MouseHover(object sender, EventArgs e)
  522. {
  523. PictureEdit pic = sender as PictureEdit;
  524. DataTable dt = (DataTable)dh.ExecuteSql("select * from DEVICEPOLLINGLOG where dpg_id=(select max(dpg_id) from DEVICEPOLLINGLOG where dpg_decode='" + pic.Name + "')", "select");
  525. if (dt.Rows.Count > 0)
  526. {
  527. sql.Clear();
  528. sql.Append("开始时间" + dt.Rows[0]["dpg_starttime"].ToString() + "\n");
  529. sql.Append("发送数据大小" + dt.Rows[0]["dpg_senddatasize"].ToString() + "字节\n");
  530. sql.Append("接收数据大小" + dt.Rows[0]["dpg_receivedatasize"].ToString() + "字节\n");
  531. sql.Append("轮询次数" + dt.Rows[0]["dpg_count"].ToString() + "次\n");
  532. CommonTipController.ShowHint(sql.ToString(), pic.Name);
  533. }
  534. else
  535. {
  536. CommonTipController.ShowHint("暂无信息", pic.Name);
  537. }
  538. }
  539. //重新展示设备图像界面
  540. private void ShowDeviceStatus()
  541. {
  542. DataTable dt = (DataTable)dh.ExecuteSql(DeviceStatusQuerySQL.ToString(), "select");
  543. int CountPerRow = PanelDeviceStatus.Width / 110;
  544. int RightPadding = PanelDeviceStatus.Width % 110;
  545. int Count = dt.Rows.Count;
  546. int RowCount = Count % CountPerRow == 0 ? Count / CountPerRow : Count / CountPerRow + 1;
  547. int LastRowCount = 0;
  548. //只更换现有控件的图片,不必重新绘制控件
  549. for (int j = 0; j < RowCount; j++)
  550. {
  551. //如果是最后一行则循环尾数
  552. if (j == RowCount - 1)
  553. {
  554. LastRowCount = Count % CountPerRow;
  555. }
  556. for (int i = 0; i < (j == RowCount - 1 ? LastRowCount : CountPerRow); i++)
  557. {
  558. if (dt.Rows[j * CountPerRow + i]["dpc_status"].ToString() != "Running")
  559. {
  560. (PanelDeviceStatus.Controls[dt.Rows[j * CountPerRow + i]["de_code"].ToString() + i + j] as PictureEditWithText).Picedit.Image = Properties.Resources.network_offline;
  561. }
  562. else
  563. {
  564. (PanelDeviceStatus.Controls[dt.Rows[j * CountPerRow + i]["de_code"].ToString() + i + j] as PictureEditWithText).Picedit.Image = Properties.Resources.net_connected;
  565. }
  566. }
  567. }
  568. BaseUtil.CleanMemory();
  569. }
  570. //刷新界面
  571. private void TimerDeviceStatus_Tick(object sender, EventArgs e)
  572. {
  573. if (PageDeviceStatus.PageVisible)
  574. {
  575. ShowDeviceStatus();
  576. }
  577. }
  578. private void PanelDeviceStatus_SizeChanged(object sender, EventArgs e)
  579. {
  580. if (PageDeviceStatus.PageVisible)
  581. {
  582. ShowDeviceStatus();
  583. TimerDeviceStatus.Start();
  584. }
  585. }
  586. #endregion
  587. }
  588. }