Selaa lähdekoodia

发送锁协议修改

whj 4 vuotta sitten
vanhempi
commit
9ffdc83bb0

+ 4 - 2
app/Admin/Controllers/Qrcode.php

xqd
@@ -15,9 +15,11 @@ class Qrcode implements  Renderable
         }
         $url = env('APP_URL').'/test_op?id='.$device->device_name;
         $html = <<<Script
-        <div class="qrcode"></div>
+        <div id="qrcode"></div>
         <script type="text/javascript">
-        new QRCode(document.getElementById("qrcode"), '$url');
+        var  qrcode;
+          document.getElementById("qrcode").innerHTML=""
+          qrcode = new QRCode(document.getElementById("qrcode"), '$url');
           qrcode.makeCode('$url');
         </script>
 Script;

+ 7 - 8
app/Console/Commands/SendDeviceRule.php

xqd xqd xqd
@@ -5,6 +5,7 @@ namespace App\Console\Commands;
 use App\Model\DeviceInfo;
 use App\Model\LockInfo;
 use App\Server\DeviceServer;
+use Doctrine\DBAL\Schema\Schema;
 use Illuminate\Console\Command;
 use Illuminate\Support\Facades\Log;
 
@@ -51,23 +52,23 @@ class SendDeviceRule extends Command
         } else if($type == 2){
             $this->sendBox($id);
         } else if ($type == 3){
-            $this->cronSend($id);
+            $this->cronSend();
         }
         dd('ok');
     }
 
