dbHandle = $f3->get('DB'); while ($this->findBoxCore == null) try { $this->findBoxCore = new FindBoxCoreSystem(...array_values($f3->get('CORE.fb'))); } catch (\Exception $e) { $this->log2console("Can't find FindBoxCore, retry in 1 second..\n"); ob_flush(); flush(); sleep(1); } $this->labelToBlink = []; } private function log2console(string $msg) { echo '[' . date("d/m/Y H:i:s") . "] $msg"; ob_flush(); flush(); } private function updateServiceStatus() { $qs = new QueueSystem('FindBoxService_isAlive'); if ($qs->check()) { $qs->reset(); $qs->command = time(); $qs->servicename = 'FindBoxService_isAlive'; $qs->save(); } else { $qs->command = time(); } $qs->update(); } private function getCurrentQueue() { global $f3; $table = new QueueSystem('FindBoxService'); while (!$table->check()) { /** @var QueueCommand $qCommand */ $qCommand = $table->getCommandObject(); switch ($qCommand->operation) { case Q_SAVEPRODUCT: $this->saveProduct($qCommand); break; case Q_BLINK_ON: case Q_BLINK_OFF: $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']); break; } $table->erase(); $table->next(); } } /** * @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 $label * @return bool */ public function saveProduct($label) { if ($label->operation == Q_SAVEPRODUCT) { $this->findBoxCore->SaveProducts($label->id, $label->description, $label->params['dataFields']); $this->log2console("Save product {$label->id}\n"); return true; } return false; } /** * Called from php spawn process. This script end after MAX_BLINK_TIME * * @param $f3 * @param $args * @throws \Exception */ public function Main($f3) { $this->log2console("Starting FindBoxService main loop\n"); while (true) { $this->updateServiceStatus(); $this->getCurrentQueue(); 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(); 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; } } ob_flush(); flush(); usleep($f3->get('FINDBOXSERVICE.SLEEPTIME') * 1000); } return; } }