Browse Source

ADD: FindBoxCoreSystem::getLabelsFromResponse()
CHG: messages log
CHG: Q_BLINK_OFF, moved on main cycle together Q_BLINK_ON
ADD: FindBoxService::ManageBlinkFaults()
ADD: QueueCommand::getter/setter for manage dynamic properties

Giancarlo Voci 7 years ago
parent
commit
66510eea2a

+ 2 - 0
cfg/config.ini

@@ -10,6 +10,8 @@ DSN="mysql:host=localhost;port=3306;dbname=omlink"
 dbuser=root
 dbuser=root
 dbpass=openmind
 dbpass=openmind
 MAX_BLINK_TIME_SEC=600
 MAX_BLINK_TIME_SEC=600
+MAX_RETRY_FOR_FAULT=5
+DEV=true
 
 
 ; Path
 ; Path
 BASEURL=/omlink
 BASEURL=/omlink

+ 7 - 2
src/OpenMind/FindBox/FindBoxCoreSystem.php

@@ -176,12 +176,17 @@ class FindBoxCoreSystem
   }
   }
 
 
   /**
   /**
+   * Check the response of FindBoxOS, return an array if no error, else NULL
+   *
    * @param array $fbOS_Response
    * @param array $fbOS_Response
-   * @return array|null
+   * @return NULL|array
    */
    */
   public function getLabelsFromResponse(array $fbOS_Response): ?array
   public function getLabelsFromResponse(array $fbOS_Response): ?array
   {
   {
-    if (($fbOS_Response['value']['code'] != 0) || ($fbOS_Response['value']['value'] == false))
+    // Check if response have field 'value' that is an array
+    $haveArray = isset($fbOS_Response['value']['value']) && is_array($fbOS_Response['value']['value']);
+
+    if (!$haveArray)
       return NULL;
       return NULL;
 
 
     return $fbOS_Response['value']['value'];
     return $fbOS_Response['value']['value'];

+ 83 - 64
src/OpenMind/FindBox/FindBoxService.php

@@ -36,7 +36,7 @@ class FindBoxService
 
 
   private function log2console(string $msg)
   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();
     ob_flush();
     flush();
     flush();
   }
   }
@@ -57,55 +57,71 @@ class FindBoxService
 
 
   private function getCurrentQueue()
   private function getCurrentQueue()
   {
   {
-    //echo $this->getStrTime() . "Checking QueueSystem               \n";
+    global $f3;
     $table = new QueueSystem('FindBoxService');
     $table = new QueueSystem('FindBoxService');
     while (!$table->check()) {
     while (!$table->check()) {
 
 
       /** @var QueueCommand $qCommand */
       /** @var QueueCommand $qCommand */
       $qCommand = $table->getCommandObject();
       $qCommand = $table->getCommandObject();
-      $eraseRecord = true;
       switch ($qCommand->operation) {
       switch ($qCommand->operation) {
         case Q_SAVEPRODUCT:
         case Q_SAVEPRODUCT:
           $this->saveProduct($qCommand);
           $this->saveProduct($qCommand);
           break;
           break;
         case Q_BLINK_ON:
         case Q_BLINK_ON:
-          $this->labelToBlink[$qCommand->id] = $qCommand;
-          break;
         case Q_BLINK_OFF:
         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;
           break;
         case Q_DISPLAY:
         case Q_DISPLAY:
           $this->log2console("Change display for label {$qCommand->id} requested\n");
           $this->log2console("Change display for label {$qCommand->id} requested\n");
           $esl = new SmartESL($this->findBoxCore, [$qCommand->id]);
           $esl = new SmartESL($this->findBoxCore, [$qCommand->id]);
           $esl->Display($qCommand->params['templateId'], $qCommand->params['barcode']);
           $esl->Display($qCommand->params['templateId'], $qCommand->params['barcode']);
-          unset($this->labelToBlink[$qCommand->id]);
           break;
           break;
       }
       }
 
 
-      if ($eraseRecord)
-        $table->erase();
-
+      $table->erase();
       $table->next();
       $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
    * Send SaveProduct to Label
    *
    *
-   * @param  QueueCommand $label
+   * @param $label
    * @return bool
    * @return bool
    */
    */
   public function saveProduct($label)
   public function saveProduct($label)
@@ -128,58 +144,61 @@ class FindBoxService
   public function Main($f3)
   public function Main($f3)
   {
   {
     $this->log2console("Starting FindBoxService main loop\n");
     $this->log2console("Starting FindBoxService main loop\n");
+
     while (true) {
     while (true) {
 
 
       $this->updateServiceStatus();
       $this->updateServiceStatus();
 
 
       $this->getCurrentQueue();
       $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();
       ob_flush();
       flush();
       flush();
       usleep($f3->get('FINDBOXSERVICE.SLEEPTIME') * 1000);
       usleep($f3->get('FINDBOXSERVICE.SLEEPTIME') * 1000);

+ 22 - 0
src/OpenMind/FindBox/QueueCommand.php

@@ -30,6 +30,28 @@ class QueueCommand
     $this->tsFirstBlink = time();
     $this->tsFirstBlink = time();
   }
   }
 
 
+  /**
+   * Add custom property
+   *
+   * @param $name
+   * @param $value
+   */
+  public function __set($name, $value)
+  {
+    $this->{$name} = $value;
+  }
+
+  /**
+   * Get custom property
+   *
+   * @param $name
+   * @return mixed
+   */
+  public function __get($name)
+  {
+    return $this->{$name};
+  }
+
   public function setLeds(int $pattern, int $repeat, int $iter_time, int $iterations, int $color)
   public function setLeds(int $pattern, int $repeat, int $iter_time, int $iterations, int $color)
   {
   {
     $this->operation = Q_BLINK_ON;
     $this->operation = Q_BLINK_ON;