/
HWgSTE.class.php
88 lines (70 loc) · 2.48 KB
/
HWgSTE.class.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
<?php
/**
* Драйвер за IP сензор HWg-STE - мери температура и влажност
*
*
* @category bgerp
* @package hwgroup
*
* @author Dimiter Minekov <mitko@extrapack.com>
* @copyright 2006 - 2015 Experta OOD
* @license GPL 3
*
* @since v 0.1
* @title Сензори HWgSTE
*/
class hwgroup_HWgSTE extends sens2_ProtoDriver
{
/**
* Заглавие на драйвера
*/
public $title = 'HWgSTE';
/**
* Описание на входовете
*/
public $inputs = array(
'T' => array('caption' => 'Температура', 'uom' => 'ºC', 'xmlPath' => '/SenSet[1]/Entry[1]/Value[1]'),
'Hr' => array('caption' => 'Влажност', 'uom' => '%', 'xmlPath' => '/SenSet[1]/Entry[2]/Value[1]'),
);
/**
* Подготвя форма с настройки на контролера, като добавя полета с $form->FLD(....)
*
* @see sens2_ControllerIntf
*
* @param core_Form
*/
public function prepareConfigForm($form)
{
$form->FNC('ip', 'ip', 'caption=IP,hint=Въведете IP адреса на устройството, input, mandatory');
$form->FNC('port', 'int(5)', 'caption=Port,hint=Порт, input, mandatory');
// Параметри по подразбиране за настройките
$form->setDefault('port', 80);
}
/**
* Прочита стойностите от сензорните входове
*
* @see sens2_ControllerIntf
*
* @param array $inputs
* @param array $config
* @param array $persistentState
*
* @return mixed
*/
public function readInputs($inputs, $config, &$persistentState)
{
$state = array();
$url = "http://{$config->ip}:{$config->port}/values.xml";
$context = stream_context_create(array('http' => array('timeout' => 4)));
$xml = @file_get_contents($url, false, $context);
if (empty($xml) || !$xml) {
return "Грешка при четене от {$config->ip}:{$config->port}";
}
$parsed = $res = array();
core_Xml::toArrayFlat(simplexml_load_string($xml), $parsed);
foreach ($this->inputs as $param => $details) {
$res[$param] = $parsed[$details['xmlPath']];
}
return $res;
}
}