Browse Source

NEW: RESTful module

Giancarlo Voci 7 years ago
parent
commit
ff310bb2a3
2 changed files with 218 additions and 0 deletions
  1. 212 0
      src/Modules/restful/restful.php
  2. 6 0
      src/Modules/restful/restful.tpl

+ 212 - 0
src/Modules/restful/restful.php

@@ -0,0 +1,212 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: Giancarlo
+ * Date: 11/10/2018
+ * Time: 14:57
+ */
+
+namespace Modules\restful;
+
+use Base;
+use DB\SQL;
+use OpenMind\FindBox\FindBoxCoreSystem;
+use OpenMind\FindBox\QueueCommand;
+use OpenMind\FindBox\SmartESL;
+use OpenMind\System\PluginStrategy;
+use OpenMind\System\QueueSystem;
+use Template;
+
+class restful extends PluginStrategy
+{
+	/** @var FindBoxCoreSystem $findBoxCore */
+	private $findBoxCore;
+
+	public function __construct()
+	{
+		global $f3;
+
+		$this->AddRoute("GET /api.ws", __NAMESPACE__ . '\restful->RESTful_ListAPIS');
+
+		$this->AddRoute("POST /api.ws.bind/@labelid", __NAMESPACE__ . '\restful->RESTful_bind');
+		$this->AddRoute("POST /api.ws.blinkon/@labelid/@color", __NAMESPACE__ . '\restful->RESTful_blinkon');
+		$this->AddRoute("POST /api.ws.blinkoff/@labelid", __NAMESPACE__ . '\restful->RESTful_blinkoff');
+		$this->AddRoute("POST /api.ws.setdisplay/@labelid/@templateid", __NAMESPACE__ . '\restful->RESTful_SetDisplayTemplate');
+		$this->AddRoute("POST /api.ws.saveproduct/@labelid", __NAMESPACE__ . '\restful->RESTful_SaveProduct');
+		$this->AddRoute("POST /api.ws.getstatus/@labelid", __NAMESPACE__ . '\restful->RESTful_GetStatus');
+		$this->AddRoute("POST /api.ws.getlabelsfromap/@ap_id", __NAMESPACE__ . '\restful->RESTful_GetLabelsFromAP');
+
+		$this->findBoxCore = new FindBoxCoreSystem(...array_values($f3->get('CORE.fb')));
+	}
+
+	public function RESTful_ListAPIS($f3)
+	{
+		$apiList = array_filter(array_keys($f3->ROUTES), function ($item) {
+			return (strpos($item, '/api.ws.') === 0);
+		});
+
+		$f3->APILIST = $apiList;
+		$f3->SITENAME = 'OM-LINK API List';
+		$f3->CONTENT = '../../src/Modules/restful/restful.tpl';
+		echo Template::instance()->render('masterpage.tpl');
+	}
+
+	public function RESTful_blinkon($f3, $args)
+	{
+		$msgBlink = new QueueCommand($args['labelid']);
+
+		$msgBlink->setLeds(2, 1, 500, 10, $args['color']);
+
+		$qm = new QueueSystem();
+		$qm->send($msgBlink);
+
+		global $frameworkUtils;
+		$frameworkUtils->getLogger()->write(__FUNCTION__ . " request blink for label {$args['labelid']}");
+	}
+
+	public function RESTful_blinkoff($f3, $args)
+	{
+		$msgBlink = new QueueCommand($args['labelid']);
+
+		$msgBlink->setToBlinkOff();
+
+		$qm = new QueueSystem();
+		$qm->send($msgBlink);
+
+		global $frameworkUtils;
+		$frameworkUtils->getLogger()->write(__FUNCTION__ . " request blink off for label {$args['labelid']}");
+	}
+
+	public function RESTful_SetDisplayTemplate($f3, $args)
+	{
+		$msgBlink = new QueueCommand($args['labelid']);
+
+		$msgBlink->setDisplay($args['templateid'], $args['labelid']);
+
+		$qm = new QueueSystem();
+		$qm->send($msgBlink);
+
+		global $frameworkUtils;
+		$frameworkUtils->getLogger()->write(__FUNCTION__ . " request set display to default for label {$args['labelid']}");
+	}
+
+	/**
+	 * @param Base $f3
+	 * @param $args
+	 */
+	public function RESTful_SaveProduct($f3, $args)
+	{
+		global $frameworkUtils;
+
+		$post = $f3->get('BODY');
+
+		if (empty($post)) {
+			echo $this->returnCode(1, 'POST data format incorrect');
+			return;
+		}
+
+		$dataFields = json_decode($post, true);
+
+		if ($dataFields == null) {
+			echo $this->returnCode(1, 'POST data format incorrect');
+			return;
+		}
+
+		$msgSave = new QueueCommand($args['labelid']);
+		$msgSave->saveProducts('', $dataFields);
+
+		$qm = new QueueSystem();
+		$qm->send($msgSave);
+
+		$frameworkUtils->getLogger()->write(__FUNCTION__ . " save product for label {$args['labelid']} fields: {$post}");
+
+		echo $this->returnCode(0, 'OK');
+	}
+
+	/**
+	 * This private function return the REST response code
+	 *
+	 * @param int $code
+	 * @param string $text
+	 * @return false|string
+	 */
+	private function returnCode(int $code, string $text)
+	{
+		return json_encode(["code" => $code, "text" => $text]);
+	}
+
+	public function RESTful_GetStatus($f3, $args)
+	{
+		$esl = new SmartESL($this->findBoxCore, [$args['labelid']]);
+		$status = $esl->GetStatus();
+
+		$labelStatus = $status[$args['labelid']];
+
+		echo json_encode($this->stripStatusFields($labelStatus));
+	}
+
+	/**
+	 * @param $status
+	 * @return array
+	 */
+	private function stripStatusFields($status): array
+	{
+		return [
+			'rssi' => $status['status']['rssi'],
+			'bat_level' => $status['status']['bat_level'],
+			'esl_state' => $status['data']['rssi'],
+			'esl_state_msg' => $status['data']['esl_state_msg'],
+			'rssi_ap' => $status['rssi']
+		];
+	}
+
+	public function RESTful_GetLabelsFromAP($f3, $args)
+	{
+		$esl = new SmartESL($this->findBoxCore, []);
+		$status = $esl->GetStatus();
+
+		if ($status == null) {
+			echo $this->returnCode(0, 'can\'t get label status = null');
+			return;
+		}
+
+		array_walk($status, function (&$item) use ($args) {
+			$rssi = array_intersect_key($item['rssi'], [$args['ap_id'] => '']);
+
+			$newLabelObj = $this->stripStatusFields($item);
+			$newLabelObj['rssi_ap'] = $rssi;
+
+			$item = $newLabelObj;
+		});
+
+		$result = array_filter($status, function ($item) use ($args) {
+			return isset($item['rssi_ap'][$args['ap_id']]);
+		});
+
+
+		echo $this->returnCode(0, json_encode($result));
+	}
+
+	/**
+	 * @param Base $f3
+	 * @param $args
+	 */
+	public function RESTful_bind($f3, $args)
+	{
+		global $frameworkUtils;
+
+		$this->findBoxCore->Bind($args['labelid'], $args['labelid']);
+
+		$frameworkUtils->getLogger()->write(__FUNCTION__ . " label {$args['labelid']} binded");
+
+		/** @var SQL $db */
+		$db = $f3->get('DB');
+
+		$exists = $db->exec("SELECT label_id FROM label_bindings WHERE `label_id`='{$args['label_id']}' LIMIT 1");
+
+		if (count($exists) == 0)
+			$db->exec("INSERT INTO label_bindings(label_id, barcode) VALUES('{$args['labelid']}', '{$args['labelid']}')");
+
+		echo $this->returnCode(0, 'OK');
+	}
+}

+ 6 - 0
src/Modules/restful/restful.tpl

@@ -0,0 +1,6 @@
+<ul class="list-group">
+	<li class="list-group-item active text-center font-weight-bold">API List</li>
+	<repeat group="{{@APILIST}}" value="{{@API}}">
+		<li class="list-group-item">{{@API}}</li>
+	</repeat>
+</ul>