ClearDataService.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. namespace App\Service;
  3. use App\Model\DeviceData;
  4. use Illuminate\Support\Facades\Redis;
  5. class ClearDataService extends Service
  6. {
  7. /*
  8. * 压力传感器数据
  9. * [ "obj" => array:10 [
  10. "applicationID" => "2" // 应用ID
  11. "applicationName" => "cloud2" // 应用名称
  12. "data" => "A3sAAA=="
  13. "devEUI" => "24e124126c481114" // 设备EUI
  14. "deviceName" => "设备二" // 设备名称
  15. "fCnt" => 6 // 帧计数
  16. "fPort" => 85 // 应用端口
  17. "rxInfo" => array:1 [
  18. 0 => array:8 [
  19. "altitude" => 0 // 网关海拔
  20. "latitude" => 0 // 网关经度
  21. "longitude" => 0 // 网关纬度
  22. "loRaSNR" => 13.2 // 信噪比
  23. "mac" => "24e124fffef7887c" // 网关ID
  24. "name" => "Local Gateway" // 网关名称
  25. "rssi" => -24 // 信号强度 (dBm)
  26. "time" => "2023-08-03T05:47:47.337673Z"
  27. ]
  28. ]
  29. "time" => "2023-08-03T05:47:47.337673Z"
  30. "txInfo" => array:4 [ // 节点信息
  31. "adr" => true // 设备ADR状态
  32. "codeRate" => "4/5" // 编码率
  33. "dataRate" => array:3 [
  34. "bandwidth" => 125 // 带宽
  35. "modulation" => "LORA" // LORA调制
  36. "spreadFactor" => 7 // 扩频因子
  37. ]
  38. "frequency" => 473300000 // 使用频率
  39. ]
  40. ]
  41. "pressure" => 0
  42. ]
  43. *
  44. *温度传感器数据
  45. *
  46. * ["obj" => array:10 [
  47. "applicationID" => "1"
  48. "applicationName" => "cloud"
  49. "data" => "A2cAAQ=="
  50. "devEUI" => "24e124126d054217"
  51. "deviceName" => "设备一"
  52. "fCnt" => 983
  53. "fPort" => 85
  54. "rxInfo" => array:1 [
  55. 0 => array:8 [
  56. "altitude" => 0
  57. "latitude" => 0
  58. "loRaSNR" => 13.5
  59. "longitude" => 0
  60. "mac" => "24e124fffef7887c"
  61. "name" => "Local Gateway"
  62. "rssi" => -31
  63. "time" => "2023-08-02T09:50:44.880803Z"
  64. ]
  65. ]
  66. "time" => "2023-08-02T09:50:44.880803Z"
  67. "txInfo" => array:4 [
  68. "adr" => true
  69. "codeRate" => "4/5"
  70. "dataRate" => array:3 [
  71. "bandwidth" => 125
  72. "modulation" => "LORA"
  73. "spreadFactor" => 7
  74. ]
  75. "frequency" => 471900000
  76. ]
  77. ]
  78. "temperature" => 25.6
  79. ]
  80. *
  81. * */
  82. public static function saveData($data){
  83. try{
  84. //保存数据
  85. $model = new DeviceData();
  86. // $model->data = json_encode($data);//源数据
  87. $model->dev_eui = $data['obj']['devEUI'];
  88. $model->device_name = $data['obj']['deviceName'];
  89. $model->source_ip = self::getIP();
  90. if(isset($data['temperature'])){
  91. $model->data_type = 1;
  92. $model->happening_data = $data['temperature'];
  93. }elseif (isset($data['pressure'])){
  94. $model->data_type = 2;
  95. $model->happening_data = $data['pressure'];
  96. }
  97. $model->save();
  98. }catch (\Exception $exception){
  99. file_put_contents('record_errors.txt',json_encode($data) . PHP_EOL . $exception->getFile().$exception->getLine().$exception->getCode(),8);
  100. }
  101. }
  102. public static function clearData($data){
  103. $return['is_clear_data'] = 1;
  104. $return['dev_eui'] = $data['obj']['devEUI'] ?? '';
  105. if(isset($data['temperature'])){
  106. $return['value'] = $data['temperature'];
  107. }elseif (isset($data['pressure'])){
  108. $return['value'] = $data['pressure'];
  109. }else{
  110. $return['value'] = 0;
  111. }
  112. return $return;
  113. }
  114. public static function getUrl($data){
  115. //根据业务将数据发送到对应的地址
  116. // 温度传感器数据包含temperature 字段 压力传感器包含pressure 字段
  117. $url = 'http://fyy_api.qingyaokeji.com/api/testData';
  118. $url = '';
  119. return $url;
  120. }
  121. public static function getIP(){
  122. if (getenv('HTTP_CLIENT_IP')) {
  123. $ip = getenv('HTTP_CLIENT_IP');
  124. }
  125. elseif (getenv('HTTP_X_REAL_IP')) {
  126. $ip = getenv('HTTP_X_REAL_IP');
  127. } elseif (getenv('HTTP_X_FORWARDED_FOR')) {
  128. $ip = getenv('HTTP_X_FORWARDED_FOR');
  129. $ips = explode(',', $ip);
  130. $ip = $ips[0];
  131. } elseif (getenv('REMOTE_ADDR')) {
  132. $ip = getenv('REMOTE_ADDR');
  133. } else {
  134. $ip = '0.0.0.0';
  135. }
  136. return $ip;
  137. }
  138. //"status": "error",
  139. // "userType": null,
  140. // "userDto": null,
  141. // "token": null,
  142. // "errorMessage": "用户名或密码不正确",
  143. // "brushFaceFlag": false,
  144. // "brushFaceValidationResult": null
  145. public static function getToken(){
  146. $token_key = 'big_king_login_token';
  147. $token = Redis::get($token_key);
  148. if(! $token){
  149. $url = config('ip.zs');
  150. $post = array("name" => "admin","password"=>"admin","rememberMe"=>true);
  151. $header = ['Content-Type:application/json'];
  152. $curl = curl_init();
  153. curl_setopt_array($curl, array(
  154. CURLOPT_URL => $url . 'jbl/api/mes/login',
  155. CURLOPT_RETURNTRANSFER => true,
  156. CURLOPT_ENCODING => '',
  157. CURLOPT_MAXREDIRS => 10,
  158. CURLOPT_TIMEOUT => 0,
  159. CURLOPT_FOLLOWLOCATION => true,
  160. CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  161. CURLOPT_CUSTOMREQUEST => 'POST',
  162. CURLOPT_POSTFIELDS => json_encode($post),
  163. CURLOPT_HTTPHEADER => $header,
  164. ));
  165. $response = curl_exec($curl);
  166. curl_close($curl);
  167. $result = json_decode($response,true);
  168. if(empty($result['token'])) {
  169. file_put_contents('big_king_token_error.txt',date('Y-m-d H:i:s'). PHP_EOL . $response .PHP_EOL,8);
  170. return [false,''];
  171. }else{
  172. $token = $result['token'];
  173. $expire_time = 1728000; //20天
  174. Redis::set($token_key,$token);
  175. Redis::expire($token_key, $expire_time);
  176. return [true,$token];
  177. }
  178. }
  179. return [true,$token];
  180. }
  181. }