Make plugins list and switcher
This commit is contained in:
		@@ -19,6 +19,7 @@ $router->any('/logout', 'Logout');
 | 
			
		||||
$router->any('/automation', 'Automation');
 | 
			
		||||
$router->any('/setting', 'Setting');
 | 
			
		||||
$router->any('/device', 'Device');
 | 
			
		||||
$router->any('/plugins', 'Plugins');
 | 
			
		||||
$router->any('/ajax', 'Ajax');
 | 
			
		||||
$router->any('/oauth', 'Oauth');
 | 
			
		||||
 | 
			
		||||
@@ -28,12 +29,12 @@ $router->post('/api/logout', 'AuthApi@logout');
 | 
			
		||||
$router->get('/api/rooms', 'RoomsApi@default');
 | 
			
		||||
$router->get('/api/rooms/{roomId}/update', 'RoomsApi@update');
 | 
			
		||||
$router->get('/api/devices', 'DevicesApi@default');
 | 
			
		||||
$router->get('/api/plugins', 'PluginsApi@default');
 | 
			
		||||
$router->get('/api/users', 'UsersApi@default');
 | 
			
		||||
$router->get('/api/server', 'ServerApi@default');
 | 
			
		||||
$router->get('/api/server/log', 'ServerApi@logStatus');
 | 
			
		||||
$router->post('/api/widgets/{widgetId}/run', 'WidgetApi@run');
 | 
			
		||||
$router->get('/api/widgets/{widgetId}/detail', 'WidgetApi@detail');
 | 
			
		||||
$router->get('/adminer', 'WidgetApi@detail');
 | 
			
		||||
 | 
			
		||||
//cron
 | 
			
		||||
$router->post('/cron/clean', 'CronApi@clean');
 | 
			
		||||
 
 | 
			
		||||
