//
// Main Page period status update
//
// var mySite = 'http://192.168.1.23'; // from myip.js
var url = mySite + '/state';
setInterval(RefreshStatus, 2500);

getIt = function (aUrl, callback) {
	var xhr = new XMLHttpRequest();
	xhr.open('GET', aUrl, true);
	xhr.responseType = 'text';
	xhr.onload = function (e) {
		if (this.status === 200) {
			callback(this.response);
		}
	};
	xhr.send();
};

function ConvertTempToString(tempC, showF) {
	var tempF = tempC * 9 / 5 + 32;
	if (showF)
		return tempF.toFixed(1) + "&deg;F";
	else
		return tempC.toFixed(1) + "&deg;C";
}

function RefreshStatus() {
	getIt(url,
		function(data) {
			obj = JSON.parse(data);
			if (obj.state === 1)
				state = "On";
			else
				state = "Off";
			document.getElementById('version').innerHTML = obj.version;
			document.getElementById('name').innerHTML = obj.name;
			//document.getElementById('state').innerHTML = 'Output: ' + state;
			//document.getElementById('raw').innerHTML = 'raw: ' + obj.raw;
			//document.getElementById('sense').innerHTML = 'Sense: ' + obj.sense;
			//document.getElementById('toggle').innerHTML = 'Toggle: ' + obj.toggle;
			//document.getElementById('reset').innerHTML = 'Reset: ' + obj.reset;
            //document.getElementById('countdown').innerHTML = 'Countdown: ' + obj.countdown;
            document.getElementById('uptime').innerHTML = 'Uptime: ' + obj.uptime;
			document.getElementById('wifimode').innerHTML = 'WiFi Mode: ' + obj.wifimode;
			// IP Address
			var elms = document.querySelectorAll('.' + 'ip'), i;
			for (i = 0; i < elms.length; ++i) {
				elms[i].textContent = obj.ip;
			}
			// Plant status
			document.getElementById('TimeOfDay').innerHTML = obj.TimeOfDay;
			var userF = obj.Fahrenheit;

			if (typeof obj.AmbientTemp_C != "undefined") {
				document.getElementById('AmbientTemp_C').innerHTML = ConvertTempToString(obj.AmbientTemp_C, userF);
			} else {
				document.getElementById('AmbientTemp_C').innerHTML = "--";
			}

			if (typeof obj.ChamberTemp_C != "undefined") {
				document.getElementById('ChamberTemp_C').innerHTML = ConvertTempToString(obj.ChamberTemp_C, userF);
				document.getElementById('ChamberHumi').innerHTML = parseInt(obj.ChamberHumi) + "%";
			} else {
				document.getElementById('ChamberTemp_C').innerHTML = "--";
				document.getElementById('ChamberHumi').innerHTML = "--"
			}				

			if (typeof obj.SoilHeater != "undefined") {
				document.getElementById('SoilHeater').innerHTML = obj.SoilHeater;
			} else {
				document.getElementById('SoilHeater').innerHTML = "--";
			}
			
			if (typeof obj.SoilTemp_C != "undefined") {
				document.getElementById('SoilTemp_C').innerHTML = ConvertTempToString(obj.SoilTemp_C, userF);
			} else {
				document.getElementById('SoilTemp_C').innerHTML = "--";
			}
			if (typeof obj.SoilTemp_C != "undefined") {
				document.getElementById('SoilMoisture_X').innerHTML = obj.SoilMoisture_X;
			} else {
				document.getElementById('SoilMoisture_X').innerHTML = "--";
			}
			
			if (obj.DrumDoorIcon == 1) {
				document.getElementById('DrumDoorIcon').style.display === "none";
			} else {
				document.getElementById('DrumDoorIcon').style.display === "block";
			}
			document.getElementById('DrumOpenSensor').innerHTML = obj.DrumOpenSensor;
			document.getElementById('DrumClosedSensor').innerHTML = obj.DrumClosedSensor;
			document.getElementById('DrumMotorStatus').innerHTML = obj.DrumMotorStatus;
			document.getElementById('DrumMotorOn_sec').innerHTML = obj.DrumMotorOn_sec + "s";
			document.getElementById('DrumMotor_V').innerHTML = parseFloat(obj.DrumMotor_V).toFixed(1) + "V";
			document.getElementById('DrumMotor_I').innerHTML = parseFloat(obj.DrumMotor_I).toFixed(1) + "A";
			
			document.getElementById('WaterInTank').innerHTML = obj.WaterInTank;
			document.getElementById('WaterPumpStatus').innerHTML = obj.WaterPumpStatus;
			document.getElementById('WaterMotor_V').innerHTML = parseFloat(obj.WaterMotor_V).toFixed(1) + "V";
			document.getElementById('WaterCyclePeriod_sec').innerHTML = obj.WaterCyclePeriod_sec + "s";
			document.getElementById('WaterCycleOn_sec').innerHTML = obj.WaterCycleOn_sec + "s";
			document.getElementById('WaterCycle_sec').innerHTML = obj.WaterCycle_sec + "s";
		}
	);
}
