package org.josast.influxdb;

import org.influxdb.InfluxDB;
import org.influxdb.dto.BatchPoints;
import org.influxdb.dto.Point;
import org.influxdb.dto.Pong;
import org.josast.EntrySat.Telemetry;
import org.josast.kaikai.generated.FrameTlmEntrySat;
import org.josast.kaikai.generated.FrameTlmEntrySat.Ax25Frame;
import org.josast.kaikai.generated.FrameTlmEntrySat.UiFrameTlmEntrySat;

import io.kaitai.struct.ByteBufferKaitaiStream;

import java.util.Date;
import java.util.concurrent.TimeUnit;

import org.influxdb.*;

/**
 * EntrySat
 *
 */
public class Entrysat 
{
	String databasename = null;
	InfluxDB influxDB=null; 
	
	public void conect (String databaseURL, String userName,String password )
	{
		 influxDB = InfluxDBFactory.connect( databaseURL, userName, password);
		Pong response =  influxDB.ping();
		if (response.getVersion().equalsIgnoreCase("unknown")) {
		   
			System.out.println("Error pinging server.");
		} else
		{
			System.out.println(response.getVersion());
		}
			
	}
	public void createDataBase(String database)
	{
		if (influxDB.databaseExists(database)==false)
		{
		System.out.println("new database");
		influxDB.createDatabase(database);
		} 
		System.out.println("database exist");
		databasename=database;
	}
	
	

   
	public int addEntrySatData(String source, String data) {
		
		byte[] tlm = hexStringToByteArray(data);
		int res = 0;
		FrameTlmEntrySat frame = new FrameTlmEntrySat(new ByteBufferKaitaiStream (tlm) );
		Ax25Frame ax = frame.frameTlmEntrySat();
		
		System.out.println(ax.ax25Header().srcCallsignRaw().srcCallsignRor().srcCallsign());
		
		if (ax.ax25Header().srcCallsignRaw().srcCallsignRor().srcCallsign().compareTo("ON02FR")==0)
		{
		Telemetry tlm1 = new Telemetry();
		tlm1.setValue(ax);
		
		UiFrameTlmEntrySat Ui = (UiFrameTlmEntrySat) ax.payload();
		// verifie que c'est bien la trame avec SID 6
		res = Ui.telemetryData().sid();
		if (Ui.telemetryData().sid() == 6) {
		
		BatchPoints batchPoints = BatchPoints
				  .database(databasename)
   		//	      .retentionPolicy("defaultPolicy")
				  .build();
				 

				 
				Point point1 = Point.measurement("BATT-TEMP")
						  .time(System.currentTimeMillis(), TimeUnit.MILLISECONDS)
						  .addField("value", tlm1.getValueDecoded(Telemetry.BATT_TEMP))
						  .addField("Source",source)
						  .addField("timFrame",tlm1.getTimeFrame())
						  .build();
				Point point2 = Point.measurement("EPS-3.3V-C")
						  .time(System.currentTimeMillis(), TimeUnit.MILLISECONDS)
						  .addField("value", tlm1.getValueDecoded(Telemetry.EPS_3_3V_C))
						  .addField("Source",source)
						  .addField("timFrame",tlm1.getTimeFrame())
						  .build();
				Point point3 = Point.measurement("EPS_5_V_C")
						  .time(System.currentTimeMillis(), TimeUnit.MILLISECONDS)
						  .addField("value", tlm1.getValueDecoded(Telemetry.EPS_5_V_C))
						  .addField("Source",source)
						  .addField("timFrame",tlm1.getTimeFrame())
						  .build();
				Point point4 = Point.measurement( "EPS-BATT-VCURRENT")
						  .time(System.currentTimeMillis(), TimeUnit.MILLISECONDS)
						  .addField("value", tlm1.getValueDecoded(Telemetry.EPS_BATT_VCURRENT))
						  .addField("Source",source)
						  .addField("timFrame",tlm1.getTimeFrame())
						  .build();
				Point point5 = Point.measurement( "EPS-TEMP")
						  .time(System.currentTimeMillis(), TimeUnit.MILLISECONDS)
						  .addField("value", tlm1.getValueDecoded(Telemetry.EPS_TEMP))
						  .addField("Source",source)
						  .addField("timFrame",tlm1.getTimeFrame())
						  .build();
				Point point6 = Point.measurement("EPS-VBATT")
						  .time(System.currentTimeMillis(), TimeUnit.MILLISECONDS)
						  .addField("value", tlm1.getValueDecoded(Telemetry.EPS_VBATT))
						  .addField("Source",source)
						  .addField("timFrame",tlm1.getTimeFrame())
						  .build();
				Point point7 = Point.measurement("TRX-TEMP")
						  .time(System.currentTimeMillis(), TimeUnit.MILLISECONDS)
						  .addField("value", tlm1.getValueDecoded(Telemetry.TRX_TEMP))
						  .addField("Source",source)
						  .addField("timFrame",tlm1.getTimeFrame())
						  .build();
				 
				batchPoints.point(point1);
				batchPoints.point(point2);
				batchPoints.point(point3);
				batchPoints.point(point4);
				batchPoints.point(point5);
				batchPoints.point(point6);
				batchPoints.point(point7);

				influxDB.write(batchPoints);
		}
		}
		
		return res;
		
	}
	public byte[] hexStringToByteArray(String s) {
	    byte[] b = new byte[s.length() / 2];
	    for (int i = 0; i < b.length; i++) {
	      int index = i * 2;
	      int v = Integer.parseInt(s.substring(index, index + 2), 16);
	      b[i] = (byte) v;
	    }
	    return b;
	  }
	
	
	
