浏览代码

锁协议修改

whj 4 年之前
父节点
当前提交
30b8c90968
共有 3 个文件被更改,包括 37 次插入60 次删除
  1. 18 11
      app/Admin/Controllers/DeviceController.php
  2. 9 35
      app/Listeners/DeviceEventListener.php
  3. 10 14
      app/Model/DeviceInfo.php

+ 18 - 11
app/Admin/Controllers/DeviceController.php

xqd xqd xqd xqd xqd
@@ -48,14 +48,28 @@ class DeviceController extends AdminController
 
         $grid = new Grid(new DeviceInfo());
         $status = DeviceInfo::getStatus();
-        $breakDown = DeviceInfo::getBrakdown();
         $grid->disableCreateButton(false);
-
 //        $exporter = Exporter::get($grid);
 //        $exporter->setFileName('设备导出.xlsx');
+        $grid->disableCreateButton();
         $grid->tools(function (Grid\Tools $tools) {
             $tools->append(new Reflash());
         });
+//        $grid->column('link','二维码')->qrcode()->default('http://www.baidu.com');
+//        $grid->column('device_qrcode', __('二维码'))->display(function (){return '<i class="fa fa-qrcode"></i>';})
+//            ->modal("设备二维码",function (){
+//                $id = $this->id;
+//                $path = "/vendor/laravel-admin/AdminLTE/dist/img/avatar.jpg";
+//                if (file_exists(public_path('upload/DeviceQRCODE/'.$id.'.jpg')))
+//                    $path = '/upload/DeviceQRCODE/'.$id.'.jpg?'.rand(1,99999);
+//                $html = <<<HTML
+//                <img id="show_$id" style="width: 300px;height: 350px;;" src="$path" alt="">
+// <p>避免一次性从微信服务器加载多个造成缓慢,请手动点击: <a href="javascript:loadMiniAppQrcode($id,{$this->id})" class="">加载该设备小程序二维码</a>
+//</p>
+//HTML;
+//
+//                return $html;
+//            });
         $grid->model()->orderBy('status','asc');
         $grid->header(function (){
             return "<script src='/js/loadMiniApp.js?24'></script><style>.dropdown-menu{}</style>";
@@ -64,7 +78,7 @@ class DeviceController extends AdminController
         $grid->column('device_name', __('设备名称'))->editable();
         $grid->column('device_type', __('类型'))->display(function ($w){
             if(empty($w)) return '无';
-            return DeviceType::where(['id'=>$w])->value('name');
+            return DeviceInfo::getTypes()[$w];
         });
 
         $grid->column('query', __('刷新时间'));
@@ -76,13 +90,9 @@ class DeviceController extends AdminController
         });
 
 
-
-
         $grid->column('updated_at', __('更新时间'))->sortable();
