From 7d839787bdaae42f5b251b60c5d86d00520482e9 Mon Sep 17 00:00:00 2001 From: Kris Moore Date: Wed, 20 Jan 2016 12:13:25 -0500 Subject: [PATCH 1/4] Add first API call for the "systeminfo" class This API call will return a list of external mounts on the system TYPE: UNKNOWN/USB/HDRIVE/DVD/SDCARD REST Request: ------------------------------- PUT /sysadm/systeminfo { "action" : "externalmounts" } REST Response: ------------------------------- { "args": { "externalmounts": { "/dev/fuse": { "filesystem": "fusefs", "path": "/usr/home/kris/.gvfs", "type": "UNKNOWN" } } } } WebSocket Request: ------------------------------- { "id" : "fooid", "namespace" : "sysadm", "name" : "systeminfo", "args" : { "action" : "externalmounts" } } WebSocket Response: ------------------------------- { "args": { "externalmounts": { "/dev/fuse": { "filesystem": "fusefs", "path": "/usr/home/kris/.gvfs", "type": "UNKNOWN" } } }, "id": "fooid", "name": "response", "namespace": "sysadm" } --- .gitignore | 2 ++ src/library/library.pro | 2 ++ src/library/sysadm-systeminfo.cpp | 52 +++++++++++++++++++++++++++++++ src/library/sysadm-systeminfo.h | 22 +++++++++++++ src/server/WebBackend.cpp | 27 ++++++++++++++++ src/server/WebSocket.h | 2 ++ 6 files changed, 107 insertions(+) create mode 100644 src/library/sysadm-systeminfo.cpp create mode 100644 src/library/sysadm-systeminfo.h diff --git a/.gitignore b/.gitignore index f43bafc..348226d 100644 --- a/.gitignore +++ b/.gitignore @@ -12,6 +12,8 @@ src/library/sysadm-general.o src/library/sysadm-lifepreserver.o src/library/sysadm-network.o src/library/sysadm-firewall.o +src/library/sysadm-servicemanager.o +src/library/sysadm-systeminfo.o src/library/sysadm-update.o src/library/sysadm-usermanager.o src/server/AuthorizationManager.o diff --git a/src/library/library.pro b/src/library/library.pro index 61c4696..b0c650e 100644 --- a/src/library/library.pro +++ b/src/library/library.pro @@ -16,6 +16,7 @@ HEADERS += sysadm-global.h \ sysadm-network.h \ sysadm-firewall.h \ sysadm-servicemanager.h\ + sysadm-systeminfo.h\ sysadm-update.h \ sysadm-usermanager.h @@ -25,6 +26,7 @@ SOURCES += NetDevice.cpp \ sysadm-network.cpp \ sysadm-firewall.cpp \ sysadm-servicemanager.cpp \ + sysadm-systeminfo.cpp \ sysadm-update.cpp \ sysadm-usermanager.cpp diff --git a/src/library/sysadm-systeminfo.cpp b/src/library/sysadm-systeminfo.cpp new file mode 100644 index 0000000..b0ae88d --- /dev/null +++ b/src/library/sysadm-systeminfo.cpp @@ -0,0 +1,52 @@ +//=========================================== +// PC-BSD source code +// Copyright (c) 2015, PC-BSD Software/iXsystems +// Available under the 3-clause BSD license +// See the LICENSE file for full details +//=========================================== +#include "sysadm-general.h" +#include "sysadm-systeminfo.h" +#include "sysadm-global.h" + +using namespace sysadm; + +//PLEASE: Keep the functions in the same order as listed in pcbsd-general.h + +// ==== ExternalDevicePaths() ==== +QJsonObject SysInfo::ExternalDevicePaths() { + QJsonObject retObject; + + //Returns: QStringList[::::::::] + //Note: = [USB, HDRIVE, DVD, SDCARD, UNKNOWN] + QStringList devs = General::RunCommand("mount").split("\n"); + + //Now check the output + for(int i=0; i +#include "sysadm-global.h" + +namespace sysadm{ + +class SysInfo{ +public: + static QJsonObject ExternalDevicePaths(); +}; + +} //end of pcbsd namespace + +#endif diff --git a/src/server/WebBackend.cpp b/src/server/WebBackend.cpp index 5f3c058..6b28d2e 100644 --- a/src/server/WebBackend.cpp +++ b/src/server/WebBackend.cpp @@ -12,6 +12,7 @@ #include "sysadm-general.h" #include "sysadm-lifepreserver.h" #include "sysadm-network.h" +#include "sysadm-systeminfo.h" #include "sysadm-update.h" #include "syscache-client.h" @@ -67,6 +68,8 @@ RestOutputStruct::ExitCode WebSocket::EvaluateBackendRequest(const RestInputStru return EvaluateSysadmNetworkRequest(IN.args, out); }else if(namesp=="sysadm" && name=="lifepreserver"){ return EvaluateSysadmLifePreserverRequest(IN.args, out); + }else if(namesp=="sysadm" && name=="systeminfo"){ + return EvaluateSysadmSystemInfoRequest(IN.args, out); }else if(namesp=="sysadm" && name=="update"){ return EvaluateSysadmUpdateRequest(IN.args, out); }else{ @@ -245,6 +248,30 @@ RestOutputStruct::ExitCode WebSocket::EvaluateSysadmLifePreserverRequest(const Q return RestOutputStruct::OK; } +//==== SYSADM -- SysInfo ==== +RestOutputStruct::ExitCode WebSocket::EvaluateSysadmSystemInfoRequest(const QJsonValue in_args, QJsonObject *out){ + if(in_args.isObject()){ + QStringList keys = in_args.toObject().keys(); + bool ok = false; + if(keys.contains("action")){ + QString act = JsonValueToString(in_args.toObject().value("action")); + if(act=="externalmounts"){ + ok = true; + out->insert("externalmounts", sysadm::SysInfo::ExternalDevicePaths()); + } + + } //end of "action" key usage + + //If nothing done - return the proper code + if(!ok){ + return RestOutputStruct::BADREQUEST; + } + }else{ // if(in_args.isArray()){ + return RestOutputStruct::BADREQUEST; + } + return RestOutputStruct::OK; +} + //==== SYSADM -- Update ==== RestOutputStruct::ExitCode WebSocket::EvaluateSysadmUpdateRequest(const QJsonValue in_args, QJsonObject *out){ if(in_args.isObject()){ diff --git a/src/server/WebSocket.h b/src/server/WebSocket.h index a4a45ae..7ab88e8 100644 --- a/src/server/WebSocket.h +++ b/src/server/WebSocket.h @@ -50,6 +50,8 @@ private: RestOutputStruct::ExitCode EvaluateSysadmNetworkRequest(const QJsonValue in_args, QJsonObject *out); // -- sysadm LifePreserver API RestOutputStruct::ExitCode EvaluateSysadmLifePreserverRequest(const QJsonValue in_args, QJsonObject *out); + // -- sysadm info API + RestOutputStruct::ExitCode EvaluateSysadmSystemInfoRequest(const QJsonValue in_args, QJsonObject *out); // -- sysadm Update API RestOutputStruct::ExitCode EvaluateSysadmUpdateRequest(const QJsonValue in_args, QJsonObject *out); From 783bca9dbf221320e2917f3fd6e99e27b9044f53 Mon Sep 17 00:00:00 2001 From: dlavigne Date: Wed, 20 Jan 2016 13:08:51 -0500 Subject: [PATCH 2/4] Doc new systeminfo class. --- api/classes/systeminfo.rst | 92 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 api/classes/systeminfo.rst diff --git a/api/classes/systeminfo.rst b/api/classes/systeminfo.rst new file mode 100644 index 0000000..ad03da4 --- /dev/null +++ b/api/classes/systeminfo.rst @@ -0,0 +1,92 @@ +.. _systeminfo: + +systeminfo +********** + +The systeminfo class is used to retrieve information about the system. Every systeminfo class request contains the following parameters: + ++---------------------------------+---------------+----------------------------------------------------------------------------------------------------------------------+ +| **Parameter** | **Value** | **Description** | +| | | | ++=================================+===============+======================================================================================================================+ +| id | | any unique value for the request; examples include a hash, checksum, or uuid | +| | | | ++---------------------------------+---------------+----------------------------------------------------------------------------------------------------------------------+ +| name | systeminfo | | +| | | | ++---------------------------------+---------------+----------------------------------------------------------------------------------------------------------------------+ +| namespace | sysadm | | +| | | | ++---------------------------------+---------------+----------------------------------------------------------------------------------------------------------------------+ +| action | | supported actions include "externalmounts" | +| | | | ++---------------------------------+---------------+----------------------------------------------------------------------------------------------------------------------+ + +The rest of this section provides examples of the available *actions* for each type of request, along with their responses. + +.. index:: externalmounts, systeminfo + +.. _List External Mounts: + +List External Mounts +==================== + +The "externalmounts" action returns a list of mounted external devices. Supported device types are UNKNOWN, USB, HDRIVE (external hard drive), DVD, and SDCARD. +For each mounted device, the response will include the device name, filesystem, mount path, and device type. + +**REST Request** + +.. code-block:: json + + PUT /sysadm/systeminfo + { + "action" : "externalmounts" + } + +**REST Response** + +.. code-block:: json + + { + "args": { + "externalmounts": { + "/dev/fuse": { + "filesystem": "fusefs", + "path": "/usr/home/kris/.gvfs", + "type": "UNKNOWN" + } + } + } + } + +**WebSocket Request** + +.. code-block:: json + + { + "id" : "fooid", + "namespace" : "sysadm", + "name" : "systeminfo", + "args" : { + "action" : "externalmounts" + } + } + +**WebSocket Response** + +.. code-block:: json + + { + "args": { + "externalmounts": { + "/dev/fuse": { + "filesystem": "fusefs", + "path": "/usr/home/kris/.gvfs", + "type": "UNKNOWN" + } + } + }, + "id": "fooid", + "name": "response", + "namespace": "sysadm" + } \ No newline at end of file From 58e7e7a4e375a5c167b9a444d3f500604c18cc79 Mon Sep 17 00:00:00 2001 From: Kris Moore Date: Wed, 20 Jan 2016 13:26:38 -0500 Subject: [PATCH 3/4] Add new API call for batteryinfo Will return battery:"true/false" if one exists If one exists, it will also return "level":"1-99" and "status":"offline/charging/backup/unknown" REST Request: ------------------------------- PUT /sysadm/systeminfo { "action" : "batteryinfo" } REST Response: ------------------------------- { "args": { "batteryinfo": { "battery": "false" } } } WebSocket Request: ------------------------------- { "namespace" : "sysadm", "name" : "systeminfo", "id" : "fooid", "args" : { "action" : "batteryinfo" } } WebSocket Response: ------------------------------- { "args": { "batteryinfo": { "battery": "false" } }, "id": "fooid", "name": "response", "namespace": "sysadm" } --- src/library/sysadm-systeminfo.cpp | 36 ++++++++++++++++++++++++++++++- src/library/sysadm-systeminfo.h | 3 ++- src/server/WebBackend.cpp | 6 +++++- 3 files changed, 42 insertions(+), 3 deletions(-) diff --git a/src/library/sysadm-systeminfo.cpp b/src/library/sysadm-systeminfo.cpp index b0ae88d..c6d14de 100644 --- a/src/library/sysadm-systeminfo.cpp +++ b/src/library/sysadm-systeminfo.cpp @@ -12,8 +12,42 @@ using namespace sysadm; //PLEASE: Keep the functions in the same order as listed in pcbsd-general.h + +//Battery Availability +QJsonObject SysInfo::batteryInfo(){ + QJsonObject retObject; + bool ok; + + int val = General::RunCommand("apm -l").toInt(&ok); + if ( ok && (val >= 0 && val <= 100) ) { + retObject.insert("battery", "true"); + } else { + retObject.insert("battery", "false"); + return retObject; + } + + // We have a battery, return info about it + //Battery Charge Level + QString tmp; + tmp.setNum(val); + retObject.insert("level", tmp); + + //Battery Charging State + int state = General::RunCommand("apm -a").toInt(&ok); + if ( ok && state == 0 ) + retObject.insert("status", "offline"); + else if ( ok && state == 1 ) + retObject.insert("status", "charging"); + else if ( ok && state == 2 ) + retObject.insert("status", "backup"); + else + retObject.insert("status", "unknown"); + + return retObject; +} + // ==== ExternalDevicePaths() ==== -QJsonObject SysInfo::ExternalDevicePaths() { +QJsonObject SysInfo::externalDevicePaths() { QJsonObject retObject; //Returns: QStringList[::::::::] diff --git a/src/library/sysadm-systeminfo.h b/src/library/sysadm-systeminfo.h index 47322bc..b090134 100644 --- a/src/library/sysadm-systeminfo.h +++ b/src/library/sysadm-systeminfo.h @@ -14,7 +14,8 @@ namespace sysadm{ class SysInfo{ public: - static QJsonObject ExternalDevicePaths(); + static QJsonObject batteryInfo(); + static QJsonObject externalDevicePaths(); }; } //end of pcbsd namespace diff --git a/src/server/WebBackend.cpp b/src/server/WebBackend.cpp index 6b28d2e..70f344c 100644 --- a/src/server/WebBackend.cpp +++ b/src/server/WebBackend.cpp @@ -257,7 +257,11 @@ RestOutputStruct::ExitCode WebSocket::EvaluateSysadmSystemInfoRequest(const QJso QString act = JsonValueToString(in_args.toObject().value("action")); if(act=="externalmounts"){ ok = true; - out->insert("externalmounts", sysadm::SysInfo::ExternalDevicePaths()); + out->insert("externalmounts", sysadm::SysInfo::externalDevicePaths()); + } + if(act=="batteryinfo"){ + ok = true; + out->insert("batteryinfo", sysadm::SysInfo::batteryInfo()); } } //end of "action" key usage From 9b065a19376ab8f92b6dc4b56c35d19eb6279efb Mon Sep 17 00:00:00 2001 From: dlavigne Date: Wed, 20 Jan 2016 13:57:07 -0500 Subject: [PATCH 4/4] Doc batteryinfo action. --- api/classes/systeminfo.rst | 61 +++++++++++++++++++++++++++++++++++++- 1 file changed, 60 insertions(+), 1 deletion(-) diff --git a/api/classes/systeminfo.rst b/api/classes/systeminfo.rst index ad03da4..c6cd0e2 100644 --- a/api/classes/systeminfo.rst +++ b/api/classes/systeminfo.rst @@ -18,12 +18,71 @@ The systeminfo class is used to retrieve information about the system. Every sys | namespace | sysadm | | | | | | +---------------------------------+---------------+----------------------------------------------------------------------------------------------------------------------+ -| action | | supported actions include "externalmounts" | +| action | | supported actions include "batteryinfo", "externalmounts" | | | | | +---------------------------------+---------------+----------------------------------------------------------------------------------------------------------------------+ The rest of this section provides examples of the available *actions* for each type of request, along with their responses. +.. index:: batteryinfo, systeminfo + +.. _Battery Information: + +Battery Information +=================== + +The "batteryinfo" action will indicate whether or not a battery exists. If it does, it will also report its current charge percentage level (1-99) and its +status (offline, charging, on backup, or unknown). + +**REST Request** + +.. code-block:: json + + PUT /sysadm/systeminfo + { + "action" : "batteryinfo" + } + +**REST Response** + +.. code-block:: json + + { + "args": { + "batteryinfo": { + "battery": "false" + } + } + } + +**WebSocket Request** + +.. code-block:: json + + { + "namespace" : "sysadm", + "name" : "systeminfo", + "id" : "fooid", + "args" : { + "action" : "batteryinfo" + } + } + +**WebSocket Response** + +.. code-block:: json + + { + "args": { + "batteryinfo": { + "battery": "false" + } + }, + "id": "fooid", + "name": "response", + "namespace": "sysadm" + } + .. index:: externalmounts, systeminfo .. _List External Mounts: