/** * Save the data from CGPS class to DB * * @param CGPS $pcGPS */ private function saveToDb(CGPS $pcGPS) { // Check amount of parts in the data $dataParts = $pcGPS->GetDataPartCount(); // Loop trough the parts for ($i = 0; $i < $dataParts; $i++) { // Select next data package and check if part is valid if (!$pcGPS->SelectDataPart($i) or !$pcGPS->IsValid()) { Log::error($pcGPS->GetLastError() . '. Data string: ' . $pcGPS->GetHttpData()); continue; } // Should we write to the database? if ($this->writeToDB === false) { continue; } // Find the device $IMEI = $pcGPS->GetImei(); // If not in DB add $device = Device::firstOrCreate(array('IMEI' => $IMEI)); // $time1 = $pcGPS->GetUtcTimeMySQL(); // $time2 = $pcGPS->GetUtcTime(); // $time3 = Date("Y-m-d H:i:s", $time2); // Write report for this device to the DB $report = $device->reports()->firstOrCreate(['datetime' => Date("Y-m-d H:i:s", $pcGPS->GetUtcTime()), 'switch' => $pcGPS->GetSwitch(), 'eventId' => $pcGPS->CanGetEventID(), 'lat' => ($pcGPS->CanGetLatLong() or $pcGPS->CanGetLatLongInaccurate()) ? sprintf('%.5f', (double) $pcGPS->GetLatitudeFloat()) : null, 'lon' => ($pcGPS->CanGetLatLong() or $pcGPS->CanGetLatLongInaccurate()) ? sprintf('%.5f', (double) $pcGPS->GetLongitudeFloat()) : null, 'IO' => $pcGPS->GetIO()]); // Save data $reportData = new Data(['data' => $pcGPS->GetBinaryData()]); // $data = $report->data; // $data->data = $pcGPS->GetBinaryData(); $report->data()->save($reportData); // Save voltage if ($pcGPS->CanGetAnalogInputs()) { $report->voltages()->saveMany([new Voltage(['input' => '1', 'value' => $pcGPS->GetAnalogInput1()]), new Voltage(['input' => '2', 'value' => $pcGPS->GetAnalogInput2()]), new Voltage(['input' => '3', 'value' => $pcGPS->GetAnalogInput3()]), new Voltage(['input' => '4', 'value' => $pcGPS->GetAnalogInput4()])]); } } }
})->with('ranks', 'ranks.category')->get(); return view('categories.show', compact('category', 'places')); }); Route::get('/nearby', function () { // -27.49611, 153.00207 -> brisbane $lat = 48.842147; $lon = 2.321984; $radius = 3; $places = \App\Place::select(DB::raw("*, (6371 * acos( cos( radians({$lat}) ) * cos( radians( latitude ) ) * cos( radians( {$lon} ) - radians(longitude) ) + sin( radians({$lat}) ) * sin( radians(latitude) ) )) AS distance"))->having('distance', '<', $radius)->orderby('distance', 'asc')->with('ranks', 'ranks.category')->get(); return view('places.index', compact('places')); }); Route::group(array('prefix' => 'api/v1'), function () { Route::post('/devices', function () { // Save the device $data = \Illuminate\Support\Facades\Input::all(); $device = \App\Device::firstOrCreate($data); return response()->json($device); }); Route::get('/devices', function () { $devices = \App\Device::all(); return response()->json($devices); }); Route::get('/nearby', function () { // -27.49611, 153.00207 -> brisbane $lat = \Illuminate\Support\Facades\Input::get('lat', -27.49611); $lon = \Illuminate\Support\Facades\Input::get('lon', 153.00207); // Check if a valid location $radius = 25; $city = \App\City::select(DB::raw("*, (6371 * acos( cos( radians({$lat}) ) * cos( radians( latitude ) ) * cos( radians( {$lon} ) - radians(longitude) ) + sin( radians({$lat}) ) * sin( radians(latitude) ) )) AS distance"))->having('distance', '<', $radius)->orderby('distance', 'asc')->first(); if (!$city) { abort(400, 'City not supported.');