-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
386 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
rafaelpv
Author
Collaborator
|
||
$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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
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".