SystemlService.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. namespace App\Service;
  3. use App\Model\Device;
  4. use App\Model\DeviceType;
  5. use App\Model\ScreenDevice;
  6. use App\Model\SystemL;
  7. class SystemlService extends Service
  8. {
  9. public static function getLastData($code = []){
  10. if(empty($code)) return [];
  11. $time = strtotime('-1 min',time());
  12. $time = $time * 1000;
  13. $result = SystemL::whereIn('device_no',$code)
  14. ->where('push_time','>=',$time)
  15. ->select('data_point_name','value','device_no')
  16. ->orderBy('id','desc')
  17. ->get()->toArray();
  18. $return = [];
  19. if(! empty($result)){
  20. $map = Device::where('del_time',0)
  21. ->whereIn('code',$code)
  22. ->pluck('device_type_id','code')
  23. ->toArray();
  24. foreach ($result as $value){
  25. $tmp = $map[$value['device_no']] ?? 0;
  26. if(! $tmp) continue;
  27. if(isset($return[$value['device_no']])) continue;
  28. //只取每个设备最新的一条数据
  29. $return[$value['device_no']] = [
  30. 'data_point_name' => $value['data_point_name'],
  31. 'value' => $value['value']
  32. ];
  33. }
  34. }
  35. return $return;
  36. }
  37. //'gateway_id' => 键,'screen_id' => 值
  38. public static function getIsOnlineStatus($map = []){
  39. if(empty($map)) return [];
  40. //仓是否在线
  41. $result = [];
  42. foreach ($map as $value){
  43. $result[$value] = 0;
  44. }
  45. //设备 和 仓的关系
  46. $return = [];
  47. $device = Device::where('del_time',0)
  48. ->whereIn('gateway_id',array_keys($map))
  49. ->select('code','gateway_id')->get()->toArray();
  50. foreach ($device as $value){
  51. if(isset($map[$value['gateway_id']])){
  52. $return[$value['code']] = $map[$value['gateway_id']];
  53. }
  54. }
  55. //获取最新设备的数据
  56. $data = self::getLastData(array_column($device,'code'));
  57. foreach ($data as $key => $value){
  58. if(isset($return[$key])){
  59. $result[$return[$key]] = 1;
  60. }
  61. }
  62. return $result;
  63. }
  64. public static function getGpsStatus($screen_id){
  65. if(empty($screen_id)) return [];
  66. //仓 载运2/闲置1/停运0
  67. $result = [];
  68. foreach ($screen_id as $value){
  69. $result[$value] = 0;
  70. }
  71. $screen = ScreenDevice::from('screen_device as a')
  72. ->leftJoin('device as b','b.id','a.device_id')
  73. ->leftJoin('screen as c','c.id','a.screen_id')
  74. ->where('b.device_type_id',DeviceType::type_four)
  75. ->whereIn('a.screen_id',$screen_id)
  76. ->select('b.code','c.coordinate','a.screen_id')
  77. ->get()->toArray();
  78. if(empty($screen)) return $result;
  79. $screen_map = array_column($screen,null,'code');
  80. //获取最新设备的数据
  81. $data = self::getLastData(array_column($screen,'code'));
  82. foreach ($data as $key => $value){
  83. if(isset($screen_map[$key])){
  84. $tmp = $screen_map[$key];
  85. $tmp_coordinate = $tmp['coordinate'];
  86. if(isset($result[$tmp['screen_id']]) && $tmp_coordinate){
  87. //计算偏差值
  88. try {
  89. $meter = self::calculateDistance($tmp_coordinate,$value['value']);
  90. if($meter > 200){
  91. $result[$tmp['screen_id']] = 2;
  92. }else{
  93. $result[$tmp['screen_id']] = 1;
  94. }
  95. }catch (\Exception $e){
  96. continue;
  97. }
  98. }
  99. }
  100. }
  101. return $result;
  102. }
  103. public static function calculateDistance($coordinate1, $coordinate2) {
  104. $earthRadius = 6371000; // 地球半径,单位为米
  105. list($lat1,$lon1) = explode(',',$coordinate1);
  106. list($lat2,$lon2) = explode(',',$coordinate2);
  107. $lat1Rad = deg2rad($lat1);
  108. $lon1Rad = deg2rad($lon1);
  109. $lat2Rad = deg2rad($lat2);
  110. $lon2Rad = deg2rad($lon2);
  111. $deltaLat = $lat2Rad - $lat1Rad;
  112. $deltaLon = $lon2Rad - $lon1Rad;
  113. $a = sin($deltaLat / 2) * sin($deltaLat / 2) +
  114. cos($lat1Rad) * cos($lat2Rad) *
  115. sin($deltaLon / 2) * sin($deltaLon / 2);
  116. $c = 2 * atan2(sqrt($a), sqrt(1 - $a));
  117. $distance = $earthRadius * $c;
  118. return $distance;
  119. }
  120. public static function getExceptionStatus($screen_id){
  121. if(empty($screen_id)) return [];
  122. //仓 正常1/异常0
  123. $result = [];
  124. foreach ($screen_id as $value){
  125. $result[$value] = 0;
  126. }
  127. $screen = ScreenDevice::from('screen_device as a')
  128. ->leftJoin('device as b','b.id','a.device_id')
  129. ->whereIn('b.device_type_id',[DeviceType::type_two, DeviceType::type_three])
  130. ->whereIn('a.screen_id',$screen_id)
  131. ->select('b.code','a.screen_id')
  132. ->get()->toArray();
  133. if(empty($screen)) return $result;
  134. $screen_map = array_column($screen,null,'code');
  135. //获取最新设备的数据
  136. $data = self::getLastData(array_column($screen,'code'));
  137. foreach ($data as $key => $value){
  138. if(isset($screen_map[$key])){
  139. $tmp = $screen_map[$key];
  140. if(isset($result[$tmp['screen_id']]) && $value['value'] == 1){
  141. $result[$tmp['screen_id']] = 1;
  142. }
  143. }
  144. }
  145. return $result;
  146. }
  147. }