Main.cs 30 KB

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