implement EDCE checker
This commit is contained in:
@@ -3,3 +3,5 @@ log4j.rootLogger=DEBUG, stdout
|
|||||||
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
|
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
|
||||||
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
|
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
|
||||||
log4j.appender.stdout.layout.ConversionPattern=%p: %d{dd.MM.yyyy HH:mm:ss} (%F:%L) - %m%n
|
log4j.appender.stdout.layout.ConversionPattern=%p: %d{dd.MM.yyyy HH:mm:ss} (%F:%L) - %m%n
|
||||||
|
|
||||||
|
log4j.logger.org.apache=INFO
|
||||||
|
|||||||
@@ -1,28 +1,45 @@
|
|||||||
package ru.trader;
|
package ru.trader;
|
||||||
|
|
||||||
|
import javafx.application.Platform;
|
||||||
|
import javafx.util.Pair;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
import ru.trader.controllers.Screeners;
|
||||||
|
import ru.trader.edce.Converter;
|
||||||
import ru.trader.edce.EDCEParser;
|
import ru.trader.edce.EDCEParser;
|
||||||
import ru.trader.edce.EDSession;
|
import ru.trader.edce.EDSession;
|
||||||
|
import ru.trader.edce.ED_SESSION_STATUS;
|
||||||
import ru.trader.edce.entities.*;
|
import ru.trader.edce.entities.*;
|
||||||
import ru.trader.edce.entities.System;
|
import ru.trader.edce.entities.System;
|
||||||
import ru.trader.model.*;
|
import ru.trader.model.*;
|
||||||
import ru.trader.model.support.StationUpdater;
|
import ru.trader.model.support.StationUpdater;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
|
import java.util.concurrent.ScheduledFuture;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
public class EDCE {
|
public class EDCE {
|
||||||
private final static Logger LOG = LoggerFactory.getLogger(EMDNUpdater.class);
|
private final static Logger LOG = LoggerFactory.getLogger(EMDNUpdater.class);
|
||||||
|
private static ScheduledExecutorService executor;
|
||||||
|
private static ScheduledFuture<?> checker;
|
||||||
|
|
||||||
private final ProfileModel profile;
|
private final ProfileModel profile;
|
||||||
private final MarketModel world;
|
private final MarketModel world;
|
||||||
private final StationUpdater updater;
|
private final StationUpdater updater;
|
||||||
private final EDSession session;
|
private final EDSession session;
|
||||||
|
private long interval;
|
||||||
|
private boolean forceUpdate;
|
||||||
|
|
||||||
public EDCE(ProfileModel profile, MarketModel world) throws IOException, ClassNotFoundException {
|
public EDCE(ProfileModel profile, MarketModel world) throws IOException, ClassNotFoundException {
|
||||||
this.profile = profile;
|
this.profile = profile;
|
||||||
this.world = world;
|
this.world = world;
|
||||||
this.session = new EDSession();
|
this.session = new EDSession();
|
||||||
this.updater = new StationUpdater(world);
|
this.updater = new StationUpdater(world);
|
||||||
|
interval = 5;
|
||||||
|
forceUpdate = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void parseAndCheck(String json) {
|
private void parseAndCheck(String json) {
|
||||||
@@ -70,14 +87,50 @@ public class EDCE {
|
|||||||
}
|
}
|
||||||
SystemModel sModel = profile.getSystem();
|
SystemModel sModel = profile.getSystem();
|
||||||
StationModel station = sModel.get(starport.getName());
|
StationModel station = sModel.get(starport.getName());
|
||||||
boolean found = station == ModelFabric.NONE_STATION;
|
boolean found = station != ModelFabric.NONE_STATION;
|
||||||
if (!found){
|
if (!found){
|
||||||
|
forceUpdate = false;
|
||||||
LOG.info("Not found station {}, adding", starport.getName());
|
LOG.info("Not found station {}, adding", starport.getName());
|
||||||
station = sModel.add(starport.getName());
|
updater.create(sModel);
|
||||||
|
updater.setName(starport.getName());
|
||||||
|
station = updateStation(starport);
|
||||||
|
} else {
|
||||||
|
if (!profile.getStation().equals(station) || forceUpdate){
|
||||||
|
forceUpdate = false;
|
||||||
|
updater.edit(station);
|
||||||
|
updateStation(starport);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
profile.setStation(station);
|
profile.setStation(station);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private StationModel updateStation(Starport starport) {
|
||||||
|
updater.setName(starport.getName());
|
||||||
|
for (Commodity commodity : starport.getCommodities()) {
|
||||||
|
Optional<ItemModel> item = world.getItem(Converter.getItemId(commodity.getId()));
|
||||||
|
if (item.isPresent()){
|
||||||
|
Optional<StationUpdater.FakeOffer> offer = updater.getOffer(item.get());
|
||||||
|
if (offer.isPresent()){
|
||||||
|
fillOffers(offer.get(), commodity);
|
||||||
|
} else {
|
||||||
|
LOG.error("Not found offer in updater, item: {}", item.get());
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
LOG.warn("Not found item id: {}, name: {}, group: {}", commodity.getId(), commodity.getName(), commodity.getCategoryname());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
StationModel res = updater.commit();
|
||||||
|
updater.reset();
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void fillOffers(StationUpdater.FakeOffer offer, Commodity commodity){
|
||||||
|
offer.setBprice(commodity.getBuyPrice());
|
||||||
|
offer.setSprice(commodity.getSellPrice());
|
||||||
|
offer.setDemand(commodity.getDemand());
|
||||||
|
offer.setSupply(commodity.getStock());
|
||||||
|
}
|
||||||
|
|
||||||
private void checkShip(Ship ship){
|
private void checkShip(Ship ship){
|
||||||
if (ship == null){
|
if (ship == null){
|
||||||
LOG.warn("Don't read ship");
|
LOG.warn("Don't read ship");
|
||||||
@@ -87,13 +140,65 @@ public class EDCE {
|
|||||||
profile.setShipTank(ship.getFuelCapacity());
|
profile.setShipTank(ship.getFuelCapacity());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void run(){
|
||||||
|
if (executor == null) executor = Executors.newSingleThreadScheduledExecutor();
|
||||||
|
LOG.debug("Start EDCE checker each {} sec", interval);
|
||||||
|
checker = executor.scheduleAtFixedRate(new EDCEChecker(), interval, interval, TimeUnit.SECONDS);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void shutdown() throws IOException {
|
||||||
|
if (executor != null) {
|
||||||
|
LOG.debug("Shutdown EDCE checker");
|
||||||
|
if (checker != null) checker.cancel(true);
|
||||||
|
executor.shutdownNow();
|
||||||
|
executor = null;
|
||||||
|
checker = null;
|
||||||
|
}
|
||||||
|
session.close();
|
||||||
|
}
|
||||||
|
|
||||||
private class EDCEChecker implements Runnable {
|
private class EDCEChecker implements Runnable {
|
||||||
|
private boolean waiting;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
|
LOG.trace("EDCE check, waiting {}", waiting);
|
||||||
|
try {
|
||||||
|
if (waiting) return;
|
||||||
|
if (session.getLastStatus() == ED_SESSION_STATUS.OK) {
|
||||||
|
LOG.trace("Read profile from ED");
|
||||||
|
session.readProfile(EDCE.this::parseAndCheck);
|
||||||
|
}
|
||||||
|
if (session.getLastStatus() == ED_SESSION_STATUS.LOGIN_REQUIRED) {
|
||||||
|
waiting = true;
|
||||||
|
Platform.runLater(() -> {
|
||||||
|
Optional<Pair<String, String>> login = Screeners.showLogin();
|
||||||
|
if (login.isPresent()) {
|
||||||
|
String email = login.get().getKey();
|
||||||
|
String pass = login.get().getValue();
|
||||||
|
session.login(email, pass);
|
||||||
|
if (session.getLastStatus() == ED_SESSION_STATUS.VERIFICATION_REQUIRED) {
|
||||||
|
LOG.trace("Verification required, send request");
|
||||||
|
Optional<String> code = Screeners.showVerifyCodeDialog();
|
||||||
|
if (code.isPresent()) {
|
||||||
|
session.submitVerifyCode(code.get());
|
||||||
|
session.login(email, pass);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (session.getLastStatus() == ED_SESSION_STATUS.OK) {
|
||||||
|
LOG.trace("Read profile from ED");
|
||||||
session.readProfile(EDCE.this::parseAndCheck);
|
session.readProfile(EDCE.this::parseAndCheck);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
waiting = false;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} catch (Exception ex){
|
||||||
|
LOG.error("",ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import javafx.scene.Scene;
|
|||||||
import javafx.stage.Stage;
|
import javafx.stage.Stage;
|
||||||
import org.apache.log4j.PropertyConfigurator;
|
import org.apache.log4j.PropertyConfigurator;
|
||||||
import org.controlsfx.control.action.Action;
|
import org.controlsfx.control.action.Action;
|
||||||
import org.controlsfx.dialog.*;
|
import org.controlsfx.dialog.Dialog;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import ru.trader.controllers.Screeners;
|
import ru.trader.controllers.Screeners;
|
||||||
@@ -36,7 +36,7 @@ public class Main extends Application {
|
|||||||
Main.primaryStage = primaryStage;
|
Main.primaryStage = primaryStage;
|
||||||
loadMainScene();
|
loadMainScene();
|
||||||
loadResources();
|
loadResources();
|
||||||
EMDNUpdater.init();
|
ServicesManager.runAll();
|
||||||
primaryStage.show();
|
primaryStage.show();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -77,7 +77,7 @@ public class Main extends Application {
|
|||||||
if (res == Dialog.ACTION_YES) World.save();
|
if (res == Dialog.ACTION_YES) World.save();
|
||||||
else if (res == Dialog.ACTION_CANCEL) we.consume();
|
else if (res == Dialog.ACTION_CANCEL) we.consume();
|
||||||
}
|
}
|
||||||
EMDNUpdater.shutdown();
|
ServicesManager.stopAll();
|
||||||
SETTINGS.save();
|
SETTINGS.save();
|
||||||
Screeners.closeAll();
|
Screeners.closeAll();
|
||||||
} catch (FileNotFoundException | UnsupportedEncodingException | XMLStreamException e) {
|
} catch (FileNotFoundException | UnsupportedEncodingException | XMLStreamException e) {
|
||||||
@@ -98,6 +98,7 @@ public class Main extends Application {
|
|||||||
Screeners.loadFilterStage(getUrl(("filter.fxml")));
|
Screeners.loadFilterStage(getUrl(("filter.fxml")));
|
||||||
Screeners.loadItemAddStage(getUrl("itemAdd.fxml"));
|
Screeners.loadItemAddStage(getUrl("itemAdd.fxml"));
|
||||||
Screeners.loadGroupAddStage(getUrl("groupAdd.fxml"));
|
Screeners.loadGroupAddStage(getUrl("groupAdd.fxml"));
|
||||||
|
Screeners.loadLoginStage(getUrl("login.fxml"));
|
||||||
}
|
}
|
||||||
|
|
||||||
private static URL getUrl(String filename) throws MalformedURLException {
|
private static URL getUrl(String filename) throws MalformedURLException {
|
||||||
|
|||||||
51
client/src/main/java/ru/trader/ServicesManager.java
Normal file
51
client/src/main/java/ru/trader/ServicesManager.java
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
package ru.trader;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import ru.trader.controllers.MainController;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
public class ServicesManager {
|
||||||
|
private final static Logger LOG = LoggerFactory.getLogger(ServicesManager.class);
|
||||||
|
private static EDCE edce;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public static void runAll(){
|
||||||
|
runEDCE();
|
||||||
|
runEMDN();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void stopAll(){
|
||||||
|
stopEDCE();
|
||||||
|
stopEMDN();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void runEDCE() {
|
||||||
|
try {
|
||||||
|
edce = new EDCE(MainController.getProfile(), MainController.getWorld());
|
||||||
|
edce.run();
|
||||||
|
} catch (IOException | ClassNotFoundException e) {
|
||||||
|
LOG.warn("Error on init EDCE", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void stopEDCE(){
|
||||||
|
if (edce != null){
|
||||||
|
try {
|
||||||
|
edce.shutdown();
|
||||||
|
} catch (IOException e) {
|
||||||
|
LOG.warn("Error on stop EDCE", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void runEMDN() {
|
||||||
|
EMDNUpdater.init();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void stopEMDN(){
|
||||||
|
EMDNUpdater.shutdown();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -8,7 +8,9 @@ import org.slf4j.Logger;
|
|||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import ru.trader.EMDNUpdater;
|
import ru.trader.EMDNUpdater;
|
||||||
import ru.trader.core.SERVICE_TYPE;
|
import ru.trader.core.SERVICE_TYPE;
|
||||||
import ru.trader.model.*;
|
import ru.trader.model.ItemModel;
|
||||||
|
import ru.trader.model.StationModel;
|
||||||
|
import ru.trader.model.SystemModel;
|
||||||
import ru.trader.model.support.StationUpdater;
|
import ru.trader.model.support.StationUpdater;
|
||||||
import ru.trader.view.support.Localization;
|
import ru.trader.view.support.Localization;
|
||||||
import ru.trader.view.support.NumberField;
|
import ru.trader.view.support.NumberField;
|
||||||
@@ -132,15 +134,21 @@ public class StationEditorController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void showDialog(Parent parent, Parent content, StationModel station){
|
public void showDialog(Parent parent, Parent content, StationModel station){
|
||||||
showDialog(parent, content, station.getSystem(), station);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void showDialog(Parent parent, Parent content, SystemModel system, StationModel station){
|
|
||||||
if (dlg == null){
|
if (dlg == null){
|
||||||
createDialog(parent, content);
|
createDialog(parent, content);
|
||||||
}
|
}
|
||||||
dlg.setTitle(Localization.getString(station == null ? "vEditor.title.add" : "vEditor.title.edit"));
|
dlg.setTitle(Localization.getString("vEditor.title.edit"));
|
||||||
updater.init(system, station);
|
updater.edit(station);
|
||||||
|
dlg.showAndWait();
|
||||||
|
updater.reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void showDialog(Parent parent, Parent content, SystemModel system){
|
||||||
|
if (dlg == null){
|
||||||
|
createDialog(parent, content);
|
||||||
|
}
|
||||||
|
dlg.setTitle(Localization.getString("vEditor.title.add"));
|
||||||
|
updater.create(system);
|
||||||
dlg.showAndWait();
|
dlg.showAndWait();
|
||||||
updater.reset();
|
updater.reset();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ import ru.trader.services.RoutesSearchTask;
|
|||||||
import ru.trader.view.support.Localization;
|
import ru.trader.view.support.Localization;
|
||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
import java.util.Optional;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
|
|
||||||
@@ -125,6 +126,10 @@ public class MarketModel {
|
|||||||
return items;
|
return items;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Optional<ItemModel> getItem(String id){
|
||||||
|
return Optional.ofNullable(modeler.get(market.getItem(id)));
|
||||||
|
}
|
||||||
|
|
||||||
public ItemModel add(String name, GroupModel group) {
|
public ItemModel add(String name, GroupModel group) {
|
||||||
ItemModel item = modeler.get(market.addItem(name, group.getGroup()));
|
ItemModel item = modeler.get(market.addItem(name, group.getGroup()));
|
||||||
LOG.info("Add item {} to market {}", item, this);
|
LOG.info("Add item {} to market {}", item, this);
|
||||||
|
|||||||
@@ -9,6 +9,8 @@ import ru.trader.core.OFFER_TYPE;
|
|||||||
import ru.trader.core.SERVICE_TYPE;
|
import ru.trader.core.SERVICE_TYPE;
|
||||||
import ru.trader.model.*;
|
import ru.trader.model.*;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
|
||||||
public class StationUpdater {
|
public class StationUpdater {
|
||||||
private final static Logger LOG = LoggerFactory.getLogger(StationUpdater.class);
|
private final static Logger LOG = LoggerFactory.getLogger(StationUpdater.class);
|
||||||
@@ -35,7 +37,15 @@ public class StationUpdater {
|
|||||||
this.updateOnly = false;
|
this.updateOnly = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void init(SystemModel system, StationModel station){
|
public void edit(StationModel station){
|
||||||
|
init(station.getSystem(), station);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void create(SystemModel system){
|
||||||
|
init(system, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void init(SystemModel system, StationModel station){
|
||||||
LOG.debug("Init update of {}", station);
|
LOG.debug("Init update of {}", station);
|
||||||
this.station = station;
|
this.station = station;
|
||||||
this.system = system;
|
this.system = system;
|
||||||
@@ -76,6 +86,10 @@ public class StationUpdater {
|
|||||||
return offers;
|
return offers;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Optional<FakeOffer> getOffer(final ItemModel item){
|
||||||
|
return offers.stream().filter(o -> o.hasItem(item)).findAny();
|
||||||
|
}
|
||||||
|
|
||||||
public StationModel getStation() {
|
public StationModel getStation() {
|
||||||
return station;
|
return station;
|
||||||
}
|
}
|
||||||
@@ -88,6 +102,10 @@ public class StationUpdater {
|
|||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name.set(name);
|
||||||
|
}
|
||||||
|
|
||||||
public double getDistance() {
|
public double getDistance() {
|
||||||
return distance.get();
|
return distance.get();
|
||||||
}
|
}
|
||||||
@@ -96,6 +114,10 @@ public class StationUpdater {
|
|||||||
return distance;
|
return distance;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setDistance(double distance) {
|
||||||
|
this.distance.set(distance);
|
||||||
|
}
|
||||||
|
|
||||||
public BooleanProperty serviceProperty(SERVICE_TYPE service){
|
public BooleanProperty serviceProperty(SERVICE_TYPE service){
|
||||||
return services[service.ordinal()];
|
return services[service.ordinal()];
|
||||||
}
|
}
|
||||||
@@ -104,7 +126,7 @@ public class StationUpdater {
|
|||||||
offers.add(index, new FakeOffer(item));
|
offers.add(index, new FakeOffer(item));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void commit(){
|
public StationModel commit(){
|
||||||
LOG.debug("Save changes of {}", station);
|
LOG.debug("Save changes of {}", station);
|
||||||
if (isNew()) {
|
if (isNew()) {
|
||||||
Notificator notificator = market.getNotificator();
|
Notificator notificator = market.getNotificator();
|
||||||
@@ -131,6 +153,7 @@ public class StationUpdater {
|
|||||||
}
|
}
|
||||||
offers.forEach(FakeOffer::commit);
|
offers.forEach(FakeOffer::commit);
|
||||||
}
|
}
|
||||||
|
return station;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void reset(){
|
public void reset(){
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package ru.trader.view.support;
|
|||||||
|
|
||||||
import com.sun.javafx.scene.control.skin.TableViewSkin;
|
import com.sun.javafx.scene.control.skin.TableViewSkin;
|
||||||
import com.sun.javafx.scene.control.skin.VirtualFlow;
|
import com.sun.javafx.scene.control.skin.VirtualFlow;
|
||||||
|
import javafx.application.Platform;
|
||||||
import javafx.collections.ObservableList;
|
import javafx.collections.ObservableList;
|
||||||
import javafx.scene.control.TablePosition;
|
import javafx.scene.control.TablePosition;
|
||||||
import javafx.scene.control.TableView;
|
import javafx.scene.control.TableView;
|
||||||
@@ -32,4 +33,12 @@ public class ViewUtils {
|
|||||||
}
|
}
|
||||||
editNext(tableView);
|
editNext(tableView);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void doFX(Runnable runnable){
|
||||||
|
if (Platform.isFxApplicationThread()){
|
||||||
|
runnable.run();
|
||||||
|
} else {
|
||||||
|
Platform.runLater(runnable);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user