Browse Source

NEW: OMLINK Service-> more control on jobs, added QueueCommand::getJobs and QueueCommand::clearQueue, it return an already decoded JSON from Service

Giancarlo Voci 7 years ago
parent
commit
3e29861adf

+ 69 - 21
src/Modules/Blank/Pages.php

@@ -48,55 +48,103 @@ class Pages extends Controller
 			'0113C337',
 			'0113C371',
 			'0113C374',
+			'0113C377',
+			'011306C5',
+			'01130713',
+			'01130747',
+			'011307A4',
+			'011307B4',
+			'01130D98',
+			'01130D9A',
+			'01130DA5',
+			'01130E03',
+			'01130E27',
+			'01130F13',
+			'01131170',
+			'01131749',
+			'0113174B',
+			'01131777',
+			'01131797',
+			'01131798',
+			'0113C2FB',
+			'0113C2FE',
+			'0113C330',
+			'0113C333',
+			'0113C337',
+			'0113C371',
+			'0113C374',
+			'0113C377',
+			'011306C5',
+			'01130713',
+			'01130747',
+			'011307A4',
+			'011307B4',
+			'01130D98',
+			'01130D9A',
+			'01130DA5',
+			'01130E03',
+			'01130E27',
+			'01130F13',
+			'01131170',
+			'01131749',
+			'0113174B',
+			'01131777',
+			'01131797',
+			'01131798',
+			'0113C2FB',
+			'0113C2FE',
+			'0113C330',
+			'0113C333',
+			'0113C337',
+			'0113C371',
+			'0113C374',
 			'0113C377'
 		];
 
-		/*		$findBoxCore = new FindBoxCoreSystem(...array_values($f3->get('CORE.fb')));
+		$this->BlinkON($labels);
 
-				foreach($labels as $label) {
-					$findBoxCore->Bind($label, $label);
-					//$findBoxCore->UnBind($label);
-				}*/
+		$qc = new QueueCommand();
+		$qc->getJobs();
+		$qs = new QueueSystem();
+		$resp = $qs->send($qc, true);
+		echo $resp->jobCount . "<BR/>";
 
-		/*	foreach($labels as $label) {
-				$this->BlinkONW([$label]);
-			}*/
+		$qc->clearQueue();
+		$resp = $qs->send($qc, true);
+		echo $resp->jobClear . "<BR/>";
 
-/*
-		foreach($labels as $i => $label) {
-			if ($i > 10)
-				break;
-			$this->BlinkOFF([$label]);
-		}*/
+		$qc->getJobs();
+		$resp = $qs->send($qc, true);
+		echo $resp->jobCount . "<BR/>";
 
-		$this->BlinkOFF($labels);
+		die;
 
 		$f3->set('MESSAGE', 'Hello World');
 		$f3->set('CONTENT', 'message.tpl');
 		echo Template::instance()->render('masterpage.tpl');
 	}
 
-	public function BlinkOFF(array $labels)
+	public function BlinkON(array $labels)
 	{
 		$qs = new QueueSystem();
 		$cmd = new QueueCommand($labels);
-		$cmd->setToBlinkOff();
+		$cmd->setLeds(2, 1, 1000, 300, 1);
 		$qs->send($cmd);
 	}
 
-	public function BlinkONW(array $labels)
+	public function BlinkOFF(array $labels)
 	{
 		$qs = new QueueSystem();
 		$cmd = new QueueCommand($labels);
-		$cmd->setLeds(2, 1, 1000, 300, 0);
+		$cmd->setToBlinkOff();
 		$qs->send($cmd);
 	}
 
-	public function BlinkON(array $labels)
+	public function BlinkONW(array $labels)
 	{
 		$qs = new QueueSystem();
 		$cmd = new QueueCommand($labels);
-		$cmd->setLeds(2, 1, 1000, 300, 1);
+		$cmd->setLeds(2, 1, 1000, 300, 0);
 		$qs->send($cmd);
 	}
 }

+ 60 - 30
src/OpenMind/FindBox/FindBoxService.php

@@ -21,6 +21,14 @@ class JobManager
 	private $jobs;
 
 	public function __construct()
+	{
+		$this->Reset();
+	}
+
+	/*
+	 * Reset Job Manager and clear active queue
+	 */
+	public function Reset()
 	{
 		$this->jobs = [];
 	}
@@ -51,6 +59,11 @@ class JobManager
 	{
 		$this->jobs[] = $job;
 	}
+
+	public function count()
+	{
+		return count($this->jobs);
+	}
 }
 
 class AsyncJobClass
@@ -187,6 +200,9 @@ class FindBoxService
 	/** @var QueueSystem $qs_Server */
 	private $qs_Server;
 
