在我的linux框中,我运行以下php代码
<?php
//Get the response from the server and append timestamp data
function get_server_response() {
$curl_post_data = null;
$service_url = 'http://serverip/source.php';
$curl = curl_init($service_url);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "user:pass");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$curl_response = curl_exec($curl);
$response = json_decode($curl_response, true);
curl_close($curl);
$time = time();
$check = $time+date("Z",$time);
$data['timestamp'] = array('Year' => intval(strftime('%Y', $check)), 'Month' => intval(strftime('%m', $check)), 'Day' => intval(strftime('%d', $check)),
'Hour' => intval(strftime('%H', $check)+3), 'Minutes' => intval(strftime('%M', $check)), 'Seconds' => intval(strftime('%S', $check)));
return array_merge($data,$response);
}
if (!file_exists('results.json')) {
$response = get_server_response();
$data = new stdClass();
$data-> Data1 = $response;
//Create and data to local json file
$fp = fopen('results.json', 'a+');
fwrite($fp, json_encode($data, JSON_PRETTY_PRINT));
fclose($fp);
} else {
//Read the data from existing local json file
$file = file_get_contents("results.json");
$data = json_decode($file);
$object = json_decode(json_encode($data), FALSE);
unset($file);
//Increment based on the last object and append new key-value pair
end($object);
$key = key($object);
$int = filter_var($key, FILTER_SANITIZE_NUMBER_INT);
++$int;
$response = get_server_response();
$next = 'Data' . $int;
$object-> $next = $response;
//Update the local json file
file_put_contents('results.json', json_encode($object, JSON_PRETTY_PRINT));
}
?>
将以下内容输出到results.json
{
"Data1": {
"timestamp": {
"Year": 2015,
"Month": 9,
"Day": 14,
"Hour": 16,
"Minutes": 1,
"Seconds": 36
},
"CG_Uptime": 199481,
"MHS_Avg": 2661410.65,
"MHS_5s": 3102456.1,
"MHS_1m": 2806133.89,
"MHS_5m": 2700647.04,
"MHS_15m": 2720732.61,
"MHS_Now": "2667260",
"DegC_In": "35",
"DegC_TopOut": "59",
"DegC_BotOut": "61",
"Unit_Uptime": 510403,
"FreeMem": "403",
"PSUVolt_Top": "233",
"PSUVolt_Bot": "235",
"FAN": "50",
"VST": "612",
"VSB": "612",
"VMAX": "622",
"AC_TOP": "1100",
"AC_BOT": "1100",
"DC_AMP": "150",
"PSUWall_Top": "640",
"PSUWatt_Top": "(592",
"PSUWall_Bot": "640",
"PSUWatt_Bot": "(596",
"Total_Watts": 1280
},
"Data2": {
"timestamp": {
"Year": 2015,
"Month": 9,
"Day": 14,
"Hour": 16,
"Minutes": 1,
"Seconds": 59
},
"CG_Uptime": 199504,
"MHS_Avg": 2661347.27,
"MHS_5s": 2042760.72,
"MHS_1m": 2595202.5,
"MHS_5m": 2657730.87,
"MHS_15m": 2705433.81,
"MHS_Now": "2667260",
"DegC_In": "35",
"DegC_TopOut": "59",
"DegC_BotOut": "61",
"Unit_Uptime": 510426,
"FreeMem": "403",
"PSUVolt_Top": "233",
"PSUVolt_Bot": "235",
"FAN": "50",
"VST": "612",
"VSB": "612",
"VMAX": "622",
"AC_TOP": "1100",
"AC_BOT": "1100",
"DC_AMP": "150",
"PSUWall_Top": "656",
"PSUWatt_Top": "(600",
"PSUWall_Bot": "640",
"PSUWatt_Bot": "(596",
"Total_Watts": 1296
},
"Data3": {
"timestamp": {
"Year": 2015,
"Month": 9,
"Day": 14,
"Hour": 21,
"Minutes": 25,
"Seconds": 18
},
"CG_Uptime": 218903,
"MHS_Avg": 2657940.86,
"MHS_5s": 3701517.96,
"MHS_1m": 2658126.3,
"MHS_5m": 2572589.83,
"MHS_15m": 2584535.75,
"MHS_Now": "2667260",
"DegC_In": "34",
"DegC_TopOut": "57",
"DegC_BotOut": "60",
"Unit_Uptime": 529826,
"FreeMem": "405",
"PSUVolt_Top": "233",
"PSUVolt_Bot": "235",
"FAN": "50",
"VST": "612",
"VSB": "612",
"VMAX": "622",
"AC_TOP": "1100",
"AC_BOT": "1100",
"DC_AMP": "150",
"PSUWall_Top": "640",
"PSUWatt_Top": "(596",
"PSUWall_Bot": "624",
"PSUWatt_Bot": "(592",
"Total_Watts": 1264
}
....
....
}
我想要做的是在JSON文件中添加一个额外的字段(名为“Total_KWh”),计算总千瓦时。换句话说,要随时间积分“Total_Watts”值并将结果添加到每个json块。计算将是增量以保存CPU。 所以在“Data1”处我以“Total_KWh”开头:0然后在“Data2”上我计算以小时为单位的时差(在这种情况下,23秒是0.006388小时),然后我乘以Data2和I的“Total_Watts”值会获得新的“Total_KWh”值。 因此,在此示例中,“Total_KWh”将为0.006388 * 1.296 = 0.00827。 你能帮我修改一下php代码吗?
答案 0 :(得分:0)
添加额外的项目会更容易&#34; timestamp_raw&#34;到每个Data *块,用于计算Data * set中每个下一对之间的时差:
function get_server_response() {
...
curl_close($curl);
$time = time();
$check = $time+date("Z",$time);
$data['timestamp_raw'] = $check;
...
}
然后,我们可以这样通过:
...
$key = key($object);
$int = filter_var($key, FILTER_SANITIZE_NUMBER_INT);
++$int;
$response = get_server_response();
$next = 'Data' . $int;
$response["Total_KWh"] = number_format(($response["timestamp_raw"] - $obj->$key->timestamp_raw)/60/60, 6) * ($response["Total_Watts"]/1000);
$object->$next = $response;
...