Browse Source

UPD: findbox service.

Giancarlo Voci 7 years ago
parent
commit
d04962bfde

+ 84 - 27
src/Modules/Blank/Pages.php

@@ -23,31 +23,88 @@ class Pages extends Controller
 	public function HelloWorld($f3)
 	{
 
-		$this->BlinkOFF('0113C333');
-		$this->BlinkOFF('0113C371');
-		$this->BlinkOFF('0113C330');
-		$this->BlinkOFF('0113C374');
+		$this->BlinkON([
+			'0113067F',
+			'011306C5',
+			'01130713',
+			'01130747',
+			'011307A4',
+			'011307B4',
+			'01130D98',
+			'01130D9A',
+			'01130DA5']);
+		$this->BlinkON([
+			'01130E03',
+			'01130E27',
+			'01130F13',
+			'01131170',
+			'01131749',
+			'0113174B',
+			'01131777',
+			'01131797',
+			'01131798'
+		]);
+		$this->BlinkON([
+			'0113C333',
+			'0113C371',
+			'0113C330',
+			'0113C374',
+			'0113C337',
+			'0113C2FE',
+			'0113C377',
+			'0113C2FB'
+		]);
 
-		$this->BlinkON('0113C333');
-		$this->BlinkON('0113C371');
+		sleep(30);
 
-		$this->BlinkON('0113C330');
-		$this->BlinkON('0113C374');
-		$this->BlinkOFF('0113C333');
-		$this->BlinkOFF('0113C371');
+		$this->BlinkOFF([
+			'0113067F',
+			'011306C5',
+			'01130713',
+			'01130747',
+			'011307A4',
+			'011307B4',
+			'01130D98',
+			'01130D9A',
+			'01130DA5']);
+		$this->BlinkOFF([
+			'01130E03',
+			'01130E27',
+			'01130F13',
+			'01131170',
+			'01131749',
+			'0113174B',
+			'01131777',
+			'01131797',
+			'01131798']);
+		$this->BlinkOFF([
+			'0113C333',
+			'0113C371',
+			'0113C330',
+			'0113C374',
+			'0113C337',
+			'0113C2FE',
+			'0113C377',
+			'0113C2FB'
+		]);
 
-		$this->BlinkON('0113C337');
-		$this->BlinkON('0113C2FE');
-		$this->BlinkON('0113C377');
-		$this->BlinkON('0113C2FB');
+		/*		$this->BlinkON('0113C333');
+						$this->BlinkON('0113C371');
+						$this->BlinkON('0113C330');
+						$this->BlinkON('0113C374');
+						$this->BlinkON('0113C337');
+						$this->BlinkON('0113C2FE');
+						$this->BlinkON('0113C377');
+						$this->BlinkON('0113C2FB');*/
 
-
-		$this->BlinkOFF('0113C330');
-		$this->BlinkOFF('0113C374');
-		$this->BlinkOFF('0113C337');
-		$this->BlinkOFF('0113C2FE');
-		$this->BlinkOFF('0113C377');
-		$this->BlinkOFF('0113C2FB');
+//		$this->BlinkOFF('0113C333');
+		/*		$this->BlinkOFF('0113C371');
+				$this->BlinkOFF('0113C330');
+				$this->BlinkOFF('0113C374');
+				$this->BlinkOFF('0113C337');
+				$this->BlinkOFF('0113C2FE');
+				$this->BlinkOFF('0113C377');
+				$this->BlinkOFF('0113C2FB');*/
 
 
 		$f3->set('MESSAGE', 'Hello World');
@@ -55,19 +112,19 @@ class Pages extends Controller
 		echo Template::instance()->render('masterpage.tpl');
 	}
 
-	public function BlinkOFF(string $labelID)
+	public function BlinkON(array $labels)
 	{
 		$qs = new QueueSystem();
-		$cmd = new QueueCommand($labelID);
-		$cmd->setToBlinkOff();
+		$cmd = new QueueCommand($labels);
+		$cmd->setLeds(2, 1, 1000, 300, 1);
 		$qs->send($cmd);
 	}
 
