Created
January 26, 2015 01:04
-
-
Save rob42/b7d695c447293336c42b to your computer and use it in GitHub Desktop.
Process n2k from n2kMapping.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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