ClearDataService.php 6.4 KB

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