Skip to content

Commit 1d0b53a

Browse files
committed
[general] Testing fixes (#243)
1 parent 5880326 commit 1d0b53a

File tree

172 files changed

+4188
-3464
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

172 files changed

+4188
-3464
lines changed

.editorconfig

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ end_of_line = lf
44
indent_size = 4
55
indent_style = space
66
insert_final_newline = false
7-
max_line_length = 150
7+
max_line_length = 120
88
tab_width = 4
99
ij_continuation_indent_size = 8
1010
ij_formatter_off_tag = @formatter:off

.github/workflows/ci.yaml

+4-4
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ jobs:
2727
strategy:
2828
fail-fast: false
2929
matrix:
30-
php-version: [ "8.1", "8.2" ]
30+
php-version: [ "8.2" ]
3131
operating-system: [ "ubuntu-latest" ]
3232

3333
steps:
@@ -77,7 +77,7 @@ jobs:
7777
strategy:
7878
fail-fast: false
7979
matrix:
80-
php-version: [ "8.1", "8.2" ]
80+
php-version: [ "8.2" ]
8181
operating-system: [ "ubuntu-latest" ]
8282

8383
steps:
@@ -128,7 +128,7 @@ jobs:
128128
strategy:
129129
fail-fast: false
130130
matrix:
131-
php-version: [ "8.1", "8.2" ]
131+
php-version: [ "8.2" ]
132132
operating-system: [ "ubuntu-latest" ]
133133

134134
steps:
@@ -339,7 +339,7 @@ jobs:
339339

340340
strategy:
341341
matrix:
342-
php-version: [ "8.1", "8.2" ]
342+
php-version: [ "8.2" ]
343343
operating-system: [ "ubuntu-latest" ]
344344
fail-fast: false
345345

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ and is licensed under [Apache2](http://www.apache.org/licenses/LICENSE-2.0).
3636

3737
## Requirements
3838

39-
Shelly connector is tested against PHP 8.1 and require installed [Process Control](https://www.php.net/manual/en/book.pcntl.php)
39+
Shelly connector is tested against PHP 8.2 and require installed [Process Control](https://www.php.net/manual/en/book.pcntl.php)
4040
PHP extension.
4141

4242
## Installation

composer.json

+3-9
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,20 @@
3333
"source": "https://github.com/FastyBird/shelly-connector"
3434
},
3535
"require": {
36-
"php": ">=8.1.0",
36+
"php": ">=8.2.0",
3737
"ext-pcntl": "*",
3838
"clue/multicast-react": "^1.1",
3939
"contributte/console": "^0.9",
4040
"contributte/monolog": "^0.5",
4141
"contributte/translation": "^2.0",
4242
"cweagans/composer-patches": "^1.7",
43-
"evenement/evenement": "^3.0",
44-
"fastybird/bootstrap-library": "dev-main",
43+
"fastybird/application-library": "dev-main",
4544
"fastybird/datetime-factory": "^0.6",
4645
"fastybird/devices-module": "dev-main",
4746
"fastybird/metadata-library": "dev-main",
4847
"guzzlehttp/guzzle": "^7.5",
4948
"nette/di": "^3.0",
49+
"orisai/nette-object-mapper": "^0.1",
5050
"psr/log": "^3.0",
5151
"ratchet/pawl": "^0.4",
5252
"react/async": "^4.0",
@@ -110,12 +110,6 @@
110110
}
111111
}
112112
},
113-
"repositories": [
114-
{
115-
"type": "vcs",
116-
"url": "https://github.com/mathsolver/mathsolver.git"
117-
}
118-
],
119113
"minimum-stability": "dev",
120114
"prefer-stable": true
121115
}

