Merge branch 'feature/RaspberryDisplay' into 'master'
created RasberryDisplay Module See merge request fronk/thetool!245
This commit is contained in:
6
application/RaspberryDisplay/RaspberryDisplay.php
Normal file
6
application/RaspberryDisplay/RaspberryDisplay.php
Normal file
@@ -0,0 +1,6 @@
|
||||
<?php
|
||||
|
||||
class RaspberryDisplay extends mfBaseModel
|
||||
{
|
||||
|
||||
}
|
||||
130
application/RaspberryDisplay/RaspberryDisplayController.php
Normal file
130
application/RaspberryDisplay/RaspberryDisplayController.php
Normal file
@@ -0,0 +1,130 @@
|
||||
<?php
|
||||
|
||||
use phpseclib3\Net\SSH2;
|
||||
|
||||
class RaspberryDisplayController extends mfBaseController
|
||||
{
|
||||
private int $port = 22;
|
||||
private string $username = XINON_RASPBERRY_DISPLAY_SSH_USER;
|
||||
private string $password = XINON_RASPBERRY_DISPLAY_SSH_PASS;
|
||||
|
||||
protected function init(): void
|
||||
{
|
||||
$me = new User();
|
||||
$me->loadMe();
|
||||
$this->me = $me;
|
||||
$this->layout()->set("me", $me);
|
||||
}
|
||||
|
||||
protected function restartRaspberryPi($id) {
|
||||
$display = RaspberryDisplayModel::get($id);
|
||||
|
||||
$ssh = new SSH2($display->ip_address, $this->port);
|
||||
$ssh->login($this->username, $this->password);
|
||||
$ssh->exec('sudo reboot now');
|
||||
return true;
|
||||
}
|
||||
|
||||
protected function getDisplaysApi(): array
|
||||
{
|
||||
$displays = RaspberryDisplayModel::getAll();
|
||||
$result = [];
|
||||
foreach ($displays as $display) {
|
||||
$result[] = [
|
||||
"display_label" => $display->display_label,
|
||||
"hostname" => $display->hostname,
|
||||
"ip" => $display->ip_address,
|
||||
"display_url" => $display->display_url,
|
||||
"auto_refresh_enabled" => $display->auto_refresh_enabled === "1",
|
||||
"margin_hot_fix_enabled" => $display->margin_hot_fix_enabled === "1",
|
||||
"custom_style" => $display->custom_style,
|
||||
"id" => $display->id,
|
||||
];
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
protected function change()
|
||||
{
|
||||
$displayID = $this->request->displayID;
|
||||
$field = $this->request->field;
|
||||
$value = $this->request->value;
|
||||
$value = $value === "true" ? 1 : ($value === "false" ? 0 : $value);
|
||||
$display = RaspberryDisplayModel::get($displayID);
|
||||
if ($display === null) {
|
||||
return false;
|
||||
}
|
||||
$display->$field = $value;
|
||||
$display->save();
|
||||
return true;
|
||||
}
|
||||
|
||||
protected function getConfig() {
|
||||
$ip = $_SERVER['REMOTE_ADDR'];
|
||||
$hostname = $this->request->hostname;
|
||||
|
||||
$displays = RaspberryDisplayModel::getByHostnameAndIp($hostname, $ip);
|
||||
|
||||
if ($displays === null) {
|
||||
die("No display found for this hostname and ip:" . $hostname . " X " . $ip);
|
||||
}
|
||||
|
||||
return array_map(function ($display) {
|
||||
return [
|
||||
"display_url" => $display->data->display_url,
|
||||
"auto_refresh_enabled" => $display->data->auto_refresh_enabled === "1",
|
||||
"margin_hot_fix_enabled" => $display->data->margin_hot_fix_enabled === "1",
|
||||
"id" => $display->id,
|
||||
];
|
||||
}
|
||||
, $displays);
|
||||
}
|
||||
protected function apiAction() {
|
||||
$do = $this->request->do;
|
||||
|
||||
if (!$this->me->is("employee") && !in_array($do, ["getDisplays", "change", "reboot"])) {
|
||||
$this->redirect("dashboard");
|
||||
}
|
||||
|
||||
switch ($do) {
|
||||
case "getDisplays":
|
||||
$return = $this->getDisplaysApi();
|
||||
break;
|
||||
case "change":
|
||||
$return = $this->change();
|
||||
break;
|
||||
case "reboot":
|
||||
$return = $this->restartRaspberryPi($this->request->displayID);
|
||||
break;
|
||||
case "getConfig":
|
||||
$return = $this->getConfig();
|
||||
break;
|
||||
default:
|
||||
$return = false;
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
$data = [];
|
||||
|
||||
if ($return === true) {
|
||||
$data = ["status" => "success"];
|
||||
$this->returnJson($data);
|
||||
}
|
||||
|
||||
if(!is_array($return) || !count($return)) {
|
||||
$data = ["status" => "error"];
|
||||
$this->returnJson($data);
|
||||
}
|
||||
$data['status'] = "OK";
|
||||
$data['result'] = $return;
|
||||
$this->returnJson($data);
|
||||
}
|
||||
|
||||
protected function indexAction(): void
|
||||
{
|
||||
$this->layout()->setTemplate("RaspberryDisplay/Index");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
103
application/RaspberryDisplay/RaspberryDisplayModel.php
Normal file
103
application/RaspberryDisplay/RaspberryDisplayModel.php
Normal file
@@ -0,0 +1,103 @@
|
||||
<?php
|
||||
|
||||
class RaspberryDisplayModel
|
||||
{
|
||||
public $display_label;
|
||||
public $hostname;
|
||||
public $ip_address;
|
||||
public $display_url;
|
||||
public $auto_refresh_enabled;
|
||||
public $margin_hot_fix_enabled;
|
||||
|
||||
public function __construct($data = [])
|
||||
{
|
||||
foreach ($data as $field => $value) {
|
||||
if (property_exists(get_called_class(), $field)) {
|
||||
$this->$field = $value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static function get($id)
|
||||
{
|
||||
$db = FronkDB::singleton();
|
||||
|
||||
$res = $db->select("RaspberryDisplay", "*", "id = $id");
|
||||
if ($db->num_rows($res)) {
|
||||
return new RaspberryDisplay($db->fetch_object($res));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static function getByHostnameAndIp($hostname, $ip)
|
||||
{
|
||||
$db = FronkDB::singleton();
|
||||
|
||||
$res = $db->select("RaspberryDisplay", "*", "hostname = '$hostname' AND ip_address = '$ip'");
|
||||
//fetch 2 rows
|
||||
|
||||
if ($db->num_rows($res)) {
|
||||
while ($data = $db->fetch_object($res)) {
|
||||
$items[] = new RaspberryDisplay($data);
|
||||
}
|
||||
return $items;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static function create(array $data)
|
||||
{
|
||||
$model = new RaspberryDisplay();
|
||||
|
||||
foreach ($data as $field => $value) {
|
||||
if (property_exists(get_called_class(), $field)) {
|
||||
$model->$field = $value;
|
||||
}
|
||||
}
|
||||
|
||||
$me = new User();
|
||||
$me->loadMe();
|
||||
|
||||
if ($model->create_by === null) {
|
||||
$model->create_by = $me->id;
|
||||
}
|
||||
if ($model->edit_by === null) {
|
||||
$model->edit_by = $me->id;
|
||||
}
|
||||
|
||||
return $model;
|
||||
}
|
||||
|
||||
public static function getAll()
|
||||
{
|
||||
$items = [];
|
||||
|
||||
$db = FronkDB::singleton();
|
||||
|
||||
$res = $db->select("RaspberryDisplay", "id, display_label, hostname, ip_address,custom_style, display_url, auto_refresh_enabled, margin_hot_fix_enabled");
|
||||
if ($db->num_rows($res)) {
|
||||
while ($data = $db->fetch_object($res)) {
|
||||
$items[] = new RaspberryDisplay($data);
|
||||
}
|
||||
}
|
||||
return $items;
|
||||
|
||||
}
|
||||
|
||||
public static function save(RaspberryDisplay $model)
|
||||
{
|
||||
$db = FronkDB::singleton();
|
||||
|
||||
$data = $model->data;
|
||||
|
||||
if ($model->id) {
|
||||
$db->update("RaspberryDisplay", $data, "id=" . $model->id);
|
||||
} else {
|
||||
$model->create = date("U");
|
||||
$model->edit = date("U");
|
||||
$model->id = $db->insert("RaspberryDisplay", $data);
|
||||
}
|
||||
|
||||
return $model;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user