@@ -22,19 +22,34 @@ class CronApi extends ApiController
 | 
			
		||||
		$dir = $_SERVER['DOCUMENT_ROOT'] . BASEDIR . 'app/plugins/';
 | 
			
		||||
		$pluginsFiles = array_diff(scandir($dir), ['..', '.']);
 | 
			
		||||
		foreach ($pluginsFiles as $key => $pluginFile) {
 | 
			
		||||
			$className = str_replace(".php", "", $pluginFile);
 | 
			
		||||
			if (strpos($pluginFile, '_') === true) {
 | 
			
		||||
				continue;
 | 
			
		||||
			}
 | 
			
		||||
			if (!class_exists($className)) {
 | 
			
		||||
				continue;
 | 
			
		||||
			}
 | 
			
		||||
			$pluginMakeClass = new $className;
 | 
			
		||||
			if (!method_exists($pluginMakeClass, 'make')) {
 | 
			
		||||
			if (strpos($pluginFile, "!") === false) {
 | 
			
		||||
				$className = str_replace(".php", "", $pluginFile);
 | 
			
		||||
				if (strpos($pluginFile, '_') === true) {
 | 
			
		||||
					continue;
 | 
			
		||||
				}
 | 
			
		||||
				if (!class_exists($className)) {
 | 
			
		||||
					continue;
 | 
			
		||||
				}
 | 
			
		||||
				$pluginMakeClass = new $className;
 | 
			
		||||
				if (!method_exists($pluginMakeClass, 'make')) {
 | 
			
		||||
 | 
			
		||||
				continue;
 | 
			
		||||
					continue;
 | 
			
		||||
				}
 | 
			
		||||
				$result[$className] = $pluginMakeClass->make();
 | 
			
		||||
			} else {
 | 
			
		||||
				$className = str_replace("!", "", str_replace(".php", "", $pluginFile));
 | 
			
		||||
				if (strpos($pluginFile, '_') === true) {
 | 
			
		||||
					continue;
 | 
			
		||||
				}
 | 
			
		||||
				if (!class_exists($className)) {
 | 
			
		||||
					continue;
 | 
			
		||||
				}
 | 
			
		||||
				$pluginMakeClass = new $className;
 | 
			
		||||
				if (!method_exists($pluginMakeClass, 'disable')) {
 | 
			
		||||
					continue;
 | 
			
		||||
				}
 | 
			
		||||
				$result[$className] = $pluginMakeClass->disable();
 | 
			
		||||
			}
 | 
			
		||||
			$result[$className] = $pluginMakeClass->make();
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		//Print Result
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										12
									
								
								app/api/PluginsApi.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								app/api/PluginsApi.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,12 @@
 | 
			
		||||
<?php
 | 
			
		||||
class PluginsApi extends ApiController{
 | 
			
		||||
 | 
			
		||||
	public function default(){
 | 
			
		||||
		$this->requireAuth();
 | 
			
		||||
		$response = [];
 | 
			
		||||
 | 
			
		||||
		// TODO: process the request
 | 
			
		||||
 | 
			
		||||
		$this->response($response);
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										16
									
								
								app/controllers/pluginsController.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								app/controllers/pluginsController.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,16 @@
 | 
			
		||||
<?php
 | 
			
		||||
if (!empty ($_POST)){
 | 
			
		||||
	if (
 | 
			
		||||
		isset($_POST['name']) &&
 | 
			
		||||
		$_POST['name'] != '' &&
 | 
			
		||||
		isset($_POST['actualStatus'])
 | 
			
		||||
	){
 | 
			
		||||
		if ($_POST['actualStatus']) {
 | 
			
		||||
			rename($_SERVER['DOCUMENT_ROOT'] . BASEDIR . 'app/plugins/' . $_POST['name'] . ".php", $_SERVER['DOCUMENT_ROOT'] . BASEDIR . 'app/plugins/!' . $_POST['name'] . ".php");
 | 
			
		||||
		} else {
 | 
			
		||||
			rename($_SERVER['DOCUMENT_ROOT'] . BASEDIR . 'app/plugins/!' . $_POST['name'] . ".php", $_SERVER['DOCUMENT_ROOT'] . BASEDIR . 'app/plugins/' . $_POST['name'] . ".php");
 | 
			
		||||
		}
 | 
			
		||||
		header('Location: ./plugins');
 | 
			
		||||
		die();
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										52
									
								
								app/plugins/!AirQuality.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										52
									
								
								app/plugins/!AirQuality.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,52 @@
 | 
			
		||||
<?php
 | 
			
		||||
class AirQuality extends VirtualDeviceManager
 | 
			
		||||
{
 | 
			
		||||
	private $city_sluig = "prague";
 | 
			
		||||
	private $app_id = "53ccbc353bb0bd0b05515169a593b96c38d57c48";
 | 
			
		||||
	private $api_uri = 'http://api.waqi.info/feed/%s/?token=%s'; // Your redirect uri
 | 
			
		||||
	private $virtual_device_name = "Air Quality";
 | 
			
		||||
	private $subdevice_type = "air-quality";
 | 
			
		||||
 | 
			
		||||
	function make()
 | 
			
		||||
	{
 | 
			
		||||
		try {
 | 
			
		||||
			if (DeviceManager::registeret($this->virtual_device_name)) {
 | 
			
		||||
				$deviceId = DeviceManager::getDeviceByToken($this->virtual_device_name)['device_id'];
 | 
			
		||||
				if (!$subDevice = SubDeviceManager::getSubDeviceByMaster($deviceId, $this->subdevice_type)) {
 | 
			
		||||
					SubDeviceManager::create($deviceId, $this->subdevice_type, '');
 | 
			
		||||
					sleep(1);
 | 
			
		||||
					$subDevice = SubDeviceManager::getSubDeviceByMaster($deviceId, strtolower($this->subdevice_type));
 | 
			
		||||
				}
 | 
			
		||||
 | 
			
		||||
				//if (!$this->fetchEnabled($deviceId,$subDevice['subdevice_id'])) die();
 | 
			
		||||
 | 
			
		||||
				$finalUrl = sprintf($this->api_uri, $this->city_sluig, $this->app_id);
 | 
			
		||||
				$json = json_decode(Utilities::CallAPI('GET', $finalUrl, ''), true);
 | 
			
		||||
				RecordManager::create($deviceId, $this->subdevice_type, $json['data']['aqi']);
 | 
			
		||||
			} else {
 | 
			
		||||
				DeviceManager::create($this->virtual_device_name, $this->virtual_device_name, 'senzore-virtual');
 | 
			
		||||
				DeviceManager::approved($this->virtual_device_name);
 | 
			
		||||
			}
 | 
			
		||||
			return 'sucessful';
 | 
			
		||||
		} catch(Exception $e) {
 | 
			
		||||
			return 'exception: ' . $e->getMessage();
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	function translate($value){
 | 
			
		||||
		if ($value < 50) {
 | 
			
		||||
			return 'Good';
 | 
			
		||||
		}  else if  ($value > 51 && $value < 100) {
 | 
			
		||||
			return 'Moderate';
 | 
			
		||||
		} else if ($value > 101 && $value < 150) {
 | 
			
		||||
			return 'Normal';
 | 
			
		||||
		} else if ($value > 151 && $value < 200) {
 | 
			
		||||
			return 'Unhealthy';
 | 
			
		||||
		} else if ($value > 201 && $value < 300) {
 | 
			
		||||
			return 'Very Unhealthy';
 | 
			
		||||
		} else if ($value > 301 ) {
 | 
			
		||||
			return 'Hazardous';
 | 
			
		||||
		}
 | 
			
		||||
		return '';
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
@@ -11,8 +11,8 @@ class Device extends Template
 | 
			
		||||
		$roomManager = new RoomManager ();
 | 
			
		||||
		$langMng = new LanguageManager ('en');
 | 
			
		||||
 | 
			
		||||
		if (!$userManager->isLogin ()) {
 | 
			
		||||
			header ('Location: ' . BASEURL . 'device');
 | 
			
		||||
		if (!$userManager->isLogin()){
 | 
			
		||||
			header('Location: ' . BASEURL . 'login');
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		$template = new Template ('device');
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										38
									
								
								app/views/Plugins.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										38
									
								
								app/views/Plugins.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,38 @@
 | 
			
		||||
<?php
 | 
			
		||||
class Plugins extends Template
 | 
			
		||||
{
 | 
			
		||||
	function __construct()
 | 
			
		||||
	{
 | 
			
		||||
		$userManager = new UserManager();
 | 
			
		||||
		$langMng = new LanguageManager('en');
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		if (!$userManager->isLogin()){
 | 
			
		||||
			header('Location: ' . BASEURL . 'login');
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		$dir = $_SERVER['DOCUMENT_ROOT'] . BASEDIR . 'app/plugins/';
 | 
			
		||||
		$pluginsFiles = array_diff(scandir($dir), ['..', '.']);
 | 
			
		||||
 | 
			
		||||
		$plugins = array();
 | 
			
		||||
 | 
			
		||||
		foreach ($pluginsFiles as $key => $pluginFile) {
 | 
			
		||||
			$status = (strpos($pluginFile, "!") !== false ? false : true);
 | 
			
		||||
			$plugins[$key]['name'] = str_replace("!", "", str_replace(".php", "", $pluginFile));
 | 
			
		||||
			$plugins[$key]['status'] = $status;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		sort($plugins);
 | 
			
		||||
 | 
			
		||||
		$template = new Template('plugins');
 | 
			
		||||
		$template->prepare('baseDir', BASEDIR);
 | 
			
		||||
		$template->prepare('debugMod', DEBUGMOD);
 | 
			
		||||
		$template->prepare('title', 'Plugins');
 | 
			
		||||
		$template->prepare('langMng', $langMng);
 | 
			
		||||
		$template->prepare('plugins', $plugins);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		$template->render();
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
@@ -6,6 +6,11 @@
 | 
			
		||||
			  'lngKey' => 'devices',
 | 
			
		||||
			  'path' => 'device',
 | 
			
		||||
		 ],
 | 
			
		||||
		 'fas fa-plug' => [
 | 
			
		||||
			 'slug' => 'plugins',
 | 
			
		||||
			 'lngKey' => 'plugins',
 | 
			
		||||
			 'path' => 'plugins',
 | 
			
		||||
		],
 | 
			
		||||
        'fa-wrench' => [
 | 
			
		||||
            'slug' => 'setting',
 | 
			
		||||
            'lngKey' => 'settings',
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										54
									
								
								app/views/templates/plugins.phtml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										54
									
								
								app/views/templates/plugins.phtml
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,54 @@
 | 
			
		||||
<!DOCTYPE html>
 | 
			
		||||
<html lang="en">
 | 
			
		||||
<head>
 | 
			
		||||
	<?php
 | 
			
		||||
	$partial = new Partial('head');
 | 
			
		||||
	$partial->prepare('baseDir', $BASEDIR);
 | 
			
		||||
	$partial->render();
 | 
			
		||||
	?>
 | 
			
		||||
	<title><?php echo $TITLE ?></title>
 | 
			
		||||
</head>
 | 
			
		||||
<body class="no-transitions">
 | 
			
		||||
	<div class="row no-gutters main">
 | 
			
		||||
		<div class="col-md-3 d-sm-none"></div>
 | 
			
		||||
		<div class="col-md-3 nav-container">
 | 
			
		||||
			<?php
 | 
			
		||||
			$partial = new Partial('menu');
 | 
			
		||||
			$partial->prepare('item', 'plugins');
 | 
			
		||||
			$partial->prepare('langMng',$LANGMNG);
 | 
			
		||||
			$partial->prepare('debugMod',$DEBUGMOD);
 | 
			
		||||
			$partial->render();
 | 
			
		||||
			?>
 | 
			
		||||
		</div>
 | 
			
		||||
		<div class="col-md-9 main-body">
 | 
			
		||||
			<div class="col-12 col-sm-9 mx-auto mt-4">
 | 
			
		||||
				<h1><?php echo $LANGMNG->get('t_Plugins'); ?></h1>
 | 
			
		||||
 | 
			
		||||
				<div class="row mb-4">
 | 
			
		||||
					<?php if ($PLUGINS): ?>
 | 
			
		||||
						<?php foreach ($PLUGINS as $plugin): ?>
 | 
			
		||||
							<div class="content">
 | 
			
		||||
								<form method="post" action="">
 | 
			
		||||
									<input type="hidden" name="name" value="<?php echo $plugin['name']; ?>"/>
 | 
			
		||||
									<input type="hidden" name="actualStatus" value="<?php echo $plugin['status']; ?>"/>
 | 
			
		||||
									<a onclick="$(this).closest('form').submit();">
 | 
			
		||||
										<div class="panel box content <?php echo ($plugin['status'] ? '' : 'disabled '); ?>p-4">
 | 
			
		||||
											<h5><?php echo $plugin['name']; ?></h5>
 | 
			
		||||
										</div>
 | 
			
		||||
									</a>
 | 
			
		||||
								</form>
 | 
			
		||||
							</div>
 | 
			
		||||
						<?php endforeach; ?>
 | 
			
		||||
					<?php endif; ?>
 | 
			
		||||
				</div>
 | 
			
		||||
			</div>
 | 
			
		||||
		</div>
 | 
			
		||||
 | 
			
		||||
		<?php
 | 
			
		||||
		$partial = new Partial('footer');
 | 
			
		||||
		$partial->prepare('baseDir', BASEDIR);
 | 
			
		||||
		$partial->render();
 | 
			
		||||
		//TODO js do main.js
 | 
			
		||||
		?>
 | 
			
		||||
	</body>
 | 
			
		||||
	</html>
 | 
			
		||||
@@ -8,6 +8,7 @@ return $lang = [
 | 
			
		||||
	'm_scenes' => 'Scény',
 | 
			
		||||
	'm_log' => 'Log',
 | 
			
		||||
	'm_devices' => 'Zařízení',
 | 
			
		||||
	'm_plugins' => 'Pluginy',
 | 
			
		||||
 | 
			
		||||
	//Buttons
 | 
			
		||||
	'b_year' => 'Rok',
 | 
			
		||||
@@ -88,6 +89,7 @@ return $lang = [
 | 
			
		||||
	't_listRooms' => 'Seznam Místností',
 | 
			
		||||
	't_roomName' => 'Jméno Místnosti',
 | 
			
		||||
	't_createRoom' => 'Vytvořit Místnost',
 | 
			
		||||
	't_Plugins' => 'Pluginy',
 | 
			
		||||
 | 
			
		||||
	//constants
 | 
			
		||||
	'temp' => 'Teplota',
 | 
			
		||||
 
 | 
			
		||||
@@ -8,6 +8,7 @@ return $lang = [
 | 
			
		||||
	'm_scenes' => 'Scenes',
 | 
			
		||||
	'm_log' => 'Log',
 | 
			
		||||
	'm_devices' => 'Devices',
 | 
			
		||||
	'm_plugins' => 'Plugins',
 | 
			
		||||
 | 
			
		||||
	//Buttons
 | 
			
		||||
	'b_year' => 'Year',
 | 
			
		||||
@@ -88,6 +89,7 @@ return $lang = [
 | 
			
		||||
	't_listRooms' => 'Room List',
 | 
			
		||||
	't_roomName' => 'Room Name',
 | 
			
		||||
	't_createRoom' => 'Create Rom',
 | 
			
		||||
	't_Plugins' => 'Plugins',
 | 
			
		||||
 | 
			
		||||
	//constants
 | 
			
		||||
	'humi' => 'Humidity',
 | 
			
		||||
 
 | 
			
		||||
@@ -453,6 +453,33 @@ html {
 | 
			
		||||
	color: #735b26;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  .box {
 | 
			
		||||
	border: 4px solid #43a329;
 | 
			
		||||
	background-color: #d4f6cb;
 | 
			
		||||
	color: #43a329;
 | 
			
		||||
	padding: .75rem 1rem;
 | 
			
		||||
	margin-bottom: 1rem;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  .box h5 {
 | 
			
		||||
	color: #43a329;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  .box.disabled {
 | 
			
		||||
   border: 4px solid #a32929;
 | 
			
		||||
   background-color: #f6cbcb;
 | 
			
		||||
	color: #a32929;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  .box.disabled h5 {
 | 
			
		||||
	color: #a32929;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  .content {
 | 
			
		||||
	 width: fit-content;
 | 
			
		||||
	 margin: 5px;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  .button {
 | 
			
		||||
	background-color: #d4def7;
 | 
			
		||||
	border: 0;
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										64
									
								
								serviceWorker.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										64
									
								
								serviceWorker.js
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,64 @@
 | 
			
		||||
importScripts('https://www.gstatic.com/firebasejs/7.1.0/firebase-app.js');
 | 
			
		||||
importScripts('https://www.gstatic.com/firebasejs/7.1.0/firebase-messaging.js');
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
* Cache version, change name to force reload
 | 
			
		||||
*/
 | 
			
		||||
var CACHE_VERSION = 'v1';
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
* Stuff to put in the cache at install
 | 
			
		||||
*/
 | 
			
		||||
var CACHE_FILES  = [
 | 
			
		||||
    'templates/automatio.phtml',
 | 
			
		||||
    'templates/dashboard.phtml',
 | 
			
		||||
    'templates/home.phtml',
 | 
			
		||||
    'templates/login.phtml',
 | 
			
		||||
    'templates/scene.phtml',
 | 
			
		||||
    'templates/setting.phtml',
 | 
			
		||||
    'views/Automation.phtml',
 | 
			
		||||
    'views/Dashboard.phtml',
 | 
			
		||||
    'views/Home.phtml',
 | 
			
		||||
    'views/Login.phtml',
 | 
			
		||||
    'views/Scene.phtml',
 | 
			
		||||
    'views/Setting.phtml',
 | 
			
		||||
    'assets/logo.svg'
 | 
			
		||||
];
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
self.addEventListener('install', function(event) {
 | 
			
		||||
    console.info('Installed');
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
self.addEventListener('push', function(event) {  
 | 
			
		||||
    console.log('Received a push message', event);
 | 
			
		||||
    if (event && event.data) {
 | 
			
		||||
        var data = event.data.json();
 | 
			
		||||
        data = JSON.parse(data.data.notification);
 | 
			
		||||
        console.log(data);
 | 
			
		||||
        event.waitUntil(self.registration.showNotification(data.title, {
 | 
			
		||||
            body: data.body,
 | 
			
		||||
            icon: data.icon || null
 | 
			
		||||
        }));
 | 
			
		||||
    }
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
self.addEventListener('sync', function(event) {
 | 
			
		||||
    console.info('Event: Sync');
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
self.addEventListener('fetch', function (event) {
 | 
			
		||||
    
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
self.addEventListener("online", function (event) {
 | 
			
		||||
    
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
self.addEventListener("offline", function (event) {
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
self.addEventListener('notificationclick', function(e) {
 | 
			
		||||
    
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user