Skip to content
This repository has been archived by the owner on Jul 1, 2020. It is now read-only.

Commit

Permalink
Sistemati gli alert nel settings
Browse files Browse the repository at this point in the history
  • Loading branch information
BroHPotato committed Apr 9, 2020
1 parent bf30037 commit ca45d4e
Show file tree
Hide file tree
Showing 8 changed files with 189 additions and 95 deletions.
31 changes: 30 additions & 1 deletion app/Http/Controllers/SettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Controllers;

use App\Providers\AlertServiceProvider;
use App\Providers\UserServiceProvider;
use Illuminate\Support\Facades\Auth;

Expand All @@ -12,15 +13,18 @@ class SettingsController extends Controller
*
* @return void
*/
private $provider;

public function __construct()
{
$this->middleware('auth');
$this->provider = new AlertServiceProvider();
}

public function edit()
{
$user = Auth::user();
$alerts = [];//todo faker alert
$alerts = $this->provider->findAll();
return view('settings.edit', compact(['user','alerts']));
}

Expand All @@ -47,4 +51,29 @@ public function update()
$service->update($user->getAuthIdentifier(), json_encode($data));
return redirect('/settings/edit');
}

public function updateAlerts()
{
$alerts = $this->provider->findAll();
$data = request()->validate([
'alerts.*' => 'required|numeric'
])['alerts'];
$enable = [];
$disable = [];
foreach ($alerts['enable'] as $a) {
$enable[] = $a->alertId;
}
foreach ($alerts['disable'] as $a) {
$disable[] = $a->alertId;
}
$toEnable = array_diff($data, $enable);
$toDisable = array_diff($enable, $data);
foreach ($toEnable as $e) {
$this->provider->enable($e);
}
foreach ($toDisable as $d) {
$this->provider->disable($d);
}
return redirect('/settings/edit');
}
}
17 changes: 17 additions & 0 deletions app/Models/Alert.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Alert extends Model
{
protected $fillable = ['threshold', 'type', 'deleted', 'entity', 'sensor', 'lastSent', 'alertId'];
private $relType = ['minore di', 'maggiore di'];


public function getType()
{
return $this->relType[$this->type];
}
}
83 changes: 83 additions & 0 deletions app/Providers/AlertServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

namespace App\Providers;

use App\Models\Alert;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use Illuminate\Support\Facades\Auth;

