123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Xml;
- using Aitex.Core.RT.IOCore;
- using Aitex.Core.RT.SCCore;
- using Aitex.Core.Util;
- using Aitex.Core.RT.Log;
- namespace Aitex.Core.RT.Device
- {
- public interface IDevice
- {
- string Module { get; set; }
- string Name { get; set; }
- bool Initialize();
- void Monitor();
- void Terminate();
- void Reset();
- }
- public interface IModuleDevice
- {
- bool IsReady { get; }
- bool IsError { get; }
- bool IsInit { get; }
- bool Home(out string reason);
- }
- public class BaseDevice
- {
- public string UniqueName { get; set; }
- public string Module { get; set; }
- public string Name { get; set; }
- public string Display { get; set; }
- public string DeviceID { get; set; }
-
- private DeviceTimer counter = new DeviceTimer();
- public string ScBasePath { get; set; }
- public string IoBasePath { get; set; }
- public BaseDevice()
- {
- ScBasePath = "System";
- IoBasePath = "System";
- }
- public BaseDevice(string module, string name, string display, string id):this()
- {
- display = string.IsNullOrEmpty(display) ? name : display;
- id = string.IsNullOrEmpty(id) ? name : id;
- Module = module;
- Name = name;
- Display = display;
- DeviceID = id;
- UniqueName = $"{module}.{name}";
- }
- public DOAccessor ParseDoNode(string name, XmlElement node, string ioModule = "")
- {
- if (!string.IsNullOrEmpty(node.GetAttribute(name).Trim()))
- return IO.DO[string.IsNullOrEmpty(ioModule) ? node.GetAttribute(name).Trim() : $"{ioModule}.{node.GetAttribute(name).Trim()}"];
- //LOG.Write(string.Format("{0},未定义{1}.{2}.{3}.{4}", node.InnerXml, Module, Display, Name, name));
- return null;
- }
- public DIAccessor ParseDiNode(string name, XmlElement node, string ioModule = "")
- {
- if (!string.IsNullOrEmpty(node.GetAttribute(name).Trim()))
- return IO.DI[string.IsNullOrEmpty(ioModule) ? node.GetAttribute(name).Trim() : $"{ioModule}.{node.GetAttribute(name).Trim()}"];
- //LOG.Write(string.Format("{0},未定义{1}.{2}.{3}.{4}", node.InnerXml, Module, Display, Name, name));
- return null;
- }
- public AOAccessor ParseAoNode(string name, XmlElement node, string ioModule = "")
- {
- if (!string.IsNullOrEmpty(node.GetAttribute(name).Trim()))
- return IO.AO[string.IsNullOrEmpty(ioModule) ? node.GetAttribute(name).Trim() : $"{ioModule}.{node.GetAttribute(name).Trim()}"];
- //LOG.Write(string.Format("{0},未定义{1}.{2}.{3}.{4}", node.InnerXml, Module, Display, Name, name));
- return null;
- }
- public AIAccessor ParseAiNode(string name, XmlElement node, string ioModule = "")
- {
- if (!string.IsNullOrEmpty(node.GetAttribute(name).Trim()))
- return IO.AI[string.IsNullOrEmpty(ioModule) ? node.GetAttribute(name).Trim() : $"{ioModule}.{node.GetAttribute(name).Trim()}"];
- //LOG.Write(string.Format("{0},未定义{1}.{2}.{3}.{4}", node.InnerXml, Module, Display, Name, name));
- return null;
- }
- public SCConfigItem ParseScNode(string name, XmlElement node, string ioModule="", string defaultScPath="")
- {
- SCConfigItem result = null;
- if (!string.IsNullOrEmpty(node.GetAttribute(name).Trim()))
- result = SC.GetConfigItem(node.GetAttribute(name));
- if (result == null && !string.IsNullOrEmpty(defaultScPath) && SC.ContainsItem(defaultScPath))
- result = SC.GetConfigItem(defaultScPath);
- return result;
- }
- public SCConfigItem ParseScNodeEx(string name, XmlElement node, string ioModule = "")
- {
- if (!string.IsNullOrEmpty(node.GetAttribute(name).Trim()))
- {
- string aName = node.GetAttribute(name);
- if (string.IsNullOrWhiteSpace(ioModule))
- {
- return SC.GetConfigItem(aName);
- }
- else
- {
- return SC.GetConfigItem($"{ioModule}.{aName}");
- }
- }
- //LOG.Write(string.Format("{0},未定义{1}.{2}.{3}.{4}", node.InnerXml, Module, Display, Name, name));
- return null;
- }
- public static T ParseDeviceNode<T>(string name, XmlElement node) where T : class, IDevice
- {
- if (!string.IsNullOrEmpty(node.GetAttribute(name).Trim()))
- return DEVICE.GetDevice<T>(node.GetAttribute(name));
- //LOG.Write(string.Format("{0},未定义{1}", node.InnerXml, name));
- return null;
- }
- public static T ParseDeviceNode<T>(string module, string name, XmlElement node) where T : class, IDevice
- {
- string device_id = node.GetAttribute(name);
- if (!string.IsNullOrEmpty(device_id) && !string.IsNullOrEmpty(device_id.Trim()))
- {
- return DEVICE.GetDevice<T>($"{module}.{device_id}");
- }
- //LOG.Write(string.Format("{0},未定义{1}", node.InnerXml, name));
- return null;
- }
- protected void Counter()
- {
- if (counter.IsIdle())
- {
- counter.Start(10000);
- }
- else
- {
- //LOG.Info(String.Format("{0} Monitor elapsed {1}", Name, counter.GetElapseTime()));
- counter.Start(10000);
- }
- }
- protected float _GetRealFloat(IOAccessor<short> accessor)
- {
- byte[] low = BitConverter.GetBytes(accessor.Buffer[accessor.Index]);
- byte[] high = BitConverter.GetBytes(accessor.Buffer[accessor.Index + 1]);
- float res = BitConverter.ToSingle(new[] { low[0], low[1], high[0], high[1] }, 0);
- return res;
- }
- protected void _SetRealFloat(IOAccessor<short> accessor, float val)
- {
- //val = val / 50 * 4000;
- byte[] val2 = BitConverter.GetBytes(val);
- accessor.Buffer[accessor.Index] = BitConverter.ToInt16(val2, 0);
- accessor.Buffer[accessor.Index + 1] = BitConverter.ToInt16(val2, 2);
- }
- protected float GetAiValue(string name)
- {
- byte[] high = BitConverter.GetBytes(IO.AI[name].Buffer[IO.AI[name].Index]);
- byte[] low = BitConverter.GetBytes(IO.AI[name].Buffer[IO.AI[name].Index + 1]);
- float flow = BitConverter.ToSingle(new[] { high[0], high[1], low[0], low[1] }, 0);
- return flow;
- }
- protected float GetAoValue(string name)
- {
- byte[] high = BitConverter.GetBytes(IO.AO[name].Buffer[IO.AO[name].Index]);
- byte[] low = BitConverter.GetBytes(IO.AO[name].Buffer[IO.AO[name].Index + 1]);
- float flow = BitConverter.ToSingle(new[] { high[0], high[1], low[0], low[1] }, 0);
- return flow;
- }
- }
- }
|