php - Integrate from JSON file and return value to it -
in linux box running following php code
<?php //get response server , 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 , data local json file $fp = fopen('results.json', 'a+'); fwrite($fp, json_encode($data, json_pretty_print)); fclose($fp); } else { //read data 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 last object , 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 local json file file_put_contents('results.json', json_encode($object, json_pretty_print)); } ?>
which outputs following 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 want add field in json file (named "total_kwh"), calculates total kilowatt-hours. in other words, integrate "total_watts" value on time , add results each json block. calculation incremental save cpu. @ "data1" start "total_kwh":0 on "data2" calculate time difference in hours (in case, 23 seconds 0.006388 hours), multiply "total_watts" value data2 , new "total_kwh" value. in example, "total_kwh" 0.006388*1.296 = 0.00827. can me modify php code?
it easier add item "timestamp_raw" every data* block calculating time difference between every next pair in data* set:
function get_server_response() { ... curl_close($curl); $time = time(); $check = $time+date("z",$time); $data['timestamp_raw'] = $check; ... }
then, can through in such way:
... $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; ...
Comments
Post a Comment