ClearDataService.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. $dev_eui = $data['obj']['devEUI'];
  85. $device_name = $data['obj']['deviceName'];
  86. $source_ip = self::getIP();
  87. //保存数据
  88. if(isset($data['temperature']) && isset($data['humidity'])){
  89. $insert[] = [
  90. 'dev_eui' => $dev_eui . '1',
  91. 'device_name' => $device_name,
  92. 'source_ip' => $source_ip,
  93. 'data_type' => 4,
  94. 'happening_data' => $data['temperature']
  95. ];
  96. $insert[] = [
  97. 'dev_eui' => $dev_eui . '2',
  98. 'device_name' => $device_name,
  99. 'source_ip' => $source_ip,
  100. 'data_type' => 4,
  101. 'happening_data' => $data['humidity']
  102. ];
  103. DeviceData::insert($insert);
  104. }else{
  105. $model = new DeviceData();
  106. // $model->data = json_encode($data);//源数据
  107. $model->dev_eui = $dev_eui;
  108. $model->device_name = $device_name;
  109. $model->source_ip = $source_ip;
  110. if(isset($data['temperature'])){
  111. $model->data_type = 1;
  112. $model->happening_data = $data['temperature'];
  113. }elseif (isset($data['pressure'])){
  114. $model->data_type = 2;
  115. $model->happening_data = $data['pressure'];
  116. }
  117. $model->save();
  118. }
  119. }catch (\Exception $exception){
  120. file_put_contents('record_errors.txt',json_encode($data) . PHP_EOL . $exception->getFile().$exception->getLine().$exception->getCode(),8);
  121. }
  122. }
  123. public static function clearData($data){
  124. $return['is_clear_data'] = 1;
  125. $return['dev_eui'] = $data['obj']['devEUI'] ?? '';
  126. if(isset($data['temperature']) && isset($data['humidity'])){
  127. $return['multiple'] = [
  128. $return['dev_eui'] . '1' => $data['temperature'],
  129. $return['dev_eui'] . '2' => $data['humidity'],
  130. ];
  131. }elseif(isset($data['temperature'])){
  132. $return['value'] = $data['temperature'];
  133. }elseif (isset($data['pressure'])){
  134. $return['value'] = round($data['pressure'] / 1000,2);
  135. }else{
  136. $return['value'] = 0;
  137. }
  138. return $return;
  139. }
  140. public static function getUrl($data){
  141. //根据业务将数据发送到对应的地址
  142. // 温度传感器数据包含temperature 字段 压力传感器包含pressure 字段
  143. $url = 'http://fyy_api.qingyaokeji.com/api/testData';
  144. $url = '';
  145. return $url;
  146. }
  147. public static function getIP(){
  148. if (getenv('HTTP_CLIENT_IP')) {
  149. $ip = getenv('HTTP_CLIENT_IP');
  150. }
  151. elseif (getenv('HTTP_X_REAL_IP')) {
  152. $ip = getenv('HTTP_X_REAL_IP');
  153. } elseif (getenv('HTTP_X_FORWARDED_FOR')) {
  154. $ip = getenv('HTTP_X_FORWARDED_FOR');
  155. $ips = explode(',', $ip);
  156. $ip = $ips[0];
  157. } elseif (getenv('REMOTE_ADDR')) {
  158. $ip = getenv('REMOTE_ADDR');
  159. } else {
  160. $ip = '0.0.0.0';
  161. }
  162. return $ip;
  163. }
  164. //"status": "error",
  165. // "userType": null,
  166. // "userDto": null,
  167. // "token": null,
  168. // "errorMessage": "用户名或密码不正确",
  169. // "brushFaceFlag": false,
  170. // "brushFaceValidationResult": null
  171. public static function getToken(){
  172. $token_key = 'big_king_login_token';
  173. $token = Redis::get($token_key);
  174. if(! $token){
  175. $url = config('ip.zs');
  176. $post = array("name" => "admin","password"=>"admin","rememberMe"=>true);
  177. $header = ['Content-Type:application/json'];
  178. $curl = curl_init();
  179. curl_setopt_array($curl, array(
  180. CURLOPT_URL => $url . 'jbl/api/mes/login',
  181. CURLOPT_RETURNTRANSFER => true,
  182. CURLOPT_ENCODING => '',
  183. CURLOPT_MAXREDIRS => 10,
  184. CURLOPT_TIMEOUT => 0,
  185. CURLOPT_FOLLOWLOCATION => true,
  186. CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  187. CURLOPT_CUSTOMREQUEST => 'POST',
  188. CURLOPT_POSTFIELDS => json_encode($post),
  189. CURLOPT_HTTPHEADER => $header,
  190. ));
  191. $response = curl_exec($curl);
  192. curl_close($curl);
  193. $result = json_decode($response,true);
  194. if(empty($result['token'])) {
  195. file_put_contents('big_king_token_error.txt',date('Y-m-d H:i:s'). PHP_EOL . $response .PHP_EOL,8);
  196. return [false,''];
  197. }else{
  198. $token = $result['token'];
  199. $expire_time = 1728000; //20天
  200. Redis::set($token_key,$token);
  201. Redis::expire($token_key, $expire_time);
  202. return [true,$token];
  203. }
  204. }
  205. return [true,$token];
  206. }
  207. public static function getTokenCs(){
  208. $token_key = 'big_king_login_token_cs';
  209. $token = Redis::get($token_key);
  210. if(! $token){
  211. $url = config('ip.cs');
  212. $post = array("name" => "admin","password"=>"admin","rememberMe"=>true);
  213. $header = ['Content-Type:application/json'];
  214. $curl = curl_init();
  215. curl_setopt_array($curl, array(
  216. CURLOPT_URL => $url . 'jbl/api/mes/login',
  217. CURLOPT_RETURNTRANSFER => true,
  218. CURLOPT_ENCODING => '',
  219. CURLOPT_MAXREDIRS => 10,
  220. CURLOPT_TIMEOUT => 0,
  221. CURLOPT_FOLLOWLOCATION => true,
  222. CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  223. CURLOPT_CUSTOMREQUEST => 'POST',
  224. CURLOPT_POSTFIELDS => json_encode($post),
  225. CURLOPT_HTTPHEADER => $header,
  226. ));
  227. $response = curl_exec($curl);
  228. curl_close($curl);
  229. $result = json_decode($response,true);
  230. if(empty($result['token'])) {
  231. file_put_contents('big_king_token_error_cs.txt',date('Y-m-d H:i:s'). PHP_EOL . $response .PHP_EOL,8);
  232. return [false,''];
  233. }else{
  234. $token = $result['token'];
  235. $expire_time = 1728000; //20天
  236. Redis::set($token_key,$token);
  237. Redis::expire($token_key, $expire_time);
  238. return [true,$token];
  239. }
  240. }
  241. return [true,$token];
  242. }
  243. }