diff --git a/app/Console/Commands/BackupDatabaseCommand.php b/app/Console/Commands/BackupDatabaseCommand.php index 1d0d6ca8..d0f06261 100644 --- a/app/Console/Commands/BackupDatabaseCommand.php +++ b/app/Console/Commands/BackupDatabaseCommand.php @@ -35,13 +35,13 @@ class BackupDatabaseCommand extends Command $backup = Process::path($path)->forever()->run("sudo mariadb-backup --backup --target-dir={$path}/{$time} --user={$user} --password={$password}"); if ($backup->failed()) { - Log::channel('mariadb-backup')->info("Backup failed at {$time}"); + Log::channel('mariadb-backup')->info("Backup failed at {$time} error message : " . $backup->errorOutput()); } $prepare = Process::path($path)->forever()->run("sudo mariadb-backup --prepare --target-dir={$path}/{$time}"); if ($prepare->failed()) { - Log::channel('mariadb-backup')->info("Preparation failed at {$time}"); + Log::channel('mariadb-backup')->info("Preparation failed at {$time} error message : " . $prepare->errorOutput()); } Log::channel('mariadb-backup')->info("Backup successful at {$time}"); diff --git a/app/Console/Commands/RoadObservationProblems.php b/app/Console/Commands/RoadObservationProblems.php index 85d792bc..b585d167 100755 --- a/app/Console/Commands/RoadObservationProblems.php +++ b/app/Console/Commands/RoadObservationProblems.php @@ -19,29 +19,29 @@ use App\Models\City; class RoadObservationProblems extends Command { - /** - * The name and signature of the console command. - * - * @var string - */ - protected $signature = 'webservice:roadobserved {--daily-mode}'; + /** + * The name and signature of the console command. + * + * @var string + */ + protected $signature = 'webservice:roadobserved {--daily-mode}'; - /** - * The console command description. - * - * @var string - */ - protected $description = 'get road observed problems from 141 (samaneh sawaneh)'; + /** + * The console command description. + * + * @var string + */ + protected $description = 'get road observed problems from 141 (samaneh sawaneh)'; - /** - * Create a new command instance. - * - * @return void - */ - public function __construct() - { - parent::__construct(); - } + /** + * Create a new command instance. + * + * @return void + */ + public function __construct() + { + parent::__construct(); + } /** * Execute the console command. @@ -49,105 +49,90 @@ class RoadObservationProblems extends Command * @return int|string * @throws Exception */ - public function handle(NikarayanService $nikarayanService, NominatimService $nominatimService) - { - // try { - // $soap_client = new SoapClient($url); - // $result = $soap_client->Get_RoadObservedProblems_Current(array('strUserName' => 'vaytelrop', 'strPassword' => 'VaYtelROP*2024')); - // } catch (SoapFault $e) { - // echo $e->getMessage(); - // $msg = 'rms: error in get road observation webservice babe!!!!'."\n"; - // $msg .= $e->getMessage(); - // // self::sms_sender('09398586633', $msg); - // // self::sms_sender('09367487107', $msg); - // return 0; - // } + public function handle(NikarayanService $nikarayanService, NominatimService $nominatimService) + { + $time = $this->option('daily-mode') ? verta()->subDay() : verta(); - $time = $this->option('daily-mode') ? verta()->subDay() : verta(); + $result = $nikarayanService->getRoadObservedProblemsByDate($time); - $result = $nikarayanService->getRoadObservedProblemsByDate($time); + $result = serialize($result); + $x = strpos($result, 'fk_FeatureType >= 1 && $value->fk_FeatureType <= 6) { //// code ghabli + if (RoadObserved::query()->where('fk_RegisteredEventMessage', '=', $value->fk_RegisteredEventMessage)->exists()) { + continue; + } - $affectedRows = 0; - $cities = []; - foreach ($xml as $value) { - // if ($value->fk_FeatureType >= 1 && $value->fk_FeatureType <= 6) { //// code ghabli - if (RoadObserved::query()->where('fk_RegisteredEventMessage', '=', $value->fk_RegisteredEventMessage)->exists()) { - continue; + if ($value->fk_FeatureType >= 1 && $value->fk_FeatureType <= 24) { + $mobile = null; + + if ($value->MobileForSendEventSms && (strlen($value->MobileForSendEventSms) == 11)) { + $mobile = $value->MobileForSendEventSms; + } + + $cityModel = City::findCityWithLatLng($value->XGeo, $value->YGeo); + $rop = RoadObserved::query()->updateOrCreate( + ['fk_RegisteredEventMessage' => $value->fk_RegisteredEventMessage], + [ + 'fk_RegisteredEventMessage' => $value->fk_RegisteredEventMessage ?? null, + 'Title' => $value->Title ?? null, + 'StartTime' => $value->StartTime ?? null, + 'EndTime' => $value->EndTime ?? null, + 'StartDate' => $value->StartDate ?? null, + 'EndDate' => $value->EndDate ?? null, + 'StartTime_DateTime' => $value->StartTime_DateTime ? Carbon::createFromFormat('m/d/Y h:i:s A', $value->StartTime_DateTime)->format('Y-m-d H:i:s') : null, + 'StartTime_DateTime_fa' => Verta::instance($value->StartTime_DateTime) ?? null, + 'EndTime_DateTime' => $value->EndTime_DateTime ?? null, + 'AutoID' => $value->AutoID ?? null, + 'fk_Country' => $value->fk_Country ?? null, + 'ProvinceName' => $value->ProvinceName ?? null, + 'fk_Province' => $value->fk_Province ?? null, + 'fk_Town' => $value->fk_Town ?? null, + 'TownName' => $value->TownName ?? null, + 'lng' => $value->XGeo ?? null, + 'lat' => $value->YGeo ?? null, + 'XLAM' => $value->XLAM ?? null, + 'YLAM' => $value->YLAM ?? null, + 'WeatherName' => $value->WeatherName ?? null, + 'fk_Weather' => $value->fk_Weather ?? null, + 'EventTypeTitle' => $value->EventTypeTitle ?? null, + 'fk_EventType' => $value->fk_EventType ?? null, + 'FeatureTypeTitle' => $value->FeatureTypeTitle ?? null, + 'fk_FeatureType' => $value->fk_FeatureType ?? null, + 'Description' => (strlen($value->Description) > 5) ? $value->Description : null, + 'MobileForSendEventSms' => $mobile, +// 'rms_province_id' => $rmsProvinceId[(int) $value->fk_Province] ?? null, +// 'rms_city_id' => $rmsCityId[(int) $value->fk_Town]["InvarID"] ?? null, +// 'observed_way_id' => self::get_way_id_from_nominatim($value->XGeo, $value->YGeo) ?? null, + 'rms_province_id' => NikarayanComplaints::mapProvinces((int)$value->fk_Province) ?? null, + 'rms_city_id' => NikarayanComplaints::mapCities((int)$value->fk_Town) ?? null, + 'observed_way_id' => $nominatimService->get_way_id_from_nominatim($value->XGeo, $value->YGeo) ?? null, + 'province_id' => $cityModel->province()->first()->id, + 'city_id' => $cityModel->id, + // 'handler_type' => EdarateShahri::class, + 'edarate_shahri_id' => $cityModel->edarateShahri()->first()->id, + 'province_fa' => $cityModel->province()->first()->name_fa, + 'city_fa' => $cityModel ? $cityModel->name_fa : null, + ] + ); + $affectedRows += 1; + } } - - if ($value->fk_FeatureType >= 1 && $value->fk_FeatureType <= 24) { - $mobile = null; - - if ($value->MobileForSendEventSms && (strlen($value->MobileForSendEventSms) == 11)) { - $mobile = $value->MobileForSendEventSms; - } - - $city = findCityFromGeoJson($value->YGeo, $value->XGeo); - $cityModel = $city ? City::query()->find($city['city_id']) : City::query()->find(NikarayanComplaints::mapCities((int) $value->fk_Town)); - $rop = RoadObserved::query()->updateOrCreate( - ['fk_RegisteredEventMessage' => $value->fk_RegisteredEventMessage], - [ - 'fk_RegisteredEventMessage' => $value->fk_RegisteredEventMessage ?? null, - 'Title' => $value->Title ?? null, - 'StartTime' => $value->StartTime ?? null, - 'EndTime' => $value->EndTime ?? null, - 'StartDate' => $value->StartDate ?? null, - 'EndDate' => $value->EndDate ?? null, - 'StartTime_DateTime' => $value->StartTime_DateTime ? Carbon::createFromFormat('m/d/Y h:i:s A', $value->StartTime_DateTime)->format('Y-m-d H:i:s') : null, - 'StartTime_DateTime_fa' => Verta::instance($value->StartTime_DateTime) ?? null, - 'EndTime_DateTime' => $value->EndTime_DateTime ?? null, - 'AutoID' => $value->AutoID ?? null, - 'fk_Country' => $value->fk_Country ?? null, - 'ProvinceName' => $value->ProvinceName ?? null, - 'fk_Province' => $value->fk_Province ?? null, - 'fk_Town' => $value->fk_Town ?? null, - 'TownName' => $value->TownName ?? null, - 'lng' => $value->XGeo ?? null, - 'lat' => $value->YGeo ?? null, - 'XLAM' => $value->XLAM ?? null, - 'YLAM' => $value->YLAM ?? null, - 'WeatherName' => $value->WeatherName ?? null, - 'fk_Weather' => $value->fk_Weather ?? null, - 'EventTypeTitle' => $value->EventTypeTitle ?? null, - 'fk_EventType' => $value->fk_EventType ?? null, - 'FeatureTypeTitle' => $value->FeatureTypeTitle ?? null, - 'fk_FeatureType' => $value->fk_FeatureType ?? null, - 'Description' => (strlen($value->Description) > 5) ? $value->Description : null, - 'MobileForSendEventSms' => $mobile, -// 'rms_province_id' => $rmsProvinceId[(int) $value->fk_Province] ?? null, -// 'rms_city_id' => $rmsCityId[(int) $value->fk_Town]["InvarID"] ?? null, -// 'observed_way_id' => self::get_way_id_from_nominatim($value->XGeo, $value->YGeo) ?? null, - 'rms_province_id' => NikarayanComplaints::mapProvinces((int) $value->fk_Province) ?? null, - 'rms_city_id' => NikarayanComplaints::mapCities((int) $value->fk_Town) ?? null, - 'observed_way_id' => $nominatimService->get_way_id_from_nominatim($value->XGeo, $value->YGeo) ?? null, - - 'province_id' => $cityModel->province()->first()->id, - 'city_id' => $cityModel->id, - // 'handler_type' => EdarateShahri::class, - 'edarate_shahri_id' => $cityModel->edarateShahri()->first()->id, - 'province_fa' => $cityModel->province()->first()->name_fa, - 'city_fa' => $cityModel ? $cityModel->name_fa : null, - ] - ); - $affectedRows += 1; - } - } - } - else { - $this->info($result); - Log::channel('nikarayan')->error("the received body is empty" . date('Y-m-d H:i:s') . '\t'. $result); - return $result; - } - echo 'done at: ' . date("Y-m-d H:i:s") . "\n"; - echo "{$affectedRows} row's affected"; - return 0; - } + } else { + $this->info($result); + Log::channel('nikarayan')->error("the received body is empty" . date('Y-m-d H:i:s') . '\t' . $result); + return $result; + } + echo 'done at: ' . date("Y-m-d H:i:s") . "\n"; + echo "{$affectedRows} row's affected"; + return 0; + } } diff --git a/app/Models/City.php b/app/Models/City.php index 088c475d..43dde894 100644 --- a/app/Models/City.php +++ b/app/Models/City.php @@ -4,6 +4,7 @@ namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\ModelNotFoundException; class City extends Model { @@ -80,4 +81,30 @@ class City extends Model { return $this->belongsToMany('App\Models\EdarateShahri', 'edarate_shahri_be_city')->withPivot('is_primary','id'); } + + public static function findCityWithLatLng($lat, $lng): Model|null + { + try { + $city = City::query() + ->whereRaw(" + ST_Contains( + geometry, + ST_GeomFromText('POINT($lng $lat)', 4326) + ) + ") + ->firstOrFail(); + } + catch (ModelNotFoundException $exception) { + $city = City::query() + ->orderByRaw(" + ST_Distance( + geometry, + ST_GeomFromText('POINT($lng $lat)', 4326) + ) ASC + ") + ->first(); + } + + return $city; + } }