<?php

#$time1 = microtime(true);
$body = file_get_contents('php://input');
$t = time();
$date = date('d.m.Y');
$ip = $_SERVER["REMOTE_ADDR"];
$agent = $_SERVER["HTTP_USER_AGENT"];
$ms = round((round(microtime(true) * 1000, 2) / 1000)-$t+date('s'),3);
#$time2 = microtime(true);
#$exe = $time2-$time1;

#file_put_contents('API-wifi.raw', $body . ' ' . $t . ' ' . $date . ' ' . $ip . ' ' . $agent . ' ' . $exe . PHP_EOL, FILE_APPEND);
file_put_contents('API-wifi.raw', $body . ' ' . $t . ' ' . $date . ' ' . $ip . ' ' . $agent . PHP_EOL, FILE_APPEND);

$vars = preg_split('/\s+/', $body);

if ((string) $vars[3] !== "V") { exit(0); }
if ((float) $vars[11] > 1090 || (float) $vars[11] < 600 || (int) $vars[14] < 0) { exit(0); }

$node_id = (string) $vars[2];

# Apply a temporary filter on SBM-20 values from Panagyurishte (N142)
#if ($node_id == 'N142' && (int) $vars[14] > 300) { exit(0); }

# Apply a more specific atm. pressure filter for Galabovo (N150)
#if ($node_id == 'N150' && (float) $vars[11] > 970) { exit(0); }

# Apply a more specific atm. pressure filter for Botev_peak_Refuge (N151)
if ($node_id == 'N151' && ((float) $vars[11] > 790 || (float) $vars[11] < 730)) { exit(0); }

# Apply a temporary filter on SBM-20 values from Ledeno_ezero_Hut (N153)
if ($node_id == 'N153' && ((int) $vars[14] > 300 || (int) $vars[14] < 100)) { exit(0); }

# Apply a more specific atm. pressure filter for Slaveykovo (N159)
if ($node_id == 'N159' && ((float) $vars[11] > 1025 || (float) $vars[11] < 965)) { exit(0); }

# Apply a more specific atm. pressure filter for Petrohan (N171)
#if ($node_id == 'N171' && (float) $vars[11] < 830) { exit(0); }

# Apply a temporary filter on SBM-20 values from Vratsa (N178)
#if ($node_id == 'N178' && (int) $vars[14] > 300) { exit(0); }

# Apply a temporary filter on SBM-20 values from Glavatartsi (N193)
if ($node_id == 'N193' && (int) $vars[14] > 300) { exit(0); }

# Apply a temporary filter on SBM-20 values from Bosnek (N199)
#if ($node_id == 'N199' && (int) $vars[14] > 300) { exit(0); }

# Apply a temporary filter on SBM-20 values from Pazardzhik (N200)
if ($node_id == 'N200' && (int) $vars[14] > 300) { exit(0); }

