Commit 4edc754f authored by Thomas Eimers's avatar Thomas Eimers

Matrix Store Types and Value Types

parent 49258a17
Pipeline #94838828 failed with stages
in 10 minutes and 31 seconds
......@@ -6,7 +6,7 @@ namespace HeatingControl\Service;
use HeatingControl\HeatingException;
use HeatingControl\State\RelayState;
use HeatingControl\State\TemperaturSensorState;
use HeatingControl\State\TemperatureSensorState;
use Psr\Log\LoggerInterface;
/**
......@@ -171,7 +171,7 @@ class HeatingManagementService
return (float)$cachedValue;
}
/** @var TemperaturSensorState $sensor */
/** @var TemperatureSensorState $sensor */
$sensor = $this->sensorService->getSensorValueBySensorIdentification(
$this->config['systemSensorsIds']['outside']
);
......@@ -219,7 +219,7 @@ class HeatingManagementService
return (float)$cachedValue;
}
/** @var TemperaturSensorState $sensor */
/** @var TemperatureSensorState $sensor */
$sensor = $this->sensorService->getSensorValueBySensorIdentification(
$this->config['systemSensorsIds']['returnFlow']
);
......
......@@ -6,7 +6,8 @@ use BadMethodCallException;
use HeatingControl\HeatingException;
use HeatingControl\State\RelayState;
use HeatingControl\State\State;
use HeatingControl\State\TemperaturSensorState;
use HeatingControl\State\SwitchState;
use HeatingControl\State\TemperatureSensorState;
/**
* Get access to sensors by using SensorSettings and using the 1-wire sensors
......@@ -71,8 +72,15 @@ class SensorStateService
$pumpStatus->setIdentification($this->appConfig[RelayService::CONFIG_KEY][RelayState::PUMP_KEY]);
$pumpStatus->setId(RelayState::DIAGRAM_PUMP_ID);
$ecoStatus = new SwitchState();
$ecoStatus->setValue((string)0);
$ecoStatus->setName('Eco Mode');
$ecoStatus->setIdentification(SwitchState::ECO_KEY);
$ecoStatus->setId(11);
$sensorValues[] = $burnerStatus;
$sensorValues[] = $pumpStatus;
$sensorValues[] = $ecoStatus;
// Set name and ID for unconfigured Sensors
/** @var State $sensor */
......@@ -127,7 +135,7 @@ class SensorStateService
$result = trim($result);
$entity = new TemperaturSensorState();
$entity = new TemperatureSensorState();
$entity->setValue((string)($result / 1000));
$entity->setIdentification($sensorIdentification);
......
......@@ -2,6 +2,7 @@
namespace HeatingControl\Service;
use HeatingControl\State\StoreQueueInterface;
use HeatingControl\State\State;
use Mosquitto\Client;
use Psr\Log\LoggerInterface;
......@@ -31,10 +32,13 @@ class SensorStoreMqqt implements SensorStoreInterface
public function storeValues(State ...$sensorValues)
{
foreach ($sensorValues as $sensorValue) {
if (!is_a($sensorValue, StoreQueueInterface::class)) {
continue;
}
$this->getClient()->loop(1000);
$sensorValue->publishDiscovery($this->getClient());
$sensorStateUrl = $sensorValue->publishMqqtValue($this->getClient());
$sensorValue->publishQueueDiscovery($this->getClient());
$sensorStateUrl = $sensorValue->publishQueueValue($this->getClient());
$this->logger->debug('Stored "' . $sensorValue->getName() . '" in mqqt ' . $sensorStateUrl . ' ' . $sensorValue->getValue());
}
......
......@@ -3,6 +3,7 @@
namespace HeatingControl\Service;
use HeatingControl\HeatingException;
use HeatingControl\State\StoreRrdInterface;
use HeatingControl\State\State;
class SensorStoreRrd implements SensorStoreInterface
......@@ -40,7 +41,11 @@ class SensorStoreRrd implements SensorStoreInterface
$values = array_fill(1, 10, 'U');
foreach ($sensorValues as $sensorValue) {
$values[$sensorValue->getId()] = $sensorValue->getValue();
/** @var StoreRrdInterface $sensorValue */
if (!is_a($sensorValue, StoreRrdInterface::class)) {
continue;
}
$values[$sensorValue->getId()] = $sensorValue->getRrdValue();
}
// rrdtool update /home/pi/.temperature/temp.rrd N:42.75:54.625:13.812:22.812:41.625:28.562:U:U:U:U
......
<?php
namespace HeatingControl\State;
use Mosquitto\Client;
trait QueueUrls
{
protected function getQueueDiscoveryUrl()
{
return static::SENSOR_PREFIX . $this->getId() . '/heating/config';
}
public function getQueueStateUrl(): string
{
return static::SENSOR_PREFIX . $this->getId() . '/heating/state';
}
/**
* Das muss hier raus
*
* @param Client $client
* @return string
*/
public function publishQueueValue(Client $client): string
{
$client->publish(
$this->getQueueStateUrl(),
(string)$this->getValue()
);
return $this->getQueueStateUrl();
}
}
......@@ -2,12 +2,14 @@
namespace HeatingControl\State;
use Mosquitto\Client;
/**
* Represents the temperature measurement of a sensor
*/
class RelayState extends State
class RelayState extends SwitchState implements StoreQueueInterface, StoreRrdInterface
{
const SENSOR_PREFIX = 'homeassistant/switch/';
const SENSOR_PREFIX = 'homeassistant/binary_sensor/';
const DIAGRAM_PUMP_ID = 9;
const DIAGRAM_BURNER_ID = 10;
......@@ -15,32 +17,31 @@ class RelayState extends State
const BURNER_KEY = 'burner';
const PUMP_KEY = 'pump';
public function publishDiscovery($client): string
public function publishQueueDiscovery($client): string
{
$discoveryJson = json_encode(
[
'name' => $this->getName(),
'state_topic' => $this->getMqqtStateUrl(),
'command_topic' => $this->getMqqtStateUrl()
]
);
$client->publish(
$this->getMqqtDiscoveryUrl(),
$discoveryJson
$this->getQueueDiscoveryUrl(),
json_encode([
'name' => $this->getName(),
'state_topic' => $this->getQueueStateUrl(),
'device_class' => 'opening'
]
)
);
return $this->getMqqtDiscoveryUrl();
return $this->getQueueDiscoveryUrl();
}
public function getQueueValue(): string
{
return (parent::getValue() === true) ? 'On' : 'Off';
}
/**
* Dont direct return a boolean value. RRDTool need a int or string
*/
public function getValue()
public function getRrdValue(): int
{
if (parent::getValue() === true) {
return 10;
}
return 5;
return (parent::getValue() === true) ? 10 : 5;
}
}
......@@ -3,12 +3,8 @@ declare(strict_types=1);
namespace HeatingControl\State;
use Mosquitto\Client;
abstract class State
{
const SENSOR_PREFIX = 'unknown';
/** @var string Im Regelfall eine Temperatur, könnte aber auch z.B. % Luftfeuchtigkeit sein */
protected $value;
......@@ -24,36 +20,6 @@ abstract class State
/** @var bool All Sensors in Database are configured, all new sensors not */
protected $configured = false;
abstract public function publishDiscovery(Client $client);
/**
* Das muss hier raus
*
* @param Client $client
* @return string
*/
public function publishMqqtValue(Client $client): string
{
$client->publish(
$this->getMqqtStateUrl(),
(string)$this->getValue()
);
return $this->getMqqtStateUrl();
}
protected function getMqqtDiscoveryUrl()
{
return static::SENSOR_PREFIX . $this->getId() . '/heating/config';
}
protected function getMqqtStateUrl()
{
return static::SENSOR_PREFIX . $this->getId() . '/heating/state';
}
/**
* @return array
*/
......
<?php
namespace HeatingControl\State;
use Mosquitto\Client;
interface StoreQueueInterface
{
public function getQueueValue(): string;
public function getQueueStateUrl(): string;
public function publishQueueDiscovery(Client $client);
public function publishQueueValue(Client $client): string;
}
<?php
namespace HeatingControl\State;
interface StoreRrdInterface
{
public function getRrdValue(): int;
}
<?php
namespace HeatingControl\State;
use Mosquitto\Client;
/**
* Represents the temperature measurement of a sensor
*/
class SwitchState extends State implements StoreQueueInterface
{
use QueueUrls;
const SENSOR_PREFIX = 'homeassistant/switch/';
const ECO_KEY = 'eco';
public function publishQueueDiscovery(Client $client): string
{
$client->publish(
$this->getQueueDiscoveryUrl(),
json_encode(
[
'name' => $this->getName(),
'state_topic' => $this->getQueueStateUrl(),
'command_topic' => $this->getQueueStateUrl()
]
)
);
return $this->getQueueDiscoveryUrl();
}
public function getQueueValue(): string
{
return (parent::getValue() === true) ? 'On' : 'Off';
}
}
......@@ -7,26 +7,39 @@ use Mosquitto\Client;
/**
* Represents the temperature measurement of a sensor
*/
class TemperaturSensorState extends State
class TemperatureSensorState extends State implements StoreRrdInterface, StoreQueueInterface
{
use QueueUrls;
const SENSOR_PREFIX = 'homeassistant/sensor/';
public function publishDiscovery(Client $client): string
public function publishQueueDiscovery(Client $client): string
{
$discoveryJson = json_encode(
[
'name' => $this->getName(),
'device_class' => 'temperature',
'state_topic' => $this->getMqqtStateUrl(),
'state_topic' => $this->getQueueStateUrl(),
'unit_of_measurement' => '°C'
]
);
$client->publish(
$this->getMqqtDiscoveryUrl(),
$this->getQueueDiscoveryUrl(),
$discoveryJson
);
return $this->getMqqtDiscoveryUrl();
return $this->getQueueDiscoveryUrl();
}
public function getRrdValue(): int
{
return parent::getValue();
}
public function getQueueValue(): string
{
return parent::getValue();
}
}
......@@ -5,7 +5,7 @@ namespace HeatingControl\Test\Controller;
use HeatingControl\Controller\CliStoreController;
use HeatingControl\Service\SensorStateService;
use HeatingControl\Service\SensorStoreInterface;
use HeatingControl\State\TemperaturSensorState;
use HeatingControl\State\TemperatureSensorState;
use HeatingControl\Test\IntegrationTestCase;
use Slim\Http;
......@@ -18,7 +18,7 @@ class CliImportControllerTest extends IntegrationTestCase
$prophet = $this->prophesize(SensorStateService::class);
$prophet->getAllSensorValues()->willReturn([
(new TemperaturSensorState())
(new TemperatureSensorState())
->setName('Testsensor')
]);
......@@ -27,7 +27,7 @@ class CliImportControllerTest extends IntegrationTestCase
$prophet = $this->prophesize(SensorStoreInterface::class);
$prophet->storeValues(
(new TemperaturSensorState())
(new TemperatureSensorState())
->setName('Testsensor')
)->willReturn(true);
......
......@@ -3,7 +3,7 @@
namespace HeatingControl\Test;
use HeatingControl\Service\SensorStateService;
use HeatingControl\State\TemperaturSensorState;
use HeatingControl\State\TemperatureSensorState;
use Slim\Http;
use Slim\Route;
......@@ -31,13 +31,13 @@ class SensorControllerTest extends IntegrationTestCase
/** @var SensorStateService| $sensorService */
$sensorService = $this->prophesize(SensorStateService::class);
$sensorValue1 = new TemperaturSensorState();
$sensorValue1 = new TemperatureSensorState();
$sensorValue1->setValue('10.123123123');
$sensorValue1->setIdentification('10-00080282b5f8');
$sensorValue1->setName('innen');
$sensorValue1->setConfigured(true);
$sensorValue2 = new TemperaturSensorState();
$sensorValue2 = new TemperatureSensorState();
$sensorValue2->setValue('2.22');
$sensorValue2->setIdentification('10-00080282b5f9');
$sensorValue2->setConfigured(false);
......@@ -82,7 +82,7 @@ class SensorControllerTest extends IntegrationTestCase
/** @var SensorStateService| $sensorService */
$sensorService = $this->prophesize(SensorStateService::class);
$sensorValue = new TemperaturSensorState();
$sensorValue = new TemperatureSensorState();
$sensorValue->setId(9);
$sensorValue->setValue('10.123123123');
$sensorValue->setIdentification('20-00080282b5f8');
......
......@@ -2,14 +2,14 @@
namespace HeatingControl\Test\Entity;
use HeatingControl\State\TemperaturSensorState;
use HeatingControl\State\TemperatureSensorState;
use PHPUnit\Framework\TestCase;
class SensorsValueEntityTest extends TestCase
{
public function testHydrator()
{
$sensorValue = new TemperaturSensorState();
$sensorValue = new TemperatureSensorState();
$sensorValue->setValue('10.123123123');
$sensorValue->setIdentification('10-00080282b5f8');
$sensorValue->setName('innen');
......@@ -23,7 +23,7 @@ class SensorsValueEntityTest extends TestCase
'configured' => false
], $sensorValue->extract());
$sensorValueHydrate = new TemperaturSensorState();
$sensorValueHydrate = new TemperatureSensorState();
$sensorValueHydrate->hydrate($sensorValue->extract());
$this->assertEquals($sensorValue, $sensorValueHydrate);
}
......
......@@ -10,7 +10,7 @@ use HeatingControl\Service\SensorStateService;
use HeatingControl\Service\StateCacheService;
use HeatingControl\Service\TemperatureMappingService;
use HeatingControl\State\RelayState;
use HeatingControl\State\TemperaturSensorState;
use HeatingControl\State\TemperatureSensorState;
use HeatingControl\Test\IntegrationTestCase;
use Psr\Log\LoggerInterface;
......@@ -93,12 +93,12 @@ class HeatingManagementServiceTest extends IntegrationTestCase
$logger
) = $this->getMocks();
$outsideSensor = new TemperaturSensorState();
$outsideSensor = new TemperatureSensorState();
$outsideSensor->setValue($outsideTemp);
$sensorService->getSensorValueBySensorIdentification('10-xx1')->willReturn($outsideSensor);
$returnTempSensor = new TemperaturSensorState();
$returnTempSensor = new TemperatureSensorState();
$returnTempSensor->setValue($returnTemp);
$sensorService->getSensorValueBySensorIdentification('10-xx2')->willReturn($returnTempSensor);
......
......@@ -8,7 +8,7 @@ use DateTimeImmutable;
use HeatingControl\Service\RrdService;
use HeatingControl\Service\SensorStateService;
use HeatingControl\Service\ShellCommandService;
use HeatingControl\State\TemperaturSensorState;
use HeatingControl\State\TemperatureSensorState;
use HeatingControl\Test\IntegrationTestCase;
class RrdServiceTest extends IntegrationTestCase
......@@ -51,10 +51,10 @@ rrdtool graph /home/pi/heating-control/backend/public/images/temp.svg -a SVG --t
EOL,
$service->getImageCommand(
new DateTimeImmutable('2019-04-20 17:15'),
(new TemperaturSensorState())
(new TemperatureSensorState())
->setId(1)
->setName('Außen'),
(new TemperaturSensorState())
(new TemperatureSensorState())
->setId(3)
->setName('Wohnzimmer')
)
......@@ -89,13 +89,13 @@ rrdtool graph /home/pi/heating-control/backend/public/images/temp-week.svg -a SV
EOL,
$service->getImageCommandWeekly(
new DateTimeImmutable('2019-04-20 17:15'),
(new TemperaturSensorState())
(new TemperatureSensorState())
->setId(1)
->setName('Außen'),
(new TemperaturSensorState())
(new TemperatureSensorState())
->setId(3)
->setName('Wohnzimmer'),
(new TemperaturSensorState())
(new TemperatureSensorState())
->setId(2)
->setName('Skipmich')
)
......
......@@ -7,7 +7,7 @@ use HeatingControl\Service\ShellCommandService;
use HeatingControl\Service\SensorStateService;
use HeatingControl\Service\SensorSettingService;
use HeatingControl\Service\StateCacheService;
use HeatingControl\State\TemperaturSensorState;
use HeatingControl\State\TemperatureSensorState;
use PHPUnit\Framework\TestCase;
final class SensorServiceTest extends TestCase
......@@ -36,7 +36,7 @@ final class SensorServiceTest extends TestCase
$sensorValue = $sensorService->getSensorValueBySensorIdentification(self::EXAMPLE_SENSOR_ID);
$this->assertInstanceOf(
TemperaturSensorState::class,
TemperatureSensorState::class,
$sensorValue
);
......@@ -83,7 +83,7 @@ final class SensorServiceTest extends TestCase
$sensorValue = $sensorService->getSensorValueBySensorName('sensor1');
$this->assertInstanceOf(
TemperaturSensorState::class,
TemperatureSensorState::class,
$sensorValue
);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment