Browse Source

NEW: class JobManager

Giancarlo 7 years ago
parent
commit
973cc369b0

+ 22 - 8
src/Modules/Blank/Pages.php

@@ -9,7 +9,6 @@
 namespace Modules\Blank;
 namespace Modules\Blank;
 
 
 use Base;
 use Base;
-use OpenMind\FindBox\FindBoxCoreSystem;
 use OpenMind\FindBox\QueueCommand;
 use OpenMind\FindBox\QueueCommand;
 use OpenMind\System\QueueSystem;
 use OpenMind\System\QueueSystem;
 use OpenMind\Web\Controller;
 use OpenMind\Web\Controller;
@@ -52,16 +51,23 @@ class Pages extends Controller
 			'0113C377'
 			'0113C377'
 		];
 		];
 
 
-/*		$findBoxCore = new FindBoxCoreSystem(...array_values($f3->get('CORE.fb')));
+		/*		$findBoxCore = new FindBoxCoreSystem(...array_values($f3->get('CORE.fb')));
 
 
-		foreach($labels as $label) {
-			$findBoxCore->Bind($label, $label);
-			//$findBoxCore->UnBind($label);
-		}*/
+				foreach($labels as $label) {
+					$findBoxCore->Bind($label, $label);
+					//$findBoxCore->UnBind($label);
+				}*/
+
+		$this->BlinkOFF($labels);
+		sleep(5);
 
 
 		$this->BlinkON($labels);
 		$this->BlinkON($labels);
-		//sleep(15);
-		//$this->BlinkOFF($labels);
+
+		$this->BlinkONW([
+			'0113067F',
+			'011306C5',
+			'01130713',
+			'01130747']);
 
 
 		$f3->set('MESSAGE', 'Hello World');
 		$f3->set('MESSAGE', 'Hello World');
 		$f3->set('CONTENT', 'message.tpl');
 		$f3->set('CONTENT', 'message.tpl');
@@ -76,6 +82,14 @@ class Pages extends Controller
 		$qs->send($cmd);
 		$qs->send($cmd);
 	}
 	}
 
 
+	public function BlinkONW(array $labels)
+	{
+		$qs = new QueueSystem();
+		$cmd = new QueueCommand($labels);
+		$cmd->setLeds(2, 1, 1000, 300, 0);
+		$qs->send($cmd);
+	}
+
 	public function BlinkOFF(array $labels)
 	public function BlinkOFF(array $labels)
 	{
 	{
 		$qs = new QueueSystem();
 		$qs = new QueueSystem();

+ 5 - 1
src/OpenMind/FindBox/FindBoxCoreSystem.php

@@ -127,7 +127,11 @@ class FindBoxCoreSystem
 			fwrite($streamfbOS, $requestText);
 			fwrite($streamfbOS, $requestText);
 			if (!$skipResponse) {
 			if (!$skipResponse) {
 				while ('' == $r = stream_get_contents($streamfbOS))
 				while ('' == $r = stream_get_contents($streamfbOS))
-					yield $r;
+					// Send(true) interrupt the capp
+					if ($reason = (yield $r)) {
+						$r = "\r\n\r\n{\"value\": \"{$reason}\"}";
+						break;
+					}
 				list(, $bodyJSON) = explode("\r\n\r\n", $r, 2);
 				list(, $bodyJSON) = explode("\r\n\r\n", $r, 2);
 			}
 			}
 			fclose($streamfbOS);
 			fclose($streamfbOS);

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

@@ -14,6 +14,44 @@ use Exception;
 use Log;
 use Log;
 use OpenMind\System\QueueSystem;
 use OpenMind\System\QueueSystem;
 
 
+class JobManager
+{
+	/** @var AsyncJobClass[] $jobs */
+	private $jobs;
+
+	public function __construct()
+	{
+		$this->jobs = [];
+	}
+
+	public function RemoveLabelsFromJobs(array $labelsToRemove)
+	{
+		foreach($this->jobs as $job) {
+			$job->RemoveLabels($labelsToRemove);
+		}
+	}
+
+	/**
+	 * In callBack need to call GetCurrentValue to get the Generator value
+	 *
+	 * @param callable $callBack function(AsyncJobClass), return TRUE to remove job from queue
+	 */
+	public function Run(callable $callBack)
+	{
+		foreach($this->jobs as $k => $job) {
+			if ($callBack($job) === TRUE)
+				unset($this->jobs[$k]);
+			else
+				$job->Next();
+		}
+	}
+
+	public function add(AsyncJobClass $job): void
+	{
+		$this->jobs[] = $job;
+	}
+}
+
 class AsyncJobClass
 class AsyncJobClass
 {
 {
 	/** @var integer $retryForFault */
 	/** @var integer $retryForFault */
@@ -51,6 +89,17 @@ class AsyncJobClass
 		$this->retryForFault = $f3->get('MAX_RETRY_FOR_FAULT');
 		$this->retryForFault = $f3->get('MAX_RETRY_FOR_FAULT');
 	}
 	}
 
 
+	public function __destruct()
+	{
+		$this->Stop('Class Disposed');
+	}
+
+	public function RemoveLabels(array $labelsToFind)
+	{
+		$a = array_diff($this->labels, $labelsToFind);
+		$this->labels = $a;
+	}
+
 	/**
 	/**
 	 * @return array|string
 	 * @return array|string
 	 */
 	 */
@@ -105,6 +154,11 @@ class AsyncJobClass
 	{
 	{
 		$this->blinkGenerator->next();
 		$this->blinkGenerator->next();
 	}
 	}
+
+	public function Stop($reason)
+	{
+		return $this->blinkGenerator->send($reason);
+	}
 }
 }
 
 
 class FindBoxService
 class FindBoxService
