From 661cfc0dab47eea4a5d82925caac88b34809c8a4 Mon Sep 17 00:00:00 2001 From: Rafael Date: Sun, 9 Oct 2016 19:30:58 -0300 Subject: [PATCH] fix #6 --- app/Http/Controllers/AlertController.php | 111 +++++++++++++++++ app/Http/Controllers/TireSensorController.php | 10 +- app/Providers/AppServiceProvider.php | 33 +++++ bootstrap/app.php | 2 +- composer.json | 3 +- composer.lock | 109 ++++++++++++++++- config/mail.php | 115 ++++++++++++++++++ resources/views/mail-alert.php | 8 ++ 8 files changed, 386 insertions(+), 5 deletions(-) create mode 100644 app/Http/Controllers/AlertController.php create mode 100644 app/Providers/AppServiceProvider.php create mode 100644 config/mail.php create mode 100644 resources/views/mail-alert.php diff --git a/app/Http/Controllers/AlertController.php b/app/Http/Controllers/AlertController.php new file mode 100644 index 0000000..6bf4bf7 --- /dev/null +++ b/app/Http/Controllers/AlertController.php @@ -0,0 +1,111 @@ +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)) { + $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; + } +} diff --git a/app/Http/Controllers/TireSensorController.php b/app/Http/Controllers/TireSensorController.php index 837cb6c..4d1d095 100644 --- a/app/Http/Controllers/TireSensorController.php +++ b/app/Http/Controllers/TireSensorController.php @@ -10,6 +10,7 @@ use Log; use App\Entities\Type; use App\Entities\Model; +use App\Http\Controllers\AlertController; class TireSensorController extends Controller { @@ -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()') ), @@ -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'] + ); } } } diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php new file mode 100644 index 0000000..8f5339e --- /dev/null +++ b/app/Providers/AppServiceProvider.php @@ -0,0 +1,33 @@ +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() + { + } +} diff --git a/bootstrap/app.php b/bootstrap/app.php index 6d126af..6dde3d7 100644 --- a/bootstrap/app.php +++ b/bootstrap/app.php @@ -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); diff --git a/composer.json b/composer.json index 50240e8..f5a109c 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/composer.lock b/composer.lock index 580dbf0..0677670 100644 --- a/composer.lock +++ b/composer.lock @@ -4,8 +4,8 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "hash": "a560ab016e45ef631ab2d65459aa524a", - "content-hash": "62090bbf69fe377fa8cb9ea4a73d28ce", + "hash": "15bd9724b422bcdeea82473076d89553", + "content-hash": "7c2c6d7141e851dccb8bee3d50ba09d9", "packages": [ { "name": "alientronics/fleetany-web", @@ -1247,6 +1247,58 @@ "homepage": "http://laravel.com", "time": "2016-05-12 13:47:10" }, + { + "name": "illuminate/mail", + "version": "v5.2.45", + "source": { + "type": "git", + "url": "https://github.com/illuminate/mail.git", + "reference": "901d2233f619709fb2ad217ffa0454e0bc339a7f" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/illuminate/mail/zipball/901d2233f619709fb2ad217ffa0454e0bc339a7f", + "reference": "901d2233f619709fb2ad217ffa0454e0bc339a7f", + "shasum": "" + }, + "require": { + "illuminate/container": "5.2.*", + "illuminate/contracts": "5.2.*", + "illuminate/support": "5.2.*", + "php": ">=5.5.9", + "psr/log": "~1.0", + "swiftmailer/swiftmailer": "~5.1" + }, + "suggest": { + "aws/aws-sdk-php": "Required to use the SES mail driver (~3.0).", + "guzzlehttp/guzzle": "Required to use the Mailgun and Mandrill mail drivers (~5.3|~6.0).", + "jeremeamia/superclosure": "Required to be able to serialize closures (~2.0)." + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "5.2-dev" + } + }, + "autoload": { + "psr-4": { + "Illuminate\\Mail\\": "" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Taylor Otwell", + "email": "taylor@laravel.com" + } + ], + "description": "The Illuminate Mail package.", + "homepage": "http://laravel.com", + "time": "2016-08-04 14:15:00" + }, { "name": "illuminate/pagination", "version": "v5.2.32", @@ -2159,6 +2211,59 @@ ], "time": "2012-12-21 11:40:51" }, + { + "name": "swiftmailer/swiftmailer", + "version": "v5.4.3", + "source": { + "type": "git", + "url": "https://github.com/swiftmailer/swiftmailer.git", + "reference": "4cc92842069c2bbc1f28daaaf1d2576ec4dfe153" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/swiftmailer/swiftmailer/zipball/4cc92842069c2bbc1f28daaaf1d2576ec4dfe153", + "reference": "4cc92842069c2bbc1f28daaaf1d2576ec4dfe153", + "shasum": "" + }, + "require": { + "php": ">=5.3.3" + }, + "require-dev": { + "mockery/mockery": "~0.9.1" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "5.4-dev" + } + }, + "autoload": { + "files": [ + "lib/swift_required.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Chris Corbyn" + }, + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + } + ], + "description": "Swiftmailer, free feature-rich PHP mailer", + "homepage": "http://swiftmailer.org", + "keywords": [ + "email", + "mail", + "mailer" + ], + "time": "2016-07-08 11:51:25" + }, { "name": "symfony/console", "version": "v3.0.6", diff --git a/config/mail.php b/config/mail.php new file mode 100644 index 0000000..2c0909e --- /dev/null +++ b/config/mail.php @@ -0,0 +1,115 @@ + env('MAIL_DRIVER', 'smtp'), + + /* + |-------------------------------------------------------------------------- + | SMTP Host Address + |-------------------------------------------------------------------------- + | + | Here you may provide the host address of the SMTP server used by your + | applications. A default option is provided that is compatible with + | the Mailgun mail service which will provide reliable deliveries. + | + */ + + 'host' => env('MAIL_HOST', 'smtp.mailgun.org'), + + /* + |-------------------------------------------------------------------------- + | SMTP Host Port + |-------------------------------------------------------------------------- + | + | This is the SMTP port used by your application to deliver e-mails to + | users of the application. Like the host we have set this value to + | stay compatible with the Mailgun e-mail application by default. + | + */ + + 'port' => env('MAIL_PORT', 587), + + /* + |-------------------------------------------------------------------------- + | Global "From" Address + |-------------------------------------------------------------------------- + | + | You may wish for all e-mails sent by your application to be sent from + | the same address. Here, you may specify a name and address that is + | used globally for all e-mails that are sent by your application. + | + */ + + 'from' => [ + 'address' => 'hello@example.com', + 'name' => 'Example', + ], + + /* + |-------------------------------------------------------------------------- + | E-Mail Encryption Protocol + |-------------------------------------------------------------------------- + | + | Here you may specify the encryption protocol that should be used when + | the application send e-mail messages. A sensible default using the + | transport layer security protocol should provide great security. + | + */ + + 'encryption' => env('MAIL_ENCRYPTION', 'tls'), + + /* + |-------------------------------------------------------------------------- + | SMTP Server Username + |-------------------------------------------------------------------------- + | + | If your SMTP server requires a username for authentication, you should + | set it here. This will get used to authenticate with your server on + | connection. You may also set the "password" value below this one. + | + */ + + 'username' => env('MAIL_USERNAME'), + + /* + |-------------------------------------------------------------------------- + | SMTP Server Password + |-------------------------------------------------------------------------- + | + | Here you may set the password required by your SMTP server to send out + | messages from your application. This will be given to the server on + | connection so that the application will be able to send messages. + | + */ + + 'password' => env('MAIL_PASSWORD'), + + /* + |-------------------------------------------------------------------------- + | Sendmail System Path + |-------------------------------------------------------------------------- + | + | When using the "sendmail" driver to send e-mails, we will need to know + | the path to where Sendmail lives on this server. A default path has + | been provided here, which will work well on most of your systems. + | + */ + + 'sendmail' => '/usr/sbin/sendmail -bs', + +]; \ No newline at end of file diff --git a/resources/views/mail-alert.php b/resources/views/mail-alert.php new file mode 100644 index 0000000..45000ee --- /dev/null +++ b/resources/views/mail-alert.php @@ -0,0 +1,8 @@ +{{trans("mails.FleetNumber")}}: {{$alarm->vehicle_fleet}}
+{{trans("mails.VehiclePlate")}}: {{$alarm->vehicle_plate}}
+{{trans("mails.Driver")}}: {{$alarm->vehicle_driver}}
+{{trans("mails.TireNumber")}}: {{$alarm->tire_number}}
+{{trans("mails.AlarmType")}}: {{$alarm->type}}
+{{trans("mails.AlarmDescription")}}: {{$alarm->description}}
+{{trans("mails.VehicleLocation")}}: Latitude: {{$alarm->vehicle_latitude}}, Longitude: {{$alarm->vehicle_longitude}}
+{{url('/')}}/vehicle/{{$alarm->vehicle_id}}