-    public function cronSend($id){
+    public function cronSend(){
         //找出需要重新下发的设备
         $device_info = LockInfo::where(['status'=>1,'send_status'=>1])->distinct('device_id')->pluck('device_id');
         foreach ( $device_info as $device_id){
             //找出要重新下发的锁位
             $lock_info = LockInfo::where(['device_id'=>$device_id,'status'=>1,'send_status'=>1])->distinct('box_type')->pluck('box_type');
-            $device_name = DeviceInfo::where(['device_name'=>$id])->value('device_name');
+            $device_name = DeviceInfo::where(['id'=>$device_id])->value('device_name');
             //没有设备名称跳出
             if(empty($device_name)) continue;
             foreach ($lock_info as $box_type){
                 //没有锁协议跳出
-                $lock = LockInfo::where(['device_id'=>$id,'box_type'=>$box_type])->get();
+                $lock = LockInfo::where(['device_id'=>$device_id,'box_type'=>$box_type])->get();
                 if(empty($lock)) continue;
                 $send_rule = [];
                 foreach ($lock as $protol){
@@ -82,17 +83,15 @@ class SendDeviceRule extends Command
                 $send_rule['query'] = 3600;
                 LockInfo::where(['id'=>$protol->id])->update(['send_status'=>2]);
                 $res = (new DeviceServer())->sendMsg($device_name ,json_encode($send_rule));
-                dd($res);
                 sleep('20');
                 if($res['Success'] == 'success') {
-                    LockInfo::where(['device_id'=>$id,'box_type'=>$box_type])->update(['send_status'=>2]);
+                    LockInfo::where(['device_id'=>$device_id,'box_type'=>$box_type])->update(['send_status'=>2]);
                 }
                 Log::info('执行规则'.json_encode($send_rule).PHP_EOL);
             }
 
         }
-        dd($lock_info->toArray());
-
+       dd('command is runing end');
     }
 
     public function sendDevice($id)

+ 18 - 0
app/Helpers/function.php

xqd xqd
@@ -1,6 +1,8 @@
 <?php
 
 //数组转对象
+use App\Services\Base\ErrorCode;
+
 if(!function_exists('object_array')){
     function object_array($array) {
         if(is_object($array)) {
@@ -14,3 +16,19 @@ if(!function_exists('object_array')){
         return $array;
     }
 }
+
+if(!function_exists('genApiData')){
+    function genApiData( $code = 200, $message = '', $data = '')
+    {
+        if ($code == 200 && empty($message)) {
+            $message = 'success';
+        }
+        $ret = [
+            'status'    => $code,
+            'message'   => $message,
+            'data'      => $data
+        ];
+        return json_encode($ret);
+    }
+}
+

+ 109 - 11
app/Http/Controllers/IndexController.php

xqd xqd xqd xqd xqd xqd xqd
@@ -20,7 +20,6 @@ class IndexController extends Controller
 //        Artisan::call('SDR', [
 //            'id' => 1, 'type' => 1
 //        ]);
-
     //echo strtotime('+60 days');dd('ok');
         $id = $request->get('id');
         $device = DeviceInfo::where(['device_name'=>$id])->first();
@@ -74,9 +73,8 @@ class IndexController extends Controller
         $device  = DeviceInfo::where('device_name',$id)->first();
         $cache_key = $id .'rule_type';
         $re_type = Cache::get($cache_key);
-//        return json_encode(['code'=>200,'msg'=>'下发命令成功','data'=>[]]);
-//dd($re_type);
-        $time1 = 180;//3分钟定时
+
+        $time1 = 240;//3分钟定时
         $opt = 180;
         $cpt = 240;
         if(empty($type) || empty($id)){
@@ -87,21 +85,21 @@ class IndexController extends Controller
         }
 //        echo '缓存类型--'.$re_type.' 当前类型:--'.$type;
 
-        if(empty($re_type)){
-            if($type == 4) Cache::put($cache_key,4,$time1);
+        if(empty($re_type) && in_array([4,5,6],$re_type)){
+            if($type == 4) Cache::put($cache_key,$re_type,$time1);
         } else {
             if($type != $re_type) return json_encode(['code'=>603,'msg'=>'设备下发协议冲突']);
         }
 //        return json_encode(['code'=>200,'msg'=>'不重复']);
         switch ($type){
-            case 1;
+            case 1:
                 $rule = ['cmd'=>'start_test'];
                 break;
-            case 2;
+            case 2:
                 $rule = ["cmd"=>"stop_test"];
                 sleep(6);
                 break;
-            case 3;
+            case 3:
                 $open_time = date('H:i',(time() + $opt));
                 $close_time = date('H:i',(time() + $cpt));
                 $start_time = date('Y-m-d H:i:s',(time() - 60));
@@ -118,7 +116,7 @@ class IndexController extends Controller
 //                    $res = (new DeviceServer())->sendMsg($device->device_name ,$rule);
 //                }
                 break;
-            case 4;
+            case 4:
                 $box_name = request('box_name');
                 if(empty($box_name)){
                     return json_encode(['code'=>601,'msg'=>'缺少必要参数']);
@@ -128,6 +126,13 @@ class IndexController extends Controller
                 $start_time = date('Y-m-d H:i',(time() - 60));
                 $rule = ['box'.$box_name=>[['type'=>1,'start'=>$start_time,'open'=>$open_time,'close'=>$close_time,'value'=>''],],'query'=>3600];
                 break;
+            case 5:
+                $rule = ["cmd"=>"start_update"];
+                break;
+            case 6:
+                $rule = ["cmd"=>"reset"];
+                break;
+
         }
 //        $rule = ["cmd"=>"stop_test"];
 //        dd(json_encode($rule));
@@ -137,7 +142,7 @@ class IndexController extends Controller
         if(!empty($res) && $res['Success'] == true){
             return json_encode(['code'=>200,'msg'=>'下发命令成功']);
         } else {
-            return json_encode(['code'=>400,'msg'=>'下发命令失败']);
+            return json_encode(['code'=>601,'msg'=>'下发命令失败']);
         }
 
 
@@ -168,4 +173,97 @@ class IndexController extends Controller
         return json_encode(['code'=>200,'msg'=>'ok','data'=>$data]);
     }
 
+    //设备升级与重置命令
+    public function updateDevice()
+    {
+        $device_name = request('id');
+        $type = request('type'); //设备类型
+        $phone = request('phone'); //电话
+        $deviceService = (new DeviceServer());
+        if(empty($type) || empty($phone)){
+            return genApiData(601,'参数错误');
+        }
+        if(!preg_match("/^1[34578]\d{9}$/",$phone,$match)){
+            return genApiData(601,'手机号码错误');
+        }
+        $data['device_type'] = $type;
+        $data['phone'] = $phone;
+        $device_info = DeviceInfo::where(['device_name'=>$device_name])->first();
+        if($device_info->device_type == $type){
+            return genApiData(602,'设备类型重复请重新选择');
+        }
+        $device_property = $deviceService->getProperty($device_info->iot_id);
+        $last_version = SystemConfig::get('lock_config','soft_version');
+        $res = DeviceInfo::where('device_name',$device_name)->update($data);
+
+        if($res){
+            if($device_property['data']['version'] == '' || $device_property['data']['version'] != $last_version){
+                $rule = ["cmd"=>"start_update"];
+                $res = $deviceService->sendMsg($device_name ,json_encode($rule));
+                if($res['Success'] == 'success') {
+                    return genApiData(600,'设备更新新中');
+                }
+            } else {
+                $res = DeviceInfo::where('device_name',$device_name)->update($data);
+            }
+            return genApiData(200,'更新成功');
+        }
+        return genApiData(601,'异常设备,请联系后台管理员',$res);
+
+//        if(empty($device_info)){
+//            return genApiData(601,'异常设备,请联系后台管理员','');
+//        }
+//
+//
+//        return genApiData(601,'更新失败','');
+//
+//        if($type == 1){
+//            $rule = ["cmd"=>"start_update"];
+//        } else if ($type == 2){
+//            $rule = ["cmd"=>"reset"];
+//        } else {
+//            $rule = ["cmd"=>"start_update"];
+//        }
+//        $res = $deviceService->sendMsg($device_name,json_encode($rule));
+//        if($res['Success'] == 'success') {
+//            return  json_encode(['code'=>200,'msg'=>'发送命令成功','data'=>'']);
+//        }
+//        return  json_encode(['code'=>601,'msg'=>'发送命令成功','data'=>'']);
+    }
+
 }
+
+
+//use AlibabaCloud\Client\AlibabaCloud;
+//use AlibabaCloud\Client\Exception\ClientException;
+//use AlibabaCloud\Client\Exception\ServerException;
+//
+//// Download:https://github.com/aliyun/openapi-sdk-php
+//// Usage:https://github.com/aliyun/openapi-sdk-php/blob/master/README.md
+//
+//AlibabaCloud::accessKeyClient('<accessKeyId>', '<accessSecret>')
+//    ->regionId('cn-shanghai')
+//    ->asDefaultClient();
+//
+//try {
+//    $result = AlibabaCloud::rpc()
+//        ->product('Iot')
+//        // ->scheme('https') // https | http
+//        ->version('2018-01-20')
+//        ->action('GetThingModelTsl')
+//        ->method('POST')
+//        ->host('iot.cn-shanghai.aliyuncs.com')
+//        ->options([
+//            'query' => [
+//                'RegionId' => "cn-shanghai",
+//                'ProductKey' => "a15yVIP0Onl",
+//                'ModelVersion' => "Software_Version",
+//            ],
+//        ])
+//        ->request();
+//    print_r($result->toArray());
+//} catch (ClientException $e) {
+//    echo $e->getErrorMessage() . PHP_EOL;
+//} catch (ServerException $e) {
+//    echo $e->getErrorMessage() . PHP_EOL;
+//}

+ 11 - 68
app/Server/DeviceServer.php

xqd xqd xqd xqd
@@ -25,7 +25,13 @@ class DeviceServer
     public function __construct()
     {
         $this->property = [
-            'CurrentTemperature' => 'device_tem',
+            'box1up' => 'box1',
+            'box2up' => 'box2',
+            'box3up' => 'box3',
+            'box4up' => 'box4',
+            'box5up' => 'box5',
+            'box6up' => 'box6',
+            'Software_Version' => 'version',
             'IMEI' => 'imei',
            // 'GeoLocation' => 'location',
             'MagDoorSwitch' => 'lock_switch',
@@ -193,7 +199,7 @@ class DeviceServer
         }
         $ali = new AliYunIotServer($conf['appKey'], $conf['appSecret']);
         $deviceProperty = $ali->getDeviceProperty($iot_id);
-        dd($deviceProperty);
+//        dd($deviceProperty);
         if (isset($deviceProperty['Success']) && $deviceProperty['Success']) {
             $data = [];
             foreach ($deviceProperty['Data']['List']['PropertyStatusInfo'] as $index => $item) {
@@ -201,7 +207,9 @@ class DeviceServer
                     if (isset($item['Value'])) {
                         $data[$this->property[$item['Identifier']]] = $item['Value'];
                     } else {
-                        return ['data' => ['ErrorMessage' => "请检查设备属性[{$item['Identifier']}]是否初始化"], 'code' => 4];
+                        $data[$this->property[$item['Identifier']]] = '';
+
+//                        return ['data' => ['ErrorMessage' => "请检查设备属性[{$item['Identifier']}]是否初始化"], 'code' => 4];
                     }
                 }
             }
@@ -570,69 +578,4 @@ class DeviceServer
         return $data;
     }
 
-//    /** 投递门
-//     * @param string $iot_id
-//     * @param int $mode
-//     * @return string
-//     */RRpcRequestRRpcRequest
-//    public function doorInOperation(string $iot_id, int $mode = self::OPEN)
-//    {
-//        $conf = $this->conf;
-//
-//        if (!isset($conf['appSecret']) || empty($conf['appSecret'])) {
-//            return '请先配置阿里云app秘钥';
-//        }
-//
-//        if (!isset($conf['appKey']) || empty($conf['appKey'])) {
-//            return '请先配置阿里云appKey';
-//        }
-//        $ali = new AliYunIotServer($conf['appKey'], $conf['appSecret']);
-//        if ($mode == self::CLOSE) {
-//            $identifier = 'RemoteClose';
-//        } else {
-//            $identifier = 'RemoteOpen';
-//        }
-//        $args = (object)[];
-//        $res = $ali->invokeThingService($iot_id, $identifier, $args);
-//        Log::info(json_encode($res));
-//        if (isset($res['Success']) && $res['Success']) {
-//            DeviceInfo::where('iot_id', $iot_id)->update(['deliver_lock_switch' => $mode]);
-//            return 'success';
-//        } else {
-//            return $res['ErrorMessage'];
-//        }
-//    }
-//
-//
-//    /** 收运门
-//     * @param string $iot_id
-//     * @param int $mode
-//     * @return string
-//     */
-//    public function doorOutOperation(string $iot_id, int $mode = self::OPEN)
-//    {
-//        $conf = $this->conf;
-//
-//        if (!isset($conf['appSecret']) || empty($conf['appSecret'])) {
-//            return '请先配置阿里云app秘钥';
-//        }
-//
-//        if (!isset($conf['appKey']) || empty($conf['appKey'])) {
-//            return '请先配置阿里云appKey';
-//        }
-//        $ali = new AliYunIotServer($conf['appKey'], $conf['appSecret']);
-//        if ($mode == self::CLOSE) {
-//            $identifier = 'RemoteAgentClose';
-//        } else {
-//            $identifier = 'RemoteAgentOpen';
-//        }
-//        $args = (object)[];
-//        $res = $ali->invokeThingService($iot_id, $identifier, $args);
-//        if (isset($res['Success']) && $res['Success']) {
-//            DeviceInfo::where('iot_id', $iot_id)->update(['lock_switch' => $mode]);
-//            return 'success';
-//        } else {
-//            return $res['ErrorMessage'];
-//        }
-//    }
 }

+ 3 - 0
composer.json

xqd
@@ -39,6 +39,9 @@
         "psr-4": {
             "App\\": "app/"
         },
+        "files": [
+            "app/Helpers/function.php"
+        ],
         "classmap": [
             "database/seeds",
             "database/factories"

+ 1 - 0
routes/web.php

xqd
@@ -19,4 +19,5 @@ Route::get('/crawle', 'IndexController@index');
 Route::get('/test_op', 'IndexController@testOpreation');
 Route::get('/get_boxname', 'IndexController@get_boxname');
 Route::get('/send_protocol', 'IndexController@send_protocol');
+Route::get('/updateDevice', 'IndexController@updateDevice');