DeviceEventListener.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. namespace App\Listeners;
  3. use App\Model\DeviceBox;
  4. use App\Model\DeviceInfo;
  5. use App\Model\DeviceType;
  6. use App\Model\DeviceTypeBox;
  7. use App\Model\LockInfo;
  8. use App\Model\SystemConfig;
  9. use Illuminate\Contracts\Queue\ShouldQueue;
  10. use Illuminate\Queue\InteractsWithQueue;
  11. use App\Events\DeviceEvent;
  12. use Illuminate\Support\Carbon;
  13. use Illuminate\Support\Facades\DB;
  14. use Illuminate\Support\Facades\Log;
  15. class DeviceEventListener
  16. {
  17. /**
  18. * Create the event listener.
  19. *
  20. * @return void
  21. */
  22. public function __construct()
  23. {
  24. //
  25. }
  26. /**
  27. * Handle the event.
  28. *
  29. * @param object $event
  30. * @return void
  31. */
  32. public function handle(DeviceEvent $event)
  33. {
  34. $device = $event->device;
  35. if(!empty($device->device_type)){
  36. $this->addBox($device);
  37. Log::info('有数据'.$device->device_type);
  38. } else {
  39. Log::info('没数据'.$device->device_type);
  40. }
  41. return true;
  42. }
  43. public function addBox($device){
  44. $lock_config = SystemConfig::get('lock_config');
  45. DeviceInfo::BOXFOUR ? $n = 4 : $n = 5;
  46. $now = date('Y-m-d H:i:s',time());
  47. for($i=1;$i<=$n;$i++){
  48. $box['box'.$i][] = ['type'=>1,'open_time'=>$lock_config['am_open'],'close_time'=>$lock_config['am_close'],'start_time'=>$now];
  49. $box['box'.$i][] = ['type'=>1,'open_time'=>$lock_config['pm_open'],'close_time'=>$lock_config['pm_close'],'start_time'=>$now];
  50. }
  51. DeviceInfo::where(['id'=>$device->id])->update(['lock_rule'=>json_encode($box)]);
  52. }
  53. }