我正在从太阳能充电控制器收集数据到MySQL数据库(5.5.52)。到目前为止,我有一个TABLE,寄存器:
mysql> SELECT date,time,panelVoltage,panelCurrent,batteryVoltage,loadCurrent
FROM registers
ORDER BY date,time
LIMIT 4;
+------------+----------+--------------+--------------+----------------+-------------+
| date | time | panelVoltage | panelCurrent | batteryVoltage | loadCurrent |
+------------+----------+--------------+--------------+----------------+-------------+
| 2016-11-27 | 17:20:41 | 26.04 | 1.31 | 25.15 | 0.72 |
| 2016-11-27 | 17:21:06 | 26.06 | 1.31 | 25.15 | 0.76 |
| 2016-11-27 | 17:21:32 | 26.06 | 1.15 | 25.16 | 0.6 |
| 2016-11-27 | 17:21:57 | 26.06 | 1.3 | 25.16 | 0.68 |
+------------+----------+--------------+--------------+----------------+-------------+
我想计算面板的瓦特小时数并加载为我认为的VIEW,因此它保持最新。 (非常)伪代码:
SELECT (panelVoltage * panelCurrent) as panelWattage,
CONCAT(date, ' ', time) AS datetime
FROM registers ORDER BY datetime;
timeDiff = (currRow.datetime from prevRow.datetime)/3600 # for time decimal hours
powerSum = (currRow.panelWattage + prevRow.panelWattage)/2 # for average Wattage
Wh = timeDiff * powerSum # for Watt-hours
因此,视图/列将包含panelWh
和loadWh
,并且可能是每个panelWhSum
,loadWhSum
的运行总和。
这是我的PHP代码就是这样 - 我理解一个视图会更有效:
$qry = "SELECT CONCAT(date,' ',time) as datetime,loadCurrent,batteryVoltage,panelCurrent,panelVoltage,
panelCurrent * panelVoltage as 'panelWatt', loadCurrent * batteryVoltage as 'loadWatt' FROM registers
WHERE TIMESTAMP(`date`, `time`) > NOW() - INTERVAL ".$_GET['time']." MINUTE";
$result = mysqli_query($con,$qry);
$table = array();
$table['cols'] = array(
array('label' => 'datetime', 'type' => 'datetime'),
array('label' => 'panel Wh', 'type' => 'number'),
array('label' => 'load Wh', 'type' => 'number'),
array('label' => 'net Wh', 'type' => 'number')
foreach($result as $row){
$prevTime = $curTime;
$curTime = $datetime;
$prevPW = $curPW;
$curPW = $row['panelWatt'];
$prevLW = $curLW;
$curLW = $row['loadWatt'];
if ($i == 0) {
$prevTime = $datetime;
$prePW = $row['panelWatt'];
$preLW = $row['loadWatt'];
}
$diffInSeconds = $curTime->getTimestamp() - $prevTime->getTimestamp();
$panelWh = ($diffInSeconds * 0.5 * ($prevPW + $curPW) / 3600);
$panelWhTotal = $panelWhTotal + $panelWh;
$loadWh = ($diffInSeconds * 0.5 * ($prevLW + $curLW) / 3600);
$loadWhTotal = $loadWhTotal + $loadWh;
$temp[] = array('v' => (float) round($panelWhTotal, 2));
$temp[] = array('v' => (float) round($loadWhTotal, 2));
$temp[] = array('v' => (float) round(($panelWhTotal - $loadWhTotal), 2));
}
以上PHP代码中的示例: panel, load Watt-hours