+	/** @var JobManager $jobManager */
+	private $jobManager;
+
 	public function __construct()
 	{
 		global $f3;
@@ -218,7 +234,7 @@ class FindBoxService
 		$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');
 
-		$jobManager = new JobManager();
+		$this->jobManager = new JobManager();
 		while (true) {
 			$this->getCurrentQueue();
 
@@ -229,12 +245,12 @@ class FindBoxService
 						$labelJ = join(', ', $label->id);
 						$this->ServiceLogger->write("BlinkOff for label(s) {$labelJ} requested", 'd/m/Y H:i:s');
 
-						$jobManager->RemoveLabelsFromJobs($label->id);
+						$this->jobManager->RemoveLabelsFromJobs($label->id);
 
 						// Rimuove il JOB se esiste
 						$asyncBlinkOFF = new AsyncJobClass($this->findBoxCore, $label->id);
 						$asyncBlinkOFF->BlinkOFF();
-						$jobManager->add($asyncBlinkOFF);
+						$this->jobManager->add($asyncBlinkOFF);
 
 						unset($this->labelToBlink[$commandID]);
 
@@ -244,11 +260,11 @@ class FindBoxService
 						$labelJ = join(', ', $label->id);
 						$this->ServiceLogger->write("BlinkOn for label(s) {$labelJ} requested", 'd/m/Y H:i:s');
 
-						$jobManager->RemoveLabelsFromJobs($label->id);
+						$this->jobManager->RemoveLabelsFromJobs($label->id);
 
 						$asyncBlink = new AsyncJobClass($this->findBoxCore, $label->id, $label->params);
 						$asyncBlink->BlinkON();
-						$jobManager->add($asyncBlink);
+						$this->jobManager->add($asyncBlink);
 
 						unset($this->labelToBlink[$commandID]);
 
@@ -257,7 +273,7 @@ class FindBoxService
 			}
 
 			// Async check on JOBS
-			$jobManager->Run(function (AsyncJobClass $asyncBlink) {
+			$this->jobManager->Run(function (AsyncJobClass $asyncBlink) {
 
 				$err = $asyncBlink->GetCurrentValue();  // Fa scattare il generator e ricava il valore
 
@@ -284,31 +300,45 @@ class FindBoxService
 
 	private function getCurrentQueue()
 	{
-		if ($this->qs_Server->check()) {
-
-			/** @var QueueCommand $qCommand */
-			$qCommand = $this->qs_Server->getCommandObject();
-			switch ($qCommand->operation) {
-				case QueueCommand::$Q_SAVEPRODUCT:
-					$this->saveProduct($qCommand);
-					break;
-				case QueueCommand::$Q_BLINK_ON:
-				case QueueCommand::$Q_BLINK_OFF:
-					// Split blink list by 9 items each array
-					$labelsSplitted = array_chunk($qCommand->id, 9);
-					foreach($labelsSplitted as $labels) {
-						$cmd = clone $qCommand;
-						$cmd->id = $labels;
-						$this->labelToBlink[rand(0, 60000)] = $cmd;
-					}
-					break;
-				case QueueCommand::$Q_DISPLAY:
-					$this->ServiceLogger->write("Change display for label {$qCommand->id} requested", 'd/m/Y H:i:s');
-					$esl = new SmartESL($this->findBoxCore, $qCommand->id);
-					$esl->Display($qCommand->params['templateId'], $qCommand->params['barcode']);
-					break;
+		/** @var QueueCommand $qCommand */
+		$this->qs_Server->ServerCheckClientCommands(
+			function ($clientCommands) {
+				switch ($clientCommands->operation) {
+
+					case QueueCommand::$Q_GETJOBS:
+						return ['jobCount' => $this->jobManager->count()];
+						break;
+
+					case QueueCommand::$Q_CLEARQUEUE:
+						$this->ServiceLogger->write("Clear jobs requested", 'd/m/Y H:i:s');
+						$this->jobManager->Reset();
+						return ['jobClear' => true];
+						break;
+
+					case QueueCommand::$Q_SAVEPRODUCT:
+						$this->saveProduct($clientCommands);
+						break;
+
+					case QueueCommand::$Q_BLINK_ON:
+					case QueueCommand::$Q_BLINK_OFF:
+						// Split blink list by 9 items each array
+						$labelsSplitted = array_chunk($clientCommands->id, 9);
+						foreach($labelsSplitted as $labels) {
+							$cmd = clone $clientCommands;
+							$cmd->id = $labels;
+							$this->labelToBlink[rand(0, 60000)] = $cmd;
+						}
+						break;
+
+					case QueueCommand::$Q_DISPLAY:
+						$this->ServiceLogger->write("Change display for label {$clientCommands->id} requested", 'd/m/Y H:i:s');
+						$esl = new SmartESL($this->findBoxCore, $clientCommands->id);
+						$esl->Display($clientCommands->params['templateId'], $clientCommands->params['barcode']);
+						break;
+				}
+				return NULL;
 			}
-		}
+		);
 	}
 
 	/**

+ 14 - 2
src/OpenMind/FindBox/QueueCommand.php

@@ -14,6 +14,8 @@ class QueueCommand
 	static public $Q_BLINK_OFF = 2;
 	static public $Q_SAVEPRODUCT = 3;
 	static public $Q_DISPLAY = 4;
+	static public $Q_GETJOBS = 5;
+	static public $Q_CLEARQUEUE = 6;
 
 	public $id;
 	public $operation;
@@ -22,9 +24,9 @@ class QueueCommand
 
 	/**
 	 * QueueCommand constructor.
-	 * @param array $labels
+	 * @param array|null $labels
 	 */
-	public function __construct(array $labels)
+	public function __construct(array $labels = null)
 	{
 		$this->id = $labels;
 		$this->params = null;
@@ -100,4 +102,14 @@ class QueueCommand
 			$this->params['barcode']
 			) = func_get_args();
 	}
+
+	public function getJobs()
+	{
+		$this->operation = self::$Q_GETJOBS;
+	}
+
+	public function clearQueue()
+	{
+		$this->operation = self::$Q_CLEARQUEUE;
+	}
 }

+ 41 - 21
src/OpenMind/System/QueueSystem.php

@@ -13,7 +13,6 @@ class QueueSystem
 	// Used internally
 	private $sckFindBoxService;
 	private $serverPort;
-	private $clientRead;
 
 	public function __construct()
 	{
@@ -32,9 +31,10 @@ class QueueSystem
 	 * send() serialize $obj before send to queue
 	 *
 	 * @param $obj
-	 * @return bool true = connection successful
+	 * @param bool $waitResponse
+	 * @return true = connection successful else the JSON decoded server response
 	 */
-	public function send($obj)
+	public function send($obj, bool $waitResponse = false)
 	{
 		$serializedObject = serialize($obj);
 		$command = base64_encode($serializedObject);
@@ -44,37 +44,45 @@ class QueueSystem
 		if ($connectOK = @socket_connect($socket, 'localhost', $this->serverPort))
 			socket_write($socket, $command, strlen($command));
 
+		// TODO da testare
+		if ($waitResponse) {
+			$cmdResponse = "";
+			while ($resp = @socket_read($socket, 60 * 1024, PHP_BINARY_READ)) {
+				$cmdResponse .= $resp;
+			}
+			if ($cmdResponse)
+				$cmdResponse = json_decode($cmdResponse);
+		}
+
 		socket_close($socket);
 
-		return $connectOK;
-	}
-
-	/**
-	 * Retrieve and unserialize current object
-	 *
-	 * @return mixed
-	 */
-	public function getCommandObject()
-	{
-		$decodedObj = base64_decode($this->clientRead);
-		$obj = unserialize($decodedObj);
-		return $obj;
+		return ($waitResponse ? $cmdResponse : $connectOK);
 	}
 
 	/**
 	 * Retrieve messages
 	 *
-	 * @return array|FALSE
+	 * @param callable $callback (QueueCommand) possible return code:<BR/>
+	 *  <strong>NULL</strong>: do nothing<BR/>
+	 *  <strong>ARRAY</strong>: returning array, ServerCheckClientCommands will json_encode and send to client this data<BR/>
 	 */
-	public function check()
+	public function ServerCheckClientCommands(callable $callback): void
 	{
 		$client = $this->SocketClientAccept($this->sckFindBoxService);
 		if ($client) {
-			$this->clientRead = socket_read($client, 1000);
+			$clientRead = "";
+
+			while ($resp = socket_read($client, 60 * 1024, PHP_BINARY_READ))
+				$clientRead .= $resp;
+
+			$dataClient = $this->decodeClientData($clientRead);
+
+			$serverResponse = $callback($dataClient);
+			if ($serverResponse !== NULL)
+				socket_write($client, json_encode($serverResponse));
+
 			socket_close($client);
-			return true;
 		}
-		return false;
 	}
 
 	/**
@@ -96,4 +104,16 @@ class QueueSystem
 		return socket_accept($whichSocket);
 	}
 
+	/**
+	 * Retrieve and unserialize current object
+	 *
+	 * @return mixed
+	 */
+	private function decodeClientData($clientData)
+	{
+		$decodedObj = base64_decode($clientData);
+		$obj = unserialize($decodedObj);
+		return $obj;
+	}
+
 }