123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Drawing;
- using System.Data;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using Aitex.Core.RT.IOCore;
- using Aitex.Core.Util;
- namespace Aitex.Core.Backend
- {
- public partial class DI : UserControl, IIOView
- {
- PeriodicJob _thread;
- public DI()
- {
- InitializeComponent();
- this.Load += new EventHandler(DI_Load);
-
- }
- void DI_Load(object sender, EventArgs e)
- {
- if (this.panel1.Controls.Count > 0)
- return;
- BuildIOTable();
- _thread = new PeriodicJob(500, OnTimer, "DITimer", false, true);
- VisibleChanged += (sender1, e1) =>
- {
- if (Visible) _thread.Start(); else _thread.Pause();
- };
- }
- private void BuildIOTable()
- {
- List<Tuple<int, int, string>> ioList = IO.GetIONameList(IOType.DI);
- int index = 0;
- foreach (var item in ioList)
- {
- DICtrl diCtrl1 = new DICtrl
- {
- Location = new System.Drawing.Point(0, index * 30),
- LabelName = $"DI-{item.Item1}.{item.Item3}",
- IOName = item.Item3,
- Name = $"DI_{item.Item1}",
- Size = new System.Drawing.Size(350, 25),
- Tag = item.Item3
- };
- this.panel1.Controls.Add(diCtrl1);
- index++;
- }
- List<Tuple<int, int, string>> ioListRemote = IO.GetIONameList("remote", IOType.DI);
- index = 0;
- foreach (var item in ioListRemote)
- {
- DICtrl diCtrl1 = new DICtrl
- {
- Location = new System.Drawing.Point(355, index * 30),
- LabelName = $"DI-{item.Item1}.{item.Item3}",
- IOName = item.Item3,
- Name = $"DI_{item.Item1}",
- Size = new System.Drawing.Size(350, 25),
- Tag = item.Item3
- };
- this.panel1.Controls.Add(diCtrl1);
- index++;
- }
- // OLD
- /*
- List<Tuple<int, int, string>> ioList = IO.GetIONameList(IOType.DI);
- List<Tuple<int, int, string>> ioListRemote = IO.GetIONameList("remote", IOType.DI);
- foreach (var tuple in ioListRemote)
- {
- ioList.Add(tuple);
- }
- int totalColumn = 3;
- int countPerColumn = (ioList.Count / totalColumn) + ((ioList.Count % totalColumn) > 0 ? 1 : 0);
- int index = 0;
- foreach (var item in ioList)
- {
- int row = index % countPerColumn;
- int col = index / countPerColumn;
- DICtrl diCtrl1 = new DICtrl();
- diCtrl1.Location = new System.Drawing.Point(col * 300, row * 25);
- diCtrl1.SetName(string.Format("DI-{0}.{1}", item.Item1, item.Item3));
- diCtrl1.SetIoName("local", item.Item3);
- diCtrl1.Name = string.Format("DI_{0}", item.Item1);
- diCtrl1.Size = new System.Drawing.Size(300, 25);
- diCtrl1.Tag = item.Item3;
- diCtrl1.SetSimulatorValue += (s, b) =>
- {
- if (UserLoginView.Simulator != null)
- UserLoginView.Simulator.SetDi("local", s, b ? (byte)1 : (byte)0);
- };
- this.panel1.Controls.Add(diCtrl1);
- index++;
- }
- */
- }
- bool OnTimer()
- {
- Invoke(new Action(() =>
- {
- foreach (DICtrl ctrl in panel1.Controls)
- {
- string name = (string)ctrl.Tag;
- if (ctrl != null)
- {
- ctrl.SetValue(IO.DI[name].Value, IO.DI[name].RawData);
- }
- }
- }));
- return true;
- }
- public void EnableTimer(bool enable)
- {
- if (enable)
- _thread.Start();
- else
- _thread.Pause();
- }
- public void Close()
- {
- if (_thread != null)
- _thread.Stop();
- }
- }
- }
|