Browse Source

读取数据

master
lijunjie 3 years ago
parent
commit
10fd9f5d97
  1. 108
      app/Http/Controllers/IndexController.php
  2. 40
      app/Http/Services/CommonService.php
  3. 9
      config/custom.php
  4. BIN
      public/image/lora2/kk1_2.png
  5. 10
      resources/views/http/lora22.blade.php

108
app/Http/Controllers/IndexController.php

@ -3,6 +3,7 @@
namespace App\Http\Controllers;
use App\Http\Services\CommonService;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;
@ -248,7 +249,9 @@ class IndexController extends BaseController
public function lora2()
{
return view('http.lora22');
Cache::flush();
return view('http.lora2');
}
public function getPelmaStore()
@ -279,9 +282,39 @@ class IndexController extends BaseController
{
// echo Cache::get('blood');
$data = request('blood');
Cache::put('blood', $data);
Log::info($data);
$request_data = request()->all();
$lora_mac = config("custom.lora_mac");
if (empty($request_data['node']) || empty($request_data['data'])){
return 'error';
}
if (!in_array($request_data['node'], $lora_mac)){
return 'error';
}
//血氧80.1
$blood = substr($request_data['data'], 0, 4);
$blood1 = hexdec(substr($blood, 0, 2));
$blood2 = substr($blood, 2, 2);
$blood2 = CommonService::customHex2dec($blood2);
Cache::put('blood' . $request_data['node'], ($blood1 + $blood2) * 100);
//体温36.1
$temperature = substr($request_data['data'], 4, 4);
$temperature1 = hexdec(substr($temperature, 0, 2));
$temperature2 = substr($temperature, 2, 2);
$temperature2 = CommonService::customHex2dec($temperature2);
Cache::put('temperature' . $request_data['node'], $temperature1 + $temperature2);
//心跳70
$heart = substr($request_data['data'], 8, 4);
$heart = hexdec(substr($heart, 0, 2));
Cache::put('heart' . $request_data['node'], $heart);
//脉搏70
$heart2 = substr($request_data['data'], 12, 4);
$heart2 = hexdec(substr($heart2, 0, 2));
Cache::put('heart2' . $request_data['node'], $heart2);
return 'ok';
}
@ -325,55 +358,68 @@ class IndexController extends BaseController
*/
public function loraDeviceList()
{
return [
[
'name' => '设备1',
'mac' => '61',
'online' => 0,
],
[
'name' => '设备2',
'mac' => '62',
'online' => 0,
],
[
'name' => '设备3',
'mac' => '63',
$lora_mac = config("custom.lora_mac");
$devices = [];
foreach ($lora_mac as $key => $mac){
$devices[] = [
'name' => '设备' . ($key + 1),
'mac' => $mac,
'online' => 0,
],
];
}
return $devices;
}
public function loraData2()
{
$time = (int)(time() . '000');
$blood = [
$request_data = request()->all();
$node = $request_data['node'] ?? config("custom.lora_mac")[0];
$return_data = [];
$blood_data = Cache::get('blood' . $node, '');
if ($blood_data){
$return_data['blood'] = [
[
$time,
80,
$blood_data,
]
];
}
$temperature = [
$temperature_data = Cache::get('temperature' . $node, '');
if ($temperature_data){
$return_data['temperature'] = [
[
$time,
36.1,
$temperature_data,
]
];
}
$heart = [
$heart_data = Cache::get('heart' . $node, '');
if ($heart_data){
$return_data['heart'] = [
[
$time,
78,
$heart_data,
]
];
}
$humidity = [
$heart2_data = Cache::get('heart2' . $node, '');
if ($heart2_data){
$return_data['humidity'] = [
[
$time,
81.4,
$heart2_data,
]
];
}
$total = [50, 70, 90, 85, 80, 75, 70, 65, 60, 55];
@ -386,13 +432,7 @@ class IndexController extends BaseController
];
}
return [
'blood' => $blood,
'temperature' => $temperature,
'heart' => $heart,
'humidity' => $humidity,
'total_data' => $total_data,
];
return $return_data;
}
/**

40
app/Http/Services/CommonService.php

@ -0,0 +1,40 @@
<?php
namespace App\Http\Services;
class CommonService{
/**
* 十进制小数转十六进制
* @param float $decimal
* @param $len
* @return string
*/
public static function customDec2hex(float $decimal, $len = 8) {
$hex = '';
$count = 0;
# 只取小数部分
$decimal = fmod($decimal, 1);
while ($count++ < $len) {
$dec16 = $decimal * 16;
# 取整 转16进制
$hex .= dechex((int)$dec16);
# 取小数部分
$decimal = fmod($dec16, 1);
}
return $hex;
}
/**
* 十六进制小数转十进制
* @param $hexadecimal
* @return float|int
*/
public static function customHex2dec($hexadecimal) {
$dec = 0;
for ($i = 0; $i < strlen($hexadecimal); $i++) {
$dec += hexdec($hexadecimal[$i]) / pow(16, $i + 1);
}
return $dec;
}
}

9
config/custom.php

@ -0,0 +1,9 @@
<?php
return [
'lora_mac' => [
// "616162626364",
// "616162626365",
"616162626366",
],
];

BIN
public/image/lora2/kk1_2.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 979 B

After

Width:  |  Height:  |  Size: 3.6 KiB

10
resources/views/http/lora22.blade.php

@ -384,7 +384,7 @@
_rawData.temperature.forEach(value => data_temperature.push(value));
_rawData.heart.forEach(value => data_heart.push(value));
_rawData.humidity.forEach(value => data_humidity.push(value));
_rawData.total_data.forEach(value => data_total.push(value));
// _rawData.total_data.forEach(value => data_total.push(value));
myChart.setOption({
series: [
@ -401,10 +401,10 @@
name: '湿度',
data: data_humidity
},
{
name: '总体',
data: data_total
}
// {
// name: '总体',
// data: data_total
// }
]
});
});

Loading…
Cancel
Save