123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- <?php
- namespace App\Service;
- use App\Model\DeviceData;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Redis;
- class ClearDataService extends Service
- {
- /*
- * 压力传感器数据
- * [ "obj" => array:10 [
- "applicationID" => "2" // 应用ID
- "applicationName" => "cloud2" // 应用名称
- "data" => "A3sAAA=="
- "devEUI" => "24e124126c481114" // 设备EUI
- "deviceName" => "设备二" // 设备名称
- "fCnt" => 6 // 帧计数
- "fPort" => 85 // 应用端口
- "rxInfo" => array:1 [
- 0 => array:8 [
- "altitude" => 0 // 网关海拔
- "latitude" => 0 // 网关经度
- "longitude" => 0 // 网关纬度
- "loRaSNR" => 13.2 // 信噪比
- "mac" => "24e124fffef7887c" // 网关ID
- "name" => "Local Gateway" // 网关名称
- "rssi" => -24 // 信号强度 (dBm)
- "time" => "2023-08-03T05:47:47.337673Z"
- ]
- ]
- "time" => "2023-08-03T05:47:47.337673Z"
- "txInfo" => array:4 [ // 节点信息
- "adr" => true // 设备ADR状态
- "codeRate" => "4/5" // 编码率
- "dataRate" => array:3 [
- "bandwidth" => 125 // 带宽
- "modulation" => "LORA" // LORA调制
- "spreadFactor" => 7 // 扩频因子
- ]
- "frequency" => 473300000 // 使用频率
- ]
- ]
- "pressure" => 0
- ]
- *
- *温度传感器数据
- *
- * ["obj" => array:10 [
- "applicationID" => "1"
- "applicationName" => "cloud"
- "data" => "A2cAAQ=="
- "devEUI" => "24e124126d054217"
- "deviceName" => "设备一"
- "fCnt" => 983
- "fPort" => 85
- "rxInfo" => array:1 [
- 0 => array:8 [
- "altitude" => 0
- "latitude" => 0
- "loRaSNR" => 13.5
- "longitude" => 0
- "mac" => "24e124fffef7887c"
- "name" => "Local Gateway"
- "rssi" => -31
- "time" => "2023-08-02T09:50:44.880803Z"
- ]
- ]
- "time" => "2023-08-02T09:50:44.880803Z"
- "txInfo" => array:4 [
- "adr" => true
- "codeRate" => "4/5"
- "dataRate" => array:3 [
- "bandwidth" => 125
- "modulation" => "LORA"
- "spreadFactor" => 7
- ]
- "frequency" => 471900000
- ]
- ]
- "temperature" => 25.6
- ]
- *
- * */
- public static function saveData($data){
- try{
- //保存数据
- $model = new DeviceData();
- // $model->data = json_encode($data);//源数据
- $model->dev_eui = $data['obj']['devEUI'];
- $model->device_name = $data['obj']['deviceName'];
- $model->source_ip = self::getIP();
- if(isset($data['temperature'])){
- $model->data_type = 1;
- $model->happening_data = $data['temperature'];
- }elseif (isset($data['pressure'])){
- $model->data_type = 2;
- $model->happening_data = $data['pressure'];
- }
- $model->save();
- }catch (\Exception $exception){
- file_put_contents('record_errors.txt',json_encode($data) . PHP_EOL . $exception->getFile().$exception->getLine().$exception->getCode(),8);
- }
- }
- public static function clearData($data){
- $return['is_clear_data'] = 1;
- $return['dev_eui'] = $data['obj']['devEUI'] ?? '';
- if(isset($data['temperature'])){
- $return['value'] = $data['temperature'];
- }elseif (isset($data['pressure'])){
- $return['value'] = $data['pressure'];
- }else{
- $return['value'] = 0;
- }
- return $return;
- }
- public static function getUrl($data){
- //根据业务将数据发送到对应的地址
- // 温度传感器数据包含temperature 字段 压力传感器包含pressure 字段
- $url = 'http://fyy_api.qingyaokeji.com/api/testData';
- $url = '';
- return $url;
- }
- public static function getIP(){
- if (getenv('HTTP_CLIENT_IP')) {
- $ip = getenv('HTTP_CLIENT_IP');
- }
- elseif (getenv('HTTP_X_REAL_IP')) {
- $ip = getenv('HTTP_X_REAL_IP');
- } elseif (getenv('HTTP_X_FORWARDED_FOR')) {
- $ip = getenv('HTTP_X_FORWARDED_FOR');
- $ips = explode(',', $ip);
- $ip = $ips[0];
- } elseif (getenv('REMOTE_ADDR')) {
- $ip = getenv('REMOTE_ADDR');
- } else {
- $ip = '0.0.0.0';
- }
- return $ip;
- }
- //"status": "error",
- // "userType": null,
- // "userDto": null,
- // "token": null,
- // "errorMessage": "用户名或密码不正确",
- // "brushFaceFlag": false,
- // "brushFaceValidationResult": null
- public static function getToken(){
- $token_key = 'big_king_login_token';
- $token = Redis::get($token_key);
- if(! $token){
- $post = array("name" => "admin","password"=>"admin","rememberMe"=>true);
- $header = ['Content-Type:application/json'];
- $curl = curl_init();
- curl_setopt_array($curl, array(
- CURLOPT_URL => 'http://121.36.142.167:7774/jbl/api/mes/login',
- CURLOPT_RETURNTRANSFER => true,
- CURLOPT_ENCODING => '',
- CURLOPT_MAXREDIRS => 10,
- CURLOPT_TIMEOUT => 0,
- CURLOPT_FOLLOWLOCATION => true,
- CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
- CURLOPT_CUSTOMREQUEST => 'POST',
- CURLOPT_POSTFIELDS => json_encode($post),
- CURLOPT_HTTPHEADER => $header,
- ));
- $response = curl_exec($curl);
- curl_close($curl);
- $result = json_decode($response,true);
- if(empty($result['token'])) {
- file_put_contents('big_king_token_error.txt',date('Y-m-d H:i:s'). PHP_EOL . $response .PHP_EOL,8);
- return [false,''];
- }else{
- $token = $result['token'];
- $expire_time = 1728000; //20天
- Redis::set($token_key,$token);
- Redis::expire($token_key, $expire_time);
- return [true,$token];
- }
- }
- return [true,$token];
- }
- }
|