Main.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. using System;
  2. using DevExpress.XtraBars;
  3. using UAS_DeviceMonitor.DataOperate;
  4. using System.Data;
  5. using System.Text;
  6. using UAS_DeviceMonitor.Entity;
  7. using UAS_DeviceMonitor.Device.Command;
  8. using UAS_DeviceMonitor.PublicMethod;
  9. using DevExpress.XtraGrid.Views.Grid;
  10. using System.Collections.Generic;
  11. using System.Drawing;
  12. namespace UAS_DeviceMonitor
  13. {
  14. public partial class Main : DevExpress.XtraBars.Ribbon.RibbonForm
  15. {
  16. StringBuilder sql = new StringBuilder();
  17. DataHelper dh;
  18. #region 初始化代码
  19. public Main()
  20. {
  21. dh = new DataHelper();
  22. SystemInf.dh = dh;
  23. InitializeComponent();
  24. CheckEditCommandSet.QueryCheckStateByValue += CheckedEdit_QueryCheckStateByValue;
  25. PollSettingItemSearchLookUpEdit.ParseEditValue += PollSettingItemSearchLookUpEdit_ParseEditValue;
  26. GridViewPollSetting.RowCellStyle += GridViewPollSetting_RowCellStyle;
  27. }
  28. private void PollSettingItemSearchLookUpEdit_ParseEditValue(object sender, DevExpress.XtraEditors.Controls.ConvertEditValueEventArgs e)
  29. {
  30. e.Handled = true;
  31. }
  32. private void Main_Load(object sender, EventArgs e)
  33. {
  34. //设备列表
  35. GridDeviceList.GetDataSQL = "select de_id,de_code,de_name,de_spec,de_indate,de_runstatus,de_address,de_wccode,de_vendcode,de_vendname from device".ToUpper();
  36. ToolPageControlDeviceList.Gridcontrol = GridDeviceList;
  37. //轮询配置界面
  38. GridPollingSetting.GetDataSQL = "SELECT '' POLLSETTINGSTATUSCOLUMN,0 CHECKEDCOLUMN,DPC_ID,DPC_DECODE,DPC_INTERVAL ,DPC_DCCODE ,DPC_FUNCTION , DPC_ENABLE,DPC_STATUS ,DPC_REMARK FROM DEVICEPOLLINGCONFIG";
  39. GridPollingSetting.ID = "DPC_ID";
  40. GridPollingSetting.TableName = "DEVICEPOLLINGCONFIG";
  41. GridPollingSetting.InsertSQL = "insert into DEVICEPOLLINGCONFIG(DPC_ID,DPC_DECODE ,DPC_INTERVAL ,DPC_DCCODE ,DPC_FUNCTION , DPC_ENABLE,DPC_STATUS ,DPC_REMARK) values(DEVICEPOLLINGCONFIG_seq.nextval,:DPC_DECODE ,:DPC_INTERVAL ,:DPC_DCCODE ,:DPC_FUNCTION , :DPC_ENABLE,:DPC_STATUS ,:DPC_REMARK)";
  42. ButtonSaveCommandSet.Grid = GridPollingSetting;
  43. ButtonNewCommandSet.Grid = GridPollingSetting;
  44. ButtonDeleteCommandSet.Grid = GridPollingSetting;
  45. //指令设置界面
  46. GridCommandSetting.GetDataSQL = "select 0 CHECKEDCOLUMN,dc_id,dc_code,dc_name,dc_value from devicecommand".ToUpper();
  47. GridCommandSetting.TableName = "devicecommand";
  48. GridCommandSetting.ID = "dc_id";
  49. GridCommandSetting.InsertSQL = "insert into devicecommand(dc_id,dc_code,dc_name,dc_value) values(devicecommand_seq.nextval,:dc_code,:dc_name,:dc_value)";
  50. ButtonSaveCommand.Grid = GridCommandSetting;
  51. ButtonDeleteCommand.Grid = GridCommandSetting;
  52. //设备联网配置界面
  53. 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";
  54. GridDeviceNetSetting.ID = "dnc_id";
  55. GridDeviceNetSetting.TableName = "DEVICENETCONFIG";
  56. 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)";
  57. ButtonDeleteNetConfig.Grid = GridDeviceNetSetting;
  58. ButtonAddNetConfig.Grid = GridDeviceNetSetting;
  59. ButtonSaveNetConfig.Grid = GridDeviceNetSetting;
  60. Ptime = new Dictionary<int, PollingTimer>();
  61. }
  62. #endregion
  63. #region 界面通用事件
  64. /// <summary>
  65. /// 选项卡切换不同XPage
  66. /// </summary>
  67. /// <param name="sender"></param>
  68. /// <param name="e"></param>
  69. private void ButtonItem_ItemClick(object sender, ItemClickEventArgs e)
  70. {
  71. HideXPage(e.Item.Tag.ToString());
  72. }
  73. /// <summary>
  74. /// 根据点击按钮的Tag显示对应的XPage
  75. /// </summary>
  76. /// <param name="PageName"></param>
  77. private void HideXPage(string PageName)
  78. {
  79. for (int i = 0; i < MainTabControl.TabPages.Count; i++)
  80. {
  81. if (MainTabControl.TabPages[i].Name == PageName)
  82. MainTabControl.TabPages[i].PageVisible = true;
  83. else
  84. MainTabControl.TabPages[i].PageVisible = false;
  85. }
  86. }
  87. /// <summary>
  88. /// 勾选Grid的CheckBox
  89. /// </summary>
  90. /// <param name="sender"></param>
  91. /// <param name="e"></param>
  92. private void CheckedEdit_QueryCheckStateByValue(object sender, DevExpress.XtraEditors.Controls.QueryCheckStateByValueEventArgs e)
  93. {
  94. string val = "";
  95. if (e.Value != null)
  96. {
  97. val = e.Value.ToString();
  98. }
  99. else
  100. {
  101. val = "FALSE";//默认为不选
  102. }
  103. switch (val.ToUpper())
  104. {
  105. case "TRUE":
  106. case "YES":
  107. case "1":
  108. e.CheckState = System.Windows.Forms.CheckState.Checked;
  109. break;
  110. case "FALSE":
  111. case "NO":
  112. case "0":
  113. e.CheckState = System.Windows.Forms.CheckState.Unchecked;
  114. break;
  115. default:
  116. e.CheckState = System.Windows.Forms.CheckState.Checked;
  117. break;
  118. }
  119. e.Handled = true;
  120. }
  121. #endregion
  122. #region PageCommandSet业务代码(指令设置)
  123. /// <summary>
  124. /// 新增指令弹窗
  125. /// </summary>
  126. /// <param name="sender"></param>
  127. /// <param name="e"></param>
  128. private void ButtonNewCommand_Click(object sender, EventArgs e)
  129. {
  130. FormNewCommand fncmd = new FormNewCommand(BaseUtil.GetComboxEditValue(Brand), Brand.Text);
  131. fncmd.ShowDialog();
  132. GridCommandSetting.RefreshData();
  133. }
  134. /// <summary>
  135. /// 判断指令编号不能重复
  136. /// </summary>
  137. /// <param name="sender"></param>
  138. /// <param name="e"></param>
  139. private void ButtonSaveCommand_Click(object sender, EventArgs e)
  140. {
  141. ButtonSaveCommand.DoSave();
  142. }
  143. /// <summary>
  144. /// 加载ComBox品牌数据
  145. /// </summary>
  146. /// <param name="sender"></param>
  147. /// <param name="e"></param>
  148. private void PageCommandSet_VisibleChanged(object sender, EventArgs e)
  149. {
  150. if (PageCommandSet.PageVisible)
  151. {
  152. DataTable dt = (DataTable)SystemInf.dh.ExecuteSql("select * from devicebrand", "select");
  153. BaseUtil.FillComBoxEditWidthDataTable(Brand, "db_name", "db_code", dt);
  154. GridCommandSetting.Condition = " where dc_debrand='" + BaseUtil.GetComboxEditValue(Brand) + "'";
  155. }
  156. }
  157. private void Brand_SelectedIndexChanged(object sender, EventArgs e)
  158. {
  159. GridCommandSetting.Condition = " where dc_debrand='" + BaseUtil.GetComboxEditValue(Brand) + "'";
  160. GridCommandSetting.RefreshData();
  161. }
  162. #endregion
  163. #region PagePolling业务代码(轮询业务定义)
  164. private void GridPolling_VisibleChanged(object sender, EventArgs e)
  165. {
  166. DataTable dt = (DataTable)SystemInf.dh.ExecuteSql("select dc_name,dc_code from devicecommand ", "select");
  167. PollingSetItemLookUpEdit.DataSource = dt;
  168. PollingSetItemLookUpEdit.DisplayMember = "DC_NAME";
  169. PollingSetItemLookUpEdit.ValueMember = "DC_CODE";
  170. }
  171. #endregion
  172. #region PagePollingSetting业务代码(轮询配置)
  173. PollingTask pt = new PollingTask();
  174. /// <summary>
  175. /// 绘制轮询配置状态栏
  176. /// </summary>
  177. List<int> PollSettingPaintRowIndex = new List<int>();
  178. private void ButtonStartPolling_Click(object sender, EventArgs e)
  179. {
  180. GridView grid = GridViewPollSetting;
  181. for (int i = 0; i < GridPollingSetting.RowCount; i++)
  182. {
  183. Polling pl = new Polling();
  184. pl.Id = int.Parse(GridViewPollSetting.GetRowCellValue(i, "DPC_ID").ToString());
  185. pl.DeviceCode = GridViewPollSetting.GetRowCellValue(i, "DPC_DECODE").ToString();
  186. pl.Interval = int.Parse(GridViewPollSetting.GetRowCellValue(i, "DPC_INTERVAL").ToString());
  187. pl.Enable = GridViewPollSetting.GetRowCellValue(i, "DPC_ENABLE").ToString() != "0";
  188. pl.Dh = new DataHelper();
  189. if (pl.Enable)
  190. {
  191. pt.AddTask(RunTask, pl);
  192. //添加到状态为运行的行
  193. PollSettingPaintRowIndex.Add(i);
  194. GridPollingSetting.Focus();
  195. }
  196. }
  197. }
  198. /// <summary>
  199. /// 绘制轮询状态
  200. /// </summary>
  201. /// <param name="sender"></param>
  202. /// <param name="e"></param>
  203. private void GridViewPollSetting_RowCellStyle(object sender, RowCellStyleEventArgs e)
  204. {
  205. if (e.Column.Name.ToUpper() == "POLLSETTINGSTATUSCOLUMN" && PollSettingPaintRowIndex.Contains(e.RowHandle))
  206. {
  207. e.Appearance.BackColor = Color.Green;
  208. }
  209. }
  210. Dictionary<int, PollingTimer> Ptime;
  211. private void RunTask(object i)
  212. {
  213. PollingTimer timer = new PollingTimer();
  214. Polling pl = (Polling)i;
  215. timer.Polling = i;
  216. timer.Interval = pl.Interval * 5000;
  217. timer.Elapsed += Timer_Tick;
  218. timer.Start();
  219. try
  220. {
  221. if (!Ptime.ContainsKey(pl.Id))
  222. Ptime.Add(pl.Id, timer);
  223. }
  224. catch (Exception)
  225. {
  226. if (!Ptime.ContainsKey(pl.Id))
  227. Ptime.Add(pl.Id, timer);
  228. }
  229. }
  230. /// <summary>
  231. /// 轮询执行的业务
  232. /// </summary>
  233. /// <param name="sender"></param>
  234. /// <param name="e"></param>
  235. private void Timer_Tick(object sender, EventArgs e)
  236. {
  237. PollingTimer timer = (PollingTimer)sender;
  238. Polling pl = (Polling)timer.Polling;
  239. string Decode = pl.DeviceCode;
  240. DataHelper dh = pl.Dh;
  241. DataTable dt = (DataTable)dh.ExecuteSql("select dnc_ip from DEVICENETCONFIG where dnc_decode='" + Decode + "'", "select");
  242. Console.WriteLine(dt.Rows.Count);
  243. }
  244. /// <summary>
  245. /// 停止全部轮询
  246. /// </summary>
  247. /// <param name="sender"></param>
  248. /// <param name="e"></param>
  249. private void ButtonPausePolling_Click(object sender, EventArgs e)
  250. {
  251. foreach (var item in Ptime)
  252. {
  253. item.Value.Stop();
  254. }
  255. PollSettingPaintRowIndex.Clear();
  256. GridPollingSetting.Focus();
  257. Ptime.Clear();
  258. }
  259. private void GridViewPollSetting_CellValueChanging(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
  260. {
  261. GridViewPollSetting.SetRowCellValue(e.RowHandle, e.Column, e.Value);
  262. if (e.Column.Name.ToUpper() == "DPC_ENABLE")
  263. {
  264. if (GridViewPollSetting.GetRowCellValue(e.RowHandle, "DPC_ENABLE").ToString() == "0")
  265. {
  266. int id = int.Parse(GridViewPollSetting.GetRowCellValue(e.RowHandle, "DPC_ID").ToString());
  267. if (Ptime.ContainsKey(id))
  268. {
  269. Ptime[id].Stop();
  270. Ptime.Remove(id);
  271. PollSettingPaintRowIndex.Remove(e.RowHandle);
  272. }
  273. }
  274. else
  275. {
  276. Polling pl = new Polling();
  277. pl.Id = int.Parse(GridViewPollSetting.GetRowCellValue(e.RowHandle, "DPC_ID").ToString());
  278. pl.DeviceCode = GridViewPollSetting.GetRowCellValue(e.RowHandle, "DPC_DECODE").ToString();
  279. pl.Interval = int.Parse(GridViewPollSetting.GetRowCellValue(e.RowHandle, "DPC_INTERVAL").ToString());
  280. pl.Enable = GridViewPollSetting.GetRowCellValue(e.RowHandle, "DPC_ENABLE").ToString() != "0";
  281. if (pl.Enable)
  282. pt.AddTask(RunTask, pl);
  283. PollSettingPaintRowIndex.Add(e.RowHandle);
  284. }
  285. }
  286. }
  287. #endregion
  288. }
  289. }