Main.cs 41 KB

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