-	public function BlinkON(string $labelID)
+	public function BlinkOFF(array $labels)
 	{
 		$qs = new QueueSystem();
-		$cmd = new QueueCommand($labelID);
-		$cmd->setLeds(2, 1, 1000, 300, 1);
+		$cmd = new QueueCommand($labels);
+		$cmd->setToBlinkOff();
 		$qs->send($cmd);
 	}
 }

+ 69 - 32
src/OpenMind/FindBox/FindBoxService.php

@@ -13,14 +13,12 @@ use DB\SQL;
 use Exception;
 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;
 
@@ -30,26 +28,51 @@ class AsyncJobClass
 	/** @var bool $blinkoff */
 	private $blinkoff = false;
 
+	/** @var array labels */
+	private $labels;
+
+	/** @var FindBoxCoreSystem $fb */
+	private $fb;
+
 	/**
 	 * AsyncJobClass constructor.
 	 *
 	 * @param FindBoxCoreSystem $fb
-	 * @param string $labelID
+	 * @param array $labels
 	 * @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;
 
-		$this->esl = new SmartESL($fb, [$labelID]);
+		$this->fb = $fb;
+		$this->labels = $labels;
 		$this->ledParams = $ledParams;
 		$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()
 	{
+		$esl = new SmartESL($this->fb, $this->labels);
+
 		$this->blinkGenerator =
-			$this->esl->AsyncBlinkSpecial(
+			$esl->AsyncBlinkSpecial(
 				$this->ledParams['pattern'],
 				$this->ledParams['repeat'],
 				$this->ledParams['iter_time'],
@@ -60,8 +83,9 @@ class AsyncJobClass
 
 	public function BlinkOFF()
 	{
+		$esl = new SmartESL($this->fb, $this->labels);
 		$this->blinkoff = true;
-		$this->blinkGenerator = $this->esl->AsyncBlinkOff();
+		$this->blinkGenerator = $esl->AsyncBlinkOff();
 	}
 
 	/**
@@ -138,49 +162,53 @@ class FindBoxService
 
 			$this->getCurrentQueue();
 
-			foreach($this->labelToBlink as $labelId => &$label) {
+			foreach($this->labelToBlink as $commandID => &$label) {
 				switch ($label->operation) {
 
 					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
-						$asyncBlinkOFF = new AsyncJobClass($this->findBoxCore, $label->id, NULL);
+						$asyncBlinkOFF = new AsyncJobClass($this->findBoxCore, $label->id);
 						$asyncBlinkOFF->BlinkOFF();
-						$jobs[$label->id] = $asyncBlinkOFF;
+						$jobs[$commandID] = $asyncBlinkOFF;
 
-						unset($this->labelToBlink[$label->id]);
+						unset($this->labelToBlink[$commandID]);
 
 						break;
 
 					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->BlinkON();
-						$jobs[$label->id] = $asyncBlink;
+						$jobs[$commandID] = $asyncBlink;
 
-						unset($this->labelToBlink[$label->id]);
+						unset($this->labelToBlink[$commandID]);
 
 						break;
 				}
 			}
 
 			// 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
 
 				// Response RESTful ricevuta da Findbox Core?
 				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 {
-						// Restart job With errors
-						if ($jobs[$labelID]->isBlinkoff())
-							$jobs[$labelID]->BlinkOFF();
+						// Restart JOB if any errors
+						if ($jobs[$commandID]->isBlinkoff())
+							$jobs[$commandID]->BlinkOFF();
 						else
-							$jobs[$labelID]->BlinkON();
+							$jobs[$commandID]->BlinkON();
 					}
 
 				} else
@@ -195,8 +223,6 @@ class FindBoxService
 
 	private function getCurrentQueue()
 	{
-		global $f3;
-
 		if ($this->qs_Server->check()) {
 
 			/** @var QueueCommand $qCommand */
