package devices.ups.ica.sin2100c;

import anywheresoftware.b4a.BA;
import anywheresoftware.b4a.objects.collections.Map;

@BA.ShortName("Megatec_Data")
public class Megatec_Data {
	// command I
	private String _companyname="";
	private String _upsmodel="";
	private String _version="";
	
	// command F
	private float _ratingvoltage = 0f;
	private float _ratingcurrent = 0f;
	private float _ratingbatteryvoltage = 0f;		
	private float _ratingfrequency =0f;
	
	// command Q1
	private float _inputvoltage = 0f;
	private float _inputfaultvoltage = 0f;
	private float _outputvoltage = 0f;
	private float _outputcurrentpercentage = 0f;
	private float _inputfrequency = 0f;
	private float _batteryvoltage = 0f;
	private float _temperature = 0f;
	private int _binarystatus = 0;
	private Megatec_Data_Event javaevent = null;
	
	@BA.Hide
	public void SetJavaEvent(Megatec_Data_Event value) {
		javaevent = value;
	}
	
	/**
	 * Will return Map , with following keys :
	 *   battery.charge:
	 *   battery.voltage:
	 *   ups.mfr:
	 *   ups.model:
	 *   ups.status:
	 *   ups.temperature:
	 *   ups.firmware:
	 *   ups.load:
	 *   driver.name:
	 *   input.current.nominal:
	 *   input.frequency:
	 *   input.voltage:
	 *   output.voltage:
	 *   
	 * @return Map object
	 */
	public Map MakeMap() {
		Map result = new Map();
		float battcharge = (_batteryvoltage / _ratingbatteryvoltage)*100;
		result.Put("battery.charge:", battcharge);
		result.Put("battery.voltage:", _batteryvoltage);
		result.Put("ups.mfr:", _companyname);
		result.Put("ups.model:", _upsmodel);
		//TODO: kerjain ini 
		result.Put("ups.status:", "XXX");
		result.Put("ups.temperature:", _temperature);
		result.Put("ups.firmware:", _version);
		result.Put("ups.load:", _outputcurrentpercentage);
		result.Put("driver.name:", "Megatec");
		result.Put("input.current.nominal", _ratingcurrent);
		result.Put("input.frequency:", _inputfrequency);
		result.Put("input.voltage:", _inputvoltage);
		result.Put("output.voltage:", _outputvoltage);
		return result;
	}
	
	/**
	 * Get Manufacturer Name
	 * @return value in String
	 */
	public String getCompanyName() {
		return _companyname;
	}
	
	@BA.Hide
	public void setCompanyName(String value) {
		if (value instanceof String) {
			if (!value.isEmpty()) {
				if (!value.equals(_companyname)) {
					raise_string_event("CompanyName",_companyname,value);
				}
				_companyname = value;
				return;
			}
		}
		_companyname = "";
	}
	
	/**
	 * Get UPS Model
	 * @return value in String
	 */
	public String getUPSModel() {
		return _upsmodel;
	}
	
	@BA.Hide
	public void setUPSModel(String value) {
		if (value instanceof String) {
			if (!value.isEmpty()) {
				if (!value.equals(_upsmodel)) {
					raise_string_event("UPSModel",_upsmodel,value);
				}
				_upsmodel = value;
				return;
			}
		}
		_upsmodel = "";
	}
	
	/**
	 * Get Version of UPS
	 * @return version in String
	 */
	public String getVersion() {
		return _version;
	}
	
	@BA.Hide
	public void setVersion(String value) {
		if (value instanceof String) {
			if (!value.isEmpty()) {
				if (!value.equals(_version)) {
					raise_string_event("Version",_version,value);
				}
				_version = value;
				return;
			}
		}
		_version = "";
	}
	
	/**
	 * Get Rating Voltage
	 * @return value in Volt AC
	 */
	public float getRatingVoltage() {
		return _ratingvoltage;
	}
	