switch ($node_id) {
	case "N03":
		$location = "Dospat";
		$altitude = 1270;
		break;
        case "N06":
                $location = "Plovdiv_FC";
                $altitude = 170.8;
                break;
	case "N14":
		$location = "unknown";
		$altitude = 0;
		break;
	case "N15":
		$location = "unknown";
		$altitude = 0;
		break;
        case "N16":
                $location = "test_N16";
                $altitude = 179.0;
                break;
	case "N17":
		$location = "unknown";
		$altitude = 0;
		break;
        case "N19":
                $location = "test_dev_node_stoyancho";
                $altitude = 170.8;
                break;
	case "N20":
		$location = "Glozhene";
		$altitude = 65.2;
		break;
	case "N22":
		$location = "Mirkovo";
		$altitude = 676.4;
		break;
	case "N25":
		$location = "Troyan";
		$altitude = 399.3;
		break;
	case "N26":
		$location = "test_N26";
		$altitude = 179.0;
		break;
	case "N27":
		$location = "Smolyan";
		$altitude = 948.8;
		break;
	case "N28":
		$location = "unknown";
		$altitude = 0;
		break;
	case "N29":
		$location = "Varvara";
		$altitude = 338.1;
		break;
	case "N30":
		$location= "Gabrovo";
		$altitude = 374.53;
		break;
	case "N31":
		$location = "test_N31";
		$altitude = 179.0;
		break;
	case "N32":
		$location = "Velingrad";
		$altitude = 759.6;
		break;
	case "N33":
		$location = "Rudartsi";
		$altitude = 847.4;
		break;
	case "N34":
		$location = "Elhovo";
		$altitude = 114.7;
		break;
	case "N35":
		$location = "Plovdiv_NW";
		$altitude = 192;
		break;
	case "N36":
		$location = "Samokov";
		$altitude = 946.5;
		break;
	case "N37":
		$location = "Mosomishte";
		$altitude = 610.7;
		break;
	case "N38":
		$location = "Veliko_Tarnovo";
		$altitude = 189;
		break;
	case "N39":
		$location = "test_N39";
		$altitude = 179.0;
		break;
	case "N40":
		$location = "Sarnegor";
		$altitude = 408.3;
		break;
	case "N41":
		$location = "Panagyurski_kolonii";
		$altitude = 1067.2;
		break;
	case "N42":
		$location = "Sofia_Ilinden";
		$altitude = 560.9;
		break;
	case "N43":
		$location = "Sofia_Hladilnika";
		$altitude = 628.1;
		break;
	case "N44":
		$location = "Sofia_Yavorov";
		$altitude = 548.5;
		break;
	case "N45":
		$location = "Hayredin";
		$altitude = 67;
		break;
	case "N46":
		$location = "Sofia_Mladost";
		$altitude = 608.6;
		break;
	case "N47":
		$location = "Muselievo";
		$altitude = 33.7;
		break;
	case "N48":
		$location = "Bansko";
		$altitude = 931.4;
		break;
	case "N049":
		$location = "test_N049";
		$altitude = 179.0;
		break;
	case "N050":
		$location = "Gladno_pole";
		$altitude = 796.2;
		break;
	case "N051":
		$location = "Breznik";
		$altitude = 749.0;
		break;
	case "N052":
		$location = "test_N052";
		$altitude = 179.0;
		break;
	case "N053":
		$location = "Petrohan";
		$altitude = 1398.0;
		break;
	case "N056":
		$location = "test_N056";
		$altitude = 179.0;
		break;
	case "N057":
		$location = "Thalheim_Wels";
		$altitude = 314.47;
		break;
	case "N058":
		$location = "Pletena";
		$altitude = 1014.0;
		break;
	case "N060":
		$location = "test_N060";
		$altitude = 179.0;
		break;
	case "N061":
		$location = "Samuilovo";
		$altitude = 322.33;
		break;
	case "N063":
		$location = "test_N063";
		$altitude = 179.0;
		break;
	case "N064":
		$location = "Selishte";
		$altitude = 1278.5;
		break;
	case "N065":
		$location = "Plana_Earth_Station";
		$altitude = 1102.9;
                break;
	case "N066":
		$location = "Ruse";
		$altitude = 58.0;
		break;
	case "N067":
		$location = "Medeni_polyani";
		$altitude = 1421.4;
		break;
	case "N068":
		$location = "test_N068";
		$altitude = 179.0;
		break;
        case "N070":
		$location = "Tran";
		$altitude = 698.0;
		break;
	case "N071":
		$location = "Berkovitsa";
		$altitude = 390.0;
		break;
	case "N072":
		$location = "test_N072";
		$altitude = 179.0;
		break;
	case "N073":
		$location = "test_N073";
		$altitude = 179.0;
		break;
	case "N074":
		$location = "test_N074";
		$altitude = 179.0;
		break;
	case "N075":
		$location = "test_N075";
		$altitude = 179.0;
		break;
	case "N076":
		$location = "test_N076";
		$altitude = 179.0;
		break;
	case "N077":
		$location = "Eremia";
		$altitude = 748.0;
		break;
	case "N078":
		$location = "Balkanity_Hut";
		$altitude = 1408.5;
		break;
	case "N079":
		$location = "Koprivshtitsa";
		$altitude = 1016.18;
		break;
	case "N080":
		$location = "Krastatitsa";
		$altitude = 1087;
		break;
	case "N081":
		$location = "Dolno_Draglishte";
		$altitude = 839.15;
		break;
	case "N082":
		$location = "Gotse_Delchev";
		$altitude = 534.5;
		break;
	case "N083":
		$location = "test_N083";
		$altitude = 179.0;
		break;
	case "N084":
		$location = "Popovi_livadi";
		$altitude = 1405.79;
		break;
	case "N085":
		$location = "Blatets";
		$altitude = 1048.5;
		break;
	case "N086":
		$location = "test_N086";
		$altitude = 179.0;
                break;
	case "N087":
		$location = "Sarnitsa";
		$altitude = 1207.1;
		break;
	case "N088":
		$location = "Malyovitsa_Hut";
		$altitude = 1998.5;
		break;
	case "N089":
		$location = "test_N089";
		$altitude = 179.0;
		break;
	case "N090":
		$location = "Novi_Iskar";
		$altitude = 530.5;
		break;
	case "N091":
		$location = "Vladaya";
		$altitude = 880.11;
		break;
	case "N092":
		$location = "San_Stefano";
		$altitude = 234.0;
		break;
	case "N093":
		$location = "Harmanli";
		$altitude = 97.2;
		break;
	case "N094":
		$location = "test_N094";
		$altitude = 179.0;
		break;
	case "N096":
		$location = "test_N096";
		$altitude = 179.0;
		break;
	case "N097":
		$location = "Breznitsa";
		$altitude = 768.61;
		break;
	case "N098":
		$location = "test_N098";
		$altitude = 179.0;
		break;
        case "N099":
		$location = "Granitsa";
		$altitude = 575.0;
		break;
	case "N100":
		$location = "Dobrich";
		$altitude = 229.3;
		break;
	case "N101":
		$location = "Dzhurovo_Belyanovets";
		$altitude = 390.0;
		$rh=$vars[13]+10;
		file_put_contents('N101/data.raw', $vars[0] . ' ' . $vars[1] . ' ' . $vars[2] . ' ' . $vars[3] . ' ' . $vars[4] . ' ' . $vars[5] . ' ' . $vars[6] . ' ' . $vars[7] . ' ' . $vars[8] . ' ' . $vars[9] . ' ' . $vars[10] . ' ' . $vars[11] . ' ' . $vars[12] . ' ' . $rh . ' ' . $vars[14] . ' ' . $vars[15] . ' ' . $t . ' ' . $date . ' ' . $ms . PHP_EOL, FILE_APPEND);
		break;
	case "N102":
		$location = "Kyustendil";
		$altitude = 520.0;
		break;
	case "N104":
		$location = "Rasovo";
		$altitude = 902.0;
		break;
	case "N105":
		$location = "Panagyurishte";
		$altitude = 565.0;
		break;
	case "N106":
		$location = "test_N106";
		$altitude = 179.0;
		break;
	case "N108":
		$location = "Garmen";
		$altitude = 546.27;
		$rh=$vars[13]+10;
		file_put_contents('N108/data.raw', $vars[0] . ' ' . $vars[1] . ' ' . $vars[2] . ' ' . $vars[3] . ' ' . $vars[4] . ' ' . $vars[5] . ' ' . $vars[6] . ' ' . $vars[7] . ' ' . $vars[8] . ' ' . $vars[9] . ' ' . $vars[10] . ' ' . $vars[11] . ' ' . $vars[12] . ' ' . $rh . ' ' . $vars[14] . ' ' . $vars[15] . ' ' . $t . ' ' . $date . ' ' . $ms . PHP_EOL, FILE_APPEND);
		break;
	case "N110":
		$location = "Riltsi";
		$altitude = 378.0;
		$rh=$vars[13]+12;
		file_put_contents('N110/data.raw', $vars[0] . ' ' . $vars[1] . ' ' . $vars[2] . ' ' . $vars[3] . ' ' . $vars[4] . ' ' . $vars[5] . ' ' . $vars[6] . ' ' . $vars[7] . ' ' . $vars[8] . ' ' . $vars[9] . ' ' . $vars[10] . ' ' . $vars[11] . ' ' . $vars[12] . ' ' . $rh . ' ' . $vars[14] . ' ' . $vars[15] . ' ' . $t . ' ' . $date . ' ' . $ms . PHP_EOL, FILE_APPEND);
		break;
	case "N111":
                $location = "Sliven";
                $altitude = 262.7;
                break;
        case "N112":
                $location = "Yakimovo";
                $altitude = 100.81;
                break;
        case "N113":
                $location = "test_N113";
                $altitude = 179.0;
                break;
        case "N114":
                $location = "Rozhen_Cone";
                $altitude = 1735.1;
                break;
        case "N115":
                $location = "unknown";
                $altitude = 0;
                break;
        case "N116":
                $location = "unknown";
                $altitude = 0;
                break;
        case "N117":
                $location = "Zdravets_Hotel";
                $altitude = 1297.98;
                break;
        case "N118":
                $location = "Varna_West";
                $altitude = 95.5;
                break;
        case "N119":
                $location = "Paradhisos";
                $altitude = 3.5;
                break;
        case "N120":
                $location = "Elenovo";
                $altitude = 209.68;
                break;
        case "N121":
                $location = "Stamboliyski";
                $altitude = 187.2;
                break;
        case "N122":
                $location = "unknown";
                $altitude = 0;
                break;
        case "N123":
                $location = "Stara_Zagora";
                $altitude = 225.5;
                break;
        case "N124":
                $location = "Vasil_Levski_Hut";
                $altitude = 1347.4;
                break;
	case "N125":
		$location = "Akchelar";
		$altitude = 177.12;
		break;
        case "N126":
                $location = "Yana";
                $altitude = 589.1;
                break;
        case "N127":
                $location = "Isperih";
                $altitude = 269.8;
                break;
        case "N129":
                $location = "Svilengrad";
                $altitude = 54.7;
                break;
        case "N130":
                $location = "Boykovo";
                $altitude = 1096;
                break;
        case "N131":
                $location = "Chuchuligovo";
                $altitude = 94.8;
                break;
        case "N132":
                $location = "Yagodina";
                $altitude = 1120.8;
                break;
        case "N133":
                $location = "Razgrad";
                $altitude = 357.1;
                break;
        case "N134":
                $location = "Byala";
                $altitude = 102.8;
                break;
        case "N135":
                $location = "Shavarite";
                $altitude = 823.5;
                break;
        case "N136":
                $location = "unknown";
                $altitude = 0;
                break;
        case "N137":
                $location = "Valchi_dol";
                $altitude = 265.8;
                break;
        case "N138":
                $location = "unknown";
                $altitude = 0;
                break;
	case "N139":
		$location = "Balgari";
		$altitude = 299.68;
		break;
        case "N140":
                $location = "Popovo";
                $altitude = 243.1;
                break;
        case "N141":
                $location = "Nesebar";
                $altitude = 26.6;
                break;
        case "N142":
                $location = "test_N142";
                $altitude = 179.0;
                break;
        case "N143":
                $location = "Bozhurets";
                $altitude = 137.8;
                break;
        case "N144":
                $location = "Leinfelden";
                $altitude = 445.9;
                break;
        case "N145":
                $location = "Varhovrah";
                $altitude = 1505.9;
                break;
        case "N146":
                $location = "Sofia_Boyana_bis";
                $altitude = 750.3;
		$rh=$vars[13]+10;
		file_put_contents('N146/data.raw', $vars[0] . ' ' . $vars[1] . ' ' . $vars[2] . ' ' . $vars[3] . ' ' . $vars[4] . ' ' . $vars[5] . ' ' . $vars[6] . ' ' . $vars[7] . ' ' . $vars[8] . ' ' . $vars[9] . ' ' . $vars[10] . ' ' . $vars[11] . ' ' . $vars[12] . ' ' . $rh . ' ' . $vars[14] . ' ' . $vars[15] . ' ' . $t . ' ' . $date . ' ' . $ms . PHP_EOL, FILE_APPEND);
                break;
        case "N147":
                $location = "unknown";
                $altitude = 0;
                break;
        case "N148":
                $location = "Rozhen_Std";
                $altitude = 1735.1;
                break;
        case "N149":
                $location = "Smolyan_Ezerovo";
                $altitude = 1367.62;
                break;
	case "N150":
		$location = "Galabovo";
		$altitude = 700.0;
		break;
        case "N151":
                $location = "Botev_peak_Refuge";
                $altitude = 2370.1;
                break;
        case "N152":
                $location = "Sofia_Boyana";
                $altitude = 758.55;
		$rh=$vars[13]+10;
		file_put_contents('N152/data.raw', $vars[0] . ' ' . $vars[1] . ' ' . $vars[2] . ' ' . $vars[3] . ' ' . $vars[4] . ' ' . $vars[5] . ' ' . $vars[6] . ' ' . $vars[7] . ' ' . $vars[8] . ' ' . $vars[9] . ' ' . $vars[10] . ' ' . $vars[11] . ' ' . $vars[12] . ' ' . $rh . ' ' . $vars[14] . ' ' . $vars[15] . ' ' . $t . ' ' . $date . ' ' . $ms . PHP_EOL, FILE_APPEND);
                break;
        case "N153":
                $location = "Ledeno_ezero_Hut";
                $altitude = 2730.5;
                break;
        case "N154":
                $location = "unknown";
                $altitude = 0;
                break;
        case "N155":
                $location = "Musala";
                $altitude = 2925.4;
                break;
        case "N156":
                $location = "unknown";
                $altitude = 0;
                break;
        case "N157":
                $location = "Durankulak";
                $altitude = 7.6;
                break;
	case "N158":
		$location = "Chatama";
		$altitude = 1532.8;
		break;
        case "N159":
                $location = "Slaveykovo";
                $altitude = 166.12;
                break;
        case "N161":
                $location = "Freising";
                $altitude = 482.5;
                break;
        case "N162":
                $location = "Burgas_Dolno_Ezerovo";
                $altitude = 8.4;
                break;
        case "N163":
                $location = "unknown";
                $altitude = 0;
                break;
        case "N164":
                $location = "Ortsevo";
                $altitude = 1558;
                break;
        case "N165":
                $location = "Silistra";
                $altitude = 25.4;
                break;
        case "N166":
                $location = "Pleven";
                $altitude = 137.4;
                break;
        case "N168":
                $location = "Komotini";
                $altitude = 35.2;
                break;
        case "N169":
                $location = "Rozhen";
                $altitude = 1735.2;
                break;
        case "N170":
                $location = "Varna_Centre";
                $altitude = 19;
                break;
	case "N171":
		$location = "test_N171";
		$altitude = 179.0;
		break;
        case "N172":
                $location = "Sarantsi";
                $altitude = 649.8;
                break;
        case "N173":
                $location = "Pernik";
                $altitude = 750.4;
                break;
        case "N174":
                $location = "Dimitrovgrad";
                $altitude = 134.3;
                break;
	case "N175":
		$location = "Tevno_ezero_Hut";
		$altitude = 2525.05;
		break;
	case "N176":
		$location = "Kladnitsa";
		$altitude = 971.0;
		break;
	case "N178":
		$location = "Vratsa";
		$altitude = 343.5;
		break;
	case "N179":
		$location = "Botevgrad";
		$altitude = 388.5;
		break;
	case "N181":
		$location = "Golyama_Zhelyazna";
		$altitude = 401.82;
		break;
	case "N182":
		$location = "Pchelinski_bani";
		$altitude = 660.5;
		break;
	case "N183":
		$location = "Fakia";
		$altitude = 198.0;
		break;
	case "N184":
		$location = "Devin";
		$altitude = 771.7;
		break;
	case "N188":
		$location = "Zhilentsi";
		$altitude = 753.0;
		break;
	case "N189":
		$location = "Cherven_bryag";
		$altitude = 115.5;
		break;
	case "N192":
		$location = "Godech";
		$altitude = 719.55;
		break;
	case "N193":
		$location= "Glavatartsi";
		$altitude = 338.5;
		break;
	case "N195":
		$location= "Trivoditsi";
		$altitude = 188.0;
		break;
	case "N196":
		$location= "Gorni_Lozen";
		$altitude = 702.0;
		break;
	case "N197":
		$location= "Svirachi";
		$altitude = 267.0;
		break;
        case "N199":
                $location = "Bosnek";
                $altitude = 924.5;
                break;
	case "N200":
		$location = "Pazardzhik";
		$altitude = 204.0;
		break;
        case "N201":
                $location = "Kozloduy";
                $altitude = 54.5;
                break;
	case "N202":
		$location = "Tsarevo";
		$altitude = 45.48;
		break;
	case "N205":
		$location = "Krichim";
		$altitude = 262.0;
		break;
	case "N209":
		$location = "Sofia_Ovcha_kupel";
		$altitude = 633.3;
		break;
	case "N211":
		$location = "Makedonia_Hut";
		$altitude = 2165.0;
		break;;
	case "N212":
		$location = "Chukovets";
		$altitude = 664.3;
		break;
	case "N214":
		$location = "test_N214";
		$altitude = 179.0;
		break;
        case "N215":
                $location = "Plovdiv_NW_bis";
                $altitude = 192;
                break;
	case "N216":
		$location = "Troyan_Rastashka";
		$altitude = 464.96;
		break;
	case "N218":
		$location = "Kurtovo_Konare";
		$altitude = 197.88;
		break;
        case "N220":
                $location = "test_N220";
                $altitude = 179.0;
                break;
        case "N221":
		$location = "Obidim";
		$altitude = 1213;
		break;
	case "N222":
		$location = "Sofia_Lyulin";
		$altitude = 590.35;
		break;
	case "N223":
		$location = "Mraketintsi";
		$altitude = 792.1;
		break;
        case "N224":
		$location = "Chervena_voda";
		$altitude = 104.6;
		break;
	case "N225":
		$location = "Vetren";
		$altitude = 113.2;
		break;
	case "N227":
		$location = "Modar";
		$altitude = 1650.0;
		break;
	case "N231":
		$location = "Teteven";
		$altitude = 412.5;
		break;
	case "N233":
		$location = "Dobri_dol";
		$altitude = 122.5;
		break;
	case "N234":
		$location = "Yambol";
		$altitude = 137.44;
		break;
	case "N235":
		$location = "Sarnegor_basement";
		$altitude = 400.0;
		break;
	case "N236":
		$location = "Sevlievo";
		$altitude = 213.55;
		break;
	case "N237":
		$location = "Sofia_Gardova_glava";
		$altitude = 629.9;
		break;
	case "N238":
		$location = "Konyovets";
		$altitude = 248.53;
		break;
	case "N509":
		$location = "Tsrancha";
		$altitude = 325.3;
		break;
	default:
		file_put_contents('API-wifi-unexpected-data.raw', $body . ' ' . $t . ' ' . $date . PHP_EOL, FILE_APPEND);
		$location = "unknown";
		$altitude = 0;
	}

