add mailer and notification when suspect values

This commit is contained in:
Simon Vieille 2020-03-21 15:26:36 +01:00
parent ef9b4cf421
commit fcd9279533
Signed by: deblan
GPG key ID: 03383D15A1D31745
10 changed files with 66 additions and 2 deletions

View file

@ -22,6 +22,7 @@
"symfony/process": "4.4.*",
"symfony/security-bundle": "4.4.*",
"symfony/serializer-pack": "*",
"symfony/swiftmailer-bundle": "^3.4",
"symfony/translation": "4.4.*",
"symfony/twig-pack": "*",
"symfony/validator": "4.4.*",

View file

@ -13,4 +13,5 @@ return [
Symfony\Bundle\DebugBundle\DebugBundle::class => ['dev' => true, 'test' => true],
Symfony\Bundle\MakerBundle\MakerBundle::class => ['dev' => true],
Symfony\Bundle\WebServerBundle\WebServerBundle::class => ['dev' => true],
Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle::class => ['all' => true],
];

View file

@ -0,0 +1,4 @@
# See https://symfony.com/doc/current/email/dev_environment.html
swiftmailer:
# send all emails to a specific address
#delivery_addresses: ['me@example.com']

View file

@ -0,0 +1,3 @@
swiftmailer:
url: '%env(MAILER_URL)%'
spool: { type: 'memory' }

View file

@ -0,0 +1,2 @@
swiftmailer:
disable_delivery: true

View file

@ -14,7 +14,7 @@ class HygrometryApiController extends AbstractController
/**
* @Route("/api/hygrometry/create", name="api_hygrometry_create", methods={"POST"})
*/
public function create(Request $request, ApiValidator $validator): Response
public function create(Request $request, ApiValidator $validator, \Swift_Mailer $mailer): Response
{
if ('application/json' === $request->getContentType()) {
return $this->json([], 400);
@ -37,6 +37,22 @@ class HygrometryApiController extends AbstractController
$manager->persist($entity);
$manager->flush();
if ($entity->getValue() >= 50) {
$message = (new \Swift_Message('[Terrarium] Alerte hygométrie'))
->setFrom('terrarium@deblan.fr')
->setTo(['simon@deblan.fr', 'jennifer@derago.fr'])
->setBody(
$this->renderView(
'alert/hygrometry.txt.twig',
[
'entity' => $entity,
]
)
);
$mailer->send($message);
}
return $this->json([], 201);
}
}

View file

@ -14,7 +14,7 @@ class TemperatureApiController extends AbstractController
/**
* @Route("/api/temperature/create", name="api_temperature_create", methods={"POST"})
*/
public function create(Request $request, ApiValidator $validator): Response
public function create(Request $request, ApiValidator $validator, \Swift_Mailer $mailer): Response
{
if ('application/json' === $request->getContentType()) {
return $this->json([], 400);
@ -37,6 +37,22 @@ class TemperatureApiController extends AbstractController
$manager->persist($entity);
$manager->flush();
if ($entity->getValue() < 25) {
$message = (new \Swift_Message('[Terrarium] Alerte température'))
->setFrom('terrarium@deblan.fr')
->setTo(['simon@deblan.fr', 'jennifer@derago.fr'])
->setBody(
$this->renderView(
'alert/temperature.txt.twig',
[
'entity' => $entity,
]
)
);
$mailer->send($message);
}
return $this->json([], 201);
}
}

View file

@ -141,6 +141,9 @@
"config/packages/sensio_framework_extra.yaml"
]
},
"swiftmailer/swiftmailer": {
"version": "v6.2.3"
},
"symfony/apache-pack": {
"version": "1.0",
"recipe": {
@ -424,6 +427,20 @@
"symfony/stopwatch": {
"version": "v4.4.5"
},
"symfony/swiftmailer-bundle": {
"version": "2.5",
"recipe": {
"repo": "github.com/symfony/recipes",
"branch": "master",
"version": "2.5",
"ref": "ae4d22af30bbd484506bc1817c5a3ef72c855b93"
},
"files": [
"config/packages/dev/swiftmailer.yaml",
"config/packages/swiftmailer.yaml",
"config/packages/test/swiftmailer.yaml"
]
},
"symfony/test-pack": {
"version": "v1.0.6"
},

View file

@ -0,0 +1,2 @@
Date : {{ entity.date.format('d/m/Y H:i:s') }}
Niveau hygrométrique : {{ entity.value }}%

View file

@ -0,0 +1,2 @@
Date : {{ entity.date.format('d/m/Y H:i:s') }}
Température : {{ entity.value }}°