|
@@ -13,14 +13,12 @@ use DB\SQL;
|
|
|
use Exception;
|
|
use Exception;
|
|
|
use Log;
|
|
use Log;
|
|
|
use OpenMind\System\QueueSystem;
|
|
use OpenMind\System\QueueSystem;
|
|
|
-use OpenMind\System\System;
|
|
|
|
|
|
|
|
|
|
class AsyncJobClass
|
|
class AsyncJobClass
|
|
|
{
|
|
{
|
|
|
/** @var integer $retryForFault */
|
|
/** @var integer $retryForFault */
|
|
|
public $retryForFault;
|
|
public $retryForFault;
|
|
|
- /** @var SmartESL $esl */
|
|
|
|
|
- private $esl;
|
|
|
|
|
|
|
+
|
|
|
/** @var \Generator $blinkGenerator */
|
|
/** @var \Generator $blinkGenerator */
|
|
|
private $blinkGenerator;
|
|
private $blinkGenerator;
|
|
|
|
|
|
|
@@ -30,26 +28,51 @@ class AsyncJobClass
|
|
|
/** @var bool $blinkoff */
|
|
/** @var bool $blinkoff */
|
|
|
private $blinkoff = false;
|
|
private $blinkoff = false;
|
|
|
|
|
|
|
|
|
|
+ /** @var array labels */
|
|
|
|
|
+ private $labels;
|
|
|
|
|
+
|
|
|
|
|
+ /** @var FindBoxCoreSystem $fb */
|
|
|
|
|
+ private $fb;
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* AsyncJobClass constructor.
|
|
* AsyncJobClass constructor.
|
|
|
*
|
|
*
|
|
|
* @param FindBoxCoreSystem $fb
|
|
* @param FindBoxCoreSystem $fb
|
|
|
- * @param string $labelID
|
|
|
|
|
|
|
+ * @param array $labels
|
|
|
* @param array|null $ledParams if null, the blink become blinkOFF
|
|
* @param array|null $ledParams if null, the blink become blinkOFF
|
|
|
*/
|
|
*/
|
|
|
- public function __construct(FindBoxCoreSystem $fb, string $labelID, array $ledParams = null)
|
|
|
|
|
|
|
+ public function __construct(FindBoxCoreSystem $fb, array $labels, array $ledParams = null)
|
|
|
{
|
|
{
|
|
|
global $f3;
|
|
global $f3;
|
|
|
|
|
|
|
|
- $this->esl = new SmartESL($fb, [$labelID]);
|
|
|
|
|
|
|
+ $this->fb = $fb;
|
|
|
|
|
+ $this->labels = $labels;
|
|
|
$this->ledParams = $ledParams;
|
|
$this->ledParams = $ledParams;
|
|
|
$this->retryForFault = $f3->get('MAX_RETRY_FOR_FAULT');
|
|
$this->retryForFault = $f3->get('MAX_RETRY_FOR_FAULT');
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * @return array|string
|
|
|
|
|
+ */
|
|
|
|
|
+ public function getLabels()
|
|
|
|
|
+ {
|
|
|
|
|
+ return $this->labels;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * @param array $labels
|
|
|
|
|
+ */
|
|
|
|
|
+ public function setLabels(array $labels): void
|
|
|
|
|
+ {
|
|
|
|
|
+ $this->labels = $labels;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
public function BlinkON()
|
|
public function BlinkON()
|
|
|
{
|
|
{
|
|
|
|
|
+ $esl = new SmartESL($this->fb, $this->labels);
|
|
|
|
|
+
|
|
|
$this->blinkGenerator =
|
|
$this->blinkGenerator =
|
|
|
- $this->esl->AsyncBlinkSpecial(
|
|
|
|
|
|
|
+ $esl->AsyncBlinkSpecial(
|
|
|
$this->ledParams['pattern'],
|
|
$this->ledParams['pattern'],
|
|
|
$this->ledParams['repeat'],
|
|
$this->ledParams['repeat'],
|
|
|
$this->ledParams['iter_time'],
|
|
$this->ledParams['iter_time'],
|
|
@@ -60,8 +83,9 @@ class AsyncJobClass
|
|
|
|
|
|
|
|
public function BlinkOFF()
|
|
public function BlinkOFF()
|
|
|
{
|
|
{
|
|
|
|
|
+ $esl = new SmartESL($this->fb, $this->labels);
|
|
|
$this->blinkoff = true;
|
|
$this->blinkoff = true;
|
|
|
- $this->blinkGenerator = $this->esl->AsyncBlinkOff();
|
|
|
|
|
|
|
+ $this->blinkGenerator = $esl->AsyncBlinkOff();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -138,49 +162,53 @@ class FindBoxService
|
|
|
|
|
|
|
|
$this->getCurrentQueue();
|
|
$this->getCurrentQueue();
|
|
|
|
|
|
|
|
- foreach($this->labelToBlink as $labelId => &$label) {
|
|
|
|
|
|
|
+ foreach($this->labelToBlink as $commandID => &$label) {
|
|
|
switch ($label->operation) {
|
|
switch ($label->operation) {
|
|
|
|
|
|
|
|
case QueueCommand::$Q_BLINK_OFF:
|
|
case QueueCommand::$Q_BLINK_OFF:
|
|
|
- $this->ServiceLogger->write("BlinkOff for label {$label->id} requested", 'd/m/Y H:i:s');
|
|
|
|
|
|
|
+ $labelJ = join(', ', $label->id);
|
|
|
|
|
+ $this->ServiceLogger->write("BlinkOff for label(s) {$labelJ} requested", 'd/m/Y H:i:s');
|
|
|
|
|
|
|
|
// Rimuove il JOB se esiste
|
|
// Rimuove il JOB se esiste
|
|
|
- $asyncBlinkOFF = new AsyncJobClass($this->findBoxCore, $label->id, NULL);
|
|
|
|
|
|
|
+ $asyncBlinkOFF = new AsyncJobClass($this->findBoxCore, $label->id);
|
|
|
$asyncBlinkOFF->BlinkOFF();
|
|
$asyncBlinkOFF->BlinkOFF();
|
|
|
- $jobs[$label->id] = $asyncBlinkOFF;
|
|
|
|
|
|
|
+ $jobs[$commandID] = $asyncBlinkOFF;
|
|
|
|
|
|
|
|
- unset($this->labelToBlink[$label->id]);
|
|
|
|
|
|
|
+ unset($this->labelToBlink[$commandID]);
|
|
|
|
|
|
|
|
break;
|
|
break;
|
|
|
|
|
|
|
|
case QueueCommand::$Q_BLINK_ON:
|
|
case QueueCommand::$Q_BLINK_ON:
|
|
|
|
|
+ $labelJ = join(', ', $label->id);
|
|
|
|
|
+ $this->ServiceLogger->write("BlinkOn for label(s) {$labelJ} requested", 'd/m/Y H:i:s');
|
|
|
|
|
+
|
|
|
$asyncBlink = new AsyncJobClass($this->findBoxCore, $label->id, $label->params);
|
|
$asyncBlink = new AsyncJobClass($this->findBoxCore, $label->id, $label->params);
|
|
|
$asyncBlink->BlinkON();
|
|
$asyncBlink->BlinkON();
|
|
|
- $jobs[$label->id] = $asyncBlink;
|
|
|
|
|
|
|
+ $jobs[$commandID] = $asyncBlink;
|
|
|
|
|
|
|
|
- unset($this->labelToBlink[$label->id]);
|
|
|
|
|
|
|
+ unset($this->labelToBlink[$commandID]);
|
|
|
|
|
|
|
|
break;
|
|
break;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// Async check on JOBS
|
|
// Async check on JOBS
|
|
|
- foreach($jobs as $labelID => $asyncBlink) {
|
|
|
|
|
|
|
+ foreach($jobs as $commandID => $asyncBlink) {
|
|
|
|
|
|
|
|
$err = $asyncBlink->GetCurrentValue(); // Fa scattare il generator e ricava il valore
|
|
$err = $asyncBlink->GetCurrentValue(); // Fa scattare il generator e ricava il valore
|
|
|
|
|
|
|
|
// Response RESTful ricevuta da Findbox Core?
|
|
// Response RESTful ricevuta da Findbox Core?
|
|
|
if (is_array($err)) {
|
|
if (is_array($err)) {
|
|
|
|
|
|
|
|
- if ($this->ManageBlinkFaults($asyncBlink, $labelID, $err)) {
|
|
|
|
|
- // Rimuove il JOB
|
|
|
|
|
- unset($jobs[$labelID]);
|
|
|
|
|
|
|
+ if ($this->ManageBlinkFaults($asyncBlink, $asyncBlink->getLabels(), $err)) {
|
|
|
|
|
+ // Remove the JOB
|
|
|
|
|
+ unset($jobs[$commandID]);
|
|
|
} else {
|
|
} else {
|
|
|
- // Restart job With errors
|
|
|
|
|
- if ($jobs[$labelID]->isBlinkoff())
|
|
|
|
|
- $jobs[$labelID]->BlinkOFF();
|
|
|
|
|
|
|
+ // Restart JOB if any errors
|
|
|
|
|
+ if ($jobs[$commandID]->isBlinkoff())
|
|
|
|
|
+ $jobs[$commandID]->BlinkOFF();
|
|
|
else
|
|
else
|
|
|
- $jobs[$labelID]->BlinkON();
|
|
|
|
|
|
|
+ $jobs[$commandID]->BlinkON();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
} else
|
|
} else
|
|
@@ -195,8 +223,6 @@ class FindBoxService
|
|
|
|
|
|
|
|
private function getCurrentQueue()
|
|
private function getCurrentQueue()
|
|
|
{
|
|
{
|
|
|
- global $f3;
|
|
|
|
|
-
|
|
|
|
|
if ($this->qs_Server->check()) {
|
|
if ($this->qs_Server->check()) {
|
|
|
|
|
|
|
|
/** @var QueueCommand $qCommand */
|
|
/** @var QueueCommand $qCommand */
|
|
@@ -207,7 +233,7 @@ class FindBoxService
|
|
|
break;
|
|
break;
|
|
|
case QueueCommand::$Q_BLINK_ON:
|
|
case QueueCommand::$Q_BLINK_ON:
|
|
|
case QueueCommand::$Q_BLINK_OFF:
|
|
case QueueCommand::$Q_BLINK_OFF:
|
|
|
- $this->labelToBlink[$qCommand->id] = $qCommand;
|
|
|
|
|
|
|
+ $this->labelToBlink[rand(0, 60000)] = $qCommand;
|
|
|
break;
|
|
break;
|
|
|
case QueueCommand::$Q_DISPLAY:
|
|
case QueueCommand::$Q_DISPLAY:
|
|
|
$this->ServiceLogger->write("Change display for label {$qCommand->id} requested", 'd/m/Y H:i:s');
|
|
$this->ServiceLogger->write("Change display for label {$qCommand->id} requested", 'd/m/Y H:i:s');
|
|
@@ -235,25 +261,36 @@ class FindBoxService
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- * @param $labelId
|
|
|
|
|
|
|
+ * @param AsyncJobClass $job
|
|
|
|
|
+ * @param array $labels
|
|
|
* @param $fbError
|
|
* @param $fbError
|
|
|
* @return bool true if no error, false if error
|
|
* @return bool true if no error, false if error
|
|
|
*/
|
|
*/
|
|
|
- private function ManageBlinkFaults(AsyncJobClass &$job, $labelId, $fbError): bool
|
|
|
|
|
|
|
+ private function ManageBlinkFaults(AsyncJobClass &$job, array $labels, $fbError): bool
|
|
|
{
|
|
{
|
|
|
$labelResponses = $this->findBoxCore->getLabelsFromResponse($fbError);
|
|
$labelResponses = $this->findBoxCore->getLabelsFromResponse($fbError);
|
|
|
-
|
|
|
|
|
- if ($labelResponses && isset($labelResponses[$labelId]) && $labelResponses[$labelId]) { // Label exists and is true, mean that's all OK, no error
|
|
|
|
|
|
|
+ // Filter labels with errors
|
|
|
|
|
+ $fail = array_filter($labelResponses, function ($v) {
|
|
|
|
|
+ return !$v;
|
|
|
|
|
+ });
|
|
|
|
|
+ $fail = array_keys($fail);
|
|
|
|
|
+
|
|
|
|
|
+ // TODO fare il controllo con "isset($labelResponses[$labelId]) && $labelResponses[$labelId]"
|
|
|
|
|
+ if ($labelResponses && count($fail) == 0) { // Label exists and is true, mean that's all OK, no error
|
|
|
return true;
|
|
return true;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// Ho finito il numero di tentativi massimi?
|
|
// Ho finito il numero di tentativi massimi?
|
|
|
if ($job->retryForFault < 0) { // Rimuovo dalla coda l'etichetta
|
|
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');
|
|
|
|
|
|
|
+ $labelJ = join(', ', $labels);
|
|
|
|
|
+ $this->ServiceLogger->write("Label(s) {$labelJ} fatal error. Removing from queue", 'd/m/Y H:i:s');
|
|
|
return true;
|
|
return true;
|
|
|
} else { // Riprovo al prossimo ciclo
|
|
} else { // Riprovo al prossimo ciclo
|
|
|
- $this->ServiceLogger->write("Label {$labelId} blink error. Try next cycle...", 'd/m/Y H:i:s');
|
|
|
|
|
|
|
+ $labelJ = join(', ', $fail);
|
|
|
|
|
+ $this->ServiceLogger->write("Label(s) {$labelJ} blink error. Try next cycle...", 'd/m/Y H:i:s');
|
|
|
$job->retryForFault--;
|
|
$job->retryForFault--;
|
|
|
|
|
+ // Set failed labels into JOB
|
|
|
|
|
+ $job->setLabels($fail);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
return false; // Default return value
|
|
return false; // Default return value
|