|
|
@@ -8,19 +8,96 @@
|
|
|
|
|
|
namespace OpenMind\FindBox;
|
|
|
|
|
|
+use Base;
|
|
|
use DB\SQL;
|
|
|
use Exception;
|
|
|
-use F3;
|
|
|
use Log;
|
|
|
use OpenMind\System\QueueSystem;
|
|
|
+use OpenMind\System\System;
|
|
|
+
|
|
|
+class AsyncJobClass
|
|
|
+{
|
|
|
+ /** @var integer $retryForFault */
|
|
|
+ public $retryForFault;
|
|
|
+ /** @var SmartESL $esl */
|
|
|
+ private $esl;
|
|
|
+ /** @var \Generator $blinkGenerator */
|
|
|
+ private $blinkGenerator;
|
|
|
+
|
|
|
+ /** @var array|null ledParams */
|
|
|
+ private $ledParams;
|
|
|
+
|
|
|
+ /** @var bool $blinkoff */
|
|
|
+ private $blinkoff = false;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * AsyncJobClass constructor.
|
|
|
+ *
|
|
|
+ * @param FindBoxCoreSystem $fb
|
|
|
+ * @param string $labelID
|
|
|
+ * @param array|null $ledParams if null, the blink become blinkOFF
|
|
|
+ */
|
|
|
+ public function __construct(FindBoxCoreSystem $fb, string $labelID, array $ledParams = null)
|
|
|
+ {
|
|
|
+ global $f3;
|
|
|
+
|
|
|
+ $this->esl = new SmartESL($fb, [$labelID]);
|
|
|
+ $this->ledParams = $ledParams;
|
|
|
+ $this->retryForFault = $f3->get('MAX_RETRY_FOR_FAULT');
|
|
|
+ }
|
|
|
+
|
|
|
+ public function BlinkON()
|
|
|
+ {
|
|
|
+ $this->blinkGenerator =
|
|
|
+ $this->esl->AsyncBlinkSpecial(
|
|
|
+ $this->ledParams['pattern'],
|
|
|
+ $this->ledParams['repeat'],
|
|
|
+ $this->ledParams['iter_time'],
|
|
|
+ $this->ledParams['iterations'],
|
|
|
+ $this->ledParams['color']
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ public function BlinkOFF()
|
|
|
+ {
|
|
|
+ $this->blinkoff = true;
|
|
|
+ $this->blinkGenerator = $this->esl->AsyncBlinkOff();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @return bool
|
|
|
+ */
|
|
|
+ public function isBlinkoff(): bool
|
|
|
+ {
|
|
|
+ return $this->blinkoff;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function GetCurrentValue()
|
|
|
+ {
|
|
|
+ return $this->blinkGenerator->current();
|
|
|
+ }
|
|
|
+
|
|
|
+ public function Next()
|
|
|
+ {
|
|
|
+ $this->blinkGenerator->next();
|
|
|
+ }
|
|
|
+}
|
|
|
|
|
|
class FindBoxService
|
|
|
{
|
|
|
/** @var SQL $dbHandle */
|
|
|
private $dbHandle;
|
|
|
+
|
|
|
+ /** @var FindBoxCoreSystem|null findBoxCore */
|
|
|
private $findBoxCore = null;
|
|
|
- private $labelToBlink;
|
|
|
+
|
|
|
+ /** @var Log $ServiceLogger */
|
|
|
private $ServiceLogger;
|
|
|
+
|
|
|
+ /** @var array $labelToBlink */
|
|
|
+ private $labelToBlink;
|
|
|
+
|
|
|
+ /** @var QueueSystem $qs_Server */
|
|
|
private $qs_Server;
|
|
|
|
|
|
public function __construct()
|
|
|
@@ -45,15 +122,18 @@ class FindBoxService
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Called from php spawn process. This script end after MAX_BLINK_TIME
|
|
|
+ * Called from php spawn process.
|
|
|
*
|
|
|
- * @param F3 $f3
|
|
|
+ * @param Base $f3
|
|
|
*/
|
|
|
public function Main($f3)
|
|
|
{
|
|
|
$this->ServiceLogger->write("Starting FindBoxService main loop", 'd/m/Y H:i:s');
|
|
|
$this->ServiceLogger->write("Opening socket listen {$f3->get('FINDBOXSERVICE.PORT')}", 'd/m/Y H:i:s');
|
|
|
|
|
|
+ /** @var AsyncJobClass[] $jobs */
|
|
|
+ $jobs = [];
|
|
|
+
|
|
|
while (true) {
|
|
|
|
|
|
$this->getCurrentQueue();
|
|
|
@@ -64,47 +144,50 @@ class FindBoxService
|
|
|
case QueueCommand::$Q_BLINK_OFF:
|
|
|
$this->ServiceLogger->write("BlinkOff for label {$label->id} requested", 'd/m/Y H:i:s');
|
|
|
|
|
|
- $esl = new SmartESL($this->findBoxCore, [$label->id]);
|
|
|
- $err = $esl->BlinkOff();
|
|
|
+ // Rimuove il JOB se esiste
|
|
|
+ $asyncBlinkOFF = new AsyncJobClass($this->findBoxCore, $label->id, NULL);
|
|
|
+ $asyncBlinkOFF->BlinkOFF();
|
|
|
+ $jobs[$label->id] = $asyncBlinkOFF;
|
|
|
|
|
|
- if ($this->ManageBlinkFaults($label->id, $err))
|
|
|
- unset($this->labelToBlink[$labelId]);
|
|
|
+ unset($this->labelToBlink[$label->id]);
|
|
|
|
|
|
break;
|
|
|
|
|
|
case QueueCommand::$Q_BLINK_ON:
|
|
|
- // Priority 1
|
|
|
- // ----------
|
|
|
- // Force exit and blink off if run time script > MAX_BLINK_TIME_SEC
|
|
|
- $now = time() - $label->tsFirstBlink;
|
|
|
- if ($now > $f3->get('MAX_BLINK_TIME_SEC')) {
|
|
|
- $this->ServiceLogger->write("Stop label {$label->id} for timeout", 'd/m/Y H:i:s');
|
|
|
- $esl = new SmartESL($this->findBoxCore, [$label->id]);
|
|
|
- $esl->BlinkOff();
|
|
|
- unset($this->labelToBlink[$labelId]);
|
|
|
- continue;
|
|
|
- }
|
|
|
-
|
|
|
- // Priority 2
|
|
|
- // ----------
|
|
|
- // Restart blink if blink is ended
|
|
|
- $blinkEndTime = time() - $label->tsLastLongBlink;
|
|
|
- /** @var QueueCommand $label */
|
|
|
- $calcDuration = $label->getBlinkDuration();
|
|
|
- if ($blinkEndTime > $calcDuration) {
|
|
|
- $this->ServiceLogger->write("Auto restart blinking for label $label->id\n", 'd/m/Y H:i:s');
|
|
|
-
|
|
|
- $esl = new SmartESL($this->findBoxCore, [$label->id]);
|
|
|
- $ledParams = $label->params;
|
|
|
-
|
|
|
- $err = $esl->BlinkSpecial($ledParams['pattern'], $ledParams['repeat'], $ledParams['iter_time'], $ledParams['iterations'], $ledParams['color']);
|
|
|
-
|
|
|
- $this->ManageBlinkFaults($label->id, $err);
|
|
|
-
|
|
|
- }
|
|
|
+ $asyncBlink = new AsyncJobClass($this->findBoxCore, $label->id, $label->params);
|
|
|
+ $asyncBlink->BlinkON();
|
|
|
+ $jobs[$label->id] = $asyncBlink;
|
|
|
+
|
|
|
+ unset($this->labelToBlink[$label->id]);
|
|
|
+
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ // Async check on JOBS
|
|
|
+ foreach($jobs as $labelID => $asyncBlink) {
|
|
|
+
|
|
|
+ $err = $asyncBlink->GetCurrentValue(); // Fa scattare il generator e ricava il valore
|
|
|
+
|
|
|
+ // Response RESTful ricevuta da Findbox Core?
|
|
|
+ if (is_array($err)) {
|
|
|
+
|
|
|
+ if ($this->ManageBlinkFaults($asyncBlink, $labelID, $err)) {
|
|
|
+ // Rimuove il JOB
|
|
|
+ unset($jobs[$labelID]);
|
|
|
+ } else {
|
|
|
+ // Restart job With errors
|
|
|
+ if ($jobs[$labelID]->isBlinkoff())
|
|
|
+ $jobs[$labelID]->BlinkOFF();
|
|
|
+ else
|
|
|
+ $jobs[$labelID]->BlinkON();
|
|
|
+ }
|
|
|
+
|
|
|
+ } else
|
|
|
+ $asyncBlink->Next();
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
}
|
|
|
|
|
|
return;
|
|
|
@@ -125,7 +208,6 @@ class FindBoxService
|
|
|
case QueueCommand::$Q_BLINK_ON:
|
|
|
case QueueCommand::$Q_BLINK_OFF:
|
|
|
$this->labelToBlink[$qCommand->id] = $qCommand;
|
|
|
- $this->labelToBlink[$qCommand->id]->retryForFault = $f3->get('MAX_RETRY_FOR_FAULT');
|
|
|
break;
|
|
|
case QueueCommand::$Q_DISPLAY:
|
|
|
$this->ServiceLogger->write("Change display for label {$qCommand->id} requested", 'd/m/Y H:i:s');
|
|
|
@@ -144,7 +226,7 @@ class FindBoxService
|
|
|
*/
|
|
|
public function saveProduct($label)
|
|
|
{
|
|
|
- if ($label->operation == Q_SAVEPRODUCT) {
|
|
|
+ if ($label->operation == QueueCommand::$Q_SAVEPRODUCT) {
|
|
|
$this->findBoxCore->SaveProducts($label->id, $label->description, $label->params['dataFields']);
|
|
|
$this->ServiceLogger->write("Save product {$label->id}", 'd/m/Y H:i:s');
|
|
|
return true;
|
|
|
@@ -157,26 +239,21 @@ class FindBoxService
|
|
|
* @param $fbError
|
|
|
* @return bool true if no error, false if error
|
|
|
*/
|
|
|
- private function ManageBlinkFaults($labelId, $fbError): bool
|
|
|
+ private function ManageBlinkFaults(AsyncJobClass &$job, $labelId, $fbError): bool
|
|
|
{
|
|
|
- global $f3;
|
|
|
$labelResponses = $this->findBoxCore->getLabelsFromResponse($fbError);
|
|
|
- $label = &$this->labelToBlink[$labelId];
|
|
|
|
|
|
- if ($labelResponses && isset($labelResponses[$label->id]) && $labelResponses[$label->id]) { // Label exists and is true, mean that's all OK, no error
|
|
|
- $label->retryForFault = $f3->get('MAX_RETRY_FOR_FAULT');
|
|
|
- $label->tsLastLongBlink = time() + 2;
|
|
|
+ if ($labelResponses && isset($labelResponses[$labelId]) && $labelResponses[$labelId]) { // Label exists and is true, mean that's all OK, no error
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
// Ho finito il numero di tentativi massimi?
|
|
|
- if ($label->retryForFault < 0) { // Rimuovo dalla coda l'etichetta
|
|
|
- $this->ServiceLogger->write("Label {$label->id} fatal error. Removing from queue", 'd/m/Y H:i:s');
|
|
|
- unset($this->labelToBlink[$labelId]);
|
|
|
- } else { // Riprovo al prossimo ciclo
|
|
|
- $this->ServiceLogger->write("Label {$label->id} blink error. Try next cycle...", 'd/m/Y H:i:s');
|
|
|
- $label->tsLastLongBlink = 0;
|
|
|
- $label->retryForFault--;
|
|
|
+ if ($job->retryForFault < 0) { // Rimuovo dalla coda l'etichetta
|
|
|
+ $this->ServiceLogger->write("Label {$labelId} fatal error. Removing from queue", 'd/m/Y H:i:s');
|
|
|
+ return true;
|
|
|
+ } else { // Riprovo al prossimo ciclo
|
|
|
+ $this->ServiceLogger->write("Label {$labelId} blink error. Try next cycle...", 'd/m/Y H:i:s');
|
|
|
+ $job->retryForFault--;
|
|
|
}
|
|
|
|
|
|
return false; // Default return value
|