-
         $grid->actions(function ($actions){
             $actions->disableDelete(false);
-            $actions->add(new Box());
             $actions->add(new Lock());
             $actions->add(new sendRule());
             $actions->add(new getRule());
@@ -91,9 +101,6 @@ class DeviceController extends AdminController
         $grid->filter(function (Grid\Filter $filter){
             $filter->column(1/2, function ($filter) {
                 $filter->like('device_name','设备名称');
-                $filter->like('device_type','设备类型')->select(function (){
-                    return DeviceType::pluck('name','id');
-                });
                 $filter->equal('status','状态')->select(DeviceInfo::getStatus());
             });
         });
@@ -155,7 +162,7 @@ class DeviceController extends AdminController
         } else {
             $form->display('id', __('ID'));
         }
-        $form->select('device_type', __('设备类型'))->options(DeviceType::getType())->rules('required');
+        $form->select('device_type', __('设备类型'))->options(DeviceInfo::getTypes())->rules('required');
         $form->display('device_name', __('(阿里云)设备编号'))->rules('required');
         $form->display('iot_id', __('(阿里云)iot_id'))->rules('required');
         $form->display('product_key', __('(阿里云)product_key'))->help('阿里产品密匙')->rules('required');

+ 9 - 35
app/Listeners/DeviceEventListener.php

xqd xqd
@@ -3,9 +3,11 @@
 namespace App\Listeners;
 
 use App\Model\DeviceBox;
+use App\Model\DeviceInfo;
 use App\Model\DeviceType;
 use App\Model\DeviceTypeBox;
 use App\Model\LockInfo;
+use App\Model\SystemConfig;
 use Illuminate\Contracts\Queue\ShouldQueue;
 use Illuminate\Queue\InteractsWithQueue;
 use App\Events\DeviceEvent;
@@ -46,41 +48,13 @@ class DeviceEventListener
     }
 
     public function  addBox($device){
-        $boxmodel = DeviceTypeBox::where(['type'=>intval($device->device_type)])->get(['volume','name','id']);
-        if(!empty($boxModel)){
-           Log::info('有盒子'.count($boxmodel).'个');
-       } else {
-           Log::info('没有盒子 设备类型'.intval($device->device_type));
-       }
-        $data['device_id'] = $device->id;
-        $data['created_at'] = Carbon::now();
-        $data['updated_at'] = Carbon::now();
-        DB::beginTransaction();
-        try {
-            $shou_name = array_column($boxmodel->toArray(),'name');
-            $box_names = DeviceBox::where(['device_id'=>$device->id])->whereNotIn('name',$shou_name)->pluck('name','id')->toArray();
-            //删除盒子与锁
-            if(!empty($box_names)){
-                foreach ($box_names as $id =>$name){
-                    DeviceBox::where(['name'=>$name,'device_id'=>$device->id])->delete();
-                    LockInfo::where(['box_id'=>$id])->delete();
-                    //更新锁命令
-                }
-            }
-
-            foreach ($boxmodel as $val){
-                $data['name'] = $val->name;
-                $data['volume'] = $val->volume;
-                if($old = DeviceBox::where(['name'=>$val->name,'device_id'=>$device->id])->first()){
-                    DeviceBox::where(['id'=>$old->id])->update($data);
-                    continue;
-                }
-                DeviceBox::insert($data);
-            }
-            DB::commit();
-        } catch (\Exception $e){
-            Log::info('错误信息'.$e->getMessage().PHP_EOL);
-            DB::rollBack();
+        $lock_config = SystemConfig::get('lock_config');
+        DeviceInfo::BOXFOUR ?  $n = 4 : $n = 5;
+        $now = date('Y-m-d H:i:s',time());
+        for($i=1;$i<=$n;$i++){
+            $box['box'.$i][] = ['type'=>1,'open_time'=>$lock_config['am_open'],'close_time'=>$lock_config['am_close'],'start_time'=>$now];
+            $box['box'.$i][] = ['type'=>1,'open_time'=>$lock_config['pm_open'],'close_time'=>$lock_config['pm_close'],'start_time'=>$now];
         }
+        DeviceInfo::where(['id'=>$device->id])->update(['lock_rule'=>json_encode($box)]);
     }
 }

+ 10 - 14
app/Model/DeviceInfo.php

xqd xqd xqd xqd
@@ -13,11 +13,11 @@ class DeviceInfo extends Model
     //public $dispatcher;
 
     const ONLINE = 0, OFFLINE = 1, DISABLE = 2, UNACTIVE = 3;  //在营 离线 禁用 未激活  运行状态 (status)
+    const BOXFOUR = 1, BOXFIVE = 2; //四箱 五箱
     const OK = 0, ERROR = 1; // 正常 故障 设备状态 (runningStatus)
     const LOCK = 1, UNLOCK = 0;
     protected $table = "device_info";
     protected $guarded = [];
-//['在营','离线','禁用','未激活']
     /*
      *运行状态
      */
@@ -33,7 +33,6 @@ class DeviceInfo extends Model
     /*
      *是否故障
      */
-
     private static $_brakdown =[
         self::OK=>'正常',
         self::ERROR=>'故障',
@@ -42,6 +41,14 @@ class DeviceInfo extends Model
         return self::$_status;
     }
 
+    private static $_types =[
+        self::BOXFOUR=>'四箱',
+        self::BOXFIVE=>'五箱',
+    ];
+    protected static function getTypes(){
+        return self::$_types;
+    }
+
     /*
      *是否关锁
      */
@@ -56,16 +63,5 @@ class DeviceInfo extends Model
     protected $dispatchesEvents = [
         'updated'=>DeviceEvent::class
     ];
-
-    public static function getRestRatio($list){
-        $capacitySecurity = SystemConfig::get('system_config', 'capacitySecurity', '800');
-        $list['max_rest'] = $list['max_rest'] == 0 ? 1800 : $list['max_rest'];
-        if (($list['max_rest'] - $list['rest_capacity'] - $capacitySecurity) == 0) {
-            $value =  0;
-        } else {
-            $value = ($list['max_rest'] - $list['rest_capacity'] - $capacitySecurity) / ($list['max_rest'] - $capacitySecurity) * 100;
-        }
-        return $value;
-    }
-
+    
 }