Skip to content

Commit ebfbd3f

Browse files
committed
Merge branch 'development'
2 parents 2bab5b3 + 0bc6107 commit ebfbd3f

File tree

6 files changed

+197
-121
lines changed

6 files changed

+197
-121
lines changed

src/Actor.php

Lines changed: 0 additions & 34 deletions
This file was deleted.

src/Client.php

Lines changed: 66 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,34 @@
22

33
namespace rutgerkirkels\DomoticzPHP;
44

5+
use rutgerkirkels\DomoticzPHP\Devices\AbstractDevice;
6+
use rutgerkirkels\DomoticzPHP\Entities\SunRiseSet;
57
use rutgerkirkels\DomoticzPHP\Factories\Lighting2Factory;
68
use rutgerkirkels\DomoticzPHP\Factories\TempAndHumidityFactory;
79
use rutgerkirkels\DomoticzPHP\Factories\ThermostatFactory;
810

11+
/**
12+
* Class Client
13+
* @package rutgerkirkels\DomoticzPHP
14+
* @author Rutger Kirkels <rutger@kirkels.nl>
15+
*/
916
class Client
1017
{
11-
private $hostname = null;
12-
private static $username = null;
13-
private static $password = null;
18+
/**
19+
* @var string
20+
*/
21+
protected $host;
22+
23+
/**
24+
* @var string
25+
*/
26+
private static $username;
27+
28+
/**
29+
* @var string
30+
*/
31+
private static $password;
32+
1433
private static $version = [
1534
'major' => '1',
1635
'minor' => '0',
@@ -29,9 +48,15 @@ public static function singleton() {
2948
}
3049
}
3150

32-
public function __construct($hostname = null, $username = null, $password = null)
51+
/**
52+
* Client constructor.
53+
* @param string|null $host
54+
* @param string|null $username
55+
* @param string|null $password
56+
*/
57+
public function __construct(string $host = null, string $username = null, string $password = null)
3358
{
34-
$this->hostname = $hostname;
59+
$this->host = $host;
3560

3661
if (!empty($username)) {
3762
self::$username = $username;
@@ -42,13 +67,32 @@ public function __construct($hostname = null, $username = null, $password = null
4267
}
4368

4469
$this->api = new \GuzzleHttp\Client([
45-
'base_uri' => $hostname . '/'
70+
'base_uri' => $this->host . '/'
4671
]);
4772

48-
$connector = Connector::init($this->hostname, self::$username, self::$password);
73+
$this->connector = Connector::init($this->host, self::$username, self::$password);
4974
// $connector->setUserAgent('Domoticz PHP v' . self::$version['major'] . '.' . self::$version['minor'] . ' (' . php_uname('s') . '-' . php_uname('r') . '; PHP-' . PHP_VERSION . '; ' . PHP_SAPI . ') ');
5075
}
5176

77+
/**
78+
* @return bool|SunRiseSet Returns SunRiseSet entity or FALSE if unable to get the data from Domoticz.
79+
*/
80+
public function getSunRiseSet() {
81+
$retrievedData = $this->connector->executeCommand([
82+
'param' => 'getSunRiseSet'
83+
]);
84+
85+
if ($retrievedData) {
86+
return new SunRiseSet($retrievedData);
87+
}
88+
89+
return false;
90+
}
91+
92+
/**
93+
* @param int $idx
94+
* @return AbstractDevice|bool
95+
*/
5296
public function getDeviceByIdx(int $idx) {
5397
$this->query = [
5498
'type' => 'devices',
@@ -86,6 +130,10 @@ public function getDeviceByIdx(int $idx) {
86130
}
87131
}
88132

133+
/**
134+
* @param string|null $filter
135+
* @return array
136+
*/
89137
public function getDevices(string $filter = null) {
90138
$this->query = [
91139
'type' => 'devices',
@@ -129,27 +177,27 @@ public function getDevices(string $filter = null) {
129177
return $devices;
130178
}
131179

132-
public function executeCommand(array $parameters) {
133-
$this->query = [
134-
'type' => 'command'
135-
];
136-
array_merge($this->query, $parameters);
137-
138-
}
180+
// public function executeCommand(array $parameters) {
181+
// $this->query = [
182+
// 'type' => 'command'
183+
// ];
184+
// array_merge($this->query, $parameters);
185+
//
186+
// }
139187

140-
protected function getLightSwitch($deviceData) {
188+
protected function getLightSwitch(object $deviceData) {
141189
return new Factories\LightSwitchFactory($deviceData);
142190
}
143191

144-
protected function getLighting2($deviceData) {
192+
protected function getLighting2(object $deviceData) {
145193
return new Lighting2Factory($deviceData);
146194
}
147195

148-
protected function getTempAndHumidity($deviceData) {
196+
protected function getTempAndHumidity(object $deviceData) {
149197
return new TempAndHumidityFactory($deviceData);
150198
}
151199

152-
protected function getThermostat($deviceData) {
200+
protected function getThermostat(object $deviceData) {
153201
return new ThermostatFactory($deviceData);
154202
}
155203
}

src/Config.php

Lines changed: 0 additions & 32 deletions
This file was deleted.

src/Connector.php

Lines changed: 66 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,44 @@
11
<?php
2+
namespace rutgerkirkels\DomoticzPHP;
3+
4+
use GuzzleHttp\Exception\ClientException;
5+
26
/**
3-
* Connector Class
4-
* Provides all connectivity with the Domoticz API
7+
* Class Connector
8+
* @package rutgerkirkels\DomoticzPHP
59
*
6-
* @package rutgerkirkels\domoticz_php
710
* @author Rutger Kirkels <rutger@kirkels.nl>
811
*/
9-
namespace rutgerkirkels\DomoticzPHP;
10-
11-
1212
class Connector
1313
{
14-
protected $host = null;
15-
protected $username = null;
16-
protected $password = null;
14+
/**
15+
* @var string
16+
*/
17+
protected $host;
18+
19+
/**
20+
* @var string
21+
*/
22+
protected $username;
23+
24+
/**
25+
* @var string
26+
*/
27+
protected $password;
28+
29+
/**
30+
* @var \GuzzleHttp\Client
31+
*/
1732
protected $api;
33+
34+
/**
35+
* @var array
36+
*/
1837
protected $query;
1938

2039
private static $instance = null;
2140

22-
public static function init($host = null, $username = null, $password = null) {
41+
public static function init(string $host = null, string $username = null, string $password = null) {
2342
if (self::$instance === null) {
2443
self::$instance = new Connector($host, $username, $password);
2544
}
@@ -30,27 +49,51 @@ public static function getInstance() {
3049
return self::$instance;
3150
}
3251

33-
public function __construct(string $host, $username, $password)
52+
/**
53+
* Connector constructor.
54+
* @param string $host
55+
* @param string|null $username
56+
* @param string|null $password
57+
*/
58+
public function __construct(string $host, string $username = null, string $password = null)
3459
{
3560
$this->host = $host;
3661
$this->username = $username;
3762
$this->password = $password;
3863

39-
$this->api = new \GuzzleHttp\Client([
40-
'base_uri' => $host . '/'
41-
]);
64+
if (!is_null($this->username) && !is_null($this->password)) {
65+
$this->api = new \GuzzleHttp\Client([
66+
'base_uri' => $host . '/',
67+
'auth' => [$this->username, $this->password]
68+
]);
69+
}
70+
else {
71+
$this->api = new \GuzzleHttp\Client([
72+
'base_uri' => $host . '/'
73+
]);
74+
}
4275
}
4376

4477
public function executeCommand(array $parameters) {
45-
$this->query = [
46-
'type' => 'command'
47-
];
48-
$response = json_decode((string) $this->api->request('GET', 'json.htm', [
49-
'query' => array_merge($this->query, $parameters)
50-
])->getBody());
51-
52-
if ($response->status === 'OK') {
53-
return true;
78+
try {
79+
$this->query = [
80+
'type' => 'command'
81+
];
82+
$response = json_decode((string) $this->api->request('GET', 'json.htm', [
83+
'query' => array_merge($this->query, $parameters)
84+
])->getBody());
85+
86+
if ($response->status === 'OK') {
87+
return $response;
88+
}
89+
}
90+
catch (ClientException $exception) {
91+
switch ($exception->getCode()) {
92+
93+
case 401:
94+
return false;
95+
break;
96+
}
5497
}
5598

5699
return false;

src/Entities/SunRiseSet.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
namespace rutgerkirkels\DomoticzPHP\Entities;
4+
5+
/**
6+
* Class SunRiseSet
7+
* @package rutgerkirkels\DomoticzPHP\Entities
8+
* @author Rutger Kirkels <rutger@kirkels.nl>
9+
*/
10+
class SunRiseSet
11+
{
12+
/**
13+
* @var \DateTime
14+
*/
15+
protected $serverTime;
16+
17+
/**
18+
* @var \DateTime
19+
*/
20+
protected $sunRise;
21+
22+
/**
23+
* @var \DateTime
24+
*/
25+
protected $sunSet;
26+
27+
protected $dayLength;
28+
29+
/**
30+
* SunRiseSet constructor.
31+
* @param object $data
32+
*/
33+
public function __construct(object $data)
34+
{
35+
$this->serverTime = new \DateTime($data->ServerTime);
36+
$this->sunRise = new \DateTime($data->Sunrise);
37+
$this->sunSet = new \DateTime($data->Sunset);
38+
}
39+
40+
/**
41+
* @return \DateTime
42+
*/
43+
public function getServerTime(): \DateTime
44+
{
45+
return $this->serverTime;
46+
}
47+
48+
/**
49+
* @return \DateTime
50+
*/
51+
public function getSunRise(): \DateTime
52+
{
53+
return $this->sunRise;
54+
}
55+
56+
/**
57+
* @return \DateTime
58+
*/
59+
public function getSunSet(): \DateTime
60+
{
61+
return $this->sunSet;
62+
}
63+
64+
65+
}

0 commit comments

Comments
 (0)