Skip to content

Commit 25556d9

Browse files
committed
Zone: add Occupant Rest job
runs twice daily, cutoff period for the reset is in Meta
1 parent 03338d0 commit 25556d9

14 files changed

+331
-10
lines changed

app/Console/Kernel.php

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use App\Jobs\Snackspace\LogDebtJob;
66
use Illuminate\Console\Scheduling\Schedule;
7+
use App\Jobs\GateKeeper\ZoneOccupantResetJob;
78
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
89

910
class Kernel extends ConsoleKernel
@@ -42,6 +43,7 @@ protected function schedule(Schedule $schedule)
4243
$schedule->command('horizon:snapshot')->everyFiveMinutes();
4344

4445
$schedule->job(new LogDebtJob)->daily();
46+
$schedule->job(new ZoneOccupantResetJob)->twiceDaily();
4547
}
4648

4749
/**

app/HMS/Entities/GateKeeper/Zone.php

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
class Zone
66
{
7+
const OFF_SITE = 'Off-site';
8+
79
/**
810
* @var int
911
*/

app/HMS/Entities/GateKeeper/ZoneOccupancyLog.php

+48
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,52 @@ public function getTimeEntered()
7373
{
7474
return $this->timeEntered;
7575
}
76+
77+
/**
78+
* @param Zone $zone
79+
*
80+
* @return self
81+
*/
82+
public function setZone(Zone $zone)
83+
{
84+
$this->zone = $zone;
85+
86+
return $this;
87+
}
88+
89+
/**
90+
* @param User $user
91+
*
92+
* @return self
93+
*/
94+
public function setUser(User $user)
95+
{
96+
$this->user = $user;
97+
98+
return $this;
99+
}
100+
101+
/**
102+
* @param Carbon $timeExited
103+
*
104+
* @return self
105+
*/
106+
public function setTimeExited(Carbon $timeExited)
107+
{
108+
$this->timeExited = $timeExited;
109+
110+
return $this;
111+
}
112+
113+
/**
114+
* @param Carbon $timeEntered
115+
*
116+
* @return self
117+
*/
118+
public function setTimeEntered(Carbon $timeEntered)
119+
{
120+
$this->timeEntered = $timeEntered;
121+
122+
return $this;
123+
}
76124
}

app/HMS/Entities/GateKeeper/ZoneOccupant.php

+36-10
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,6 @@ class ZoneOccupant
2222
*/
2323
protected $timeEntered;
2424

