/** * Get all devices which should get deleted. * * @return mixed */ public function getDevices() { $date = Carbon::now()->subMinutes(15); $devices = Device::where('updated_at', '<', $date)->get(); $affectedRows = $devices->count(); foreach ($devices as $device) { $device->delete(); } return $affectedRows; }
<?php $I = new FunctionalTester($scenario); $I->wantTo('flush old devices'); $piOne = ['ip' => '192.168.1.101', 'mac' => '11:22:33:44:55:66', 'name' => 'Awesome Pi One', 'created_at' => (new \Carbon\Carbon())->subHour()->toDateTimeString(), 'updated_at' => (new \Carbon\Carbon())->subHour()->toDateTimeString()]; $I->haveRecord('devices', $piOne); $piTwo = ['ip' => '192.168.1.102', 'mac' => '11:22:33:44:55:67', 'name' => 'Awesome Pi Two', 'created_at' => (new \Carbon\Carbon())->subHour()->toDateTimeString(), 'updated_at' => (new \Carbon\Carbon())->now()->toDateTimeString()]; $I->haveRecord('devices', $piTwo); $I->assertEquals(2, \PiFinder\Device::count()); $I->runConsoleCommand('pi:flush'); $I->assertEquals(1, \PiFinder\Device::count()); $I->dontSeeRecord('devices', $piOne); $I->seeRecord('devices', $piTwo);
/** * Handle device pokes. * * @param StoreComputerRequest $request * * @throws \Exception * * @return Response */ public function poke(StoreComputerRequest $request) { $device = Device::firstOrNew(['mac' => $request->mac]); $device->fill($request->all()); $device->group = $request->get('group', null); $device->public = $request->get('public', 'auto'); $device->touch(); event(new ServerWasPoked(array_add($device, 'server_time', Carbon::now()->toDateTimeString()))); return $this->respondPoked($this->transformer->transform($device), $device->id); }
/** * Show the application welcome screen to the user. * * @return Response */ public function index() { $devices = Device::onHomePage()->get(); return view('overview')->with(compact('devices')); }
/** * Show all Devices for the given group. * * @param string $group * * @return Response */ public function show($group) { $devices = Device::where('group', $group)->get(); return view('overview')->with(compact('devices', 'group')); }
/** * Bootstrap the application services. * * @return void */ public function boot() { Device::observe(app('PiFinder\\Observers\\DeviceObserver')); }