-
Notifications
You must be signed in to change notification settings - Fork 0
/
Config.php
139 lines (122 loc) · 3.05 KB
/
Config.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
namespace OccupancyDisplay;
class Config
{
public const CONFIG_FILE = (__DIR__ . '/config.json');
private string $dataFile;
/** @var array<string,mixed> */
private array $areas;
/** @var array<string,mixed> */
private array $limits;
/** @var array<string,mixed> */
private array $texts;
public function __construct()
{
$this->dataFile = "";
$this->areas = array();
$this->limits = array();
$this->texts = array();
}
/**
* Load configuration data from $configFile into memory.
*/
public function load(string $configFile = self::CONFIG_FILE): void
{
if (!file_exists($configFile)) {
error_log("Configuration file $configFile not found");
return;
}
$json_data = json_decode(file_get_contents($configFile), true);
if (is_null($json_data) || !count($json_data)) {
error_log('Could not read configuration file, syntax error?');
return;
}
if (array_key_exists('datafile', $json_data)) {
$this->dataFile = realpath(__DIR__ . '/' . $json_data['datafile']);
if (!file_exists($this->dataFile)) {
error_log("Input file '{$json_data['datafile']}' not found");
}
}
$this->areas = $json_data['areas'] ?? array();
$this->limits = $json_data['limits'] ?? array();
uasort($this->limits, function ($a, $b) {
if ($a["threshold"] == $b["threshold"]) {
return 0;
}
return ($a["threshold"] < $b["threshold"]) ? -1 : 1;
});
$this->texts = $json_data['texts'] ?? array();
}
/**
* Return full areas array.
*
* @return array<string,mixed>
*/
public function areas(): array
{
return $this->areas;
}
/**
* Return property array for area $name.
*
* @return array<string,mixed> | null
*/
public function area(string $name): array | null
{
return $this->areas[$name] ?? null;
}
/**
* Return full limits array.
*
* @return array<string,mixed>
*/
public function limits(): array
{
return $this->limits;
}
/**
* Return property array for limit $name.
*
* @return array<string,mixed> | null
*/
public function limit(string $name): array | null
{
return $this->limits[$name] ?? null;
}
/**
* Return limit id corresponding to counter $value.
*/
public function currentState(int $value, string $id=""): string
{
$state = "";
if (strlen($id) && ($this->areas[$id]["state"] ?? false)) {
return $this->areas[$id]["state"];
}
foreach ($this->limits as $limitId => $limitData) {
if ($value >= $limitData["threshold"]) {
$state = $limitId;
}
}
return $state;
}
/**
* Return array for texts for $lang.
*
* @return array<string,mixed>
*/
public function texts(string $lang): array
{
if (!array_key_exists($lang, $this->texts)) {
error_log("No texts for language $lang defined in configuration");
return array();
}
return $this->texts[$lang];
}
/**
* Return name of input datafile.
*/
public function dataFile(): string
{
return $this->dataFile;
}
}