Main.cs 31 KB

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