package pluginreceiver.model;
import pluginreceiver.logic.Observer;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.util.logging.Logger;
import pluginreceiver.gui.*;
/**
* Main data reception and decoding unit
* @author nicATC
*/
public class XPlaneDataPacketDecoder{
// private static Logger logger = Logger.getLogger("de.georg_gruetter.xhsi");
private boolean received_hsid_packet = false;
private boolean received_fms_packet = false;
private static final int FRAMES = 10;
/** number of frames after which an update occurs*/
private int countdown = FRAMES;
/** data repository for HSID data*/
XPlaneSimDataRepository xplane_data_repository = null;
/** data repository for FMSE data*/
FMS fms = FMS.get_instance();
public XPlaneDataPacketDecoder() {
this.xplane_data_repository = XPlaneSimDataRepository.get_instance();
xplane_data_repository.add_observer(new Observer(xplane_data_repository));
}
public XPlaneDataPacketDecoder(IGUI g) {
this.xplane_data_repository = XPlaneSimDataRepository.get_instance();
xplane_data_repository.add_observer(new pluginreceiver.logic.GUIObserver(xplane_data_repository, g));
}
/**
* Decodes the stream and updates the observers
* @param sim_data the current byte package
* @throws java.lang.Exception
*/
public void new_sim_data(byte[] sim_data) throws Exception {
// identify the packet type (identified by the first four bytes)
String packet_type = new String(sim_data,0,4).trim();
if (packet_type.equals("HSID")) {
// if (this.received_hsid_packet == false)
// logger.fine("Received first HSID packet");
DataInputStream data_stream = new DataInputStream(new ByteArrayInputStream(sim_data));
data_stream.skipBytes(4); // skip the bytes containing the packet type id
int nb_of_data_points = (int) data_stream.readFloat();
// Reads the HSID data und deploys them into the data repository
for (int i=0;i<nb_of_data_points;i++) {
int data_point_id = (int) data_stream.readFloat();
this.xplane_data_repository.store_sim_value(data_point_id, data_stream.readFloat());
}
if (this.received_hsid_packet == false) {
// logger.config("... detected XHSI Plugin Version " + decode_plugin_version(this.xplane_data_repository.get_sim_value(XPlaneSimDataRepository.PLUGIN_VERSION_ID)));
// logger.fine("... HSID packet contains " + nb_of_data_points + " sim data values");
}
this.received_hsid_packet = true;
// Does the same for FMSE data
} else if (packet_type.equals("FMSE")) {
// if (this.received_fms_packet == false)
// logger.fine("Received first FMSE packet");
DataInputStream data_stream = new DataInputStream(new ByteArrayInputStream(sim_data));
data_stream.skipBytes(4); // skip the bytes containing the packet type id
this.fms.clear();
int nb_of_entries = (int) data_stream.readFloat();
// if (this.received_fms_packet == false)
// logger.fine("... FMSE packet contains " + nb_of_entries + " FMS entries");
int active_entry_index = (int) data_stream.readFloat();
data_stream.skip(4); // don't evaluate destination entry index. This info is determined by is_active flag"
for (int i=0;i<nb_of_entries;i++) {
int type = (int) data_stream.readFloat();
String id = new String(sim_data,((i*24))+20,5).trim();
data_stream.skipBytes(8);
float altitude = data_stream.readFloat();
float lat = data_stream.readFloat();
float lon = data_stream.readFloat();
boolean is_active = (i == active_entry_index);
this.fms.append_entry(new FMSEntry(id, type, lat, lon, altitude, is_active));
}
this.received_fms_packet = true;
}
// Starts updating the observers registered in the data repository
if (countdown < 1)
{
xplane_data_repository.tick_updates();
countdown = FRAMES+1;
}
countdown--;
}
}