|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace AppBundle\Variable; |
|
4
|
|
|
|
|
5
|
|
|
use AppBundle\Action\Executor\ExecutorInterface; |
|
6
|
|
|
use AppBundle\Entity\Variable; |
|
7
|
|
|
use AppBundle\Entity\VariableHistory; |
|
8
|
|
|
use AppBundle\Variable\Parser\ParserInterface; |
|
9
|
|
|
use Doctrine\Bundle\DoctrineBundle\Registry; |
|
10
|
|
|
use Doctrine\ORM\EntityManager; |
|
11
|
|
|
use Symfony\Component\Config\Definition\Exception\Exception; |
|
12
|
|
|
|
|
13
|
|
|
class Service |
|
14
|
|
|
{ |
|
15
|
|
|
|
|
16
|
|
|
private $doctrine; |
|
17
|
|
|
private $needSync = false; |
|
18
|
|
|
private $syncHost; |
|
19
|
|
|
private $actionService; |
|
20
|
|
|
|
|
21
|
4 |
|
public function __construct(Registry $doctrine, \AppBundle\Action\Service $actionService, $needSync, $syncHost) |
|
22
|
|
|
{ |
|
23
|
4 |
|
$this->doctrine = $doctrine; |
|
24
|
4 |
|
$this->needSync = $needSync; |
|
25
|
4 |
|
$this->syncHost = $syncHost; |
|
26
|
4 |
|
$this->actionService = $actionService; |
|
27
|
4 |
|
} |
|
28
|
|
|
|
|
29
|
4 |
|
private function getDoctrine() |
|
30
|
|
|
{ |
|
31
|
4 |
|
return $this->doctrine; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
4 |
|
public function get($varName) |
|
35
|
|
|
{ |
|
36
|
4 |
|
$vars = $this->getDoctrine()->getRepository('AppBundle:Variable'); |
|
37
|
|
|
|
|
38
|
|
|
/** @var Variable $var */ |
|
39
|
4 |
|
$var = $vars->findOneBy(['name'=>$varName]); |
|
40
|
4 |
|
if (!$var) { |
|
41
|
1 |
|
throw new Exception('Variable '.$varName.' not found'); |
|
42
|
|
|
} |
|
43
|
3 |
|
return $var; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
2 |
|
public function set($varName, $value) |
|
47
|
|
|
{ |
|
48
|
2 |
|
$vars = $this->getDoctrine()->getRepository('AppBundle:Variable'); |
|
49
|
|
|
|
|
50
|
|
|
/** @var Variable $var */ |
|
51
|
2 |
|
$var = $vars->findOneBy(['name'=>$varName]); |
|
52
|
2 |
|
if (!$var) { |
|
53
|
|
|
throw new Exception('Variable '.$varName.' not found'); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
2 |
|
$parser = 'AppBundle\Variable\Parser\\'.ucfirst($var->getParser()); |
|
57
|
|
|
|
|
58
|
2 |
|
if (!class_exists($parser)) { |
|
59
|
|
|
throw new Exception('Unknown parser: '.$parser); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** @var ParserInterface $parser */ |
|
63
|
2 |
|
$parser = new $parser(); |
|
64
|
|
|
|
|
65
|
2 |
|
$value = $parser->parse($value); |
|
66
|
|
|
|
|
67
|
2 |
|
if (!$value) { |
|
68
|
1 |
|
return false; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
2 |
|
if ($this->needSync) { |
|
72
|
|
|
if ($var->needSync) { |
|
73
|
|
|
@file_get_contents($this->syncHost . 'set/' . $varName . '?value=' . $value); |
|
|
|
|
|
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
2 |
|
$var->setValue($value); |
|
78
|
2 |
|
$var->setLaststatus(200); |
|
79
|
2 |
|
$var->setLastupdate(new \DateTime()); |
|
80
|
|
|
|
|
81
|
2 |
|
$this->getDoctrine()->getManagerForClass('AppBundle:Variable')->persist($var); |
|
82
|
|
|
|
|
83
|
2 |
|
if ($var->needHistory) { |
|
84
|
|
|
$state = new VariableHistory(); |
|
85
|
|
|
$state->setVar($var); |
|
86
|
|
|
$state->setTime(new \DateTime()); |
|
87
|
|
|
$state->setValue($value); |
|
88
|
|
|
|
|
89
|
|
|
$this->getDoctrine()->getManagerForClass('AppBundle:VariableHistory')->persist($state); |
|
90
|
|
|
$this->getDoctrine()->getManagerForClass('AppBundle:VariableHistory')->flush(); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
2 |
|
$this->getDoctrine()->getManagerForClass('AppBundle:Variable')->flush(); |
|
94
|
|
|
|
|
95
|
|
|
// hooks run |
|
96
|
|
|
|
|
97
|
2 |
|
$hooks = $this->getDoctrine()->getManager()->getRepository('AppBundle:VarHook')->findBy(['variable'=>$var]); |
|
98
|
|
|
|
|
99
|
2 |
|
foreach ($hooks as $varHook) { |
|
100
|
|
|
list($executor, $method) = explode(':', $varHook->getExecutor()); |
|
101
|
|
|
|
|
102
|
|
|
$executor = 'AppBundle\Action\Executor\\' . ucfirst($executor); |
|
103
|
|
|
|
|
104
|
|
|
if (!class_exists($executor)) { |
|
105
|
|
|
throw new \Exception('Unknown executor: ' . $executor); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** @var ExecutorInterface $executor */ |
|
109
|
|
|
$executor = new $executor(); |
|
110
|
|
|
$executor->setDoctrine($this->getDoctrine()); |
|
111
|
|
|
|
|
112
|
|
|
|
|
113
|
|
|
if (!method_exists($executor, $method)) { |
|
114
|
|
|
throw new \Exception('Unknown executor method: ' . $varHook->getExecutor().'()'); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
$executor->setParameters(json_decode($varHook->getArguments(), true)); |
|
118
|
|
|
|
|
119
|
|
|
try { |
|
120
|
|
|
$result = $executor->{$method}($varHook); |
|
121
|
|
|
} catch (\Exception $exception) { |
|
122
|
|
|
$result = $exception->getMessage(); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
if ($varHook->getType() == 'decider') { |
|
126
|
|
|
if ($result === true) { |
|
127
|
|
|
$this->actionService->executeReal( |
|
128
|
|
|
$varHook->getAction(), |
|
129
|
|
|
'hook', |
|
130
|
|
|
json_decode($varHook->getArguments(), true) |
|
131
|
|
|
); |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
2 |
|
return $value; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @param Variable $variable |
|
141
|
|
|
* @return array |
|
142
|
|
|
*/ |
|
143
|
|
|
public function getDayHistory(Variable $variable) |
|
144
|
|
|
{ |
|
145
|
|
|
/** @var EntityManager $em */ |
|
146
|
|
|
$em = $this->getDoctrine()->getManager(); |
|
147
|
|
|
|
|
148
|
|
|
$q = $em->createQueryBuilder(); |
|
149
|
|
|
$res = $q-> |
|
150
|
|
|
select('AVG(vh.value) as av')-> |
|
151
|
|
|
addSelect('DATE_FORMAT(vh.time,\'%Y-%m-%d %H:00\') as df')-> |
|
152
|
|
|
from('AppBundle:VariableHistory', 'vh')-> |
|
153
|
|
|
where('vh.time >= :date')-> |
|
154
|
|
|
setParameter('date', new \DateTime('-48 hour'))-> |
|
155
|
|
|
andWhere('vh.var = :var_id')-> |
|
156
|
|
|
setParameter('var_id', $variable->getId())-> |
|
157
|
|
|
groupBy('df')-> |
|
158
|
|
|
orderBy('df', 'asc')-> |
|
159
|
|
|
getQuery(); |
|
160
|
|
|
|
|
161
|
|
|
return $res->getArrayResult(); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* @param Variable $variable |
|
166
|
|
|
* @return array |
|
167
|
|
|
*/ |
|
168
|
|
|
public function getLastValue(Variable $variable) |
|
169
|
|
|
{ |
|
170
|
|
|
/** @var EntityManager $em */ |
|
171
|
|
|
$em = $this->getDoctrine()->getManager(); |
|
172
|
|
|
|
|
173
|
|
|
$q = $em->createQueryBuilder(); |
|
174
|
|
|
$res = $q-> |
|
175
|
|
|
select('vh.value as av')-> |
|
176
|
|
|
addSelect('vh.time as df')-> |
|
177
|
|
|
from('AppBundle:VariableHistory', 'vh')-> |
|
178
|
|
|
where('vh.var = :var_id')-> |
|
179
|
|
|
setParameter('var_id', $variable->getId())-> |
|
180
|
|
|
orderBy('df', 'desc')-> |
|
181
|
|
|
setMaxResults(1)-> |
|
182
|
|
|
getQuery(); |
|
183
|
|
|
|
|
184
|
|
|
return $res->getSingleResult(); |
|
185
|
|
|
} |
|
186
|
|
|
} |
|
187
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: