FindBoxService.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Giancarlo
  5. * Date: 09/08/2018
  6. * Time: 11:18
  7. */
  8. namespace OpenMind\FindBox;
  9. use DB\SQL;
  10. use OpenMind\System\QueueSystem;
  11. class FindBoxService
  12. {
  13. /** @var SQL $dbHandle */
  14. private $dbHandle;
  15. private $findBoxCore = null;
  16. private $labelToBlink;
  17. public function __construct()
  18. {
  19. global $f3;
  20. $this->dbHandle = $f3->get('DB');
  21. while ($this->findBoxCore == null)
  22. try {
  23. $this->findBoxCore = new FindBoxCoreSystem(...array_values($f3->get('CORE.fb')));
  24. } catch (\Exception $e) {
  25. $this->log2console("Can't find FindBoxCore, retry in 1 second..\n");
  26. ob_flush();
  27. flush();
  28. sleep(1);
  29. }
  30. $this->labelToBlink = [];
  31. }
  32. private function log2console(string $msg)
  33. {
  34. echo '[' . date("d/m/Y H:i:s") . "] $msg";
  35. ob_flush();
  36. flush();
  37. }
  38. private function updateServiceStatus()
  39. {
  40. $qs = new QueueSystem('FindBoxService_isAlive');
  41. if ($qs->check()) {
  42. $qs->reset();
  43. $qs->command = time();
  44. $qs->servicename = 'FindBoxService_isAlive';
  45. $qs->save();
  46. } else {
  47. $qs->command = time();
  48. }
  49. $qs->update();
  50. }
  51. private function getCurrentQueue()
  52. {
  53. global $f3;
  54. $table = new QueueSystem('FindBoxService');
  55. while (!$table->check()) {
  56. /** @var QueueCommand $qCommand */
  57. $qCommand = $table->getCommandObject();
  58. switch ($qCommand->operation) {
  59. case Q_SAVEPRODUCT:
  60. $this->saveProduct($qCommand);
  61. break;
  62. case Q_BLINK_ON:
  63. case Q_BLINK_OFF:
  64. $this->labelToBlink[$qCommand->id] = $qCommand;
  65. $this->labelToBlink[$qCommand->id]->retryForFault = $f3->get('MAX_RETRY_FOR_FAULT');
  66. break;
  67. case Q_DISPLAY:
  68. $this->log2console("Change display for label {$qCommand->id} requested\n");
  69. $esl = new SmartESL($this->findBoxCore, [$qCommand->id]);
  70. $esl->Display($qCommand->params['templateId'], $qCommand->params['barcode']);
  71. break;
  72. }
  73. $table->erase();
  74. $table->next();
  75. }
  76. }
  77. /**
  78. * @param $labelId
  79. * @param $fbError
  80. * @return bool true if no error, false if error
  81. */
  82. private function ManageBlinkFaults($labelId, $fbError): bool
  83. {
  84. global $f3;
  85. $labelResponses = $this->findBoxCore->getLabelsFromResponse($fbError);
  86. $label = &$this->labelToBlink[$labelId];
  87. if ($labelResponses == NULL) { // Fault error on label maybe because label is not binded or display changing/saveproduct
  88. $label->retryForFault--;
  89. // Ho finito il numero di tentativi massimi?
  90. if ($label->retryForFault < 0) { // Rimuovo dalla coda l'etichetta
  91. $this->log2console("Label {$label->id} fatal error. Removing from queue\n");
  92. unset($this->labelToBlink[$labelId]);
  93. } else { // Riprovo al prossimo ciclo
  94. $this->log2console("Label {$label->id} blink error. Try next cycle...\n");
  95. $label->tsLastLongBlink = 0;
  96. }
  97. } else { // A valid label id array is returned
  98. if (isset($labelResponses[$label->id]) && $labelResponses[$label->id]) { // Label exists and is true, mean that's all OK, no error
  99. $label->retryForFault = $f3->get('MAX_RETRY_FOR_FAULT');
  100. $label->tsLastLongBlink = time();
  101. return true;
  102. } else { // Retry to blink to next cycle
  103. $this->log2console("Label {$label->id} blink error. Try next cycle...\n");
  104. $label->tsLastLongBlink = 0;
  105. }
  106. }
  107. return false; // Default return value
  108. }
  109. /**
  110. * Send SaveProduct to Label
  111. *
  112. * @param $label
  113. * @return bool
  114. */
  115. public function saveProduct($label)
  116. {
  117. if ($label->operation == Q_SAVEPRODUCT) {
  118. $this->findBoxCore->SaveProducts($label->id, $label->description, $label->params['dataFields']);
  119. $this->log2console("Save product {$label->id}\n");
  120. return true;
  121. }
  122. return false;
  123. }
  124. /**
  125. * Called from php spawn process. This script end after MAX_BLINK_TIME
  126. *
  127. * @param $f3
  128. * @param $args
  129. * @throws \Exception
  130. */
  131. public function Main($f3)
  132. {
  133. $this->log2console("Starting FindBoxService main loop\n");
  134. while (true) {
  135. $this->updateServiceStatus();
  136. $this->getCurrentQueue();
  137. foreach($this->labelToBlink as $labelId => &$label) {
  138. switch ($label->operation) {
  139. case Q_BLINK_OFF:
  140. $this->log2console("BlinkOff for label {$label->id} requested\n");
  141. $esl = new SmartESL($this->findBoxCore, [$label->id]);
  142. $err = $esl->BlinkOff();
  143. if ($this->ManageBlinkFaults($label->id, $err))
  144. unset($this->labelToBlink[$labelId]);
  145. break;
  146. case Q_BLINK_ON:
  147. // Priority 1
  148. // ----------
  149. // Force exit and blink off if run time script > MAX_BLINK_TIME_SEC
  150. $now = time() - $label->tsFirstBlink;
  151. if ($now > $f3->get('MAX_BLINK_TIME_SEC')) {
  152. $this->log2console("Stop label {$label->id} for timeout\n");
  153. $esl = new SmartESL($this->findBoxCore, [$label->id]);
  154. $esl->BlinkOff();
  155. unset($this->labelToBlink[$labelId]);
  156. continue;
  157. }
  158. // Priority 2
  159. // ----------
  160. // Restart blink if blink is ended
  161. $blinkEndTime = time() - $label->tsLastLongBlink;
  162. /** @var QueueCommand $label */
  163. $calcDuration = $label->getBlinkDuration();
  164. if ($blinkEndTime > $calcDuration) {
  165. $this->log2console("Auto restart blinking for label $label->id\n");
  166. $esl = new SmartESL($this->findBoxCore, [$label->id]);
  167. $ledParams = $label->params;
  168. $err = $esl->BlinkSpecial($ledParams['pattern'], $ledParams['repeat'], $ledParams['iter_time'], $ledParams['iterations'], $ledParams['color']);
  169. $this->ManageBlinkFaults($label->id, $err);
  170. }
  171. break;
  172. }
  173. }
  174. ob_flush();
  175. flush();
  176. usleep($f3->get('FINDBOXSERVICE.SLEEPTIME') * 1000);
  177. }
  178. return;
  179. }
  180. }