using Dias_Interface;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Dias_Farm
{
public partial class UC_Device_Data : UserControl
{
Dias_Interface.DeviceInfo DEV = null;
Timer t = new Timer() { Interval = 1000,Enabled=false };
public Dias_Interface.DeviceInfo Device
{
get { return DEV; }
set
{
if (value != null && value != DEV) DEV = value;
}
}
public bool IsRuning
{
get
{
return t.Enabled;
}
set
{
t.Enabled = value;
}
}
public UC_Device_Data()
{
InitializeComponent();
this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.UserPaint | ControlStyles.ResizeRedraw, true);
this.Paint += UC_Device_Data_Paint;
t.Tick += (sender, e) =>
{
this.Refresh();
};
this.Disposed += (sender, e) =>
{
t.Enabled = false;
t.Dispose();
GC.Collect();
};
this.MouseClick += (sender, e) =>
{
OnDeviceClick?.Invoke(this, new Point(e.X, e.Y), 0);
};
this.MouseDoubleClick += (sender, e) =>
{
OnDeviceClick?.Invoke(this, new Point(e.X, e.Y), 1);
};
}
public event Action<UC_Device_Data,Point, int> OnDeviceClick;
private void UC_Device_Data_Paint(object sender, PaintEventArgs e)
{
if (DEV == null)
{
e.Graphics.DrawString($"农场设备", this.Font, new SolidBrush(this.ForeColor),
new Rectangle(Point.Empty, this.ClientSize),
new StringFormat() { LineAlignment = StringAlignment.Center, Alignment = StringAlignment.Center });
return;
}
Graphics g=e.Graphics;
switch (DEV.Type)
{
case Dias_Interface.DeviceType.Data:
{
DrawDataControls(g,DEV);
}
break;
case DeviceType.Control:
{
DrawControlControls(g,DEV);
}
break;
case DeviceType.Monitor:
{
DrawMonitorControls(g,DEV);
}
break;
default:
{
break;
}
}
}
private void DrawMonitorControls(Graphics g, IDEVICE e)
{
Image img = null;
try
{
img = Image.FromStream(new MemoryStream(e.ByteValue));
}
catch { img = null; }
if (img != null)
{
g.DrawImage(img, new Rectangle(Point.Empty, this.ClientSize),new Rectangle(Point.Empty,img.Size),GraphicsUnit.Pixel);
img.Dispose();
img = null;
}
}
private void DrawControlControls(Graphics g, IDEVICE e)
{
g.DrawString($"{e.Name}【{(e.Value>=1?"开":"关")}】", this.Font, new SolidBrush(this.ForeColor), new Rectangle(Point.Empty, this.ClientSize),
new StringFormat() { LineAlignment = StringAlignment.Center, Alignment = StringAlignment.Center });
}
private void DrawDataControls(Graphics g, IDEVICE e)
{
g.DrawString($"{e.Name}【{e.Value.ToString("0.00")}】{e.Set.Get<string>("Unit")}", this.Font, new SolidBrush(this.ForeColor), new Rectangle(Point.Empty, this.ClientSize),
new StringFormat() { LineAlignment = StringAlignment.Center, Alignment = StringAlignment.Center });
}
}
}