| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863 |
- using aflex_CSharp_Integration_Sample;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Runtime.CompilerServices;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using Team.FFFeederService.Enums;
- using Team.FFFeederService.Interfaces;
- using Team.FFFeederService.Models;
- using TouchSocket.Core;
- using TouchSocket.Sockets;
- namespace Team.FFFeederService
- {
- public struct FeederParam
- {
- public double backLightIntensity;//光源强度
- public byte configNum;//震动模式
- public double degreeNum;//震动角度
- public double vibrationIntensity;//震动强度
- public double intervalNum;//间隔时间ms
- }
- public class AfagFeeder : IVoiceCoilMotorFeeder
- {
- public TcpClient TcpClient { get; }
- public int FeederPort { get; private set; }
- public string FeederIp { get; private set; }
- public FeederBrand FeederBrand { get => FeederBrand.AFAG; }
- public FeederModel FeederModel { get; private set; } = FeederModel.Unknown;
- public Guid Id { get; set; }
- public int FeederNo { get; set; }
- public string Name { get; set; }
- public bool IsConnected => IsInit;
- public bool IsInit { get; set; }
- /// <summary>
- /// Feeder连接状态更改事件
- /// </summary>
- public event Action<object, bool> FeederConnectionStatusChanged;
- private aflexCommunicationClass objaflex;
- Timer feedStepTimer;
- uint stepNum = 0;
- TimeSpan durationNum = new TimeSpan();
- /// <summary>
- /// 配置编号
- /// </summary>
- public int ConfigIndex
- {
- get
- {
- if (objaflex == null) return 0;
- else return objaflex.actual_ConfigNumber;
- }
- }
- /// <summary>
- /// 振动强度
- /// </summary>
- public double Intensity
- {
- get
- {
- if (objaflex == null) return 0;
- else return objaflex.actual_intensity;
- }
- }
- /// <summary>
- /// 方向
- /// </summary>
- public double Direction
- {
- get
- {
- if (objaflex == null) return 0;
- else return objaflex.actual_direction;
- }
- }
- public AfagFeeder(Guid id, int feederNo, string name, string ip, int port)
- {
- Id = id;
- FeederNo = feederNo;
- Name= name;
- FeederIp = ip;
- FeederPort = port;
- objaflex = new aflexCommunicationClass();
- objaflex.aflexValueChanged += Objaflex_aflexValueChanged;
- }
- private void Objaflex_aflexValueChanged(object sender, EventArgs e)
- {
- }
- public void CloseLight()
- {
- try
- {
- if (!objaflex.actual_AutoMode) return;
- objaflex.SetAuxPowerOut(0);
- }
- catch (Exception)
- {
- }
- }
- public Task CloseLightAsync()
- {
- try
- {
- if (!objaflex.actual_AutoMode) return Task.CompletedTask;
- objaflex.SetAuxPowerOut(0);
- }
- catch (Exception)
- {
- }
- return Task.CompletedTask;
- }
- public void Connect()
- {
- if (!IsInit)
- { //Connect
- //Check IP Textbox
- IPAddress IPValue;
- if (IPAddress.TryParse(FeederIp, out IPValue))
- {
- if (/*IPValue.GetAddressBytes()[3] != 0 && */IPValue.GetAddressBytes()[3] != 255)
- {
- //Valid IP
- //Check Port
- int PortValue = FeederPort;
- if (PortValue > 0)
- {
- //Valid Port
- //Connect
- bool flog = objaflex.Connect(IPValue, PortValue);
- if (flog)
- {
- FeederConnectionStatusChanged?.Invoke(this,true);
- GeneralRelease();
- }
- else
- {
- FeederConnectionStatusChanged?.Invoke(this,false);
- }
- IsInit = true;
- }
- }
- }
- }
- //else
- //{ //Disconnect
- // objaflex.Disconnect();
- //}
- }
- public async Task ConnectAsync()
- {
- await Task.Run(async () =>
- {
- //Connect
- //Check IP Textbox
- IPAddress IPValue;
- bool resB = IPAddress.TryParse(FeederIp, out IPValue);
- int PortValue = FeederPort;
- try
- {
- if (resB && IPValue.GetAddressBytes()[3] != 255)
- {
- if (PortValue > 0)
- {
- bool flog = objaflex.Connect(IPValue, PortValue);
- if (flog)
- {
- FeederConnectionStatusChanged?.Invoke(this,true);
- GeneralRelease();
- }
- else
- {
- FeederConnectionStatusChanged?.Invoke(this,false);
- }
- IsInit = true;
- }
- }
- }
- catch (Exception)
- {
- IsInit = false;
- while (!IsInit)
- {
- try
- {
- bool flog = objaflex.Connect(IPValue, PortValue);
- if (flog)
- {
- FeederConnectionStatusChanged?.Invoke(this,true);
- GeneralRelease();
- }
- else
- {
- FeederConnectionStatusChanged?.Invoke(this,false);
- }
- IsInit = true;
- }
- catch (Exception)
- {
- await Task.Delay(1000);
- }
- }
- }
- });
- }
- /// <summary>
- /// set release
- /// </summary>
- public void GeneralRelease()
- {
- if (!objaflex.actual_GeneralRelease)
- {
- objaflex.SetGeneralRelease(!objaflex.actual_GeneralRelease);
- }
- }
- /// <summary>
- /// 设置光源亮度
- /// </summary>
- /// <param name="brightnessNum">亮度范围0-100</param>
- public void SetBackLight(double brightnessNum)
- {
- try
- {
- if (!objaflex.actual_AutoMode) return;
- if (IsInit)
- {
- GeneralRelease();
- if (brightnessNum >= 0 && brightnessNum <= 100)
- {
- objaflex.SetAuxPowerOut(brightnessNum);
- }
- }
- }
- catch (Exception)
- {
- }
- }
- /// <summary>
- /// 设置震动强度
- /// </summary>
- /// <param name="IntensValue">强度范围0-100</param>
- public void SetIntensity(double IntensValue)
- {
- try
- {
- if (!objaflex.actual_AutoMode) return;
- GeneralRelease();
- if (IntensValue >= 0 && IntensValue <= 100.0)
- {
- objaflex.SetIntensity(IntensValue);
- }
- }
- catch (Exception)
- {
- }
- }
- /// <summary>
- /// 加载配置参数
- /// </summary>
- /// <param name="modeNum">范围1-49</param>
- public void LoadConfiguration(int modeNum)
- {
- try
- {
- if (!objaflex.actual_AutoMode) return;
- Byte ConfigNumber;
- if (Byte.TryParse(modeNum.ToString(), out ConfigNumber))
- {
- if (ConfigNumber > 0 && ConfigNumber < 50)
- {
- GeneralRelease();
- objaflex.LoadConfiguration(ConfigNumber);
- }
- }
- }
- catch (Exception)
- {
- }
- }
- /// <summary>
- /// 设置震动方向
- /// </summary>
- /// <param name="DirectionValue">范围0-360°</param>
- public void SetDirection(double DirectionValue)
- {
- try
- {
- if (!objaflex.actual_AutoMode) return;
- if (DirectionValue >= 0 && DirectionValue <= 360.0)
- {
- GeneralRelease();
- objaflex.SetDirection(DirectionValue);
- }
- }
- catch (Exception)
- {
- }
- }
- public void StartShaking(double backLightIntensity, byte configNum, double degreeNum, double vibrationIntensity, double intervalNum)
- {
- FeederParam feed = new FeederParam();
- feed.backLightIntensity = backLightIntensity;
- feed.configNum = configNum;
- feed.degreeNum = degreeNum;
- feed.vibrationIntensity = vibrationIntensity;
- feed.intervalNum = intervalNum;
- stepNum = 1;
- feedStepTimer = new Timer(StepSequenceHandler, feed, 3, 3);
- }
- public void StepSequenceHandler(object param)
- {
- FeederParam feedParam = (FeederParam)param;
- switch (stepNum)
- {
- case 0:
- feedStepTimer.Dispose();
- break;
- case 1: //set general release
- if (!objaflex.actual_GeneralRelease) objaflex.SetGeneralRelease(true);
- stepNum = 2; //next step
- break;
- case 2: //设置光源
- objaflex.SetAuxPowerOut(feedParam.backLightIntensity);
- stepNum = 3;
- break;
- case 3://确认设置
- if (Math.Abs(Math.Round(objaflex.actual_auxPowerOut, 2) - feedParam.backLightIntensity) < 0.1)
- {
- stepNum = 4; //next step
- }
- break;
- case 4://加载震动模式
- objaflex.LoadConfiguration(feedParam.configNum); //move = ConfigNumber 2
- stepNum = 5; //next step
- break;
- case 5: //确认加载成功
- if (objaflex.actual_ConfigNumber == feedParam.configNum)
- {
- stepNum = 6; //next step
- }
- break;
- case 6: //设置震动方向和强度
- objaflex.SetDirection(feedParam.degreeNum);
- objaflex.SetIntensity(feedParam.vibrationIntensity);
- durationNum = durationNum.Subtract(durationNum); //reset Timespan
- stepNum = 7; //next step
- break;
- case 7: //确认设置成功
- if ((Math.Abs(Math.Round(objaflex.actual_direction, 2) - feedParam.degreeNum) < 0.1) && (Math.Abs(Math.Round(objaflex.actual_intensity, 2) - feedParam.vibrationIntensity) < 0.1))
- {
- durationNum = durationNum.Add(new TimeSpan(0, 0, 0, 0, 3));
- }
- if (durationNum.Milliseconds > feedParam.intervalNum)
- { //500ms move duration
- stepNum = 8; //next step
- }
- break;
- case 8: //设置震动强度 0%
- objaflex.SetIntensity(0);
- stepNum = 9; //next step
- break;
- case 9: //确认
- if (objaflex.actual_intensity == 0)
- {
- stepNum = 0; //next step
- }
- break;
- }
- }
- public void Dispose()
- {
- throw new NotImplementedException();
- }
- public Task<ushort> GetBackLightFlashTimeAsync()
- {
- return Task.FromResult((ushort)0);
- }
- public int GetDuration(int id)
- {
- return 0;
- }
- public Task<int> GetDurationAsync(int id)
- {
- return Task.FromResult((int)0);
- }
- public Task<int> GetInputTrigger(int i)
- {
- return Task.FromResult((int)0);
- }
- public Task<ushort> GetLightBrightnessAsync()
- {
- return Task.FromResult((ushort)objaflex.actual_auxPowerOut);
- }
- public ushort GetLightTimeout()
- {
- return 0;
- }
- public Task<ushort> GetLightTimeoutAsync()
- {
- return Task.FromResult((ushort)0);
- }
- public bool GetOutputStatus()
- {
- return false;
- }
- public Task<bool> GetOutputStatusAsync()
- {
- return Task.FromResult(false);
- }
- public Task<ushort> GetPlatShakeTimeoutAsync()
- {
- return Task.FromResult((ushort)0);
- }
- public int GetSelectedSequence()
- {
- return Convert.ToInt16(objaflex.actual_ConfigNumber);
- }
- public Task<int> GetSelectedSequenceAsync()
- {
- return Task.FromResult((int)Convert.ToInt16(objaflex.actual_ConfigNumber));
- }
- public int GetSelectedShakeParam()
- {
- return 0;
- }
- public Task<int> GetSelectedShakeParamAsync()
- {
- return Task.FromResult(0);
- }
- public Task<SingleSequenceActionGroup> GetSequenceGroupAsync(int id)
- {
- return Task.FromResult<SingleSequenceActionGroup>(null);
- }
- public SingleActionParam GetShakeGroupValue(int index)
- {
- return null;
- }
- public Task<SingleActionParam> GetShakeGroupValueAsync(int index)
- {
- return Task.FromResult<SingleActionParam>(null);
- }
- public SingleActionParam GetShakeParameters(int selectedShakeMotion)
- {
- return null;
- }
- public Task<SingleActionParam> GetShakeParametersAsync(int selectedShakeMotion)
- {
- return Task.FromResult<SingleActionParam>(null);
- }
- public string GetSingleParamValue(int id)
- {
- return "";
- }
- public Task<string> GetSingleParamValueAsync(int id)
- {
- return Task.FromResult("");
- }
- public Task<ushort> GetUpperFeederShakeTimeoutAsync()
- {
- return Task.FromResult((ushort)0);
- }
- public Task<HopperFeederParam> GetUpperParamAsync(int selectedSequence)
- {
- return Task.FromResult<HopperFeederParam>(null);
- }
- public bool LoadShakeSet(int v)
- {
- return false;
- }
- public Task<bool> LoadShakeSetAsync(int v)
- {
- return Task.FromResult(false);
- }
- public void OpenLight()
- {
- try
- {
- if (!objaflex.actual_AutoMode) return;
- double DirectionValue = 100;
- if (DirectionValue >= 0 && DirectionValue <= 360.0)
- {
- GeneralRelease();
- objaflex.SetDirection(DirectionValue);
- }
- }
- catch (Exception)
- {
- }
- }
- public Task OpenLightAsync()
- {
- try
- {
- if (!objaflex.actual_AutoMode) return Task.CompletedTask;
- double DirectionValue = 100;
- if (DirectionValue >= 0 && DirectionValue <= 360.0)
- {
- GeneralRelease();
- objaflex.SetDirection(DirectionValue);
- }
- }
- catch (Exception)
- {
- }
- return Task.CompletedTask;
- }
- public int Output(int id, int? timespan = null)
- {
- return 0;
- }
- public Task<int> OutputAsync(int id, int? timespan = null)
- {
- return Task.FromResult((int)0);
- }
- public bool QueryLightState()
- {
- if (objaflex.actual_auxPowerOut > 0)
- return false;
- else return true;
- }
- public Task<bool> QueryLightStateAsync()
- {
- if (objaflex.actual_auxPowerOut > 0)
- return Task.FromResult(false);
- else return Task.FromResult(true);
- }
- public void Reset()
- {
- }
- public Task ResetAsync()
- {
- return Task.CompletedTask;
- }
- public Task<int> RunFromCommand(string command, int? timespan = null)
- {
- return Task.FromResult<int>(0);
- }
- public Task<int> RunToBottomAsync(int? timespan = null)
- {
- return Task.FromResult<int>(0);
- }
- public Task<int> RunToBottomLeftAsync(int? timespan = null)
- {
- return Task.FromResult<int>(0);
- }
- public Task<int> RunToBottomRightAsync(int? timespan = null)
- {
- return Task.FromResult<int>(0);
- }
- public Task<int> RunToFlipAsync(int? timespan = null)
- {
- return Task.FromResult<int>(0);
- }
- public Task<int> RunToInnerHorizonAsync(int? timespan = null)
- {
- return Task.FromResult<int>(0);
- }
- public Task<int> RunToInnerVerticalAsync(int? timespan = null)
- {
- return Task.FromResult<int>(0);
- }
- public Task<int> RunToLeftAsync(int? timespan = null)
- {
- return Task.FromResult<int>(0);
- }
- public Task<int> RunToRightAsync(int? timespan = null)
- {
- return Task.FromResult<int>(0);
- }
- public Task<int> RunToTopAsync(int? timespan = null)
- {
- return Task.FromResult<int>(0);
- }
- public Task<int> RunToTopLeftAsync(int? timespan = null)
- {
- return Task.FromResult<int>(0);
- }
- public Task<int> RunToTopRightAsync(int? timespan = null)
- {
- return Task.FromResult<int>(0);
- }
- public Task SaveAllSetAsync()
- {
- return Task.CompletedTask;
- }
- public Task SaveGlobalSetAsync()
- {
- return Task.CompletedTask;
- }
- public Task SaveSequenceAsync()
- {
- return Task.CompletedTask;
- }
- public Task SaveShakeSetAsync()
- {
- return Task.CompletedTask;
- }
- public void SelectShakeParam(int id)
- {
- }
- public Task SelectShakeParamAsync(int id)
- {
- return Task.CompletedTask;
- }
- public Task SetBackLightFlashTimeAsync(ushort backLightFlash)
- {
- return Task.CompletedTask;
- }
- public Task SetInputTrigger(int id, int value)
- {
- return Task.CompletedTask;
- }
- public void SetIpAddress(string ip, string subNetMask, int port)
- {
- }
- public Task SetIpAddressAsync(string ip, string subNetMask, int port)
- {
- return Task.CompletedTask;
- }
- public void SetLightTimeOut(ushort timeout)
- {
- }
- public Task SetLightTimeOutAsync(ushort timeout)
- {
- return Task.CompletedTask;
- }
- public Task SetLightValueAsync(ushort lightValue)
- {
- if (lightValue<0 || lightValue>100)
- {
- lightValue = 100;
- }
- try
- {
- objaflex.SetAuxPowerOut(lightValue);
- }
- catch (Exception)
- {
- }
-
- return Task.CompletedTask;
- }
- public Task SetLightValueToBufferAsync(ushort lightValue)
- {
- return Task.CompletedTask;
- }
- public Task SetPlatShakeTimeoutAsync(ushort upperFeederShakeTimeout)
- {
- return Task.CompletedTask;
- }
- public void SetSequence(int groupId, int actionId, SingleSequenceAction singleSequenceAction)
- {
- }
- public Task SetSequenceAsync(int groupId, int actionId, SingleSequenceAction singleSequenceAction)
- {
- return Task.CompletedTask;
- }
- public void SetShakeParameters(int selectedShakeMotion, int id, string value)
- {
- }
- public void SetShakeParameters(int selectedShakeMotion, string value)
- {
- }
- public void SetShakeParameters(int selectedShakeMotion, SingleActionParam value)
- {
- }
- public Task SetShakeParametersAsync(int selectedShakeMotion, int id, string value)
- {
- return Task.CompletedTask;
- }
- public Task SetShakeParametersAsync(int selectedShakeMotion, string value)
- {
- return Task.CompletedTask;
- }
- public Task SetShakeParametersAsync(int selectedShakeMotion, SingleActionParam value)
- {
- return Task.CompletedTask;
- }
- public Task SetUpperFeederShakeTimeoutAsync(ushort upperFeederShakeTimeout)
- {
- return Task.CompletedTask;
- }
- public int ShakeUp()
- {
- return 0;
- }
- public Task<int> ShakeUpAsync()
- {
- return Task.FromResult(0);
- }
- public Task<int> StartAction(int index, int? timespan = null)
- {
- return Task.FromResult(0);
- }
- public Task<int> StartOneSequenceAsync(int id)
- {
- return Task.FromResult(0);
- }
- public void Stop()
- {
- SetIntensity(0);
- }
- public Task StopAsync()
- {
- SetIntensity(0);
- return Task.CompletedTask;
- }
- public Task StopOneSequenceAsync()
- {
- return Task.CompletedTask;
- }
- public void StopOutput()
- {
- }
- public Task StopOutputAsync()
- {
- return Task.CompletedTask;
- }
- public Task WriteOutputParamAsync(int groupId, HopperFeederParam param)
- {
- return Task.CompletedTask;
- }
- }
- }
|