25-
/**
26-
* Gets the value of id.
27-
*
28-
* @return int
29-
*/
30-
public function getId()
31-
{
32-
return $this->id;
33-
}
34-
3525
/**
3626
* @return User
3727
*/
@@ -55,4 +45,40 @@ public function getTimeEntered()
5545
{
5646
return $this->timeEntered;
5747
}
48+
49+
/**
50+
* @param User $user
51+
*
52+
* @return self
53+
*/
54+
public function setUser(User $user)
55+
{
56+
$this->user = $user;
57+
58+
return $this;
59+
}
60+
61+
/**
62+
* @param Zone $zone
63+
*
64+
* @return self
65+
*/
66+
public function setZone(Zone $zone)
67+
{
68+
$this->zone = $zone;
69+
70+
return $this;
71+
}
72+
73+
/**
74+
* @param Carbon $timeEntered
75+
*
76+
* @return self
77+
*/
78+
public function setTimeEntered(Carbon $timeEntered)
79+
{
80+
$this->timeEntered = $timeEntered;
81+
82+
return $this;
83+
}
5884
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace HMS\Repositories\GateKeeper\Doctrine;
4+
5+
use HMS\Entities\GateKeeper\ZoneOccupancyLog;
6+
use Doctrine\ORM\EntityRepository;
7+
use HMS\Repositories\GateKeeper\ZoneOccupancyLogRepository;
8+
9+
class DoctrineZoneOccupancyLogRepository extends EntityRepository implements ZoneOccupancyLogRepository
10+
{
11+
/**
12+
* Save ZoneOccupancyLog to the DB.
13+
*
14+
* @param ZoneOccupancyLog $zoneOccupancyLog
15+
*/
16+
public function save(ZoneOccupancyLog $zoneOccupancyLog)
17+
{
18+
$this->_em->persist($zoneOccupancyLog);
19+
$this->_em->flush();
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace HMS\Repositories\GateKeeper\Doctrine;
4+
5+
use HMS\Entities\GateKeeper\ZoneOccupant;
6+
use Doctrine\ORM\EntityRepository;
7+
use HMS\Repositories\GateKeeper\ZoneOccupantRepository;
8+
9+
class DoctrineZoneOccupantRepository extends EntityRepository implements ZoneOccupantRepository
10+
{
11+
/**
12+
* Save ZoneOccupant to the DB.
13+
*
14+
* @param ZoneOccupant $zoneOccupant
15+
*/
16+
public function save(ZoneOccupant $zoneOccupant)
17+
{
18+
$this->_em->persist($zoneOccupant);
19+
$this->_em->flush();
20+
}
21+
}

app/HMS/Repositories/GateKeeper/Doctrine/DoctrineZoneRepository.php

+22
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,28 @@
88

99
class DoctrineZoneRepository extends EntityRepository implements ZoneRepository
1010
{
11+
/**
12+
* Find all zones.
13+
*
14+
* @return Zone[]
15+
*/
16+
public function findAll()
17+
{
18+
return parent::findAll();
19+
}
20+
21+
/**
22+
* Find one zone by short name
23+
*
24+
* @param string $shortName
25+
*
26+
* @return Zone
27+
*/
28+
public function findOneByShortName(string $shortName)
29+
{
30+
return parent::findOneByShortName($shortName);
31+
}
32+
1133
/**
1234
* Save Zone to the DB.
1335
*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace HMS\Repositories\GateKeeper;
4+
5+
use HMS\Entities\GateKeeper\ZoneOccupancyLog;
6+
7+
interface ZoneOccupancyLogRepository
8+
{
9+
/**
10+
* Save ZoneOccupancyLog to the DB.
11+
*
12+
* @param ZoneOccupancyLog $zoneOccupancyLog
13+
*/
14+
public function save(ZoneOccupancyLog $zoneOccupancyLog);
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace HMS\Repositories\GateKeeper;
4+
5+
use HMS\Entities\GateKeeper\ZoneOccupant;
6+
7+
interface ZoneOccupantRepository
8+
{
9+
/**
10+
* Save ZoneOccupant to the DB.
11+
*
12+
* @param ZoneOccupant $zoneOccupant
13+
*/
14+
public function save(ZoneOccupant $zoneOccupant);
15+
}

app/HMS/Repositories/GateKeeper/ZoneRepository.php

+16
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,22 @@
66

77
interface ZoneRepository
88
{
9+
/**
10+
* Find all zones.
11+
*
12+
* @return Zone[]
13+
*/
14+
public function findAll();
15+
16+
/**
17+
* Find one zone by short name
18+
*
19+
* @param string $shortName
20+
*
21+
* @return Zone
22+
*/
23+
public function findOneByShortName(string $shortName);
24+
925
/**
1026
* Save Zone to the DB.
1127
*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
3+
namespace App\Jobs\GateKeeper;
4+
5+
use Carbon\Carbon;
6+
use Carbon\CarbonInterval;
7+
use Illuminate\Bus\Queueable;
8+
use HMS\Entities\GateKeeper\Zone;
9+
use Illuminate\Support\Facades\Log;
10+
use HMS\Repositories\MetaRepository;
11+
use Illuminate\Queue\SerializesModels;
12+
use Illuminate\Queue\InteractsWithQueue;
13+
use Illuminate\Contracts\Queue\ShouldQueue;
14+
use Illuminate\Foundation\Bus\Dispatchable;
15+
use HMS\Entities\GateKeeper\ZoneOccupancyLog;
16+
use HMS\Repositories\GateKeeper\ZoneRepository;
17+
use HMS\Repositories\GateKeeper\ZoneOccupantRepository;
18+
use HMS\Repositories\GateKeeper\ZoneOccupancyLogRepository;
19+
20+
class ZoneOccupantResetJob implements ShouldQueue
21+
{
22+
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
23+
24+
/**
25+
* Create a new job instance.
26+
*
27+
* @return void
28+
*/
29+
public function __construct()
30+
{
31+
//
32+
}
33+
34+
/**
35+
* Execute the job.
36+
*
37+
* @param MetaRepository $metaRepository
38+
* @param ZoneRepository $zoneRepository
39+
* @param ZoneOccupantRepository $zoneOccupantRepository
40+
* @param ZoneOccupancyLogRepository $zoneOccupancyLogRepository
41+
*
42+
* @return void
43+
*/
44+
public function handle(
45+
MetaRepository $metaRepository,
46+
ZoneRepository $zoneRepository,
47+
ZoneOccupantRepository $zoneOccupantRepository,
48+
ZoneOccupancyLogRepository $zoneOccupancyLogRepository
49+
) {
50+
$resetUserCount = 0;
51+
$resetPeriod = CarbonInterval::instance(
52+
new \DateInterval($metaRepository->get('zone_occupant_reset_interval'))
53+
);
54+
$resetIfBeforeDate = Carbon::now()->sub($resetPeriod);
55+
$zones = $zoneRepository->findAll();
56+
$offSiteZone = $zoneRepository->findOneByShortName(Zone::OFF_SITE);
57+
58+
foreach ($zones as $zone) {
59+
if ($zone->getShortName() == Zone::OFF_SITE) {
60+
continue;
61+
}
62+
$zoneOccupants = $zone->getZoneOccupancts();
63+
64+
foreach ($zoneOccupants as $zoneOccupant) {
65+
if ($zoneOccupant->getTimeEntered()->lessThan($resetIfBeforeDate)) {
66+
$now = Carbon::now();
67+
$user = $zoneOccupant->getUser();
68+
69+
// create new log rentry for old zone
70+
$zoneOccupancyLog = (new ZoneOccupancyLog())
71+
->setZone($zone)
72+
->setUser($user)
73+
->setTimeEntered($zoneOccupant->getTimeEntered())
74+
->setTimeExited($now);
75+
76+
// change zone to offiste set enter time
77+
$zoneOccupant->setZone($offSiteZone)
78+
->setTimeEntered($now);
79+
80+
// save them
81+
$zoneOccupantRepository->save($zoneOccupant);
82+
$zoneOccupancyLogRepository->save($zoneOccupancyLog);
83+
84+
$resetUserCount++;
85+
}
86+
}
87+
}
88+
Log::info('ZoneOccupantResetJob: Reset Zone for ' . $resetUserCount . ' users.');
89+
}
90+
}

config/repositories.php

+2
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@
4747
'GateKeeper\RfidTag',
4848
'GateKeeper\Door',
4949
'GateKeeper\Zone',
50+
'GateKeeper\ZoneOccupant',
51+
'GateKeeper\ZoneOccupancyLog',
5052
'GateKeeper\AccessLog',
5153
'Banking\Bank',
5254
'Banking\BankTransaction',

0 commit comments

Comments
 (0)