ClearDataService.php 9.2 KB

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