145 lines
3.9 KiB
PHP
145 lines
3.9 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use Illuminate\Http\Request;
|
|
use Carbon\Carbon;
|
|
|
|
|
|
class MeasurementController extends Controller
|
|
{
|
|
/**
|
|
* Display a listing of the resource.
|
|
*
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function get(Request $request)
|
|
{
|
|
$types = ['temperature','humidity','pressure'];
|
|
|
|
$type = $request->input('type', $types[0]);
|
|
$startdate = $request->input('startdate',NULL);
|
|
$enddate = $request->input('enddate',NULL);
|
|
$host = $request->input('host','balkon');
|
|
$model = $request->input('model','bme280');
|
|
|
|
if (preg_match("/^[a-zA-Z0-9_]+/", $model) == 0) $model = 'bme280';
|
|
|
|
if (!in_array($type,$types)) $type = $types[0];
|
|
if ($startdate == NULL || $startdate == '') $startdate = "now()-1d";
|
|
else $startdate = sprintf("'%s'",Carbon::parse($startdate)->toDateTimeString());
|
|
|
|
if ($enddate == NULL || $enddate == '') $enddate = "now()";
|
|
else $enddate = sprintf("'%s'",Carbon::parse($enddate)->toDateTimeString());
|
|
|
|
$q = sprintf("select time,value from %s_value where host='%s' and type='%s' and time >= %s and time <= %s",$model,$host,$type,$startdate,$enddate);
|
|
\Debugbar::info($q);
|
|
$result = \InfluxDB::query($q);
|
|
$points = $result->getPoints();
|
|
$a = [];
|
|
foreach ($points as $p) {
|
|
$a[] = [ Carbon::createFromFormat("Y-m-d\TH:i:s.u+",$p["time"],'UTC'), $p["value"]];
|
|
}
|
|
|
|
return response()->json($a);
|
|
}
|
|
/**
|
|
* Display a listing of the resource.
|
|
*
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function index(Request $request)
|
|
{
|
|
// for t in ('temperature','humidity','pressure')
|
|
$a = [];
|
|
$host = $request->input('host','balkon');
|
|
$model = $request->input('model','bme280');
|
|
|
|
if (preg_match("/^[a-zA-Z0-9_]+/", $model) == 0) $model = 'bme280';
|
|
|
|
|
|
foreach (['temperature','humidity','pressure'] as $type) {
|
|
$q = sprintf("select * from %s_value where host='%s' and type='%s' order by time desc limit 1",$model,$host,$type);
|
|
$result = \InfluxDB::query($q);
|
|
$points = $result->getPoints();
|
|
foreach ($points as $p) {
|
|
$a[$type] = $p["value"];
|
|
$a["time"] = Carbon::createFromFormat("Y-m-d\TH:i:s.u+",$p["time"],'UTC')->toATOMString();
|
|
}
|
|
}
|
|
$q = sprintf("select * from %s_value where host='%s' and type='voltage' order by time desc limit 1",$model,$host,$type);
|
|
$result = \InfluxDB::query($q);
|
|
$points = $result->getPoints();
|
|
foreach ($points as $p) {
|
|
$a["voltage"] = $p["value"];
|
|
}
|
|
return response()->json($a);
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function create()
|
|
{
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function store(Request $request)
|
|
{
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Display the specified resource.
|
|
*
|
|
* @param int $id
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function show($id)
|
|
{
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*
|
|
* @param int $id
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function edit($id)
|
|
{
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @param int $id
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function update(Request $request, $id)
|
|
{
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*
|
|
* @param int $id
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function destroy($id)
|
|
{
|
|
//
|
|
}
|
|
}
|