$t_raw = (float) $vars[12];
$p_raw = (float) $vars[11];
# Initialize rh for remaining nodes (without adjusted measurements)
if ($node_id != "N101" && $node_id != "N108" && $node_id != "N110" && $node_id != "N146" && $node_id != "N152") {
	$rh = (int) $vars[13];
}
$pm25 = (int) $vars[9];
$pm10 = (int) $vars[10];
$gamma_raw = (int) $vars[14];
$signal = (int) $vars[4];

# t_dew calculations
$a=6.1121;
$d=234.5;
if ($t_raw < 0) {
	$b=17.966;
	$c=247.15;
}
else {
	$b=17.368;
	$c=238.88;
}
if ($rh > 0) {
	$gm = log( $rh*0.01*( pow(exp(1),(($b-(1*$t_raw)/$d)*((1*$t_raw)/($c+(1*$t_raw))))) ) );
}
else {
	$gm = log( 0.1*0.01*( pow(exp(1),(($b-(1*$t_raw)/$d)*((1*$t_raw)/($c+(1*$t_raw))))) ) );
}
$t_dew = round($c*(1*$gm)/($b-(1*$gm)), 1);

# p_sea calculations
$p_sea = round($p_raw*pow((1-(0.0065*$altitude)/($t_raw+0.0065*$altitude+273.15)),-5.257), 1);

