Skip to content

Commit 8bf4920

Browse files
committed
chore: add value object for thermal zone
Signed-off-by: Daniel Kesselberg <mail@danielkesselberg.de>
1 parent bbfc99f commit 8bf4920

6 files changed

Lines changed: 81 additions & 25 deletions

File tree

js/script.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,8 +293,8 @@
293293
var data = response.ocs.data;
294294
document.getElementById("servertime").innerHTML = data.servertime;
295295
document.getElementById("uptime").innerHTML = data.uptime;
296-
for (i in data.thermalzones) {
297-
document.getElementById(data.thermalzones[i]['hash']).innerHTML = data.thermalzones[i]['temp'];
296+
for (const thermalzone of data.thermalzones) {
297+
document.getElementById(thermalzone.zone).textContent = thermalzone.temp;
298298
}
299299
},
300300
error: function (data) {

lib/Controller/ApiController.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,12 +138,11 @@ public function info(): DataResponse {
138138
public function BasicData(): DataResponse {
139139
$servertime = $this->os->getTime();
140140
$uptime = $this->formatUptime($this->os->getUptime());
141-
$thermalzones = $this->os->getThermalZones();
142141

143142
return new DataResponse([
144143
'servertime' => $servertime,
145144
'uptime' => $uptime,
146-
'thermalzones' => $thermalzones
145+
'thermalzones' => $this->os->getThermalZones()
147146
]);
148147
}
149148

lib/OperatingSystems/IOperatingSystem.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
use OCA\ServerInfo\Resources\Disk;
3030
use OCA\ServerInfo\Resources\Memory;
3131
use OCA\ServerInfo\Resources\NetInterface;
32+
use OCA\ServerInfo\Resources\ThermalZone;
3233

3334
interface IOperatingSystem {
3435
public function supported(): bool;
@@ -86,13 +87,7 @@ public function getUptime(): int;
8687
/**
8788
* Get info about available thermal zones.
8889
*
89-
* [
90-
* [
91-
* 'hash' => string,
92-
* 'type' => string,
93-
* 'temp' => float,
94-
* ],
95-
* ]
90+
* @return ThermalZone[]
9691
*/
9792
public function getThermalZones(): array;
9893
}

lib/OperatingSystems/Linux.php

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use OCA\ServerInfo\Resources\Disk;
2727
use OCA\ServerInfo\Resources\Memory;
2828
use OCA\ServerInfo\Resources\NetInterface;
29+
use OCA\ServerInfo\Resources\ThermalZone;
2930
use RuntimeException;
3031

3132
class Linux implements IOperatingSystem {
@@ -229,22 +230,24 @@ public function getDiskInfo(): array {
229230
}
230231

231232
public function getThermalZones(): array {
232-
$thermalZones = glob('/sys/class/thermal/thermal_zone*') ?: [];
233-
$result = [];
233+
$data = [];
234+
235+
$zones = glob('/sys/class/thermal/thermal_zone*');
236+
if ($zones === false) {
237+
return $data;
238+
}
234239

235-
foreach ($thermalZones as $thermalZone) {
236-
$tzone = [];
240+
foreach ($zones as $zone) {
237241
try {
238-
$tzone['hash'] = md5($thermalZone);
239-
$tzone['type'] = $this->readContent($thermalZone . '/type');
240-
$tzone['temp'] = (float)((int)($this->readContent($thermalZone . '/temp')) / 1000);
241-
} catch (RuntimeException $e) {
242-
continue;
242+
$type = $this->readContent($zone . '/type');
243+
$temp = (float)((int)($this->readContent($zone . '/temp')) / 1000);
244+
$data[] = new ThermalZone(md5($zone), $type, $temp);
245+
} catch (RuntimeException) {
246+
// unable to read thermal zone
243247
}
244-
$result[] = $tzone;
245248
}
246249

247-
return $result;
250+
return $data;
248251
}
249252

250253
/**

lib/Resources/ThermalZone.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* @copyright Copyright (c) 2023 Daniel Kesselberg <mail@danielkesselberg.de>
6+
*
7+
* @author Daniel Kesselberg <mail@danielkesselberg.de>
8+
*
9+
* @license GNU AGPL version 3 or any later version
10+
*
11+
* This program is free software: you can redistribute it and/or modify
12+
* it under the terms of the GNU Affero General Public License as
13+
* published by the Free Software Foundation, either version 3 of the
14+
* License, or (at your option) any later version.
15+
*
16+
* This program is distributed in the hope that it will be useful,
17+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
* GNU Affero General Public License for more details.
20+
*
21+
* You should have received a copy of the GNU Affero General Public License
22+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
23+
*
24+
*/
25+
26+
namespace OCA\ServerInfo\Resources;
27+
28+
class ThermalZone implements \JsonSerializable {
29+
public function __construct(
30+
private string $zone,
31+
private string $type,
32+
private float $temp) {
33+
}
34+
35+
public function getZone(): string {
36+
return $this->zone;
37+
}
38+
39+
public function getType(): string {
40+
return $this->type;
41+
}
42+
43+
public function getTemp(): float {
44+
return $this->temp;
45+
}
46+
47+
public function jsonSerialize(): array {
48+
return [
49+
'zone' => $this->zone,
50+
'type' => $this->type,
51+
'temp' => $this->temp,
52+
];
53+
}
54+
}

templates/settings-admin.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use OCA\ServerInfo\Resources\Disk;
2626
use OCA\ServerInfo\Resources\Memory;
2727
use OCA\ServerInfo\Resources\NetInterface;
28+
use OCA\ServerInfo\Resources\ThermalZone;
2829
use OCP\Util;
2930

3031
script('serverinfo', 'script');
@@ -51,6 +52,8 @@ function FormatMegabytes(int $byte): string {
5152
$disks = $_['diskinfo'];
5253
/** @var NetInterface[] $interfaces */
5354
$interfaces = $_['networkinterfaces'];
55+
/** @var ThermalZone[] $thermalZones */
56+
$thermalZones = $_['thermalzones'];
5457
/** @var bool $phpinfo */
5558
$phpinfo = $_['phpinfo'];
5659

@@ -80,6 +83,7 @@ function FormatMegabytes(int $byte): string {
8083
<p><?php p($l->t('Uptime:')); ?> <strong id="numFilesStorage"><span class="info" id="uptime"></span></strong></p>
8184
</div>
8285

86+
<?php if (count($thermalZones) > 0): ?>
8387
<div class="col col-6 col-l-12">
8488
<h2>
8589
<img class="infoicon" src="<?php p(image_path('serverinfo', 'app-dark.svg')); ?>">
@@ -90,16 +94,17 @@ function FormatMegabytes(int $byte): string {
9094
<thead>
9195
</thead>
9296
<tbody>
93-
<?php foreach ($_['thermalzones'] as $thermalzone): ?>
97+
<?php foreach ($thermalZones as $thermalZone): ?>
9498
<tr>
95-
<td><?php p($thermalzone['type'] . ':') ?></td>
96-
<td><span class="info" id="<?php p($thermalzone['hash']) ?>"></span>°C</td>
99+
<td><?php p($thermalZone->getType()) ?>:</td>
100+
<td>&nbsp;<span class="info" id="<?php p($thermalZone->getZone()) ?>"><?php p($thermalZone->getTemp()) ?></span>°C</td>
97101
</tr>
98102
<?php endforeach; ?>
99103
</tbody>
100104
</table>
101105
</div>
102106
</div>
107+
<?php endif; ?>
103108
</div>
104109
</div>
105110

0 commit comments

Comments
 (0)