package waterlevelsensor;

import anywheresoftware.b4a.BA;
import anywheresoftware.b4a.keywords.DateTime;

@BA.ShortName("UE3003Data")
public class UE3003Data {
	/**
	 * Check if this is a valid data
	 */
	public boolean valid = false;
	
	/**
	 * when the measurement taken, in datetime.tick
	 */
	public final long timetick = DateTime.getNow();
	
	/**
	 * Calculated water level, in meters
	 */
	public  double meters = 0;
	
	public final byte[] rawdatafromsensor;
	
	public UE3003Data() {
		rawdatafromsensor=new byte[0];
	}
	
	public UE3003Data(byte[] data) {
		rawdatafromsensor = data;
		if (valid_databuffer_header()) {
			try {
				meters = Double.valueOf(new String(data, 10, 5));	
				valid = true;	
			} catch(NumberFormatException e) {
				BA.Log("UE3003Data NumberFormatException , Msg : "+e.getMessage());
			}
		} 		
	}
	
	// header nya = F1 28 01 01 28 1C
	// lokasi bacaan ada di byte 11 - 15, berupa string dalam ascii 5 byte
	private boolean valid_databuffer_header() {
		if (rawdatafromsensor != null) {
			if (rawdatafromsensor.length==40) {
				if (rawdatafromsensor[0]==(byte)0xF1) {
					if (rawdatafromsensor[1]==0x28) {
						if (rawdatafromsensor[2]==1) {
							if (rawdatafromsensor[3]==1) {
								if (rawdatafromsensor[4]==0x28) {
									if (rawdatafromsensor[5]==0x1C) {
										return true;
									}
								}
							}
						}
					}
				}
			}
		}
		return false;
	}
}
