using Aurora.Api; using Aurora.Api.Devices; using AuroraHeightSensor.Keyence; using AuroraHeightSensor.MicroEpsilon; using Hermes; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Input; using static AuroraHeightSensor.HeightSensor; namespace AuroraHeightSensor { public enum eHeightSensor { KeyenceLKG85, KeyenceCL3000, KeyenceIXH, KeyenceIL, MicroEpsilon } public enum eChannel { CH0 = 0, CH1 = 1, CH2 = 2, CH3 = 3, } public interface IHeightSensor { int Measure(eChannel channel, out double value); int MeasureAll(out List value); int SetZero(eChannel channel, bool isSet = true); int SetZeroShift(eChannel channel); int SetZeroShiftReset(eChannel channel); int Reset(eChannel channel); //int Connect(IConnConfig config); //int Disconnect(); bool CheckConnected(); int SetLed(bool isOn); int Activate(IConnConfig Config); int Deactivate(); int StartStopAutoTransmit(bool isOn, ICommand callback); } public class HeightSensor : Device { Logger Log = new Logger("HeightSensor"); private IHeightSensor _HeightSensor; public double Height { get; private set; } private double _MinValue; private double _MaxValue; protected bool _isSimulBySetting; private eHeightSensor _SensorType; public HeightSensor(string deviceId, eHeightSensor sensor, bool isSimul) { _SensorType = sensor; _isSimulBySetting = isSimul; } protected override int DoActivate(IDeviceConfig config) { var heightSensorConfig = config as HeightSensorConfig; IsSimul = heightSensorConfig.IsDeviceSimulationOn || _isSimulBySetting; if (IsSimul) { _HeightSensor = new HeightSensorMock(); } else { switch (_SensorType) { case eHeightSensor.KeyenceLKG85: _HeightSensor = new HeightSensorLKG85(DeviceId); break; case eHeightSensor.MicroEpsilon: _HeightSensor = new HeightSensorME(); break; case eHeightSensor.KeyenceCL3000: _HeightSensor = new HeightSensorCL3000(DeviceId); break; case eHeightSensor.KeyenceIXH: _HeightSensor = new HeightSensorIXH(DeviceId, IsSimul); break; case eHeightSensor.KeyenceIL: _HeightSensor = new HeightSensorIL(DeviceId, IsSimul); break; default: return E1.RESOURCE; } } //var res = _HeightSensor?.Connect(heightSensorConfig.ConnConfig) ?? E1.GENERIC; var res = _HeightSensor?.Activate(heightSensorConfig.ConnConfig) ?? E1.GENERIC; if (res != E1.OK) { return res; } _MinValue = heightSensorConfig.Limit.Min; _MaxValue = heightSensorConfig.Limit.Max; if (_HeightSensor is HeightSensorMock) { (_HeightSensor as HeightSensorMock).SetRange(heightSensorConfig.Simulation.Min, heightSensorConfig.Simulation.Max); } return E1.OK; } protected override int DoDeactivate() { //return _HeightSensor.Disconnect(); return _HeightSensor.Deactivate(); } public bool IsConnected() { if (!IsActivated) { return false; } IsCommError = !_HeightSensor.CheckConnected(); return IsCommError == false; } // 현재까진 Channel 0만 쓰는 것으로 보인다. 나중에 필요시 두채널을 고려하자. public int Measure(out double value) { value = 0; if (!IsActivated) { return E2.NOT_CONNECTED; } if (_HeightSensor.Measure(eChannel.CH0, out value) == E1.OK) { Height = value; } if (value < _MinValue || value > _MaxValue) { Log.E($"Over meaure range : {value}"); return E2.UNAVAILABLE; } return E1.OK; } public int MeasureAll(out List Sensorvalue) { if(IsSimul) { Sensorvalue = new List(); var rand = new Random(); Sensorvalue.Add(rand.NextDouble()); Sensorvalue.Add(rand.NextDouble()); Sensorvalue.Add(rand.NextDouble()); Sensorvalue.Add(rand.NextDouble()); return E1.OK; } Sensorvalue = null; if (!IsActivated) { return E2.NOT_CONNECTED; } if (_HeightSensor.MeasureAll(out Sensorvalue) == E1.OK) { //Height = value; } if (Sensorvalue is null){ Log.E("Sensor not measure"); return E2.ABORTED; } //if (value < _MinValue || value > _MaxValue) //{ // Log.E($"Over meaure range : {value}"); // return E2.UNAVAILABLE; //} return E1.OK; } public void Update() { if (!IsConnected()) { return; } if (_HeightSensor.Measure(eChannel.CH0, out double value) == E1.OK) { Height = value; } } public int SetZeroShift(eChannel ch) { if (_HeightSensor.SetZeroShift(ch) == E1.OK) { return E1.OK; } else { return E2.UNKNOWN; } } public int SetZeroShiftReset(eChannel ch) { if (_HeightSensor.SetZeroShiftReset(ch) == E1.OK){ return E1.OK; } else{ return E2.UNKNOWN; } } public int Reset() { _HeightSensor.Reset(eChannel.CH0); Thread.Sleep(100); _HeightSensor.Reset(eChannel.CH1); Thread.Sleep(100); _HeightSensor.Reset(eChannel.CH2); Thread.Sleep(100); _HeightSensor.Reset(eChannel.CH3); return E1.OK; } public int SetLed(bool isOn) { if (!IsActivated) { return E2.NOT_CONNECTED; } return _HeightSensor.SetLed(isOn); } public int StartStopAutoTransmit(bool isOn, ICommand callback) { if (!IsActivated) { return E2.NOT_CONNECTED; } return _HeightSensor.StartStopAutoTransmit(isOn, callback); } } }