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
|
|
|
public function __construct(Registry $doctrine, \AppBundle\Action\Service $actionService, $needSync, $syncHost) |
22
|
|
|
{ |
23
|
|
|
$this->doctrine = $doctrine; |
24
|
|
|
$this->needSync = $needSync; |
25
|
|
|
$this->syncHost = $syncHost; |
26
|
|
|
$this->actionService = $actionService; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
private function getDoctrine() |
30
|
|
|
{ |
31
|
|
|
return $this->doctrine; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function get($varName) |
35
|
|
|
{ |
36
|
|
|
$vars = $this->getDoctrine()->getRepository('AppBundle:Variable'); |
37
|
|
|
|
38
|
|
|
/** @var Variable $var */ |
39
|
|
|
$var = $vars->findOneBy(['name'=>$varName]); |
40
|
|
|
if (!$var) { |
41
|
|
|
throw new Exception('Variable '.$varName.' not found'); |
42
|
|
|
} |
43
|
|
|
return $var; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function set($varName, $value) |
47
|
|
|
{ |
48
|
|
|
$vars = $this->getDoctrine()->getRepository('AppBundle:Variable'); |
49
|
|
|
|
50
|
|
|
/** @var Variable $var */ |
51
|
|
|
$var = $vars->findOneBy(['name'=>$varName]); |
52
|
|
|
if (!$var) { |
53
|
|
|
throw new Exception('Variable '.$varName.' not found'); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$parser = 'AppBundle\Variable\Parser\\'.ucfirst($var->getParser()); |
57
|
|
|
|
58
|
|
|
if (!class_exists($parser)) { |
59
|
|
|
throw new Exception('Unknown parser: '.$parser); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** @var ParserInterface $parser */ |
63
|
|
|
$parser = new $parser(); |
64
|
|
|
|
65
|
|
|
$value = $parser->parse($value); |
66
|
|
|
|
67
|
|
|
if (!$value) { |
68
|
|
|
return false; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
if ($this->needSync) { |
72
|
|
|
if ($var->needSync) { |
73
|
|
|
@file_get_contents($this->syncHost.'set/'.$varName.'?value='.$value); |
|
|
|
|
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$var->setValue($value); |
78
|
|
|
$var->setLaststatus(200); |
79
|
|
|
$var->setLastupdate(new \DateTime()); |
80
|
|
|
|
81
|
|
|
$this->getDoctrine()->getManagerForClass('AppBundle:Variable')->persist($var); |
82
|
|
|
|
83
|
|
|
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
|
|
|
$this->getDoctrine()->getManagerForClass('AppBundle:Variable')->flush(); |
94
|
|
|
|
95
|
|
|
|
96
|
|
|
return $value; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param Variable $variable |
101
|
|
|
* @return array |
102
|
|
|
*/ |
103
|
|
|
public function getDayHistory(Variable $variable) |
104
|
|
|
{ |
105
|
|
|
/** @var EntityManager $em */ |
106
|
|
|
$em = $this->getDoctrine()->getManager(); |
107
|
|
|
|
108
|
|
|
$q = $em->createQueryBuilder(); |
109
|
|
|
$res = $q-> |
110
|
|
|
select('AVG(vh.value) as av')-> |
111
|
|
|
addSelect('DATE_FORMAT(vh.time,\'%Y-%m-%d %H:00\') as df')-> |
112
|
|
|
from('AppBundle:VariableHistory', 'vh')-> |
113
|
|
|
where('vh.time >= :date')-> |
114
|
|
|
setParameter('date', new \DateTime('-48 hour'))-> |
115
|
|
|
andWhere('vh.var = :var_id')-> |
116
|
|
|
setParameter('var_id', $variable->getId())-> |
117
|
|
|
groupBy('df')-> |
118
|
|
|
orderBy('df', 'asc')-> |
119
|
|
|
getQuery(); |
120
|
|
|
|
121
|
|
|
return $res->getArrayResult(); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param Variable $variable |
126
|
|
|
* @return array |
127
|
|
|
*/ |
128
|
|
|
public function getLastValue(Variable $variable) |
129
|
|
|
{ |
130
|
|
|
/** @var EntityManager $em */ |
131
|
|
|
$em = $this->getDoctrine()->getManager(); |
132
|
|
|
|
133
|
|
|
$q = $em->createQueryBuilder(); |
134
|
|
|
$res = $q-> |
135
|
|
|
select('vh.value as av')-> |
136
|
|
|
addSelect('vh.time as df')-> |
137
|
|
|
from('AppBundle:VariableHistory', 'vh')-> |
138
|
|
|
where('vh.var = :var_id')-> |
139
|
|
|
setParameter('var_id', $variable->getId())-> |
140
|
|
|
orderBy('df', 'desc')-> |
141
|
|
|
setMaxResults(1)-> |
142
|
|
|
getQuery(); |
143
|
|
|
|
144
|
|
|
return $res->getSingleResult(); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: