Support for many thermostats
Uses the device ID to support several thermostats. Pretty simple - only setup is now an array of devices, now a single device. However, many changes follow...
This commit is contained in:
207
delay.php
207
delay.php
@@ -1,82 +1,141 @@
|
||||
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
|
||||
|
||||
|
||||
|
||||
<?php
|
||||
include('settings/globals.php');
|
||||
header('Content-Type: application/json');
|
||||
$conn = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
|
||||
// Check connection
|
||||
if (!$conn) {
|
||||
die("Connection failed: " . mysqli_connect_error());
|
||||
}
|
||||
$sql = 'SELECT UNIX_TIMESTAMP(date),dispTemperature,weatherTemperature,heatSetPoint,systemSwitchPosition from stat';
|
||||
include('settings/globals.php');
|
||||
|
||||
$result = $conn->query($sql);
|
||||
if (!$result) {
|
||||
printf("Errormessage: %s\n", $conn->error);
|
||||
}
|
||||
$rows = array();
|
||||
while($r = mysqli_fetch_assoc($result)) {
|
||||
$r['date'] = $r['UNIX_TIMESTAMP(date)'];
|
||||
unset($r['UNIX_TIMESTAMP(date)']);
|
||||
$rows[] = $r;
|
||||
}
|
||||
// print("<pre>");
|
||||
// print_r($rows);
|
||||
$startTime = -1;
|
||||
$startTemp = NULL;
|
||||
$startSetPoint = NULL;
|
||||
$targetSetPoint = NULL;
|
||||
$valid_thermostats = ["honeywell"];
|
||||
|
||||
$heat_up = NULL;
|
||||
$heat_down = NULL;
|
||||
$cool_up = NULL;
|
||||
$cool_down = NULL;
|
||||
$type = NULL;
|
||||
|
||||
for($i = 1; $i < count($rows); $i++){
|
||||
$now = $rows[$i];
|
||||
$last= $rows[$i-1];
|
||||
// echo $startTime;
|
||||
// print_r($now);
|
||||
if($startTime == -1){
|
||||
// echo "no start <br/>";
|
||||
if($now['heatSetPoint'] <> $last['heatSetPoint']){
|
||||
//Down!
|
||||
// echo "down<br/>";
|
||||
$startTime = $now['date'];
|
||||
$startTemp = $now['dispTemperature'];
|
||||
$startSetPoint = $last['heatSetPoint'];
|
||||
$targetSetPoint = $now['heatSetPoint'];
|
||||
if($now['heatSetPoint'] > $last['heatSetPoint']){
|
||||
$type = "up";
|
||||
} else {
|
||||
$type = "down";
|
||||
}
|
||||
}
|
||||
} else{
|
||||
//looking for end
|
||||
// echo "yes start <br/>";
|
||||
if($now['dispTemperature'] == $targetSetPoint || $now['heatSetPoint'] <> $targetSetPoint){
|
||||
$changeTime = ($now['date']-$startTime)/60;
|
||||
$changeTemp = abs($startTemp - $now['dispTemperature']);
|
||||
$outside = $now['weatherTemperature'];
|
||||
$minperdeg = $changeTime/$changeTemp;
|
||||
|
||||
if($type == "up"){
|
||||
$heat_up[] = [$minperdeg,intval($outside)];
|
||||
} else{
|
||||
$heat_down[] = [$minperdeg,intval($outside)];
|
||||
}
|
||||
if(in_array(THERMOSTAT_TYPE,$valid_thermostats)){
|
||||
require('thermostats/'.THERMOSTAT_TYPE.'/scrape.php');
|
||||
require('thermostats/'.THERMOSTAT_TYPE.'/logininfo.php');
|
||||
} else{
|
||||
die("Invalid THERMOSTAT_TYPE set in settings/globals.php");
|
||||
}
|
||||
|
||||
$id = $_GET['id']; // device ID
|
||||
|
||||
?>
|
||||
<head>
|
||||
<title>Stat delay</title>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
|
||||
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-table/1.5.0/bootstrap-table.min.css">
|
||||
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
|
||||
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
|
||||
<script src="http://code.highcharts.com/highcharts.js"></script>
|
||||
<script src="http://code.highcharts.com/modules/exporting.js"></script>
|
||||
<script type="text/javascript">
|
||||
$(function () {
|
||||
console.log('load');
|
||||
var seriesOptions = [];
|
||||
var createChart = function(){
|
||||
console.log('create');
|
||||
$('#container').highcharts({
|
||||
chart: {
|
||||
type: 'scatter',
|
||||
zoomType: 'xy'
|
||||
},
|
||||
title: {
|
||||
text: 'Time to heat or cool vs outside temperature'
|
||||
},
|
||||
subtitle: {
|
||||
text: 'Because science'
|
||||
},
|
||||
xAxis: {
|
||||
title: {
|
||||
enabled: true,
|
||||
text: 'Minutes per degree change'
|
||||
},
|
||||
startOnTick: true,
|
||||
endOnTick: true,
|
||||
showLastLabel: true
|
||||
},
|
||||
yAxis: {
|
||||
title: {
|
||||
text: 'Outside temp'
|
||||
}
|
||||
},
|
||||
legend: {
|
||||
layout: 'vertical',
|
||||
align: 'left',
|
||||
verticalAlign: 'top',
|
||||
x: 100,
|
||||
y: 70,
|
||||
floating: true,
|
||||
backgroundColor: (Highcharts.theme && Highcharts.theme.legendBackgroundColor) || '#FFFFFF',
|
||||
borderWidth: 1
|
||||
},
|
||||
plotOptions: {
|
||||
scatter: {
|
||||
marker: {
|
||||
radius: 5,
|
||||
states: {
|
||||
hover: {
|
||||
enabled: true,
|
||||
lineColor: 'rgb(100,100,100)'
|
||||
}
|
||||
}
|
||||
},
|
||||
states: {
|
||||
hover: {
|
||||
marker: {
|
||||
enabled: false
|
||||
}
|
||||
}
|
||||
},
|
||||
tooltip: {
|
||||
headerFormat: '<b>{series.name}</b><br>',
|
||||
pointFormat: '{point.x} min per degree, {point.y} degrees'
|
||||
}
|
||||
}
|
||||
},
|
||||
series: seriesOptions,
|
||||
|
||||
});
|
||||
}
|
||||
$.getJSON('calc_delay.php?id=<?php echo $id ?>', function (data) {
|
||||
console.log("data");
|
||||
seriesOptions[0] = {
|
||||
name: "Heat, Heating",
|
||||
data: data[0]
|
||||
};
|
||||
seriesOptions[1] = {
|
||||
name: "Heat, down",
|
||||
data: data[1]
|
||||
};
|
||||
|
||||
createChart();
|
||||
|
||||
|
||||
$startTime = -1;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
$return = [$heat_up, $heat_down];
|
||||
|
||||
print json_encode($return);
|
||||
</script>
|
||||
|
||||
|
||||
$conn->close();
|
||||
<body>
|
||||
|
||||
<?php
|
||||
// navbar
|
||||
include('navbar.php');
|
||||
?>
|
||||
|
||||
<h1>
|
||||
|
||||
?>
|
||||
<?php
|
||||
// title!
|
||||
echo $device_name_array[$id];
|
||||
?>
|
||||
|
||||
</h1>
|
||||
<div id="container" style="min-width: 310px; height: 400px; max-width: 800px; margin: 0 auto"></div>
|
||||
|
||||
|
||||
|
||||
</body></html>
|
||||
Reference in New Issue
Block a user