www/js/mon_fermenter.js

Wed, 09 Jan 2019 16:19:26 +0100

author
Michiel Broek <mbroek@mbse.eu>
date
Wed, 09 Jan 2019 16:19:26 +0100
changeset 184
3dbe1d2265ed
parent 183
a810539dc218
child 185
4c25db9e8102
permissions
-rw-r--r--

Removed the setpoints and temperatures from the right display panel. Added the current temperatures in text to the gauges as caption. Adjusted the gauge caption font display. Only send commands to the mqtt server just before we fetch fresh data. Added new target temperature settings in the right display box.

/*****************************************************************************
 * Copyright (C) 2019
 *
 * Michiel Broek <mbroek at mbse dot eu>
 *
 * This file is part of BMS
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2, or (at your option) any
 * later version.
 *
 * Brewery Management System istributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with ThermFerm; see the file COPYING.  If not, write to the Free
 * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
 *****************************************************************************/


$(document).ready(function () {

	var	record = {};
	var	newBase = false;
	var	newProduct = false;
	var	newSwitch = false;
	var	newProfile = false;
	var	yl = 12;	// Normal yeast temp range
	var	yh = 24;

	var gaugeoptions = {
		min: 0, max: 40, width: 375, height: 375,
		ranges: [{ startValue:  0, endValue: yl, style: { fill: '#3399FF', stroke: '#3399FF' }, endWidth: 10, startWidth: 10 },
			 { startValue: yl, endValue: yh, style: { fill: '#00CC33', stroke: '#00CC33' }, endWidth: 10, startWidth: 10 },
			 { startValue: yh, endValue: 40, style: { fill: '#FC6A6A', stroke: '#FC6A6A' }, endWidth: 10, startWidth: 10 }],
		ticksMinor: { interval: 1, size: '5%' },
		ticksMajor: { interval: 5, size: '9%' },
		labels: { interval: 5 },
		style: { fill: '#eeeeee', stroke: '#666666' },
		value: 0,
		colorScheme: 'scheme05',
		animationDuration: 1200
	};
	var gaugeSmalloptions = {
		min: -15, max: 25, width: 190, height: 190,
		ranges: [{ startValue: -15, endValue:  0, startWidth: 5, endWidth: 5, style: { fill: '#3399FF', stroke: '#3399FF' }},
		         { startValue:   0, endValue: 10, startWidth: 5, endWidth: 5, style: { fill: '#00CC33', stroke: '#00CC33' }},
			 { startValue:  10, endValue: 25, startWidth: 5, endWidth: 5, style: { fill: '#FC6A6A', stroke: '#FC6A6A' }}],
		ticksMinor: { interval: 1, size: '5%' },
		ticksMajor: { interval: 5, size: '9%' },
		labels: { interval: 5 },
		style: { fill: '#eeeeee', stroke: '#666666' },
		value: 0,
		colorScheme: 'scheme05',
		animationDuration: 1200,
		caption: { value: 'Chiller', position: 'bottom', offset: [0, 10] }
	};
	$("#gaugeContainer_air").jqxGauge( gaugeoptions );
	$("#gaugeContainer_air").jqxGauge( { caption: { value: 'Air: 00.000' }} );
	$("#gaugeContainer_beer").jqxGauge( gaugeoptions );
	$("#gaugeContainer_beer").jqxGauge( { caption: { value: 'Beer: 00.000' }} );
	$("#gaugeContainer_chiller").jqxGauge( gaugeSmalloptions );

	var switchoptions = {
		height: 68,
		width: 35,
		theme: theme,
		thumbSize:'50%',
		orientation: 'vertical'
	};
	$("#fermenter_toggle1").jqxSwitchButton( switchoptions );
	$("#fermenter_toggle2").jqxSwitchButton( switchoptions );
	$("#fermenter_toggle3").jqxSwitchButton( switchoptions );

	srcMode = [ "OFF", "NONE", "FRIDGE", "BEER", "PROFILE" ];
	srcStage = [ "PRIMARY", "SECONDARY", "TERTIARY", "CARBONATION" ];
	$("#info_mode").jqxDropDownList({ theme: theme, source: srcMode, width: 100, height: 23, dropDownHeight: 156 });
	$("#info_stage").jqxDropDownList({ theme: theme, source: srcStage, width: 150, height: 23, dropDownHeight: 125 });

	var targetoptions = {
		inputMode: 'simple',
		spinMode: 'simple',
		theme: theme,
		width: 70,
		height: 23,
		min:0, max: 40,
		decimalDigits: 1,
		spinButtons: true,
		spinButtonsStep: 0.1
	};
	$("#target_lo").jqxNumberInput( targetoptions );
	$("#target_hi").jqxNumberInput( targetoptions );

	function sendBase(stage, mode, tlo, thi) {

		console.log("sendBase("+stage+", "+mode+", "+tlo+", "+thi+")");
		var data  = 'node=rpi01&alias=unit0&payload={"stage":"'+stage;
		    data += '","mode":"'+mode+'","setpoint":{"low":'+tlo+',"high":'+thi+'}}';
		$.ajax({
			url: "cmd_fermenter.php",
			data: data,
			type: "POST",
			success: function(data) {
				//do something after something is recieved from php
			},
			error: function(jqXHR, textStatus, errorThrown) {
				console.log("sendBase() error");
			}
		});
	}

    	var url = "getfermenter.php?uuid='" + my_uuid + "'";
    	var source = {
		datatype: "json",
		datafields: [
			{ name: 'record', type: 'int' },
			{ name: 'uuid', type: 'string' },
			{ name: 'alias', type: 'string' },
			{ name: 'node', type: 'string' },
			{ name: 'online', type: 'bool' },
			{ name: 'beercode', type: 'string' },
			{ name: 'beername', type: 'string' },
			{ name: 'air_state', type: 'string' },
			{ name: 'air_temperature', type: 'float' },
			{ name: 'beer_state', type: 'string' },
			{ name: 'beer_temperature', type: 'float' },
			{ name: 'chiller_state', type: 'string' },
			{ name: 'chiller_temperature', type: 'float' },
			{ name: 'heater_state', type: 'int' },
			{ name: 'heater_usage', type: 'int' },
			{ name: 'cooler_state', type: 'int' },
			{ name: 'cooler_usage', type: 'int' },
			{ name: 'fan_state', type: 'int' },
			{ name: 'fan_usage', type: 'int' },
			{ name: 'light_state', type: 'int' },
			{ name: 'light_usage', type: 'int' },
			{ name: 'door_state', type: 'int' },
			{ name: 'psu_state', type: 'int' },
			{ name: 'mode', type: 'string' },
			{ name: 'alarm', type: 'int' },
			{ name: 'setpoint_high', type: 'float' },
			{ name: 'setpoint_low', type: 'float' },
			{ name: 'profile_uuid', type: 'string' },
			{ name: 'profile_name', type: 'string' },
			{ name: 'profile_state', type: 'string' },
			{ name: 'profile_precent', type: 'int' },
			{ name: 'profile_inittemp_high', type: 'float' },
			{ name: 'profile_inittemp_low', type: 'float' },
			{ name: 'profile_steps', type: 'string' },
			{ name: 'stage', type: 'string' }
		],
		id: 'record',
		url: url
	};

	var dataAdapter = new $.jqx.dataAdapter(source, {
		loadComplete: function (records) {
			record = dataAdapter.records[0];
			var oline = (record.online) ? "On-line" : "Off-line";
			$("#info_uuid").html(record.uuid);
			$("#info_system").html(record.node +  "/" + record.alias + " " + oline);
			$("#info_beer").html(record.beercode + " - " + record.beername);
			$("#info_mode").jqxDropDownList('selectItem', record.mode);
			$("#info_stage").jqxDropDownList('selectItem', record.stage);
			$("#target_lo").val(record.setpoint_low);
			$("#target_hi").val(record.setpoint_high);
			if (record.online && ((record.mode == "FRIDGE") || (record.mode == "BEER"))) {
				$("#target_lo").jqxNumberInput({ readOnly: false, Width: 70, spinButtons: true });
				$("#target_hi").jqxNumberInput({ readOnly: false, Width: 70, spinButtons: true });
			} else {
				$("#target_lo").jqxNumberInput({ readOnly: true, Width: 50, spinButtons: false });
				$("#target_hi").jqxNumberInput({ readOnly: true, Width: 50, spinButtons: false });
			}

			if (record.online && (record.mode != "OFF")) {
				$("#fermenter_powerled").html('<div class="LEDblue_on"></div>Power');
			} else {
				$("#fermenter_powerled").html('<div class="LEDblue_off"></div>Power');
			}
			if (record.online && (record.alarm != "0")) {
				$("#fermenter_alarmled").html('<div class="LEDred_on"></div>Alarm');
			} else {
				$("#fermenter_alarmled").html('<div class="LEDred_off"></div>Alarm');
			}

			if (record.online && (record.heater_state != "0")) {
				$("#fermenter_led1").html('<div class="LEDgreen_on"></div>Heat');
			} else {
				$("#fermenter_led1").html('<div class="LEDgreen_off"></div>Heat');
			}
			if (record.online && (record.cooler_state != "0")) {
				$("#fermenter_led2").html('<div class="LEDgreen_on"></div>Cool');
			} else {
				$("#fermenter_led2").html('<div class="LEDgreen_off"></div>Cool');
			}
			if (record.online && (record.fan_state != "0")) {
				$("#fermenter_led3").html('<div class="LEDgreen_on"></div>Fan');
			} else {
				$("#fermenter_led3").html('<div class="LEDgreen_off"></div>Fan');
			}
			if (record.online && (record.mode == "NONE")) {
				$("#fermenter_toggle1").jqxSwitchButton( 'enable' );
				$("#fermenter_toggle2").jqxSwitchButton( 'enable' );
				$("#fermenter_toggle3").jqxSwitchButton( 'enable' );
			} else {
				$("#fermenter_toggle1").jqxSwitchButton( 'disable' );
				$("#fermenter_toggle2").jqxSwitchButton( 'disable' );
				$("#fermenter_toggle3").jqxSwitchButton( 'disable' );
				$("#fermenter_toggle1").val( (record.heater_state != "0") );
				$("#fermenter_toggle2").val( (record.cooler_state != "0") );
				$("#fermenter_toggle3").val( (record.fan_state != "0") );
			}

			$("#gaugeContainer_air").jqxGauge( { caption: { value: 'Air: '+record.air_temperature.toFixed(3) }});
			$('#gaugeContainer_air').jqxGauge({ value: record.air_temperature });
			if (record.online && (record.air_state == "OK")) {
				$("#gaugeContainer_air").jqxGauge({ disabled: false });
			} else {
				$("#gaugeContainer_air").jqxGauge({ disabled: true });
			}
			$("#gaugeContainer_beer").jqxGauge( { caption: { value: 'Beer: '+record.beer_temperature.toFixed(3) }});
			$('#gaugeContainer_beer').jqxGauge({ value: record.beer_temperature });
			if (record.online && (record.beer_state == "OK")) {
				$("#gaugeContainer_beer").jqxGauge({ disabled: false });
			} else {
				$("#gaugeContainer_beer").jqxGauge({ disabled: true });
			}
			$("#gaugeContainer_chiller").jqxGauge({ value: record.chiller_temperature });
			if (record.online && (record.chiller_state == "OK")) {
				$("#gaugeContainer_chiller").jqxGauge({ disabled: false });
			} else {
				$("#gaugeContainer_chiller").jqxGauge({ disabled: true });
			}
		}
	});

	// Get the data immediatly and then at regular intervals to refresh.
	dataAdapter.dataBind();
	setInterval(function() {
		if (newBase) {
			sendBase(record.stage, record.mode, record.setpoint_low, record.setpoint_high);
			newBase = false;
		}
		dataAdapter.dataBind();
	}, 10000);

	$('#info_mode').on('change', function (event) {
		record.mode = args.item.value;
		newBase = true;
	});
	$('#info_stage').on('change', function (event) {
		record.stage = args.item.value;
		newBase = true;
	});
	$('#target_lo').on('change', function (event) {
		record.setpoint_low = parseFloat(event.args.value);
		// Keep the high target above the low.
		if (record.setpoint_low > (record.setpoint_high - 0.2)) {
			record.setpoint_high = record.setpoint_low + 0.2;
			$("#target_hi").val(record.setpoint_high);
		}
		newBase = true;
	});
	$('#target_hi').on('change', function (event) {
		record.setpoint_high = parseFloat(event.args.value);
		// Keep the low target below the high.
		if (record.setpoint_high < (record.setpoint_low + 0.2)) {
			record.setpoint_low = record.setpoint_high - 0.2;
			$("#target_lo").val(record.setpoint_low);
		}
		newBase = true;
	});


	// The chart button.
   	$("#FLog").jqxButton({ template: "info", width: '150px', theme: theme });
	$("#FLog").click(function () {
		var url="log_fermentation.php?code=" + record.beercode + "&name=" + record.beername;
		window.open(url);
	});
});

mercurial