class AlertServiceProvider extends BasicProvider
{
private $request;

/**
* EntityServiceProvider constructor.
*/
public function __construct()
{
parent::__construct(app());
$this->request = new Client([
'base_uri' => config('app.api') . '/alerts',
'headers' => [
'Content-Type' => 'application/json'
]
]);
}

/**
* @param mixed $identifier
* @return Alert
*/
public function find($identifier)
{
try {
$response = json_decode($this->request->get('/alerts/' . $identifier, $this->setHeaders())->getBody());
$alert = new Alert();
$alert->fill((array)$response);
return $alert;
} catch (RequestException $e) {
$this->isExpired($e);
abort($e->getCode(), $e->getResponse()->getReasonPhrase());
return null;
}
}

/**
* @return array|null
*/
public function findAll()
{
try {
$response = json_decode($this->request->get('', $this->setHeaders())->getBody());
$alerts = ['enable' => [], 'disable' => []];
foreach ($response->enabled as $e) {
$alert = new Alert();
$alert->fill((array)$e);
$alerts['enable'][] = $alert;
}
foreach ($response->disabled as $e) {
$alert = new Alert();
$alert->fill((array)$e);
$alerts['disable'][] = $alert;
}
return $alerts;
} catch (RequestException $e) {
$this->isExpired($e);
abort($e->getCode(), $e->getResponse()->getReasonPhrase());
return null;
}
}
public function disable($identifier)
{
$this->request->post('/alerts/' . $identifier, array_merge($this->setHeaders(), [
'query' => ['userId' => Auth::id(), 'enable' => false]
]));
}
public function enable($identifier)
{
$this->request->post('/alerts/' . $identifier, array_merge($this->setHeaders(), [
'query' => ['userId' => Auth::id(), 'enable' => true]
]));
}
}
4 changes: 2 additions & 2 deletions app/Providers/DeviceServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public function findAllFromEntity($entity)
{
try {
$response = json_decode($this->request->get('', array_merge($this->setHeaders(), [
'query' => 'entityId=' . $entity
'query' => ['entityId' => $entity]
]))->getBody());
$devices = [];
foreach ($response as $d) {
Expand All @@ -98,7 +98,7 @@ public function findAllFromGateway($gateway)
{
try {
$response = json_decode($this->request->get('', array_merge($this->setHeaders(), [
'query' => 'gatewayId=' . $gateway
'query' => ['gatewayId' => $gateway]
]))->getBody());
$devices = [];
foreach ($response as $d) {
Expand Down
8 changes: 4 additions & 4 deletions app/Providers/EntityServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function __construct()
{
parent::__construct(app());
$this->request = new Client([
'base_uri' => config('app.api') . '/entities/',
'base_uri' => config('app.api') . '/entities',
'headers' => [
'Content-Type' => 'application/json'
]
Expand All @@ -41,7 +41,7 @@ public function __construct()
public function find($identifier)
{
try {
$response = json_decode($this->request->get($identifier, $this->setHeaders())->getBody());
$response = json_decode($this->request->get('/entities/' . $identifier, $this->setHeaders())->getBody());
$entity = new Entity();
$entity->fill((array)$response);
return $entity;
Expand Down Expand Up @@ -77,11 +77,11 @@ public function findAll()
* @param $deviceId
* @return Entity|null
*/
public function findFromDevice($deviceId)
public function findFromSensor($sensorId)
{
try {
$response = json_decode($this->request->get('', array_merge($this->setHeaders(), [
'query' => 'deviceId=' . $deviceId
'query' => 'sensor=' . $sensorId
]))->getBody());
$entity = new Entity();
$entity->fill((array)$response);
Expand Down
127 changes: 40 additions & 87 deletions resources/views/settings/edit.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,95 +148,48 @@
<div class="card-header py-3">
<h6 class="m-0 font-weight-bold text-primary"><span class="fas fa-bell"></span> Notifiche alert</h6>
</div>
<!-- TODO implementare la paginazione in JS una volta realizzate le pagine sensori e dispositivi -->
<div class="card-body">
<div class="table-responsive-sm">
<table class="table table-bordered table-striped border-secondary">
<thead class="thead-dark table-borderless">
<tr>
<th width="1em" class="bg-secondary"><span class="far fa-bell"></span></th>
<th>Dispositivo</th>
<th>Sensore</th>
<th>Soglia</th>
<th>Valore</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<tr>
<td><input type="checkbox" checked name="alerts[]" value="id_sensore_db"></td>
<td>Disp1</td>
<td>Sens1</td>
<td>maggiore di</td>
<td>10</td>
<td><span class="badge badge-success">Attivo</span></td>
</tr>
<tr>
<td><input type="checkbox" checked name="alerts[]" value="id_sensore_db"></td>
<td>Disp1</td>
<td>Sens1</td>
<td>maggiore di</td>
<td>10</td>
<td><span class="badge badge-success">Attivo</span></td>
</tr>
<tr>
<td><input type="checkbox" checked name="alerts[]" value="id_sensore_db"></td>
<td>Disp1</td>
<td>Sens1</td>
<td>maggiore di</td>
<td>10</td>
<td><span class="badge badge-success">Attivo</span></td>
</tr>
<tr>
<td><input type="checkbox" checked name="alerts[]" value="id_sensore_db"></td>
<td>Disp1</td>
<td>Sens1</td>
<td>maggiore di</td>
<td>10</td>
<td><span class="badge badge-success">Attivo</span></td>
</tr>
<tr>
<td><input type="checkbox" checked name="alerts[]" value="id_sensore_db"></td>
<td>Disp1</td>
<td>Sens1</td>
<td>maggiore di</td>
<td>10</td>
<td><span class="badge badge-success">Attivo</span></td>
</tr>
<tr>
<td><input type="checkbox" checked name="alerts[]" value="id_sensore_db"></td>
<td>Disp1</td>
<td>Sens1</td>
<td>maggiore di</td>
<td>10</td>
<td><span class="badge badge-success">Attivo</span></td>
</tr>
<tr>
<td><input type="checkbox" checked name="alerts[]" value="id_sensore_db"></td>
<td>Disp1</td>
<td>Sens1</td>
<td>maggiore di</td>
<td>10</td>
<td><span class="badge badge-success">Attivo</span></td>
</tr>
<tr>
<td><input type="checkbox" checked name="alerts[]" value="id_sensore_db"></td>
<td>Disp1</td>
<td>Sens1</td>
<td>maggiore di</td>
<td>10</td>
<td><span class="badge badge-success">Attivo</span></td>
</tr>
<tr>
<td><input type="checkbox" checked name="alerts[]" value="id_sensore_db"></td>
<td>Disp1</td>
<td>Sens1</td>
<td>maggiore di</td>
<td>10</td>
<td><span class="badge badge-success">Attivo</span></td>
</tr>
</tbody>
</table>
<form action="{{route('settings.updateAlerts')}}" method="POST">
@csrf
@method('POST')
<table class="table table-bordered table-striped border-secondary">
<thead class="thead-dark table-borderless">
<tr>
<th width="1em" class="bg-secondary"><span class="far fa-bell"></span></th>
<th>Dispositivo</th>
<th>Sensore</th>
<th>Soglia</th>
<th>Valore</th>
<th>Status</th>
</tr>
</thead>
<tbody>
@foreach($alerts as $status => $a)
@foreach($a as $alert)
<tr>
<td><input type="checkbox" @if($status == "enable") checked @endif name="alerts[]" value="{{$alert->alertId}}"></td>
<td>Disp1</td>
<td>{{$alert->sensor}}</td>
<td>{{$alert->getType()}}</td>
<td>{{$alert->threshold}}</td>
@if($status == "enable")
<td><span class="badge badge-success">Attivo</span></td>
@else
<td><span class="badge badge-danger">Disattivo</span></td>
@endif
</tr>
@endforeach
@endforeach
</tbody>
</table>
<button type="submit" class="btn btn-sm btn-success btn-icon-split">
<span class="icon text-white-50">
<span class="fas fa-check-circle"></span>
</span>
<span class="text">Salva</span>
</button>
</form>
</div>
</div>
</div>
Expand Down
13 changes: 12 additions & 1 deletion resources/views/views/show.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@
</span>
<span class="text">Torna indietro</span>
</a>
<a class="btn btn-sm btn-danger btn-icon-split mb-3" href="{{ route('views.destroy', ['viewId'=>$view->viewId]) }}"
onclick="event.preventDefault(); document.getElementById('destroy-view').submit();">
<span class="icon text-white-50">
<span class="fas fa-times"></span>
</span>
<span class="text">Elimina View</span>
</a>
<form id="destroy-view" action="{{ route('views.destroy', ['viewId'=>$view->viewId]) }}" method="POST" style="display: none;">
@csrf
@method('DELETE')
</form>
<div class="row mt-2">
<div class="col-lg-12">
<div class="card shadow mb-4">
Expand All @@ -20,7 +31,7 @@
Aggiungi grafico pagina view
</h6>
</a>
<div class="collapse show" id="collapseAddView"> <!-- TODO rimuovere show una volta implementato il backend -->
<div class="collapse" id="collapseAddView">
<div class="card-body">
<form method="POST" action="{{route('graphs.store', ['viewId'=>$view->viewId])}}">
@csrf
Expand Down
1 change: 1 addition & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

//routes per gestione profilo
Route::get('/settings/edit', 'SettingsController@edit')->name('settings.edit');
Route::post('/settings', 'SettingsController@updateAlerts')->name('settings.updateAlerts');
Route::put('/settings', 'SettingsController@update')->name('settings.update');

//routes per gestione user
Expand Down

0 comments on commit ca45d4e

Please sign in to comment.