AfagFeeder.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863
  1. using aflex_CSharp_Integration_Sample;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Runtime.CompilerServices;
  7. using System.Text;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. using Team.FFFeederService.Enums;
  11. using Team.FFFeederService.Interfaces;
  12. using Team.FFFeederService.Models;
  13. using TouchSocket.Core;
  14. using TouchSocket.Sockets;
  15. namespace Team.FFFeederService
  16. {
  17. public struct FeederParam
  18. {
  19. public double backLightIntensity;//光源强度
  20. public byte configNum;//震动模式
  21. public double degreeNum;//震动角度
  22. public double vibrationIntensity;//震动强度
  23. public double intervalNum;//间隔时间ms
  24. }
  25. public class AfagFeeder : IVoiceCoilMotorFeeder
  26. {
  27. public TcpClient TcpClient { get; }
  28. public int FeederPort { get; private set; }
  29. public string FeederIp { get; private set; }
  30. public FeederBrand FeederBrand { get => FeederBrand.AFAG; }
  31. public FeederModel FeederModel { get; private set; } = FeederModel.Unknown;
  32. public Guid Id { get; set; }
  33. public int FeederNo { get; set; }
  34. public string Name { get; set; }
  35. public bool IsConnected => IsInit;
  36. public bool IsInit { get; set; }
  37. /// <summary>
  38. /// Feeder连接状态更改事件
  39. /// </summary>
  40. public event Action<object, bool> FeederConnectionStatusChanged;
  41. private aflexCommunicationClass objaflex;
  42. Timer feedStepTimer;
  43. uint stepNum = 0;
  44. TimeSpan durationNum = new TimeSpan();
  45. /// <summary>
  46. /// 配置编号
  47. /// </summary>
  48. public int ConfigIndex
  49. {
  50. get
  51. {
  52. if (objaflex == null) return 0;
  53. else return objaflex.actual_ConfigNumber;
  54. }
  55. }
  56. /// <summary>
  57. /// 振动强度
  58. /// </summary>
  59. public double Intensity
  60. {
  61. get
  62. {
  63. if (objaflex == null) return 0;
  64. else return objaflex.actual_intensity;
  65. }
  66. }
  67. /// <summary>
  68. /// 方向
  69. /// </summary>
  70. public double Direction
  71. {
  72. get
  73. {
  74. if (objaflex == null) return 0;
  75. else return objaflex.actual_direction;
  76. }
  77. }
  78. public AfagFeeder(Guid id, int feederNo, string name, string ip, int port)
  79. {
  80. Id = id;
  81. FeederNo = feederNo;
  82. Name= name;
  83. FeederIp = ip;
  84. FeederPort = port;
  85. objaflex = new aflexCommunicationClass();
  86. objaflex.aflexValueChanged += Objaflex_aflexValueChanged;
  87. }
  88. private void Objaflex_aflexValueChanged(object sender, EventArgs e)
  89. {
  90. }
  91. public void CloseLight()
  92. {
  93. try
  94. {
  95. if (!objaflex.actual_AutoMode) return;
  96. objaflex.SetAuxPowerOut(0);
  97. }
  98. catch (Exception)
  99. {
  100. }
  101. }
  102. public Task CloseLightAsync()
  103. {
  104. try
  105. {
  106. if (!objaflex.actual_AutoMode) return Task.CompletedTask;
  107. objaflex.SetAuxPowerOut(0);
  108. }
  109. catch (Exception)
  110. {
  111. }
  112. return Task.CompletedTask;
  113. }
  114. public void Connect()
  115. {
  116. if (!IsInit)
  117. { //Connect
  118. //Check IP Textbox
  119. IPAddress IPValue;
  120. if (IPAddress.TryParse(FeederIp, out IPValue))
  121. {
  122. if (/*IPValue.GetAddressBytes()[3] != 0 && */IPValue.GetAddressBytes()[3] != 255)
  123. {
  124. //Valid IP
  125. //Check Port
  126. int PortValue = FeederPort;
  127. if (PortValue > 0)
  128. {
  129. //Valid Port
  130. //Connect
  131. bool flog = objaflex.Connect(IPValue, PortValue);
  132. if (flog)
  133. {
  134. FeederConnectionStatusChanged?.Invoke(this,true);
  135. GeneralRelease();
  136. }
  137. else
  138. {
  139. FeederConnectionStatusChanged?.Invoke(this,false);
  140. }
  141. IsInit = true;
  142. }
  143. }
  144. }
  145. }
  146. //else
  147. //{ //Disconnect
  148. // objaflex.Disconnect();
  149. //}
  150. }
  151. public async Task ConnectAsync()
  152. {
  153. await Task.Run(async () =>
  154. {
  155. //Connect
  156. //Check IP Textbox
  157. IPAddress IPValue;
  158. bool resB = IPAddress.TryParse(FeederIp, out IPValue);
  159. int PortValue = FeederPort;
  160. try
  161. {
  162. if (resB && IPValue.GetAddressBytes()[3] != 255)
  163. {
  164. if (PortValue > 0)
  165. {
  166. bool flog = objaflex.Connect(IPValue, PortValue);
  167. if (flog)
  168. {
  169. FeederConnectionStatusChanged?.Invoke(this,true);
  170. GeneralRelease();
  171. }
  172. else
  173. {
  174. FeederConnectionStatusChanged?.Invoke(this,false);
  175. }
  176. IsInit = true;
  177. }
  178. }
  179. }
  180. catch (Exception)
  181. {
  182. IsInit = false;
  183. while (!IsInit)
  184. {
  185. try
  186. {
  187. bool flog = objaflex.Connect(IPValue, PortValue);
  188. if (flog)
  189. {
  190. FeederConnectionStatusChanged?.Invoke(this,true);
  191. GeneralRelease();
  192. }
  193. else
  194. {
  195. FeederConnectionStatusChanged?.Invoke(this,false);
  196. }
  197. IsInit = true;
  198. }
  199. catch (Exception)
  200. {
  201. await Task.Delay(1000);
  202. }
  203. }
  204. }
  205. });
  206. }
  207. /// <summary>
  208. /// set release
  209. /// </summary>
  210. public void GeneralRelease()
  211. {
  212. if (!objaflex.actual_GeneralRelease)
  213. {
  214. objaflex.SetGeneralRelease(!objaflex.actual_GeneralRelease);
  215. }
  216. }
  217. /// <summary>
  218. /// 设置光源亮度
  219. /// </summary>
  220. /// <param name="brightnessNum">亮度范围0-100</param>
  221. public void SetBackLight(double brightnessNum)
  222. {
  223. try
  224. {
  225. if (!objaflex.actual_AutoMode) return;
  226. if (IsInit)
  227. {
  228. GeneralRelease();
  229. if (brightnessNum >= 0 && brightnessNum <= 100)
  230. {
  231. objaflex.SetAuxPowerOut(brightnessNum);
  232. }
  233. }
  234. }
  235. catch (Exception)
  236. {
  237. }
  238. }
  239. /// <summary>
  240. /// 设置震动强度
  241. /// </summary>
  242. /// <param name="IntensValue">强度范围0-100</param>
  243. public void SetIntensity(double IntensValue)
  244. {
  245. try
  246. {
  247. if (!objaflex.actual_AutoMode) return;
  248. GeneralRelease();
  249. if (IntensValue >= 0 && IntensValue <= 100.0)
  250. {
  251. objaflex.SetIntensity(IntensValue);
  252. }
  253. }
  254. catch (Exception)
  255. {
  256. }
  257. }
  258. /// <summary>
  259. /// 加载配置参数
  260. /// </summary>
  261. /// <param name="modeNum">范围1-49</param>
  262. public void LoadConfiguration(int modeNum)
  263. {
  264. try
  265. {
  266. if (!objaflex.actual_AutoMode) return;
  267. Byte ConfigNumber;
  268. if (Byte.TryParse(modeNum.ToString(), out ConfigNumber))
  269. {
  270. if (ConfigNumber > 0 && ConfigNumber < 50)
  271. {
  272. GeneralRelease();
  273. objaflex.LoadConfiguration(ConfigNumber);
  274. }
  275. }
  276. }
  277. catch (Exception)
  278. {
  279. }
  280. }
  281. /// <summary>
  282. /// 设置震动方向
  283. /// </summary>
  284. /// <param name="DirectionValue">范围0-360°</param>
  285. public void SetDirection(double DirectionValue)
  286. {
  287. try
  288. {
  289. if (!objaflex.actual_AutoMode) return;
  290. if (DirectionValue >= 0 && DirectionValue <= 360.0)
  291. {
  292. GeneralRelease();
  293. objaflex.SetDirection(DirectionValue);
  294. }
  295. }
  296. catch (Exception)
  297. {
  298. }
  299. }
  300. public void StartShaking(double backLightIntensity, byte configNum, double degreeNum, double vibrationIntensity, double intervalNum)
  301. {
  302. FeederParam feed = new FeederParam();
  303. feed.backLightIntensity = backLightIntensity;
  304. feed.configNum = configNum;
  305. feed.degreeNum = degreeNum;
  306. feed.vibrationIntensity = vibrationIntensity;
  307. feed.intervalNum = intervalNum;
  308. stepNum = 1;
  309. feedStepTimer = new Timer(StepSequenceHandler, feed, 3, 3);
  310. }
  311. public void StepSequenceHandler(object param)
  312. {
  313. FeederParam feedParam = (FeederParam)param;
  314. switch (stepNum)
  315. {
  316. case 0:
  317. feedStepTimer.Dispose();
  318. break;
  319. case 1: //set general release
  320. if (!objaflex.actual_GeneralRelease) objaflex.SetGeneralRelease(true);
  321. stepNum = 2; //next step
  322. break;
  323. case 2: //设置光源
  324. objaflex.SetAuxPowerOut(feedParam.backLightIntensity);
  325. stepNum = 3;
  326. break;
  327. case 3://确认设置
  328. if (Math.Abs(Math.Round(objaflex.actual_auxPowerOut, 2) - feedParam.backLightIntensity) < 0.1)
  329. {
  330. stepNum = 4; //next step
  331. }
  332. break;
  333. case 4://加载震动模式
  334. objaflex.LoadConfiguration(feedParam.configNum); //move = ConfigNumber 2
  335. stepNum = 5; //next step
  336. break;
  337. case 5: //确认加载成功
  338. if (objaflex.actual_ConfigNumber == feedParam.configNum)
  339. {
  340. stepNum = 6; //next step
  341. }
  342. break;
  343. case 6: //设置震动方向和强度
  344. objaflex.SetDirection(feedParam.degreeNum);
  345. objaflex.SetIntensity(feedParam.vibrationIntensity);
  346. durationNum = durationNum.Subtract(durationNum); //reset Timespan
  347. stepNum = 7; //next step
  348. break;
  349. case 7: //确认设置成功
  350. 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))
  351. {
  352. durationNum = durationNum.Add(new TimeSpan(0, 0, 0, 0, 3));
  353. }
  354. if (durationNum.Milliseconds > feedParam.intervalNum)
  355. { //500ms move duration
  356. stepNum = 8; //next step
  357. }
  358. break;
  359. case 8: //设置震动强度 0%
  360. objaflex.SetIntensity(0);
  361. stepNum = 9; //next step
  362. break;
  363. case 9: //确认
  364. if (objaflex.actual_intensity == 0)
  365. {
  366. stepNum = 0; //next step
  367. }
  368. break;
  369. }
  370. }
  371. public void Dispose()
  372. {
  373. throw new NotImplementedException();
  374. }
  375. public Task<ushort> GetBackLightFlashTimeAsync()
  376. {
  377. return Task.FromResult((ushort)0);
  378. }
  379. public int GetDuration(int id)
  380. {
  381. return 0;
  382. }
  383. public Task<int> GetDurationAsync(int id)
  384. {
  385. return Task.FromResult((int)0);
  386. }
  387. public Task<int> GetInputTrigger(int i)
  388. {
  389. return Task.FromResult((int)0);
  390. }
  391. public Task<ushort> GetLightBrightnessAsync()
  392. {
  393. return Task.FromResult((ushort)objaflex.actual_auxPowerOut);
  394. }
  395. public ushort GetLightTimeout()
  396. {
  397. return 0;
  398. }
  399. public Task<ushort> GetLightTimeoutAsync()
  400. {
  401. return Task.FromResult((ushort)0);
  402. }
  403. public bool GetOutputStatus()
  404. {
  405. return false;
  406. }
  407. public Task<bool> GetOutputStatusAsync()
  408. {
  409. return Task.FromResult(false);
  410. }
  411. public Task<ushort> GetPlatShakeTimeoutAsync()
  412. {
  413. return Task.FromResult((ushort)0);
  414. }
  415. public int GetSelectedSequence()
  416. {
  417. return Convert.ToInt16(objaflex.actual_ConfigNumber);
  418. }
  419. public Task<int> GetSelectedSequenceAsync()
  420. {
  421. return Task.FromResult((int)Convert.ToInt16(objaflex.actual_ConfigNumber));
  422. }
  423. public int GetSelectedShakeParam()
  424. {
  425. return 0;
  426. }
  427. public Task<int> GetSelectedShakeParamAsync()
  428. {
  429. return Task.FromResult(0);
  430. }
  431. public Task<SingleSequenceActionGroup> GetSequenceGroupAsync(int id)
  432. {
  433. return Task.FromResult<SingleSequenceActionGroup>(null);
  434. }
  435. public SingleActionParam GetShakeGroupValue(int index)
  436. {
  437. return null;
  438. }
  439. public Task<SingleActionParam> GetShakeGroupValueAsync(int index)
  440. {
  441. return Task.FromResult<SingleActionParam>(null);
  442. }
  443. public SingleActionParam GetShakeParameters(int selectedShakeMotion)
  444. {
  445. return null;
  446. }
  447. public Task<SingleActionParam> GetShakeParametersAsync(int selectedShakeMotion)
  448. {
  449. return Task.FromResult<SingleActionParam>(null);
  450. }
  451. public string GetSingleParamValue(int id)
  452. {
  453. return "";
  454. }
  455. public Task<string> GetSingleParamValueAsync(int id)
  456. {
  457. return Task.FromResult("");
  458. }
  459. public Task<ushort> GetUpperFeederShakeTimeoutAsync()
  460. {
  461. return Task.FromResult((ushort)0);
  462. }
  463. public Task<HopperFeederParam> GetUpperParamAsync(int selectedSequence)
  464. {
  465. return Task.FromResult<HopperFeederParam>(null);
  466. }
  467. public bool LoadShakeSet(int v)
  468. {
  469. return false;
  470. }
  471. public Task<bool> LoadShakeSetAsync(int v)
  472. {
  473. return Task.FromResult(false);
  474. }
  475. public void OpenLight()
  476. {
  477. try
  478. {
  479. if (!objaflex.actual_AutoMode) return;
  480. double DirectionValue = 100;
  481. if (DirectionValue >= 0 && DirectionValue <= 360.0)
  482. {
  483. GeneralRelease();
  484. objaflex.SetDirection(DirectionValue);
  485. }
  486. }
  487. catch (Exception)
  488. {
  489. }
  490. }
  491. public Task OpenLightAsync()
  492. {
  493. try
  494. {
  495. if (!objaflex.actual_AutoMode) return Task.CompletedTask;
  496. double DirectionValue = 100;
  497. if (DirectionValue >= 0 && DirectionValue <= 360.0)
  498. {
  499. GeneralRelease();
  500. objaflex.SetDirection(DirectionValue);
  501. }
  502. }
  503. catch (Exception)
  504. {
  505. }
  506. return Task.CompletedTask;
  507. }
  508. public int Output(int id, int? timespan = null)
  509. {
  510. return 0;
  511. }
  512. public Task<int> OutputAsync(int id, int? timespan = null)
  513. {
  514. return Task.FromResult((int)0);
  515. }
  516. public bool QueryLightState()
  517. {
  518. if (objaflex.actual_auxPowerOut > 0)
  519. return false;
  520. else return true;
  521. }
  522. public Task<bool> QueryLightStateAsync()
  523. {
  524. if (objaflex.actual_auxPowerOut > 0)
  525. return Task.FromResult(false);
  526. else return Task.FromResult(true);
  527. }
  528. public void Reset()
  529. {
  530. }
  531. public Task ResetAsync()
  532. {
  533. return Task.CompletedTask;
  534. }
  535. public Task<int> RunFromCommand(string command, int? timespan = null)
  536. {
  537. return Task.FromResult<int>(0);
  538. }
  539. public Task<int> RunToBottomAsync(int? timespan = null)
  540. {
  541. return Task.FromResult<int>(0);
  542. }
  543. public Task<int> RunToBottomLeftAsync(int? timespan = null)
  544. {
  545. return Task.FromResult<int>(0);
  546. }
  547. public Task<int> RunToBottomRightAsync(int? timespan = null)
  548. {
  549. return Task.FromResult<int>(0);
  550. }
  551. public Task<int> RunToFlipAsync(int? timespan = null)
  552. {
  553. return Task.FromResult<int>(0);
  554. }
  555. public Task<int> RunToInnerHorizonAsync(int? timespan = null)
  556. {
  557. return Task.FromResult<int>(0);
  558. }
  559. public Task<int> RunToInnerVerticalAsync(int? timespan = null)
  560. {
  561. return Task.FromResult<int>(0);
  562. }
  563. public Task<int> RunToLeftAsync(int? timespan = null)
  564. {
  565. return Task.FromResult<int>(0);
  566. }
  567. public Task<int> RunToRightAsync(int? timespan = null)
  568. {
  569. return Task.FromResult<int>(0);
  570. }
  571. public Task<int> RunToTopAsync(int? timespan = null)
  572. {
  573. return Task.FromResult<int>(0);
  574. }
  575. public Task<int> RunToTopLeftAsync(int? timespan = null)
  576. {
  577. return Task.FromResult<int>(0);
  578. }
  579. public Task<int> RunToTopRightAsync(int? timespan = null)
  580. {
  581. return Task.FromResult<int>(0);
  582. }
  583. public Task SaveAllSetAsync()
  584. {
  585. return Task.CompletedTask;
  586. }
  587. public Task SaveGlobalSetAsync()
  588. {
  589. return Task.CompletedTask;
  590. }
  591. public Task SaveSequenceAsync()
  592. {
  593. return Task.CompletedTask;
  594. }
  595. public Task SaveShakeSetAsync()
  596. {
  597. return Task.CompletedTask;
  598. }
  599. public void SelectShakeParam(int id)
  600. {
  601. }
  602. public Task SelectShakeParamAsync(int id)
  603. {
  604. return Task.CompletedTask;
  605. }
  606. public Task SetBackLightFlashTimeAsync(ushort backLightFlash)
  607. {
  608. return Task.CompletedTask;
  609. }
  610. public Task SetInputTrigger(int id, int value)
  611. {
  612. return Task.CompletedTask;
  613. }
  614. public void SetIpAddress(string ip, string subNetMask, int port)
  615. {
  616. }
  617. public Task SetIpAddressAsync(string ip, string subNetMask, int port)
  618. {
  619. return Task.CompletedTask;
  620. }
  621. public void SetLightTimeOut(ushort timeout)
  622. {
  623. }
  624. public Task SetLightTimeOutAsync(ushort timeout)
  625. {
  626. return Task.CompletedTask;
  627. }
  628. public Task SetLightValueAsync(ushort lightValue)
  629. {
  630. if (lightValue<0 || lightValue>100)
  631. {
  632. lightValue = 100;
  633. }
  634. try
  635. {
  636. objaflex.SetAuxPowerOut(lightValue);
  637. }
  638. catch (Exception)
  639. {
  640. }
  641. return Task.CompletedTask;
  642. }
  643. public Task SetLightValueToBufferAsync(ushort lightValue)
  644. {
  645. return Task.CompletedTask;
  646. }
  647. public Task SetPlatShakeTimeoutAsync(ushort upperFeederShakeTimeout)
  648. {
  649. return Task.CompletedTask;
  650. }
  651. public void SetSequence(int groupId, int actionId, SingleSequenceAction singleSequenceAction)
  652. {
  653. }
  654. public Task SetSequenceAsync(int groupId, int actionId, SingleSequenceAction singleSequenceAction)
  655. {
  656. return Task.CompletedTask;
  657. }
  658. public void SetShakeParameters(int selectedShakeMotion, int id, string value)
  659. {
  660. }
  661. public void SetShakeParameters(int selectedShakeMotion, string value)
  662. {
  663. }
  664. public void SetShakeParameters(int selectedShakeMotion, SingleActionParam value)
  665. {
  666. }
  667. public Task SetShakeParametersAsync(int selectedShakeMotion, int id, string value)
  668. {
  669. return Task.CompletedTask;
  670. }
  671. public Task SetShakeParametersAsync(int selectedShakeMotion, string value)
  672. {
  673. return Task.CompletedTask;
  674. }
  675. public Task SetShakeParametersAsync(int selectedShakeMotion, SingleActionParam value)
  676. {
  677. return Task.CompletedTask;
  678. }
  679. public Task SetUpperFeederShakeTimeoutAsync(ushort upperFeederShakeTimeout)
  680. {
  681. return Task.CompletedTask;
  682. }
  683. public int ShakeUp()
  684. {
  685. return 0;
  686. }
  687. public Task<int> ShakeUpAsync()
  688. {
  689. return Task.FromResult(0);
  690. }
  691. public Task<int> StartAction(int index, int? timespan = null)
  692. {
  693. return Task.FromResult(0);
  694. }
  695. public Task<int> StartOneSequenceAsync(int id)
  696. {
  697. return Task.FromResult(0);
  698. }
  699. public void Stop()
  700. {
  701. SetIntensity(0);
  702. }
  703. public Task StopAsync()
  704. {
  705. SetIntensity(0);
  706. return Task.CompletedTask;
  707. }
  708. public Task StopOneSequenceAsync()
  709. {
  710. return Task.CompletedTask;
  711. }
  712. public void StopOutput()
  713. {
  714. }
  715. public Task StopOutputAsync()
  716. {
  717. return Task.CompletedTask;
  718. }
  719. public Task WriteOutputParamAsync(int groupId, HopperFeederParam param)
  720. {
  721. return Task.CompletedTask;
  722. }
  723. }
  724. }