Main.cs 36 KB

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