IndexController.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Model\DeviceInfo;
  4. use App\Model\LockInfo;
  5. use App\Model\SystemConfig;
  6. use App\Server\DeviceServer;
  7. use Illuminate\Http\Request;
  8. use Illuminate\Support\Facades\Blade;
  9. use Illuminate\Support\Facades\Cache;
  10. use QL\QueryList;
  11. class IndexController extends Controller
  12. {
  13. public function testOpreation(Request $request)
  14. {
  15. $id = $request->get('id');
  16. $device = DeviceInfo::where(['id'=>$id])->first();
  17. $rule = json_decode($device->lock_rule);
  18. foreach ($rule as $key=>$box){
  19. $device_box[] = $key;
  20. }
  21. return view('test_opreation',['box'=>$device_box,'device'=>$device]);
  22. }
  23. function object_array($array) {
  24. if(is_object($array)) {
  25. $array = (array)$array;
  26. }
  27. if(is_array($array)) {
  28. foreach($array as $key=>$value) {
  29. $array[$key] = object_array($value);
  30. }
  31. }
  32. return $array;
  33. }
  34. //下发协议
  35. public function send_protocol()
  36. {
  37. $type = request('type');
  38. $id = request('id');
  39. $device = DeviceInfo::where('id',$id)->first();
  40. $cache_key = $device->device_name.'rule_type';
  41. $re_type = Cache::get($cache_key);
  42. $time1 = 30;//3分钟定时
  43. if(empty($type) || empty($id)){
  44. return json_encode(['code'=>601,'msg'=>'缺少必要参数']);
  45. }
  46. if(empty($device)){
  47. return json_encode(['code'=>602,'msg'=>'设备不存在']);
  48. }
  49. // echo '缓存类型--'.$re_type.' 当前类型:--'.$type;
  50. if(empty($re_type)){
  51. if($type == 4) Cache::put($cache_key,4,$time1);
  52. } else {
  53. if($type != $re_type) return json_encode(['code'=>603,'msg'=>'设备下发协议冲突']);
  54. }
  55. // return json_encode(['code'=>200,'msg'=>'不重复']);
  56. switch ($type){
  57. case 1;
  58. $rule = ['cmd'=>'start_test'];
  59. break;
  60. case 2;
  61. $rule = ['cmd'=>'stop_test'];
  62. break;
  63. case 3;
  64. $open_time = date('Y-m-d H:i:s',(time() + 180));
  65. $close_time = date('Y-m-d H:i:s',(time() + 240));
  66. $start_time = date('Y-m-d H:i:s',(time() - 60));
  67. $lock_rule = json_decode($device->lock_rule);
  68. foreach ($lock_rule as $box_name){
  69. sleep(22);
  70. $rule = [$box_name=>['type'=>1,'start_time'=>$start_time,'open_time'=>$open_time,'close_time'=>$close_time,'value'=>''],'query'=>3600];
  71. $res = (new DeviceServer())->sendMsg($device->device_name ,$rule);
  72. }
  73. break;
  74. case 4;
  75. $box_name = request('box_name');
  76. if(empty($box_name)){
  77. return json_encode(['code'=>601,'msg'=>'缺少必要参数']);
  78. }
  79. $open_time = date('Y-m-d H:i:s',(time() + 60));
  80. $close_time = date('Y-m-d H:i:s',(time() + 120));
  81. $start_time = date('Y-m-d H:i:s',(time() - 60));
  82. $rule = ['box'.$box_name=>['type'=>1,'start_time'=>$start_time,'open_time'=>$open_time,'close_time'=>$close_time,'value'=>''],'query'=>3600];
  83. break;
  84. }
  85. //return json_encode(['code'=>200,'msg'=>'下发命令成功','data'=>$rule]);
  86. //$rule = ['cmd'=>'stop_test'];
  87. if($type != 3){
  88. $res = (new DeviceServer())->sendMsg($device->device_name,json_encode($rule));
  89. }
  90. if($res['Success'] == true){
  91. return json_encode(['code'=>200,'msg'=>'下发命令成功']);
  92. } else {
  93. return json_encode(['code'=>400,'msg'=>'下发命令失败']);
  94. }
  95. }
  96. //获取设备信息
  97. public function get_boxname()
  98. {
  99. $id = request('id');
  100. $device = DeviceInfo::where('device_name',$id)->first();
  101. if(empty($device)){
  102. return json_encode(['code'=>200,'msg'=>'ok','data'=>[]]);
  103. }
  104. $lock_config = SystemConfig::get('lock_config');
  105. $data['id'] = $device->id;
  106. $data['device_name'] = $device->device_name;
  107. $data['device_type'] = $device->device_type;
  108. if($device->device_type == 1){
  109. $data['device_image'] = $lock_config['box_img_four'];
  110. } else {
  111. $data['device_image'] = $lock_config['box_img_five'];
  112. }
  113. $data['device_image'] = '';
  114. return json_encode(['code'=>200,'msg'=>'ok','data'=>$data]);
  115. }
  116. }