initial import
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
package io.dietz.ed.companion.component;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.DeserializationContext;
|
||||
import com.fasterxml.jackson.databind.JsonDeserializer;
|
||||
|
||||
import org.locationtech.jts.geom.Coordinate;
|
||||
import org.locationtech.jts.geom.GeometryFactory;
|
||||
import org.locationtech.jts.geom.Point;
|
||||
|
||||
import io.dietz.ed.companion.models.eddb.System;
|
||||
|
||||
public class SystemDeserializer extends JsonDeserializer<System> {
|
||||
|
||||
@Override
|
||||
public System deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
|
||||
SystemJson jsonSystem = p.readValueAs(SystemJson.class);
|
||||
|
||||
GeometryFactory gf = new GeometryFactory();
|
||||
Point test = gf.createPoint(new Coordinate(jsonSystem.x, jsonSystem.y, jsonSystem.z));
|
||||
|
||||
return new System(jsonSystem.id, jsonSystem.name, test);
|
||||
}
|
||||
|
||||
private static class SystemJson {
|
||||
|
||||
public long id;
|
||||
|
||||
public String name;
|
||||
|
||||
public double x;
|
||||
public double y;
|
||||
public double z;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
package io.dietz.ed.companion.component;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.zip.DataFormatException;
|
||||
import java.util.zip.Inflater;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.zeromq.SocketType;
|
||||
import org.zeromq.ZContext;
|
||||
import org.zeromq.ZMQ;
|
||||
|
||||
import io.dietz.ed.companion.domain.MarketWithRoot;
|
||||
|
||||
public class ZeroMqHandler implements Runnable {
|
||||
|
||||
private ZContext context;
|
||||
|
||||
@Value("${eddn.filter:https://eddn.edcd.io/schemas/commodity/3}")
|
||||
private String filterSchema;
|
||||
@Value("${eddn.host:eddn.edcd.io}")
|
||||
private String host;
|
||||
@Value("${eddn.port:9500}")
|
||||
private String port;
|
||||
|
||||
@Autowired
|
||||
private ObjectMapper objectMapper;
|
||||
|
||||
public void msg(String msg) {
|
||||
System.out.println(msg);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
pump();
|
||||
context.close();
|
||||
}
|
||||
|
||||
public synchronized void pump() {
|
||||
Inflater inflater = new Inflater();
|
||||
context = new ZContext();
|
||||
ZMQ.Socket socket = context.createSocket(SocketType.SUB);
|
||||
socket.subscribe("");
|
||||
socket.setReceiveTimeOut(30000);
|
||||
socket.connect("tcp://" + host + ":" + port);
|
||||
|
||||
msg("EDDN Relay connected");
|
||||
ZMQ.Poller poller = context.createPoller(2);
|
||||
poller.register(socket, ZMQ.Poller.POLLIN);
|
||||
byte[] output = new byte[256 * 1024];
|
||||
while (true) {
|
||||
int poll = poller.poll(-1);
|
||||
if (poll == ZMQ.Poller.POLLIN) {
|
||||
|
||||
if (poller.pollin(0)) {
|
||||
byte[] recv = socket.recv(ZMQ.NOBLOCK);
|
||||
if (recv.length > 0) {
|
||||
// decompress
|
||||
inflater.reset();
|
||||
inflater.setInput(recv);
|
||||
try {
|
||||
int outlen = inflater.inflate(output);
|
||||
String outputString = new String(output, 0, outlen, "UTF-8");
|
||||
|
||||
// outputString contains a json message
|
||||
if (outputString.contains(filterSchema)) {
|
||||
msg(outputString);
|
||||
var test = objectMapper.readValue(outputString, MarketWithRoot.class);
|
||||
msg(test.getMarket().toString());
|
||||
}
|
||||
|
||||
} catch (DataFormatException | IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user