Skip to content

Instantly share code, notes, and snippets.

@tkurki
Forked from rob42/gist:b7d695c447293336c42b
Last active August 29, 2015 14:14
Show Gist options
  • Save tkurki/5e5bfce486271db76f6a to your computer and use it in GitHub Desktop.
Save tkurki/5e5bfce486271db76f6a to your computer and use it in GitHub Desktop.
//java, the main part of my processor.
/*
* {
"timestamp": "2013-10-08-15:47:28.264",
"prio": "2",
"src": "2",
"dst": "255",
"pgn": "129025",
"description": "Position, Rapid Update",
"fields": {
"Latitude": "60.1445540",
"Longitude": "24.7921348"
}
}
*/
//@Override
public Json handle(String n2kmsg) {
//get the pgn value
String pgn = JsonPath.read(n2kmsg,"$.pgn");
logger.debug("processing n2k pgn "+pgn );
if(mappings.has(pgn)){
//process it, mappings is n2kMapping.json as a json object
Json mapping = mappings.at(pgn);
if( mapping==null)return null;
//make a dummy signalk object
SignalKModel temp = SignalKModelFactory.getCleanInstance();
//mapping contains an array
for(Json map : mapping.asJsonList()){
//TODO: precompile the mappings for performance, precompile the msg to json Document
//TODO: changes to n2k formatted output mean we will get back string, int,long,float, double here
String var = JsonPath.read(n2kmsg, map.at(FILTER).asString()+"."+map.at(SOURCE).asString());
Object obj = resolve(var);
String node = map.at(NODE).asString();
if(node!=null && var!=null){
//put in signalk tree
temp.putWith(self+node, obj);
}
}
logger.debug("N2KProcessor output "+temp );
return (Json) temp;
}
return null;
}
private Object resolve(String var) {
if(var==null) return null;
try {
return numberFormat.parse(var);
} catch (ParseException e) {
return var;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment