In my linux box I am running the following php code
<?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));
}
?>
Which outputs the following into 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
}
....
....
}
What I want to do is to add an extra field in the JSON file (named "Total_KWh"), that calculates the total Kilowatt-hours. In other words, to integrate the "Total_Watts" value over time and add the results to each json block. The calculation would be incremental to save cpu. So at "Data1" I start with "Total_KWh":0 then on "Data2" I calculate the time difference in hours (in this case, 23 seconds which is 0.006388 hours), then I multiply with "Total_Watts" value from Data2 and I would get the new "Total_KWh" value. So in this example, "Total_KWh" would be 0.006388*1.296 = 0.00827. Can you help me modify the php code?