@@ -207,7 +233,7 @@ class FindBoxService
 					break;
 				case QueueCommand::$Q_BLINK_ON:
 				case QueueCommand::$Q_BLINK_OFF:
-					$this->labelToBlink[$qCommand->id] = $qCommand;
+					$this->labelToBlink[rand(0, 60000)] = $qCommand;
 					break;
 				case QueueCommand::$Q_DISPLAY:
 					$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
 	 * @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);
-
-		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;
 		}
 
 		// Ho finito il numero di tentativi massimi?
 		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;
 		} 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--;
+			// Set failed labels into JOB
+			$job->setLabels($fail);
 		}
 
 		return false; // Default return value

+ 1 - 1
src/OpenMind/FindBox/QueueCommand.php

@@ -20,7 +20,7 @@ class QueueCommand
 	public $description;
 	public $params;
 
-	public function __construct(string $labelID)
+	public function __construct($labelID)
 	{
 		$this->id = $labelID;
 		$this->params = null;

+ 24 - 25
src/OpenMind/FindBox/SmartESL.php

@@ -53,25 +53,9 @@ class SmartESL
 		return array_values(array_filter_keys($softVersion, ['soft_major', 'soft_minor', 'soft_patch']));
 	}
 
-	/**
-	 * Blink the label with parameters. BlinkSpecial make use of getData() and check how many leds has to auto select the firmware version.
-	 *
-	 * @param int $pattern
-	 * @param int $repeat
-	 * @param int $iter_time
-	 * @param int $iterations
-	 * @param int|0 $ledColor
-	 * @return array
-	 */
-	public function BlinkSpecial(int $pattern, int $repeat, int $iter_time, int $iterations, int $ledColor = 0)
+	public function AsyncBlinkOff()
 	{
-
-		$uriParams = json_encode(array_merge(
-			array($this->labels),
-			array($pattern, $ledColor, $repeat, $iter_time, $iterations, 1)));
-
-		$URI = "/1.0.0/index.php/smartesl.blink_special/2.0.0/";
-		return $this->core->CallAPI($URI, $uriParams);
+		return $this->AsyncBlinkSpecial(0, 0, 0, 0, 0);
 	}
 
 	public function AsyncBlinkSpecial(int $pattern, int $repeat, int $iter_time, int $iterations, int $ledColor = 0)
@@ -85,12 +69,6 @@ class SmartESL
 		return $this->core->AsyncCallAPI($URI, $uriParams);
 	}
 
-	public function AsyncBlinkOff()
-	{
-		$URI = "/1.0.0/index.php/smartesl.blink_special/2.0.0/";
-		return $this->core->AsyncCallAPI($URI, "[[\"{$this->labels[0]}\"],0,0,0,0,0,1]");
-	}
-
 	public function getData()
 	{
 		return $this->labelProperties[$this->labels[0]]['data'];
@@ -101,8 +79,29 @@ class SmartESL
 	 */
 	public function BlinkOff()
 	{
+		return $this->BlinkSpecial(0, 0, 0, 0, 0);
+
+	}
+
+	/**
+	 * Blink the label with parameters. BlinkSpecial make use of getData() and check how many leds has to auto select the firmware version.
+	 *
+	 * @param int $pattern
+	 * @param int $repeat
+	 * @param int $iter_time
+	 * @param int $iterations
+	 * @param int|0 $ledColor
+	 * @return array
+	 */
+	public function BlinkSpecial(int $pattern, int $repeat, int $iter_time, int $iterations, int $ledColor = 0)
+	{
+
+		$uriParams = json_encode(array_merge(
+			array($this->labels),
+			array($pattern, $ledColor, $repeat, $iter_time, $iterations, 1)));
+
 		$URI = "/1.0.0/index.php/smartesl.blink_special/2.0.0/";
-		return $this->core->CallAPI($URI, "[[\"{$this->labels[0]}\"],0,0,0,0,0,1]");
+		return $this->core->CallAPI($URI, $uriParams);
 	}
 
 	public function Display(string $templateId, string $barcode)