AfagFeeder.cs 23 KB

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