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