Main.cs 35 KB

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