Archived
0

implement EDDN shipyard parser

This commit is contained in:
iMoHax
2016-10-05 17:24:07 +03:00
parent 4170d5d4e7
commit 0ac740bff2
8 changed files with 348 additions and 2 deletions

View File

@@ -44,6 +44,8 @@ public class EMDNParser {
} }
private enum PARSERS { private enum PARSERS {
V1_SHIPYARD("http://schemas.elite-markets.net/eddn/shipyard/1"),
V2_SHIPYARD("http://schemas.elite-markets.net/eddn/shipyard/2"),
V1("http://schemas.elite-markets.net/eddn/commodity/1"){ V1("http://schemas.elite-markets.net/eddn/commodity/1"){
@Override @Override
protected Body parseBody(JsonNode node) { protected Body parseBody(JsonNode node) {
@@ -191,7 +193,8 @@ public class EMDNParser {
JsonNode stationName = node.get("stationName"); JsonNode stationName = node.get("stationName");
JsonNode timestamp = node.get("timestamp"); JsonNode timestamp = node.get("timestamp");
JsonNode commodities = node.get("commodities"); JsonNode commodities = node.get("commodities");
if (systemName == null || stationName == null || timestamp == null || commodities == null){ JsonNode ships = node.get("ships");
if (systemName == null || stationName == null || timestamp == null || (commodities == null && ships == null)){
LOG.warn("Body EDDN message don't have required fields"); LOG.warn("Body EDDN message don't have required fields");
return null; return null;
} }
@@ -200,7 +203,12 @@ public class EMDNParser {
LocalDateTime dt = LocalDateTime.parse(timestamp.asText(), DateTimeFormatter.ISO_OFFSET_DATE_TIME); LocalDateTime dt = LocalDateTime.parse(timestamp.asText(), DateTimeFormatter.ISO_OFFSET_DATE_TIME);
Body body = new Body(starSystem, station); Body body = new Body(starSystem, station);
body.setTimestamp(dt); body.setTimestamp(dt);
body.addAll(parseCommodities(commodities)); if (commodities != null){
body.addAll(parseCommodities(commodities));
}
if (ships != null){
body.addShips(parseShips(ships));
}
return body; return body;
} }
@@ -235,6 +243,17 @@ public class EMDNParser {
return res; return res;
} }
protected Collection<Ship> parseShips(JsonNode ships) {
Collection<Ship> res = new ArrayList<>();
for (Iterator<JsonNode> iterator = ships.elements(); iterator.hasNext(); ) {
JsonNode node = iterator.next();
String name = node.asText();
Ship ship = new Ship(name);
res.add(ship);
}
return res;
}
private PARSERS(String schema) { private PARSERS(String schema) {
this.schema = schema; this.schema = schema;
} }

View File

@@ -9,11 +9,13 @@ public class Body {
private final Station station; private final Station station;
private LocalDateTime timestamp; private LocalDateTime timestamp;
private final Collection<Item> commodities; private final Collection<Item> commodities;
private final Collection<Ship> ships;
public Body(StarSystem system, Station station) { public Body(StarSystem system, Station station) {
this.system = system; this.system = system;
this.station = station; this.station = station;
commodities = new ArrayList<>(); commodities = new ArrayList<>();
ships = new ArrayList<>();
} }
public StarSystem getSystem() { public StarSystem getSystem() {
@@ -44,6 +46,18 @@ public class Body {
commodities.addAll(items); commodities.addAll(items);
} }
public Collection<Ship> getShips() {
return ships;
}
public void add(Ship ship){
ships.add(ship);
}
public void addShips(Collection<Ship> ships){
this.ships.addAll(ships);
}
@Override @Override
public String toString() { public String toString() {
return "Body{" + return "Body{" +
@@ -51,6 +65,7 @@ public class Body {
", station=" + station + ", station=" + station +
", timestamp=" + timestamp + ", timestamp=" + timestamp +
", commodities=" + commodities + ", commodities=" + commodities +
", ships=" + ships +
'}'; '}';
} }
} }

View File

@@ -0,0 +1,40 @@
package ru.trader.emdn.entities;
public class Ship {
private final String name;
private Long price;
private Long id;
public Ship(String name) {
this.name = name;
}
public String getName() {
return name;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Long getPrice() {
return price;
}
public void setPrice(Long price) {
this.price = price;
}
@Override
public String toString() {
return "Ship{" +
"name='" + name + '\'' +
", id=" + id +
", price=" + price +
"} ";
}
}

View File

@@ -255,4 +255,107 @@ public class ParserTest extends Assert {
} }
@Test
public void testParseShipV1() throws Exception {
EMDNParser parser = new EMDNParser();
try (InputStream is = getClass().getResourceAsStream("/emdn/ship_v1.json")) {
String json = TestUtils.read(is);
Message message = parser.parse(json);
assertNotNull(message);
assertEquals("http://schemas.elite-markets.net/eddn/shipyard/1", message.getSchemaRef());
Header header = message.getHeader();
assertNotNull(header);
assertEquals("Marek Ce'ex", header.getUploaderId());
assertEquals("E:D Market Connector [Mac OS]", header.getSoftwareName());
assertEquals("2.1.6.1", header.getSoftwareVersion());
assertEquals(LocalDateTime.of(2016, 10, 5, 13, 53, 33, 930428000), header.getGatewayTimestamp());
Body body = message.getBody();
assertNotNull(body);
assertEquals(LocalDateTime.of(2016, 10, 5, 13, 53, 25), body.getTimestamp());
StarSystem system = body.getSystem();
assertNotNull(system);
assertEquals("Venegana", system.getName());
assertNull(system.getId());
assertNull(system.getAddress());
Station station = body.getStation();
assertNotNull(station);
assertEquals("Shull Ring", station.getName());
assertNull(station.getId());
Collection<Item> items = body.getCommodities();
assertNotNull(items);
assertEquals(0, items.size());
Collection<Ship> ships = body.getShips();
assertNotNull(ships);
assertEquals(8, ships.size());
int found = 0;
for (Ship ship : ships) {
assertNotNull(ship);
if ("Sidewinder".equals(ship.getName())){
found++;
assertNull(ship.getId());
assertNull(ship.getPrice());
} else
if ("Type-7 Transporter".equals(ship.getName())){
found++;
assertNull(ship.getId());
assertNull(ship.getPrice());
}
}
assertEquals("Expected ships not found", 2, found);
}
}
@Test
public void testParseShipV2() throws Exception {
EMDNParser parser = new EMDNParser();
try (InputStream is = getClass().getResourceAsStream("/emdn/ship_v2.json")) {
String json = TestUtils.read(is);
Message message = parser.parse(json);
assertNotNull(message);
assertEquals("http://schemas.elite-markets.net/eddn/shipyard/2", message.getSchemaRef());
Header header = message.getHeader();
assertNotNull(header);
assertEquals("Amadeus Sheperd", header.getUploaderId());
assertEquals("E:D Market Connector [Windows]", header.getSoftwareName());
assertEquals("2.1.7.2", header.getSoftwareVersion());
assertEquals(LocalDateTime.of(2016, 10, 5, 13, 16, 12, 490637000), header.getGatewayTimestamp());
Body body = message.getBody();
assertNotNull(body);
assertEquals(LocalDateTime.of(2016, 10, 5, 13, 15, 51), body.getTimestamp());
StarSystem system = body.getSystem();
assertNotNull(system);
assertEquals("Sothis", system.getName());
assertNull(system.getId());
assertNull(system.getAddress());
Station station = body.getStation();
assertNotNull(station);
assertEquals("Newholm Station", station.getName());
assertNull(station.getId());
Collection<Item> items = body.getCommodities();
assertNotNull(items);
assertEquals(0, items.size());
Collection<Ship> ships = body.getShips();
assertNotNull(ships);
assertEquals(5, ships.size());
int found = 0;
for (Ship ship : ships) {
assertNotNull(ship);
if ("SideWinder".equals(ship.getName())){
found++;
assertNull(ship.getId());
assertNull(ship.getPrice());
} else
if ("Type7".equals(ship.getName())){
found++;
assertNull(ship.getId());
assertNull(ship.getPrice());
}
}
assertEquals("Expected ships not found", 2, found);
}
}
} }

View File

@@ -0,0 +1,24 @@
{
"header": {
"softwareVersion": "2.1.6.1",
"gatewayTimestamp": "2016-10-05T13:53:33.930428Z",
"softwareName": "E:D Market Connector [Mac OS]",
"uploaderID": "Marek Ce'ex"
},
"$schemaRef": "http://schemas.elite-markets.net/eddn/shipyard/1",
"message": {
"systemName": "Venegana",
"ships": [
"Sidewinder",
"Eagle",
"Asp",
"Asp Scout",
"Adder",
"Anaconda",
"Type-7 Transporter",
"Federal Gunship"
],
"stationName": "Shull Ring",
"timestamp": "2016-10-05T13:53:25Z"
}
}

View File

@@ -0,0 +1,21 @@
{
"header": {
"softwareVersion": "2.1.7.2",
"gatewayTimestamp": "2016-10-05T13:16:12.490637Z",
"softwareName": "E:D Market Connector [Windows]",
"uploaderID": "Amadeus Sheperd"
},
"$schemaRef": "http://schemas.elite-markets.net/eddn/shipyard/2",
"message": {
"systemName": "Sothis",
"ships": [
"Adder",
"Eagle",
"Hauler",
"SideWinder",
"Type7"
],
"stationName": "Newholm Station",
"timestamp": "2016-10-05T13:15:51Z"
}
}

View File

@@ -0,0 +1,62 @@
{
"$schema" : "http://json-schema.org/draft-04/schema#",
"id" : "http://schemas.elite-markets.net/eddn/shipyard/1#",
"type" : "object",
"additionalProperties" : false,
"required" : [ "$schemaRef", "header", "message" ],
"properties" : {
"$schemaRef": {
"type" : "string"
},
"header": {
"type" : "object",
"additionalProperties" : true,
"required" : [ "uploaderID", "softwareName", "softwareVersion" ],
"properties" : {
"uploaderID": {
"type" : "string"
},
"softwareName": {
"type" : "string"
},
"softwareVersion": {
"type" : "string"
},
"gatewayTimestamp": {
"type" : "string",
"format" : "date-time",
"description" : "Timestamp upon receipt at the gateway. If present, this property will be overwritten by the gateway; submitters are not intended to populate this property."
}
}
},
"message": {
"type" : "object",
"additionalProperties" : false,
"required" : [ "systemName", "stationName", "timestamp", "ships" ],
"properties" : {
"systemName": {
"type" : "string",
"minLength" : 1
},
"stationName": {
"type" : "string",
"minLength" : 1
},
"timestamp": {
"type" : "string",
"format" : "date-time"
},
"ships": {
"type" : "array",
"uniqueItems" : true,
"items" : {
"type" : "string",
"minLength" : 1,
"description" : "Ship name in English as displayed in-game. i.e. one of: Adder, Anaconda, Asp, Asp Scout, Cobra Mk III, Cobra MkIV, DiamondBack Scout, Diamondback Explorer, Eagle, Federal Assault Ship, Federal Corvette, Federal Dropship, Federal Gunship, Fer-de-Lance, Hauler, Imperial Clipper, Imperial Courier, Imperial Cutter, Imperial Eagle, Keelback, Orca, Python, Sidewinder, Type-6 Transporter, Type-7 Transporter, Type-9 Heavy, Viper, Viper MkIV, Vulture"
}
}
}
}
}
}

View File

@@ -0,0 +1,62 @@
{
"$schema" : "http://json-schema.org/draft-04/schema#",
"id" : "http://schemas.elite-markets.net/eddn/shipyard/2#",
"type" : "object",
"additionalProperties" : false,
"required" : [ "$schemaRef", "header", "message" ],
"properties" : {
"$schemaRef": {
"type" : "string"
},
"header": {
"type" : "object",
"additionalProperties" : true,
"required" : [ "uploaderID", "softwareName", "softwareVersion" ],
"properties" : {
"uploaderID": {
"type" : "string"
},
"softwareName": {
"type" : "string"
},
"softwareVersion": {
"type" : "string"
},
"gatewayTimestamp": {
"type" : "string",
"format" : "date-time",
"description" : "Timestamp upon receipt at the gateway. If present, this property will be overwritten by the gateway; submitters are not intended to populate this property."
}
}
},
"message": {
"type" : "object",
"additionalProperties" : false,
"required" : [ "systemName", "stationName", "timestamp", "ships" ],
"properties" : {
"systemName": {
"type" : "string",
"minLength" : 1
},
"stationName": {
"type" : "string",
"minLength" : 1
},
"timestamp": {
"type" : "string",
"format" : "date-time"
},
"ships": {
"type" : "array",
"minItems" : 1,
"uniqueItems" : true,
"items" : {
"type" : "string",
"minLength" : 1,
"description" : "Ship symbolic name. i.e. one of: SideWinder, Adder, Anaconda, Asp, Asp_Scout CobraMkIII, CobraMkIV, Cutter, DiamondBack, DiamondBackXL, Eagle, Empire_Courier, Empire_Eagle, Empire_Trader, Federation_Corvette, Federation_Dropship, Federation_Dropship_MkII, Federation_Gunship, FerDeLance, Hauler, Independant_Trader, Orca, Python, Type6, Type7, Type9, Viper, Viper_MkIV, Vulture"
}
}
}
}
}
}