|
|
@@ -36,7 +36,7 @@ class FindBoxService
|
|
|
|
|
|
private function log2console(string $msg)
|
|
|
{
|
|
|
- echo '[' . date("d/m/Y H:i:s") . "] FindBoxService: $msg";
|
|
|
+ echo '[' . date("d/m/Y H:i:s") . "] $msg";
|
|
|
ob_flush();
|
|
|
flush();
|
|
|
}
|
|
|
@@ -57,55 +57,71 @@ class FindBoxService
|
|
|
|
|
|
private function getCurrentQueue()
|
|
|
{
|
|
|
- //echo $this->getStrTime() . "Checking QueueSystem \n";
|
|
|
+ global $f3;
|
|
|
$table = new QueueSystem('FindBoxService');
|
|
|
while (!$table->check()) {
|
|
|
|
|
|
/** @var QueueCommand $qCommand */
|
|
|
$qCommand = $table->getCommandObject();
|
|
|
- $eraseRecord = true;
|
|
|
switch ($qCommand->operation) {
|
|
|
case Q_SAVEPRODUCT:
|
|
|
$this->saveProduct($qCommand);
|
|
|
break;
|
|
|
case Q_BLINK_ON:
|
|
|
- $this->labelToBlink[$qCommand->id] = $qCommand;
|
|
|
- break;
|
|
|
case Q_BLINK_OFF:
|
|
|
- $this->log2console("BlinkOff for label {$qCommand->id} requested\n");
|
|
|
-
|
|
|
- $esl = new SmartESL($this->findBoxCore, [$qCommand->id]);
|
|
|
- $err = $esl->BlinkOff();
|
|
|
-
|
|
|
- $labelResponses = $this->findBoxCore->getLabelsFromResponse($err);
|
|
|
- if ($labelResponses === NULL) {
|
|
|
- $this->log2console("Label {$qCommand->id} fatal error. Removing from queue\n");
|
|
|
- } else {
|
|
|
- $eraseRecord = $labelResponses[$qCommand->id];
|
|
|
- }
|
|
|
-
|
|
|
- unset($this->labelToBlink[$qCommand->id]);
|
|
|
+ $this->labelToBlink[$qCommand->id] = $qCommand;
|
|
|
+ $this->labelToBlink[$qCommand->id]->retryForFault = $f3->get('MAX_RETRY_FOR_FAULT');
|
|
|
break;
|
|
|
case Q_DISPLAY:
|
|
|
$this->log2console("Change display for label {$qCommand->id} requested\n");
|
|
|
$esl = new SmartESL($this->findBoxCore, [$qCommand->id]);
|
|
|
$esl->Display($qCommand->params['templateId'], $qCommand->params['barcode']);
|
|
|
- unset($this->labelToBlink[$qCommand->id]);
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
- if ($eraseRecord)
|
|
|
- $table->erase();
|
|
|
-
|
|
|
+ $table->erase();
|
|
|
$table->next();
|
|
|
- $this->log2console("Label {$qCommand->id} readed\n");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * @param $labelId
|
|
|
+ * @param $fbError
|
|
|
+ * @return bool true if no error, false if error
|
|
|
+ */
|
|
|
+ private function ManageBlinkFaults($labelId, $fbError): bool
|
|
|
+ {
|
|
|
+ global $f3;
|
|
|
+ $labelResponses = $this->findBoxCore->getLabelsFromResponse($fbError);
|
|
|
+ $label = &$this->labelToBlink[$labelId];
|
|
|
+
|
|
|
+ if ($labelResponses == NULL) { // Fault error on label maybe because label is not binded or display changing/saveproduct
|
|
|
+ $label->retryForFault--;
|
|
|
+ // Ho finito il numero di tentativi massimi?
|
|
|
+ if ($label->retryForFault < 0) { // Rimuovo dalla coda l'etichetta
|
|
|
+ $this->log2console("Label {$label->id} fatal error. Removing from queue\n");
|
|
|
+ unset($this->labelToBlink[$labelId]);
|
|
|
+ } else { // Riprovo al prossimo ciclo
|
|
|
+ $this->log2console("Label {$label->id} blink error. Try next cycle...\n");
|
|
|
+ $label->tsLastLongBlink = 0;
|
|
|
+ }
|
|
|
+ } else { // A valid label id array is returned
|
|
|
+ if (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();
|
|
|
+ return true;
|
|
|
+ } else { // Retry to blink to next cycle
|
|
|
+ $this->log2console("Label {$label->id} blink error. Try next cycle...\n");
|
|
|
+ $label->tsLastLongBlink = 0;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return false; // Default return value
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* Send SaveProduct to Label
|
|
|
*
|
|
|
- * @param QueueCommand $label
|
|
|
+ * @param $label
|
|
|
* @return bool
|
|
|
*/
|
|
|
public function saveProduct($label)
|
|
|
@@ -128,58 +144,61 @@ class FindBoxService
|
|
|
public function Main($f3)
|
|
|
{
|
|
|
$this->log2console("Starting FindBoxService main loop\n");
|
|
|
+
|
|
|
while (true) {
|
|
|
|
|
|
$this->updateServiceStatus();
|
|
|
|
|
|
$this->getCurrentQueue();
|
|
|
|
|
|
- //echo $this->getStrTime() . 'Label in cache: ' . count($this->labelToBlink) . " \n";
|
|
|
- foreach($this->labelToBlink as $labelId => $label) {
|
|
|
-
|
|
|
- // 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->log2console("Stop label {$label->id} for timeout\n");
|
|
|
- $esl = new SmartESL($this->findBoxCore, [$label->id]);
|
|
|
- $esl->BlinkOff();
|
|
|
- unset($this->labelToBlink[$labelId]);
|
|
|
- continue;
|
|
|
- }
|
|
|
+ foreach($this->labelToBlink as $labelId => &$label) {
|
|
|
+ switch ($label->operation) {
|
|
|
+
|
|
|
+ case Q_BLINK_OFF:
|
|
|
+ $this->log2console("BlinkOff for label {$label->id} requested\n");
|
|
|
+
|
|
|
+ $esl = new SmartESL($this->findBoxCore, [$label->id]);
|
|
|
+ $err = $esl->BlinkOff();
|
|
|
|
|
|
- // Priority 2
|
|
|
- // ----------
|
|
|
- // Restart blink if blink is ended
|
|
|
- $blinkEndTime = time() - $label->tsLastLongBlink;
|
|
|
- /** @var QueueCommand $label */
|
|
|
- $calcDuration = $label->getBlinkDuration();
|
|
|
- if ($blinkEndTime > $calcDuration) {
|
|
|
- $this->log2console("Auto restart blinking for label $label->id\n");
|
|
|
-
|
|
|
- $esl = new SmartESL($this->findBoxCore, [$label->id]);
|
|
|
- $ledParams = $label->params;
|
|
|
-
|
|
|
- $err = $esl->BlinkSpecial($ledParams['pattern'], $ledParams['repeat'], $ledParams['iter_time'], $ledParams['iterations'], $ledParams['color']);
|
|
|
-
|
|
|
- $labelResponses = $this->findBoxCore->getLabelsFromResponse($err);
|
|
|
- if ($labelResponses === NULL) {
|
|
|
- $this->log2console("Label {$label->id} fatal error. Removing from queue\n");
|
|
|
- unset($this->labelToBlink[$labelId]);
|
|
|
- } else {
|
|
|
- if ($labelResponses[$label->id])
|
|
|
- $label->tsLastLongBlink = time();
|
|
|
- else { // Retry to blink to next cycle
|
|
|
- $this->log2console("Label {$label->id} blink error. Try next cycle...\n");
|
|
|
- $label->tsLastLongBlink = 0;
|
|
|
+ if ($this->ManageBlinkFaults($label->id, $err))
|
|
|
+ unset($this->labelToBlink[$labelId]);
|
|
|
+
|
|
|
+ break;
|
|
|
+
|
|
|
+ case 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->log2console("Stop label {$label->id} for timeout\n");
|
|
|
+ $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->log2console("Auto restart blinking for label $label->id\n");
|
|
|
+
|
|
|
+ $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);
|
|
|
+
|
|
|
+ }
|
|
|
+ break;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- //echo $this->getStrTime() . "Restart loop \n";
|
|
|
ob_flush();
|
|
|
flush();
|
|
|
usleep($f3->get('FINDBOXSERVICE.SLEEPTIME') * 1000);
|