private int fieldSize(Field f) { FlightSimData fsd = f.getAnnotation(FlightSimData.class); if (f.getType().equals(Float.class) || f.getType().equals(float.class) || f.getType().equals(Integer.class) || f.getType().equals(int.class) || f.getType().equals(Boolean.class) || f.getType().equals(boolean.class) || f.getType().equals(Short.class) || f.getType().equals(short.class)) return 4; if (f.getType().equals(Double.class) || f.getType().equals(double.class) || f.getType().equals(Long.class) || f.getType().equals(long.class)) return 8; if (f.getType().equals(XYZ.class)) return DataType.XYZ.size(); if (f.getType().equals(MarkerState.class)) return DataType.MARKERSTATE.size(); if (f.getType().equals(Waypoint.class)) return DataType.WAYPOINT.size(); if (f.getType().equals(LatLonAlt.class)) return DataType.LATLONALT.size(); else if (f.getType().equals(String.class)) { int len = fsd.stringWidth(); return len; } return 0; // fallback }
private void wrapField(Object o, Field f, DataWrapper dw) throws IllegalDataDefinition, IllegalAccessException { FlightSimData fsd = f.getAnnotation(FlightSimData.class); boolean accStatus = f.isAccessible(); f.setAccessible(true); // determinate field class if (f.getType().equals(Float.class) || f.getType().equals(float.class)) { Float val = (Float) f.get(o); dw.putFloat32(val.floatValue()); } else if (f.getType().equals(Double.class) || f.getType().equals(double.class)) { Double val = (Double) f.get(o); dw.putFloat64(val.doubleValue()); } else if (f.getType().equals(Integer.class) || f.getType().equals(int.class)) { Integer val = (Integer) f.get(o); dw.putInt32(val.intValue()); } else if (f.getType().equals(Long.class) || f.getType().equals(long.class)) { Long val = (Long) f.get(o); dw.putInt64(val.longValue()); } else if (f.getType().equals(Boolean.class) || f.getType().equals(boolean.class)) { Boolean val = (Boolean) f.get(o); dw.putInt32(val ? 1 : 0); } else if (f.getType().equals(Short.class) || f.getType().equals(short.class)) { Short val = (Short) f.get(o); dw.putInt32(val.intValue()); } else if (f.getType().equals(LatLonAlt.class)) { LatLonAlt val = (LatLonAlt) f.get(o); dw.putData(val); } else if (f.getType().equals(XYZ.class)) { XYZ val = (XYZ) f.get(o); dw.putData(val); } else if (f.getType().equals(Waypoint.class)) { Waypoint val = (Waypoint) f.get(o); dw.putData(val); } else if (f.getType().equals(MarkerState.class)) { MarkerState val = (MarkerState) f.get(o); dw.putData(val); } else if (f.getType().equals(String.class)) { String s = (String) f.get(o); if (s == null) s = ""; int len = fsd.stringWidth(); dw.putString(s, len); } f.setAccessible(accStatus); }
/** * Unwrap a field * * @param o destination object * @param f field * @param ro receive bufer * @throws IllegalArgumentException * @throws IllegalAccessException */ private void unwrapField(Object o, Field f, RecvSimObjectData ro) throws IllegalArgumentException, IllegalAccessException { FlightSimData fsd = f.getAnnotation(FlightSimData.class); boolean accStatus = f.isAccessible(); f.setAccessible(true); // determinate field class if (f.getType().equals(Float.class) || f.getType().equals(float.class)) { Float val = new Float(ro.getDataFloat32()); f.set(o, val); } else if (f.getType().equals(Double.class) || f.getType().equals(double.class)) { Double val = new Double(ro.getDataFloat64()); f.set(o, val); } else if (f.getType().equals(Integer.class) || f.getType().equals(int.class)) { Integer val = new Integer(ro.getDataInt32()); f.set(o, val); } else if (f.getType().equals(Long.class) || f.getType().equals(long.class)) { Long val = new Long(ro.getDataInt64()); f.set(o, val); } else if (f.getType().equals(Boolean.class) || f.getType().equals(boolean.class)) { Boolean val = new Boolean(ro.getDataInt32() != 0); f.set(o, val); } else if (f.getType().equals(Short.class) || f.getType().equals(short.class)) { Short val = new Short((short) ro.getDataInt32()); f.set(o, val); } else if (f.getType().equals(LatLonAlt.class)) { LatLonAlt val = ro.getLatLonAlt(); f.set(o, val); } else if (f.getType().equals(XYZ.class)) { XYZ val = ro.getXYZ(); f.set(o, val); } else if (f.getType().equals(Waypoint.class)) { Waypoint val = ro.getWaypoint(); f.set(o, val); } else if (f.getType().equals(MarkerState.class)) { MarkerState val = ro.getMarkerState(); f.set(o, val); } else if (f.getType().equals(String.class)) { int len = fsd.stringWidth(); String val = null; switch (len) { case 8: val = ro.getDataString8(); break; case 32: val = ro.getDataString32(); break; case 64: val = ro.getDataString64(); break; case 128: val = ro.getDataString128(); break; case 256: val = ro.getDataString256(); break; case 260: val = ro.getDataString260(); break; } f.set(o, val); } f.setAccessible(accStatus); }
/** * Register a class prior to use. This is mandatory before calling {@link * #requestSimObjectData(Class, int, SimConnectPeriod, boolean)} or {@link * #unwrap(RecvSimObjectData)} or {@link #setSimObjectData(Object, int)} * * @param c class to use * @param dataDefID force data definition ID * @return data definition ID (here <code>dataDefID</code>) * @throws IllegalDataDefinition if the class <code>c</code> contains invalid or non-mappable * fields * @throws IOException SimConnect IO Errors */ public int registerClass(int dataDefID, Class<?> c) throws IOException, IllegalDataDefinition { // // don't do it twice if (definitionIds.containsKey(c)) { return definitionIds.get(c).intValue(); } int fieldAdded = 0; for (Field f : c.getDeclaredFields()) { if (f.isAnnotationPresent(FlightSimData.class)) { // register field FlightSimData fsd = f.getAnnotation(FlightSimData.class); String variable = fsd.variable(); String units = fsd.units(); // determinate field simconnect type from its class SimConnectDataType type = SimConnectDataType.INVALID; if (f.getType().equals(Float.class) || f.getType().equals(float.class)) type = SimConnectDataType.FLOAT32; else if (f.getType().equals(Double.class) || f.getType().equals(double.class)) type = SimConnectDataType.FLOAT64; else if (f.getType().equals(Integer.class) || f.getType().equals(int.class)) type = SimConnectDataType.INT32; else if (f.getType().equals(Long.class) || f.getType().equals(long.class)) type = SimConnectDataType.INT64; else if (f.getType().equals(Boolean.class) || f.getType().equals(boolean.class)) type = SimConnectDataType.INT32; else if (f.getType().equals(Short.class) || f.getType().equals(short.class)) type = SimConnectDataType.INT32; else if (f.getType().equals(LatLonAlt.class)) type = SimConnectDataType.LATLONALT; else if (f.getType().equals(XYZ.class)) type = SimConnectDataType.XYZ; else if (f.getType().equals(Waypoint.class)) type = SimConnectDataType.WAYPOINT; else if (f.getType().equals(MarkerState.class)) type = SimConnectDataType.MARKERSTATE; else if (f.getType().equals(String.class)) { int len = fsd.stringWidth(); switch (len) { case 8: type = SimConnectDataType.STRING8; break; case 32: type = SimConnectDataType.STRING32; break; case 64: type = SimConnectDataType.STRING64; break; case 128: type = SimConnectDataType.STRING128; break; case 256: type = SimConnectDataType.STRING256; break; case 260: type = SimConnectDataType.STRING260; break; default: throw new IllegalDataDefinition("Invalid string length (" + len + ")"); } } if (type == SimConnectDataType.INVALID) { throw new IllegalDataDefinition("Invalid field type (" + f.getType().getName() + ")"); } // // build simconnect data def simConnect.addToDataDefinition(dataDefID, variable, units, type); fieldAdded++; } } // register it if we had something to write if (fieldAdded > 0) definitionIds.put(c, dataDefID); return requestID; }