@@ -155,9 +209,7 @@ class FindBoxService
 		$this->ServiceLogger->write("Starting FindBoxService main loop", 'd/m/Y H:i:s');
 		$this->ServiceLogger->write("Starting FindBoxService main loop", 'd/m/Y H:i:s');
 		$this->ServiceLogger->write("Opening socket listen {$f3->get('FINDBOXSERVICE.PORT')}", 'd/m/Y H:i:s');
 		$this->ServiceLogger->write("Opening socket listen {$f3->get('FINDBOXSERVICE.PORT')}", 'd/m/Y H:i:s');
 
 
-		/** @var AsyncJobClass[] $jobs */
-		$jobs = [];
-
+		$jobManager = new JobManager();
 		while (true) {
 		while (true) {
 			$this->getCurrentQueue();
 			$this->getCurrentQueue();
 
 
@@ -168,10 +220,12 @@ class FindBoxService
 						$labelJ = join(', ', $label->id);
 						$labelJ = join(', ', $label->id);
 						$this->ServiceLogger->write("BlinkOff for label(s) {$labelJ} requested", 'd/m/Y H:i:s');
 						$this->ServiceLogger->write("BlinkOff for label(s) {$labelJ} requested", 'd/m/Y H:i:s');
 
 
+						$jobManager->RemoveLabelsFromJobs($label->id);
+
 						// Rimuove il JOB se esiste
 						// Rimuove il JOB se esiste
 						$asyncBlinkOFF = new AsyncJobClass($this->findBoxCore, $label->id);
 						$asyncBlinkOFF = new AsyncJobClass($this->findBoxCore, $label->id);
 						$asyncBlinkOFF->BlinkOFF();
 						$asyncBlinkOFF->BlinkOFF();
-						$jobs[$commandID] = $asyncBlinkOFF;
+						$jobManager->add($asyncBlinkOFF);
 
 
 						unset($this->labelToBlink[$commandID]);
 						unset($this->labelToBlink[$commandID]);
 
 
@@ -181,9 +235,11 @@ class FindBoxService
 						$labelJ = join(', ', $label->id);
 						$labelJ = join(', ', $label->id);
 						$this->ServiceLogger->write("BlinkOn for label(s) {$labelJ} requested", 'd/m/Y H:i:s');
 						$this->ServiceLogger->write("BlinkOn for label(s) {$labelJ} requested", 'd/m/Y H:i:s');
 
 
+						$jobManager->RemoveLabelsFromJobs($label->id);
+
 						$asyncBlink = new AsyncJobClass($this->findBoxCore, $label->id, $label->params);
 						$asyncBlink = new AsyncJobClass($this->findBoxCore, $label->id, $label->params);
 						$asyncBlink->BlinkON();
 						$asyncBlink->BlinkON();
-						$jobs[$commandID] = $asyncBlink;
+						$jobManager->add($asyncBlink);
 
 
 						unset($this->labelToBlink[$commandID]);
 						unset($this->labelToBlink[$commandID]);
 
 
@@ -192,7 +248,7 @@ class FindBoxService
 			}
 			}
 
 
 			// Async check on JOBS
 			// Async check on JOBS
-			foreach($jobs as $commandID => $asyncBlink) {
+			$jobManager->Run(function ($asyncBlink) {
 
 
 				$err = $asyncBlink->GetCurrentValue();  // Fa scattare il generator e ricava il valore
 				$err = $asyncBlink->GetCurrentValue();  // Fa scattare il generator e ricava il valore
 
 
@@ -200,19 +256,16 @@ class FindBoxService
 				if (is_array($err)) {
 				if (is_array($err)) {
 					if ($this->ManageBlinkFaults($asyncBlink, $asyncBlink->getLabels(), $err)) {
 					if ($this->ManageBlinkFaults($asyncBlink, $asyncBlink->getLabels(), $err)) {
 						// Remove the JOB
 						// Remove the JOB
-						unset($jobs[$commandID]);
+						return TRUE;
 					} else {
 					} else {
 						// Restart JOB if any errors
 						// Restart JOB if any errors
-						if ($jobs[$commandID]->isBlinkoff())
-							$jobs[$commandID]->BlinkOFF();
+						if ($asyncBlink->isBlinkoff())
+							$asyncBlink->BlinkOFF();
 						else
 						else
-							$jobs[$commandID]->BlinkON();
+							$asyncBlink->BlinkON();
 					}
 					}
-
-				} else
-					$asyncBlink->Next();
-
-			}
+				}
+			});
 
 
 		}
 		}
 
 
@@ -231,6 +284,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:
+					// Split blink list by 9 items each array
 					$labelsSplitted = array_chunk($qCommand->id, 9);
 					$labelsSplitted = array_chunk($qCommand->id, 9);
 					foreach($labelsSplitted as $labels) {
 					foreach($labelsSplitted as $labels) {
 						$cmd = clone $qCommand;
 						$cmd = clone $qCommand;