Skip to content

Commit

Permalink
fix #6
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelpv committed Oct 9, 2016
1 parent f81a83a commit 661cfc0
Show file tree
Hide file tree
Showing 8 changed files with 386 additions and 5 deletions.
111 changes: 111 additions & 0 deletions app/Http/Controllers/AlertController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php

namespace App\Http\Controllers;

use App\Company;
use App\Http\Controllers\Controller;
use App\Entities\User;
use App\Entities\Vehicle;
use App\Entities\Part;
use App\Entities\Gps;
use Log;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Lang;

class AlertController extends Controller
{
public function checkAlerts($company_id, $tireSensor, $vehicle_id)
{
$company = Company::where('id', $company_id)
->first();

$ideal_pressure = (($tireSensor->temperature - 20) / 5.5556) * 0.02 *
$company->ideal_pressure + $company->ideal_pressure;

if (((((1 - $company->delta_pressure) * $ideal_pressure) - 1.5) > $tireSensor->pressure) ||
($tireSensor->pressure > (((1 + $company->delta_pressure) * $ideal_pressure) + 1.5)) ||
$tireSensor->temperature > $company->limit_temperature) { // 1,5 is the sensor accuracy

if (empty($company->alert_date_time == null) ||
$company->alert_date_time == '0000-00-00 00:00:00') {
$diffHours = sprintf('%2d', (strtotime(date("Y-m-d H:i:s") -
strtotime($company->alert_date_time)) / 3600));
if ($diffHours >= 12 && $this->sendAlertMail($company, $vehicle_id, $tireSensor, $ideal_pressure)) {

This comment has been minimized.

Copy link
@felipemelz

felipemelz Oct 21, 2016

Acho que está errado aqui. A intenção é enviar o 1ª email mesmo não estando 12h ou mais de que o alerta foi gerado. A partir de ter enviado o primeiro email, daí sim deve ser enviado um novo email a cada 12h que se passou, se o alarme ainda estiver "ligado".

This comment has been minimized.

Copy link
@rafaelpv

rafaelpv Oct 21, 2016

Author Collaborator

Sim, eu fiz outras correções já. Mas a fórmula está correta?

$company->alert_date_time = null;
$company->save();
}
}
} else {
$company->alert_date_time = null;
$company->save();
}
}

private function sendAlertMail($company, $vehicle_id, $tireSensor, $ideal_pressure)
{
try {
$users = User::select('users.*')
->join('role_user', 'role_user.user_id', '=', 'users.id')
->where('users.company_id', $company->id)
->where('role_user.role_id', 1)
->get();

$vehicle = Vehicle::where('id', $vehicle_id)
->first();

$part = Part::where('id', $tireSensor->part_id)
->first();

$gps = Gps::select('gps.*', 'contacts.name as driver_name')
->join('contacts', 'gps.driver_id', '=', 'contacts.id')
->where('gps.vehicle_id', $company->id)
->orderBy('gps.created_at', 'desc')
->first();

if (!empty($users)) {
foreach ($users as $user) {
$alertType = $this->getAlertType($company, $tireSensor, $ideal_pressure);
$alarm = new \stdClass();
$alarm->vehicle_fleet = $vehicle->fleet;
$alarm->vehicle_plate = $vehicle->plate;
$alarm->vehicle_driver = $gps->driver_name;
$alarm->tire_number = $part->position;
$alarm->type = $alertType['type'];
$alarm->description = $alertType['description'];
$alarm->vehicle_latitude = $gps->latitude;
$alarm->vehicle_longitude = $gps->longitude;
$alarm->vehicle_id = $vehicle->id;

Mail::send('mail-alert', ['alarm' => $alarm], function ($m) use ($user, $vehicle) {
$m->from(env('MAIL_SENDER'), 'fleetany sender');

$m->to($user->email, $user->name)->subject(Lang::get('mails.AlertSubject', [
'vehicle_number' => $vehicle->fleet,
'vehicle_plate' => $vehicle->number,
]));
});
}
}
} catch (\Exception $e) {
Log::info($e->getMessage());
}

return true;
}

private function getAlertType($company, $tireSensor, $ideal_pressure)
{
$alertType = [];
if ((((1 - $company->delta_pressure) * $ideal_pressure) - 1.5) > $tireSensor->pressure) {
$alertType['type'] = Lang::get('mails.Pressure');
$alertType['description'] = Lang::get('mails.LowPressure');
} elseif ($tireSensor->temperature > $company->limit_temperature) {
$alertType['type'] = Lang::get('mails.Temperature');
$alertType['description'] = Lang::get('mails.HighTemperature');
} else {
$alertType['type'] = Lang::get('mails.Pressure');
$alertType['description'] = Lang::get('mails.HighPressure');
}
return $alertType;
}
}
10 changes: 9 additions & 1 deletion app/Http/Controllers/TireSensorController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Log;
use App\Entities\Type;
use App\Entities\Model;
use App\Http\Controllers\AlertController;

class TireSensorController extends Controller
{
Expand Down Expand Up @@ -48,7 +49,7 @@ public function create(Request $request)
if (isset($json['id']) && isset($json['tp']) && isset($json['pr']) && isset($json['pos'])) {
$part = $this->getPart($user, $json, $inputs);

TireSensor::forceCreate(["latitude" => $this->validateNumeric($json['latitude']),
$tireSensor = TireSensor::forceCreate(["latitude" => $this->validateNumeric($json['latitude']),
"longitude" => $this->validateNumeric($json['longitude']),
//"created_at" => ( $this->validateDate($json['ts']) ?
// $json['ts'] : \DB::raw('NOW()') ),
Expand All @@ -58,6 +59,13 @@ public function create(Request $request)
"battery" => $this->validateNumeric($json['ba']),
"part_id" => $this->validateNumeric($part->id)
]);

$objAlerts = new AlertController();
$objAlerts->checkAlerts(
$user->company_id,
$tireSensor,
$inputs['vehicle_id']
);
}
}
}
Expand Down
33 changes: 33 additions & 0 deletions app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace App\Providers;

use App\Company;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
$this->app->singleton('mailer', function ($app) {
$app->configure('services');
return $app->loadComponent('mail', 'Illuminate\Mail\MailServiceProvider', 'mailer');
});
}

/**
* Boot the authentication services for the application.
*
* @return void
*/
public function boot()
{
}
}
2 changes: 1 addition & 1 deletion bootstrap/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
|
*/

// $app->register(App\Providers\AppServiceProvider::class);
$app->register(App\Providers\AppServiceProvider::class);
$app->register(App\Providers\AuthServiceProvider::class);
// $app->register(App\Providers\EventServiceProvider::class);

Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
"alientronics/laravel-acl": "dev-master",
"prettus/laravel-validation": "1.1.*",
"caouecs/laravel4-lang": "~2.0",
"fzaninotto/faker": "~1.4"
"fzaninotto/faker": "~1.4",
"illuminate/mail": "5.*"
},
"require-dev": {
"phpunit/phpunit": "~4.0",
Expand Down
109 changes: 107 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 661cfc0

Please sign in to comment.