# gamma_cpm calculations
$gamma_cpm = round($gamma_raw/5, 0);

# Submit data to the raw and current data files (except for nodes width adjusted measurements)
if ($node_id != "N101" && $node_id != "N108" && $node_id != "N110" && $node_id != "N146" && $node_id != "N152") {
        file_put_contents($node_id . "/data.raw", $body . ' ' . $t . ' ' . $date . ' ' . $ms . PHP_EOL, FILE_APPEND);
}
file_put_contents($node_id . "/data.current", $t . ',' . $location . ',' . $altitude . ',' . $t_raw . ',' . $t_dew . ',' . $p_raw . ',' . $p_sea . ',' . $rh . ',' . $pm25 . ',' . $pm10 . ',' . $gamma_cpm . ',' . $signal . PHP_EOL);

# Submit data to local instance of Timeseries DB
#$cmd_2_timeseriesDB="/usr/local/bin/psql -U meter -d meter -c \"INSERT INTO public.nodes VALUES (NOW(), '$node_id', $t_raw, $t_dew, $p_raw, $p_sea, $rh, $pm25, $pm10, $gamma_cpm)\"";
#shell_exec( $cmd_2_timeseriesDB );

# Submit data to local instance of InfluxDB through HTTP API call
$pm25 .= "i";
$pm10 .= "i";
$gamma_raw .= "i";
$gamma_cpm .= "i";
$signal .= "i";
$ts = $t;
$ts .= "i";

$curl = curl_init();
curl_setopt_array($curl, array(
	CURLOPT_URL => 'http://localhost:8086/write?db=meter&precision=s&u=editor&p=HFZhChwzKv2yahsF',
	CURLOPT_SSL_VERIFYPEER => 0,
	CURLOPT_SSL_VERIFYHOST => 0,
	CURLOPT_RETURNTRANSFER => 0,
	CURLOPT_POST => 1,
	CURLOPT_POSTFIELDS => "box,node_id=$node_id,altitude=$altitude,location=$location,phy=wifi t_raw=$t_raw,t_dew=$t_dew,p_raw=$p_raw,p_sea=$p_sea,rh=$rh,pm25=$pm25,pm10=$pm10,gamma_raw=$gamma_raw,gamma_cpm=$gamma_cpm,signal=$signal,ip=\"$ip\",ts=$ts $t",
	CURLOPT_HTTPHEADER => array('Content-Type: text/plain')
));
curl_exec($curl);
curl_close($curl);
?>