	public int addEntrySatData(String source, String data, Date time) {
		byte[] tlm = hexStringToByteArray(data);
		int res = 0;
		FrameTlmEntrySat frame = new FrameTlmEntrySat(new ByteBufferKaitaiStream (tlm) );
		Ax25Frame ax = frame.frameTlmEntrySat();
		
		System.out.println(ax.ax25Header().srcCallsignRaw().srcCallsignRor().srcCallsign());
		if (ax.ax25Header().srcCallsignRaw().srcCallsignRor().srcCallsign().compareTo("ON02FR")==0)
		{
		Telemetry tlm1 = new Telemetry();
		tlm1.setValue(ax);
		
		UiFrameTlmEntrySat Ui = (UiFrameTlmEntrySat) ax.payload();
		
		
		BatchPoints batchPoints = BatchPoints
				  .database(databasename)
 		//	      .retentionPolicy("defaultPolicy")
				  .build();
		
		
		// verifie que c'est bien la trame avec SID 6
		res = Ui.telemetryData().sid();
		
		Point point0 = Point.measurement("SID")
				  .time(time.getTime(), TimeUnit.MILLISECONDS)
				  .addField("value", res)
				  .addField("Source",source)
				  .addField("timFrame",tlm1.getTimeFrame())
				  .build();
		
		if (Ui.telemetryData().sid() == 6) {
		
				 
				Point point1 = Point.measurement("BATT-TEMP")
						  .time(time.getTime(), TimeUnit.MILLISECONDS)
						  .addField("value", tlm1.getValueDec(Telemetry.BATT_TEMP))
						  .addField("Source",source)
						  .addField("timFrame",tlm1.getTimeFrame())
						  .build();
				Point point2 = Point.measurement("EPS-3.3V-C")
						  .time(time.getTime(), TimeUnit.MILLISECONDS)
						  .addField("value",tlm1.getValueDec(Telemetry.EPS_3_3V_C))
						  .addField("Source",source)
						  .addField("timFrame",tlm1.getTimeFrame())
						  .build();
				Point point3 = Point.measurement("EPS_5_V_C")
						  .time(time.getTime(), TimeUnit.MILLISECONDS)
						  .addField("value", tlm1.getValueDec(Telemetry.EPS_5_V_C))
						  .addField("Source",source)
						  .addField("timFrame",tlm1.getTimeFrame())
						  .build();
				Point point4 = Point.measurement( "EPS-BATT-VCURRENT")
						  .time(time.getTime(), TimeUnit.MILLISECONDS)
						  .addField("value", tlm1.getValueDec(Telemetry.EPS_BATT_VCURRENT))
						  .addField("Source",source)
						  .addField("timFrame",tlm1.getTimeFrame())
						  .build();
				Point point5 = Point.measurement( "EPS-TEMP")
						  .time(time.getTime(), TimeUnit.MILLISECONDS)
						  .addField("value", tlm1.getValueDec(Telemetry.EPS_TEMP))
						  .addField("Source",source)
						  .addField("timFrame",tlm1.getTimeFrame())
						  .build();
				Point point6 = Point.measurement("EPS-VBATT")
						  .time(time.getTime(), TimeUnit.MILLISECONDS)
						  .addField("value", tlm1.getValueDec(Telemetry.EPS_VBATT))
						  .addField("Source",source)
						  .addField("timFrame",tlm1.getTimeFrame())
						  .build();
				Point point7 = Point.measurement("TRX-TEMP")
						  .time(time.getTime(), TimeUnit.MILLISECONDS)
						  .addField("value", tlm1.getValueDec(Telemetry.TRX_TEMP))
						  .addField("Source",source)
						  .addField("timFrame",tlm1.getTimeFrame())
						  .build();

				batchPoints.point(point1);
				batchPoints.point(point2);
				batchPoints.point(point3);
				batchPoints.point(point4);
				batchPoints.point(point5);
				
				batchPoints.point(point6);
				batchPoints.point(point7);

				
		}
		
		batchPoints.point(point0);
		influxDB.write(batchPoints);
		}
		
		return res;
	}
}