	@BA.Hide
	public void setRatingVoltage(float value) {
		if (value<0f) value = 0f;
		if (value>999.9f) value = 999.9f;
		if (value != _ratingvoltage) {
			raise_float_event("RatingVoltage", _ratingvoltage, value);
		}
		_ratingvoltage = value;
	}
	
	/**
	 * Get Output Rating Current
	 * @return value in Amp
	 */
	public float getRatingCurrent() {
		return _ratingcurrent;
	}
	
	@BA.Hide
	public void setRatingCurrent(float value) {
		if (value<0f) value = 0f;
		if (value>999f) value = 999f;
		if (value != _ratingcurrent) {
			raise_float_event("RatingCurrent", _ratingcurrent, value);
		}
		_ratingcurrent= value;
	}
	
	/**
	 * Get Rating Battery Voltage
	 * @return value in Volt DC
	 */
	public float getRatingBatteryVoltage() {
		return _ratingbatteryvoltage;
	}
	
	@BA.Hide
	public void setRatingBatteryVoltage(float value) {
		if (value<0f)  value = 0f;
		if (value>999.9f) value = 999.9f;
		if (value != _ratingbatteryvoltage) {
			raise_float_event("RatingBatteryVoltage", _ratingbatteryvoltage, value);
		}
		_ratingbatteryvoltage = value;
	}
	
	/**
	 * Get Rating Frequency
	 * @return value in Hz
	 */
	public float getRatingFrequency() {
		return _ratingfrequency;
	}
	
	@BA.Hide
	public void setRatingFrequency(float value) {
		if (value<0f) value = 0f;
		if (value>99.9f) value = 99.9f;
		if (value != _ratingfrequency) {
			raise_float_event("RatingFrequency", _ratingfrequency, value);
		}
		_ratingfrequency = value;
	}
	
	/**
	 * Get Input Voltage
	 * @return value in Volt AC
	 */
	public float getInputVoltage() {
		return _inputvoltage;
	}
	
	@BA.Hide
	public void setInputVoltage(float value) {
		if (value<0f) value = 0f;
		if (value>999.9f) value = 999.9f;
		if (value != _inputvoltage) {
			raise_float_event("InputVoltage", _inputvoltage, value);
		}
		_inputvoltage = value;
	}
	
	/**
	 * Get Threshold of  Input Voltage, where this condition is considered to be Utility Fault
	 * @return value in Volt AC
	 */
	public float getInputFaultVoltage() {
		return _inputfaultvoltage;
	}
	
	@BA.Hide
	public void setInputFaultVoltage(float value) {
		if (value<0f) value = 0f;
		if (value>999.9f) value = 999.9f;
		
		if (value != _inputfaultvoltage) {
			raise_float_event("InputFaultVoltage", _inputfaultvoltage, value);
		}
		_inputfaultvoltage = value;
	}
	
	/**
	 * Get Output Voltage
	 * @return value in Volt AC
	 */
	public float getOutputVoltage() {
		return _outputvoltage;
	}
	
	@BA.Hide
	public void setOutputVoltage(float value) {
		if (value<0f) value = 0f;
		if (value>999.9f) value = 999.9f;
		
		if (value != _outputvoltage) {
			raise_float_event("OutputVoltage", _outputvoltage, value);
		}
		_outputvoltage = value;
	}
	
	/**
	 * Get Output Current load in percent of rating current
	 * @return value in percent
	 */
	public float getOutputCurrentPercentage() {
		return _outputcurrentpercentage;
	}
	
	@BA.Hide
	public void setOutputCurrentPercentage(float value) {
		if (value<0f) value = 0f;
		if (value>100f) value = 100f;
		if (value != _outputcurrentpercentage) {
			raise_float_event("OutputCurrentPercentage", _outputcurrentpercentage, value);
		}
		_outputcurrentpercentage = value;
	}
	
