taurus-api/src/App/Service/TrInfoCollectorService.php

87 lines
2.3 KiB
PHP
Raw Normal View History

<?php
namespace App\Service;
use Symfony\Component\CssSelector\CssSelectorConverter;
use Zend\Config\Config;
use Zend\Http\Client;
class TrInfoCollectorService
{
/**
* @var Config
*/
private $config;
/**
* @var Client
*/
private $httpClient;
/**
* @var array
*/
private $tempSensors = [
'Temp 5' => 'back_left',
'Temp 4' => 'back_middle',
'Temp 3' => 'back_right',
];
/**
* JiraClientService constructor.
* @param Client $client
* @param Config $config
*/
public function __construct(Client $client, Config $config)
{
$this->httpClient = $client;
$this->config = $config;
}
public function getLabTemperatureData()
{
/** @var Config $labTemperatureUrl */
$labTemperatureUrl = $this->config->get('url.labTemperatureUrl');
$response = $this->httpClient
->setUri($labTemperatureUrl)
->send();
if(!$response->isSuccess()) {
throw new \UnexpectedValueException("Bad LAB result", $response->getStatusCode());
}
return $this->parseHtml($response->getBody());
}
private function parseHtml($html): array
{
$cssToXpathConverter = new CssSelectorConverter();
$xpathLabelQuery = $cssToXpathConverter->toXPath('a.sensormenu.isnotpaused');
$xpathValueQuery = $cssToXpathConverter->toXPath('div.graphlabel2');
$xmlErrorHandling = libxml_use_internal_errors(TRUE);
$domDocument = new \DOMDocument();
$domDocument->loadHTML($html);
libxml_clear_errors();
libxml_use_internal_errors($xmlErrorHandling);
$documentXpath = new \DOMXPath($domDocument);
/** @var \DOMNodeList $element */
$element = $documentXpath->query($xpathLabelQuery);
$thing = [];
/** @var \DOMElement $item */
foreach($element as $item) {
$sensorName = trim($item->nodeValue);
if( in_array($sensorName, array_keys($this->tempSensors)) ){
/** @var \DOMNodeList $element */
$valueElement = $documentXpath->query($xpathValueQuery, $item->parentNode->parentNode);
$thing[$this->tempSensors[$sensorName]] = $valueElement->item(0)->nodeValue;
}
}
return $thing;
}
}