/**
  * Execute the console command.
  *
  * @return mixed
  */
 public function handle()
 {
     //Close records that were left open
     $records = $this->equipmentLogRepository->getActiveRecords();
     foreach ($records as $log) {
         if ($log->last_update && $log->last_update->lt(\Carbon\Carbon::now()->subHour())) {
             //Last update received over an hour ago
             //End the session with the end date being the last update date
             $this->equipmentLogRepository->endSession($log->id, $log->last_update);
         } elseif (!$log->last_update && $log->started->lt(\Carbon\Carbon::now()->subHour())) {
             //started over an hour ago, no updates
             //We don't know how long the user was active so record a minute
             $this->equipmentLogRepository->endSession($log->id, $log->started->addMinute());
         }
     }
     //Combine logs that are very close to each other - this will run over all inactive records that haven't been billed
     $this->combineEquipmentLogs->run();
     //check through all the unbilled inactive records and remove the small ones
     $unbilledRecords = $this->equipmentLogRepository->getUnbilledRecords();
     foreach ($unbilledRecords as $record) {
         $secondsActive = $record->finished->diffInSeconds($record->started);
         //If the record is less than 60 seconds ignore it
         if ($secondsActive <= 60) {
             $record->removed = true;
         }
         //Processing is finished
         //$record->processed = true;
         $record->save();
     }
 }
 private function processEndAction()
 {
     //Close the session
     $sessionId = $this->equipmentLogRepository->findActiveUserSession($this->user->id, $this->deviceKey);
     if ($sessionId !== false) {
         $this->equipmentLogRepository->endSession($sessionId);
     } else {
         $sessionId = $this->equipmentLogRepository->recordStartCloseExisting($this->user->id, $this->keyFob->id, $this->deviceKey, 'inaccurate start');
         $this->equipmentLogRepository->endSession($sessionId);
     }
 }
 /**
  * Show the form for creating a new resource.
  *
  * @return \Illuminate\Http\Response
  *
  * @SWG\Delete(
  *     path="/acs/activity/{activityId}",
  *     tags={"activity"},
  *     description="End a period of an activity",
  *     @SWG\Parameter(name="activityId", in="path", type="string", required=true),
  *     @SWG\Response(response="204", description="Activity ended/deleted"),
  *     @SWG\Response(response="400", description="Session invalid"),
  *     security={{"api_key": {}}}
  * )
  */
 public function destroy(Request $request, $activityId)
 {
     $keyFob = $this->fobAccess->extendedKeyFobLookup($request->get('tagId'));
     $this->equipmentLogRepository->endSession($activityId);
     return response()->json([], 204);
 }