	/**
	 * Get Utility Frequency
	 * @return value in Hz
	 */
	public float getInputFrequency() {
		return _inputfrequency;
	}
	
	@BA.Hide
	public void setInputFrequency(float value) {
		if (value<0f) value = 0f;
		if (value>99.9f) value = 99.9f;
		if (value != _inputfrequency) {
			raise_float_event("InputFrequency", _inputfrequency, value);
		}
		_inputfrequency = value;
	}
	
	/**
	 * Get Battery Voltage
	 * @return value in Volt DC
	 */
	public float getBatteryVoltage() {
		return _batteryvoltage;
	}
	
	@BA.Hide
	public void setBatteryVoltage(float value) {
		if (value<0f) value = 0f;
		if (value>999.9f) value = 999.9f;
		if (value != _batteryvoltage) {
			raise_float_event("BatteryVoltage", _batteryvoltage, value);
		}
		_batteryvoltage = value;
	}
	
	/**
	 * Get UPS Temperature
	 * @return value in Celcius
	 */
	public float getTemperature() {
		return _temperature;
	}
	
	@BA.Hide
	public void setTemperature(float value) {
		if (value<0f) value = 0f;
		if (value>99.9f) value = 99.9f;
		if (value != _temperature) {
			raise_float_event("Temperature", _temperature, value);
		}
		_temperature = value;
	}
	
	@BA.Hide
	public void setBinaryStatus(int value) {
		value = value & 0xFF;
		if (value != _binarystatus) {
			raise_float_event("BinaryStatus",_binarystatus, value);
		}
		_binarystatus = value;
	}
	
	/**
	 * If true , Utility Electric is failed 
	 * @return true or false
	 */
	public boolean getUtilityFail() {
		return ((_binarystatus & (1<<7))>0);
	}
	
	/**
	 * If true, Battery is Low
	 * @return true or false
	 */
	public boolean getBatteryLow() {
		return ((_binarystatus & (1<<6))>0);
	}
	
	/**
	 * If true, UPS in Bypass stage
	 * @return true or false
	 */
	public boolean getUPSIsBypass() {
		return ((_binarystatus & (1<<5))>0);
	}
	
	/**
	 * If true, UPS Buck/Boost in active stage
	 * This is opposite of UPSIsBypass()
	 * @return true or false
	 */
	public boolean getUPSBuckBoostIsActive() {
		return ((_binarystatus & (1<<5))==0);
	}
	
	/**
	 * If true, UPS is Failed
	 * @return true or false
	 */
	public boolean getUPSFail() {
		return ((_binarystatus & (1<<4))>0);
	}
	
	/**
	 * If True , UPS in Standby Stage (off power)
	 * @return true or false
	 */
	public boolean getUPSIsStandby() {
		return ((_binarystatus & (1<<3))>0);
	}
	
	/**
	 * If true, UPS in ON stage (on power)
	 * @return true or false
	 */
	public boolean getUPSIsON() {
		return ((_binarystatus & (1<<3))==0);
	}
	
	/**
	 * If true, UPS in Testing stage
	 * @return true or false
	 */
	public boolean getUPSIsTesting() {
		return ((_binarystatus & (1<<2))>0);
	}
	
	/**
	 * If true, UPS in Shutdown stage
	 * @return true or false
	 */
	public boolean getUPSIsShutdown() {
		return ((_binarystatus & (1<<1))>0);
	}
	
	/**
	 * if true, Beeper is ON
	 * @return true or false
	 */
	public boolean getBeepIsON() {
		return ((_binarystatus & 1)>0);
	}
	
	private void raise_string_event(String field, String olddata, String newdata) {
		if (javaevent instanceof Megatec_Data_Event) {
			javaevent.newdatastring(field, olddata, newdata);
		}
	}
	
	private void raise_float_event(String field, float olddata, float newdata) {
		if (javaevent instanceof Megatec_Data_Event) {
			javaevent.newdatafloat(field, olddata, newdata);
		}
	}
}