resources/gen2_components.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -276,9 +276,9 @@
276276
"relative_humidity": {
277277
"optional": false,
278278
"name": "Relative Humidity",
279-
"data_type": "float",
279+
"data_type": "uchar",
280280
"unit": "%",
281-
"format": null,
281+
"format": [0, 100],
282282
"invalid": null,
283283
"queryable": true,
284284
"settable": false

src/API/ConnectionManager.php

+36-10
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,15 @@
1515

1616
namespace FastyBird\Connector\Shelly\API;
1717

18+
use FastyBird\Connector\Shelly\Documents;
1819
use FastyBird\Connector\Shelly\Entities;
20+
use FastyBird\Connector\Shelly\Exceptions;
1921
use FastyBird\Connector\Shelly\Helpers;
20-
use FastyBird\Library\Metadata\Documents as MetadataDocuments;
2122
use FastyBird\Library\Metadata\Exceptions as MetadataExceptions;
2223
use FastyBird\Module\Devices\Exceptions as DevicesExceptions;
2324
use Nette;
25+
use TypeError;
26+
use ValueError;
2427
use function array_key_exists;
2528

2629
/**
@@ -84,11 +87,14 @@ public function getGen2HttpApiConnection(): Gen2HttpApi
8487

8588
/**
8689
* @throws DevicesExceptions\InvalidState
90+
* @throws Exceptions\InvalidArgument
8791
* @throws MetadataExceptions\InvalidArgument
8892
* @throws MetadataExceptions\InvalidState
93+
* @throws TypeError
94+
* @throws ValueError
8995
*/
9096
public function getGen2WsApiConnection(
91-
Entities\ShellyDevice|MetadataDocuments\DevicesModule\Device $device,
97+
Entities\Devices\Device|Documents\Devices\Device $device,
9298
): Gen2WsApi
9399
{
94100
if (!array_key_exists($device->getId()->toString(), $this->gen2WsApiConnection)) {
@@ -106,12 +112,17 @@ public function getGen2WsApiConnection(
106112

107113
/**
108114
* @throws DevicesExceptions\InvalidState
115+
* @throws Exceptions\InvalidArgument
109116
* @throws MetadataExceptions\InvalidArgument
110117
* @throws MetadataExceptions\InvalidState
118+
* @throws TypeError
119+
* @throws ValueError
111120
*/
112-
private function getIpAddress(Entities\ShellyDevice|MetadataDocuments\DevicesModule\Device $device): string|null
121+
private function getIpAddress(
122+
Entities\Devices\Device|Documents\Devices\Device $device,
123+
): string|null
113124
{
114-
if ($device instanceof Entities\ShellyDevice) {
125+
if ($device instanceof Entities\Devices\Device) {
115126
return $device->getIpAddress();
116127
}
117128

@@ -120,12 +131,17 @@ private function getIpAddress(Entities\ShellyDevice|MetadataDocuments\DevicesMod
120131

121132
/**
122133
* @throws DevicesExceptions\InvalidState
134+
* @throws Exceptions\InvalidArgument
123135
* @throws MetadataExceptions\InvalidArgument
124136
* @throws MetadataExceptions\InvalidState
137+
* @throws TypeError
138+
* @throws ValueError
125139
*/
126-
private function getDomain(Entities\ShellyDevice|MetadataDocuments\DevicesModule\Device $device): string|null
140+
private function getDomain(
141+
Entities\Devices\Device|Documents\Devices\Device $device,
142+
): string|null
127143
{
128-
if ($device instanceof Entities\ShellyDevice) {
144+
if ($device instanceof Entities\Devices\Device) {
129145
return $device->getDomain();
130146
}
131147

@@ -134,12 +150,17 @@ private function getDomain(Entities\ShellyDevice|MetadataDocuments\DevicesModule
134150

135151
/**
136152
* @throws DevicesExceptions\InvalidState
153+
* @throws Exceptions\InvalidArgument
137154
* @throws MetadataExceptions\InvalidArgument
138155
* @throws MetadataExceptions\InvalidState
156+
* @throws TypeError
157+
* @throws ValueError
139158
*/
140-
private function getUsername(Entities\ShellyDevice|MetadataDocuments\DevicesModule\Device $device): string|null
159+
private function getUsername(
160+
Entities\Devices\Device|Documents\Devices\Device $device,
161+
): string|null
141162
{
142-
if ($device instanceof Entities\ShellyDevice) {
163+
if ($device instanceof Entities\Devices\Device) {
143164
return $device->getUsername();
144165
}
145166

@@ -148,12 +169,17 @@ private function getUsername(Entities\ShellyDevice|MetadataDocuments\DevicesModu
148169

149170
/**
150171
* @throws DevicesExceptions\InvalidState
172+
* @throws Exceptions\InvalidArgument
151173
* @throws MetadataExceptions\InvalidArgument
152174
* @throws MetadataExceptions\InvalidState
175+
* @throws TypeError
176+
* @throws ValueError
153177
*/
154-
private function getPassword(Entities\ShellyDevice|MetadataDocuments\DevicesModule\Device $device): string|null
178+
private function getPassword(
179+
Entities\Devices\Device|Documents\Devices\Device $device,
180+
): string|null
155181
{
156-
if ($device instanceof Entities\ShellyDevice) {
182+
if ($device instanceof Entities\Devices\Device) {
157183
return $device->getPassword();
158184
}
159185

src/API/Gen1Coap.php

+27-20
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,8 @@
1616
namespace FastyBird\Connector\Shelly\API;
1717

1818
use BadMethodCallException;
19-
use Evenement;
19+
use Closure;
2020
use FastyBird\Connector\Shelly;
21-
use FastyBird\Connector\Shelly\Entities;
2221
use FastyBird\Connector\Shelly\Exceptions;
2322
use FastyBird\Connector\Shelly\Helpers;
2423
use FastyBird\Connector\Shelly\Services;
@@ -51,28 +50,32 @@
5150
*
5251
* @author Adam Kadlec <[email protected]>
5352
*/
54-
final class Gen1Coap implements Evenement\EventEmitterInterface
53+
final class Gen1Coap
5554
{
5655

5756
use Nette\SmartObject;
58-
use Evenement\EventEmitterTrait;
59-
60-
private const COAP_ADDRESS = '224.0.1.187';
61-
62-
private const COAP_PORT = 5_683;
6357

6458
private const STATE_MESSAGE_CODE = 30;
6559

6660
private const STATE_MESSAGE_SCHEMA_FILENAME = 'gen1_coap_state.json';
6761

62+
/** @var array<Closure(Messages\Message $message): void> */
63+
public array $onMessage = [];
64+
65+
/** @var array<Closure(): void> */
66+
public array $onClosed = [];
67+
68+
/** @var array<Closure(Throwable $error): void> */
69+
public array $onError = [];
70+
6871
/** @var array<string, string> */
6972
private array $validationSchemas = [];
7073

7174
private Datagram\SocketInterface|null $server = null;
7275

7376
public function __construct(
7477
private readonly Services\MulticastFactory $multicastFactory,
75-
private readonly Helpers\Entity $entityHelper,
78+
private readonly Helpers\MessageBuilder $messageBuilder,
7679
private readonly Shelly\Logger $logger,
7780
private readonly MetadataSchemas\Validator $schemaValidator,
7881
)
@@ -85,26 +88,29 @@ public function __construct(
8588
*/
8689
public function connect(): void
8790
{
88-
$this->server = $this->multicastFactory->create(self::COAP_ADDRESS, self::COAP_PORT);
91+
$this->server = $this->multicastFactory->create(
92+
Shelly\Constants::COAP_ADDRESS,
93+
Shelly\Constants::COAP_PORT,
94+
);
8995

9096
$this->server->on('message', function ($message, $remote): void {
9197
$this->handlePacket($message, $remote);
9298
});
9399

94100
$this->server->on('error', function (Throwable $ex): void {
95-
$this->emit('error', [$ex]);
101+
Utils\Arrays::invoke($this->onError, $ex);
96102
});
97103

98104
$this->server->on('close', function (): void {
99105
$this->logger->debug(
100106
'CoAP connection was successfully closed',
101107
[
102-
'source' => MetadataTypes\ConnectorSource::SOURCE_CONNECTOR_SHELLY,
108+
'source' => MetadataTypes\Sources\Connector::SHELLY->value,
103109
'type' => 'gen1-coap-api',
104110
],
105111
);
106112

107-
$this->emit('closed');
113+
Utils\Arrays::invoke($this->onClosed);
108114
});
109115
}
110116

@@ -202,7 +208,7 @@ private function handlePacket(string $packet, string $remote): void
202208
str_replace(' ', '', $message),
203209
),
204210
[
205-
'source' => MetadataTypes\ConnectorSource::SOURCE_CONNECTOR_SHELLY,
211+
'source' => MetadataTypes\Sources\Connector::SHELLY->value,
206212
'type' => 'gen1-coap-api',
207213
],
208214
);
@@ -215,7 +221,7 @@ private function handlePacket(string $packet, string $remote): void
215221
try {
216222
$this->handleStatusMessage($deviceIdentifier, $message, $remote);
217223
} catch (Exceptions\CoapError | Exceptions\InvalidState $ex) {
218-
$this->emit('error', [$ex]);
224+
Utils\Arrays::invoke($this->onError, $ex);
219225
}
220226
}
221227
}
@@ -267,18 +273,19 @@ private function handleStatusMessage(
267273
}
268274

269275
try {
270-
$this->emit('message', [
271-
$this->entityHelper->create(
272-
Entities\API\Gen1\ReportDeviceState::class,
276+
Utils\Arrays::invoke(
277+
$this->onMessage,
278+
$this->messageBuilder->create(
279+
Messages\Response\Gen1\ReportDeviceState::class,
273280
[
274281
'identifier' => $deviceIdentifier,
275282
'ip_address' => preg_replace('/(:[0-9]+)+$/', '', $remote),
276283
'states' => $statuses,
277284
],
278285
),
279-
]);
286+
);
280287
} catch (Exceptions\Runtime $ex) {
281-
throw new Exceptions\InvalidState('Could not map payload to entity', $ex->getCode(), $ex);
288+
throw new Exceptions\InvalidState('Could not map payload to message', $ex->getCode(), $ex);
282289
}
283290
}
284291

0 commit comments

Comments
 (0)