Commit 3c999777 by root

first test of saving data

parent bb48b780
......@@ -17,9 +17,12 @@ class Kernel extends ConsoleKernel
protected function schedule(Schedule $schedule)
{
// $schedule->command('inspire')->hourly();
$schedule->call(function () {
ShellyLogController::fetch_data();
})->everyMinute();
$schedule->call(function () { ShellyLogController::fetch_data_and_store(); })->everyMinute();
$schedule->call(function () { ShellyLogController::fetch_ten_minute_data(); })->everyTenMinutes();
$schedule->call(function () { ShellyLogController::fetch_hourly_data(); })->hourly();
$schedule->call(function () { ShellyLogController::fetch_daily_data(); })->dailyAt('23:59');
$schedule->call(function () { ShellyLogController::test(); })->weeklyOn(7, '23:59');
$schedule->call(function () { ShellyLogController::test(); })->lastDayOfMonth('23:59');
//$schedule->call('ShellyLogController@test')->everyMinute;
//$schedule->call('App\Http\Controllers\ShellyLogController@fetch_data')->everyMinute();
......
......@@ -4,12 +4,15 @@
use Illuminate\Http\Request;
use App\Models\SolarPowerMinute;
use App\Models\SolarPowerTenMinute;
use App\Models\SolarPowerHour;
use App\Models\SolarPowerDay;
use Carbon\Carbon;
class ShellyLogController extends Controller
{
public function test()
static public function test()
{
$json = file_get_contents('http://admin:Mamueller88*@192.168.252.250/status');
......@@ -23,14 +26,11 @@ public function test()
echo round($sum_power, 2).'W <br>';
}
static public function fetch_data()
static public function fetch_data_and_store()
{
$json = file_get_contents('http://admin:Mamueller88*@192.168.252.250/status');
$data = json_decode($json,true);
$sum_power = $data['meters'][0]['counters'][0];
//echo round($sum_power, 2).'W <br>';
Carbon::now()->format('Y');
$year = Carbon::now()->format('Y');
$month = Carbon::now()->format('m');
......@@ -40,18 +40,13 @@ static public function fetch_data()
$startday = Carbon::create($year, $month, $day, 0, 0, 0, 'Europe/Berlin')->timestamp;
$endday = Carbon::create($year, $month, $day, 23, 59, 59, 'Europe/Berlin')->timestamp;
//dd($startday);
$timestamp = Carbon::now()->timestamp;
$wattnow = $data['meters'][0]['counters'][0];
//$wattnow = 182.53;
$power_last = SolarPowerMinute::take(1)->where('timestamp', '>', $startday)->where('timestamp', '<', $endday)->orderBy('timestamp', 'desc')->get();
//dd($power_last);
$watt_total = $wattnow;
foreach ($power_last as $p) {
//dd($p->timestamp);
if(empty($p->wattminutetotal)){
$watt_total = $wattnow;
......@@ -61,15 +56,92 @@ static public function fetch_data()
}
$log = new SolarPowerMinute([
$data_minute = new SolarPowerMinute([
'timestamp' => $timestamp,
'wattminute' => $wattnow,
'wattminutetotal' => $watt_total
]);
$log->save();
$data_minute->save();
}
static public function fetch_ten_minute_data() {
$timestamp = Carbon::now()->timestamp;
$power_last_ten_minutes = SolarPowerMinute::take(10)->orderBy('timestamp', 'desc')->get();
$power_last_ten_minutes_count = $power_last_ten_minutes->count();
$wattminute_sum = $power_last_ten_minutes->sum('wattminute');
$wattminute_sum_avg = $wattminute_sum / $power_last_ten_minutes_count;
$data_ten_minute = new SolarPowerTenMinute([
'timestamp' => $timestamp,
'wattminute' => $wattminute_sum_avg,
'wattminutetotal' => $wattminute_sum
]);
$data_ten_minute->save();
}
static function fetch_hourly_data() {
$timestamp = Carbon::now()->timestamp;
$power_last_hour = SolarPowerMinute::take(60)->orderBy('timestamp', 'desc')->get();
$power_last_hour_count = $power_last_hour->count();
$wattminute_sum = $power_last_hour->sum('wattminute');
$wattminute_sum_avg = $wattminute_sum / $power_last_hour_count;
$data_hour = new SolarPowerHour([
'timestamp' => $timestamp,
'wattminute' => $wattminute_sum_avg,
'wattminutetotal' => $wattminute_sum
]);
$data_hour->save();
}
static function fetch_daily_data() {
$timestamp = Carbon::now()->timestamp;
$power_last_day = SolarPowerMinute::take(1440)->orderBy('timestamp', 'desc')->get();
$power_last_day_count = $power_last_day->count();
$wattminute_sum = $power_last_day->sum('wattminute');
$wattminute_sum_avg = $wattminute_sum / $power_last_day_count;
$data_day = new SolarPowerDay([
'timestamp' => $timestamp,
'wattminute' => $wattminute_sum_avg,
'wattminutetotal' => $wattminute_sum
]);
$data_day->save();
}
public function fetch_monthly_data() {
}
public function show_chart()
{
//$devices = Devices::whereIn('devices_team', $teams_arr)->get();
$power_last = SolarPowerTenMinute::take(144)->orderBy('timestamp', 'desc')->get();
$power_last_sum = SolarPowerMinute::take(1440)->orderBy('timestamp', 'desc')->get();
$gesamt = round($power_last_sum->sum('wattminute') / 60 / 1000, 2);
//dd($power_last);
$data = [
'data_set' => $power_last,
'gesamt' => $gesamt,
'test' => $power_last->sum('wattminute'),
];
return view('chart', $data);
}
}
......@@ -7,5 +7,7 @@
class SolarPowerDay extends Model
{
use HasFactory;
protected $table = 'solar_power_days';
protected $primaryKey = 'id';
protected $fillable = ['timestamp', 'wattminute', 'wattminutetotal'];
}
......@@ -7,5 +7,7 @@
class SolarPowerHour extends Model
{
use HasFactory;
protected $table = 'solar_power_hours';
protected $primaryKey = 'id';
protected $fillable = ['timestamp', 'wattminute', 'wattminutetotal'];
}
......@@ -5,7 +5,7 @@
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class SolarPowerYear extends Model
class SolarPowerMonth extends Model
{
use HasFactory;
}
......@@ -7,5 +7,7 @@
class SolarPowerTenMinute extends Model
{
use HasFactory;
protected $table = 'solar_power_ten_minutes';
protected $primaryKey = 'id';
protected $fillable = ['timestamp', 'wattminute', 'wattminutetotal'];
}
......@@ -15,7 +15,10 @@ public function up()
{
Schema::create('solar_power_ten_minutes', function (Blueprint $table) {
$table->id();
$table->timestamps();
$table->integer('timestamp', $precision = 0);
$table->float("wattminute",5, 2);
$table->float("wattminutetotal", 10, 2);
$table->timestampsTz();
});
}
......
......@@ -15,7 +15,10 @@ public function up()
{
Schema::create('solar_power_hours', function (Blueprint $table) {
$table->id();
$table->timestamps();
$table->integer('timestamp', $precision = 0);
$table->float("wattminute",5, 2);
$table->float("wattminutetotal", 10, 2);
$table->timestampsTz();
});
}
......
......@@ -15,7 +15,10 @@ public function up()
{
Schema::create('solar_power_days', function (Blueprint $table) {
$table->id();
$table->timestamps();
$table->integer('timestamp', $precision = 0);
$table->float("wattminute",10, 2);
$table->float("wattminutetotal", 20, 2);
$table->timestampsTz();
});
}
......
......@@ -13,9 +13,12 @@
*/
public function up()
{
Schema::create('solar_power_years', function (Blueprint $table) {
Schema::create('solar_power_months', function (Blueprint $table) {
$table->id();
$table->timestamps();
$table->integer('timestamp', $precision = 0);
$table->float("wattminute",10, 2);
$table->float("wattminutetotal", 20, 2);
$table->timestampsTz();
});
}
......@@ -26,6 +29,6 @@ public function up()
*/
public function down()
{
Schema::dropIfExists('solar_power_years');
Schema::dropIfExists('solar_power_months');
}
};
This source diff could not be displayed because it is too large. You can view the blob instead.
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<script>
window.onload = function() {
var chart = new CanvasJS.Chart("chartContainer", {
animationEnabled: false,
title: {
text: "Erzeugung letzte 24h ({{$gesamt}}kWh)"
},
axisY: {
title: "Leistung",
suffix: "W",
includeZero: true
},
axisX:{
valueFormatString: "HH:mm",
ValueType: "dateTime",
},
data: [{
type: "splineArea",
//name: "CPU Utilization",
connectNullData: true,
//nullDataLineDashType: "solid",
xValueType: "dateTime",
xValueFormatString: "HH:mm \"Uhr\"",
yValueFormatString: "#,##0.##\"W\"",
dataPoints: [
@foreach($data_set as $data)
{ x: {{$data->timestamp}}000, y: {{$data->wattminute}} },
@endforeach
]
}]
});
chart.render();
}
</script>
</head>
<body>
<div id="chartContainer" style="height: 500px; max-width: 1500px; margin: 0px auto;"></div>
<script src="js/canvasjs.min.js"></script>
</body>
</html>
\ No newline at end of file
......@@ -21,5 +21,5 @@
});
Route::get('/test', [ShellyLogController::class, 'test']);
//Route::get('/data', [ShellyLogController::class, 'fetch_data']);
//Route::get('/test', [ShellyLogController::class, 'fetch_daily_data']);
Route::get('/show', [ShellyLogController::class, 'show_chart']);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment