Skip to content

Commit

Permalink
Improve logging; fix settings storage
Browse files Browse the repository at this point in the history
  • Loading branch information
kenda committed Dec 23, 2023
1 parent 9155c32 commit 6d44aec
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 23 deletions.
4 changes: 2 additions & 2 deletions js/openhab-admin.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/openhab-admin.js.map

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions lib/Controller/PageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function index() {
* @NoAdminRequired
*/
public function getSitemaps() {
return $this->apiService->sendRequest('/rest/sitemaps');
return $this->apiService->sendRequest('/rest/sitemaps', 'Get sitemaps for overview');
}

/**
Expand All @@ -47,7 +47,7 @@ public function getSitemaps() {
* @param string $id
*/
public function getSitemap(string $id) {
return $this->apiService->sendRequest('/rest/sitemaps/' . $id);
return $this->apiService->sendRequest('/rest/sitemaps/' . $id, 'Get config for sitemap "' . $id . '"' );
}

/**
Expand All @@ -56,6 +56,6 @@ public function getSitemap(string $id) {
* @param string $id
*/
public function getItemPersistence(string $item) {
return $this->apiService->sendRequest('/rest/persistence/items/' . $item);
return $this->apiService->sendRequest('/rest/persistence/items/' . $item, 'Get data for item "' . $item . '"');
}
}
45 changes: 32 additions & 13 deletions lib/Controller/SettingsController.php
Original file line number Diff line number Diff line change
@@ -1,38 +1,50 @@
<?php
namespace OCA\Openhab\Controller;

use OCA\Openhab\Services\ApiService;
use OCP\IRequest;
use OCP\IConfig;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Controller;
use Psr\Log\LoggerInterface;

class SettingsController extends Controller {

private $apiService;

private $config;

protected $logger;

private $CONFIG_KEYS = [
'server.type',
'server.url',
'server.username',
'server.password',
'server.ignoreSSL',
'server.interval',
];

public function __construct($appName,
ApiService $apiService,
IConfig $config,
IRequest $request){
IRequest $request,
LoggerInterface $logger){
parent::__construct($appName, $request);

$this->apiService = $apiService;

$this->config = $config;

$this->logger = $logger;
}

/**
* @NoAdminRequired
* @NoCSRFRequired
*/
public function index() {
$settings = [
'server.type',
'server.url',
'server.username',
'server.password',
'server.ignoreSSL',
'server.interval',
];

foreach ($settings as $setting) {
foreach ($this->CONFIG_KEYS as $setting) {
$result[$setting] = $this->config->getAppValue(
$this->appName,
$setting
Expand All @@ -45,7 +57,14 @@ public function index() {
/**
* @NoAdminRequired
*/
public function update() {
return $this->apiService->sendRequest('/rest/sitemaps');
public function update($settings) {
$this->logger->debug('Updating settings value', $settings);

foreach ($settings as $settingsKey => $value) {
$this->config->setAppValue($this->appName, $settingsKey, $value);
}

// send test request
return $this->apiService->sendRequest('/rest/sitemaps', 'Get sitemaps to test changed server settings');
}
}
29 changes: 26 additions & 3 deletions lib/Services/ApiService.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@

use OCP\IRequest;
use OCP\IConfig;
use Psr\Log\LoggerInterface;

class ApiService {

private $server;

protected $logger;

public function __construct($appName,
IConfig $config) {
IConfig $config,
LoggerInterface $logger) {
$this->logger = $logger;

$this->server = [
'url' => $config->getAppValue('openhab', 'server.url', 'http://localhost:8080'),
Expand All @@ -21,7 +26,7 @@ public function __construct($appName,
];
}

public function sendRequest(string $path) {
public function sendRequest(string $path, string $description) {
$ch = curl_init();

$host = $this->server['url'];
Expand All @@ -33,6 +38,7 @@ public function sendRequest(string $path) {
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Accept: application/json"
));
Expand All @@ -46,9 +52,26 @@ public function sendRequest(string $path) {
curl_setopt($ch, CURLOPT_USERPWD, $this->server['username'] . ':' . $this->server['password']);
}

$this->logger->debug($description . " - request send", curl_getinfo($ch));

$data = curl_exec($ch);
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$headers = substr($data, 0, $header_size);
$body = substr($data, $header_size);

$this->logger->debug($description . " - response headers", explode("\r\n", $headers));
if ($body != null) {
$this->logger->debug($description . " - response body", json_decode($body, true));
}

if (curl_errno($ch)) {
$this->logger->error($description . " - error while api request", [0 => curl_error($ch)]);
} else {
$this->logger->debug($description . " - request successful");
}

curl_close($ch);

return json_decode($data);
return json_decode($body);
}
}
4 changes: 3 additions & 1 deletion src/Settings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,9 @@ export default {
async submit() {
this.loading = true
try {
await axios.put(generateUrl('/apps/openhab/settings'), this.settings)
await axios.put(generateUrl('/apps/openhab/settings'), {
settings: this.settings,
})
this.success = true;
} catch (e) {
showError(this.t('openhab', 'Failed to save settings'))
Expand Down

0 comments on commit 6d44aec

Please sign in to comment.