2015-05-05 16:05:22 +02:00
|
|
|
package org.hwo.pulscounter;
|
|
|
|
|
2016-09-06 16:49:11 +02:00
|
|
|
import java.io.FileNotFoundException;
|
2015-05-05 16:05:22 +02:00
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
2016-09-06 16:49:11 +02:00
|
|
|
import java.util.Vector;
|
|
|
|
import java.util.prefs.BackingStoreException;
|
|
|
|
import java.util.prefs.Preferences;
|
2015-05-05 16:05:22 +02:00
|
|
|
|
2016-09-06 16:49:11 +02:00
|
|
|
import org.hwo.configuration.ConfigurableObjects;
|
2015-05-05 16:05:22 +02:00
|
|
|
import org.hwo.io.SerialPort;
|
2016-09-06 16:49:11 +02:00
|
|
|
import org.hwo.io.NewSerialPort.NewSerialPort;
|
|
|
|
import org.hwo.servicelink.ServiceLink;
|
|
|
|
import org.hwo.servicelink.ServiceLinkListener;
|
|
|
|
import org.hwo.tasklet.TaskletManager;
|
|
|
|
import org.hwo.pulscounter.SnapshotManager.Notification;
|
2015-05-05 16:05:22 +02:00
|
|
|
import org.hwo.pulscounter.ui.AppSettingsListener;
|
2016-09-06 16:49:11 +02:00
|
|
|
import org.hwo.scheduler.Scheduler;
|
2015-05-05 16:05:22 +02:00
|
|
|
|
2016-09-06 16:49:11 +02:00
|
|
|
public class PulsCounter2Application implements ServiceLinkListener{
|
2015-05-05 16:05:22 +02:00
|
|
|
|
|
|
|
static PulsCounter2Application _application;
|
|
|
|
public static PulsCounter2Application getApplication(){
|
|
|
|
if (_application == null)
|
|
|
|
_application = new PulsCounter2Application();
|
|
|
|
|
|
|
|
return _application;
|
|
|
|
}
|
|
|
|
|
2016-09-06 16:49:11 +02:00
|
|
|
private NewSerialPort serialPort;
|
|
|
|
private ServiceLink serviceLink;
|
2015-05-05 16:05:22 +02:00
|
|
|
|
2016-09-06 16:49:11 +02:00
|
|
|
private List<AppSettingsListener> appSettingsListeners;
|
|
|
|
private List<PulsCounterApplicationListener> applicationListeners;
|
2015-05-05 16:05:22 +02:00
|
|
|
|
2016-09-06 16:49:11 +02:00
|
|
|
private boolean snapshotLock;
|
|
|
|
|
|
|
|
private Vector<String> unseenMessages;
|
|
|
|
|
|
|
|
private SnapshotManager snapshotManager;
|
|
|
|
|
|
|
|
private List<ExportSetting> exportSettings;
|
|
|
|
|
|
|
|
private Scheduler scheduler;
|
|
|
|
|
|
|
|
public PulsCounter2Application() {
|
2015-05-05 16:05:22 +02:00
|
|
|
appSettingsListeners = new LinkedList<AppSettingsListener>();
|
2016-09-06 16:49:11 +02:00
|
|
|
applicationListeners = new LinkedList<PulsCounterApplicationListener>();
|
|
|
|
unseenMessages = new Vector<String>();
|
|
|
|
exportSettings = new LinkedList<ExportSetting>();
|
|
|
|
scheduler = new Scheduler();
|
|
|
|
|
|
|
|
loadPrefs();
|
|
|
|
|
|
|
|
try {
|
|
|
|
snapshotManager = new SnapshotManager();
|
|
|
|
snapshotManager.notify(Notification.INITIALIZE);
|
|
|
|
|
|
|
|
} catch (FileNotFoundException e){
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
2015-05-05 16:05:22 +02:00
|
|
|
}
|
2016-09-06 16:49:11 +02:00
|
|
|
|
|
|
|
|
2015-05-05 16:05:22 +02:00
|
|
|
|
|
|
|
public void addAppSettingsListener(AppSettingsListener listener){
|
|
|
|
appSettingsListeners.add(listener);
|
|
|
|
}
|
|
|
|
public void removeAppSettingsListener(AppSettingsListener listener){
|
|
|
|
appSettingsListeners.remove(listener);
|
|
|
|
}
|
|
|
|
|
2016-09-06 16:49:11 +02:00
|
|
|
public void addPulsCounterApplicationListener(PulsCounterApplicationListener listener){
|
|
|
|
applicationListeners.add(listener);
|
|
|
|
}
|
|
|
|
public void removePulsCounterApplicationListener(PulsCounterApplicationListener listener){
|
|
|
|
applicationListeners.remove(listener);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void fireServiceLinkChanged(){
|
2015-05-05 16:05:22 +02:00
|
|
|
for (AppSettingsListener l: appSettingsListeners){
|
|
|
|
l.ServiceLinkChanged(serviceLink);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-06 16:49:11 +02:00
|
|
|
public void fireSerialPortChanged(){
|
|
|
|
for (PulsCounterApplicationListener listener: applicationListeners){
|
|
|
|
listener.serialPortChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void fireConnectionStateChanged(){
|
|
|
|
fireConnectionStateChanged(getServiceLink().isOpen());
|
|
|
|
}
|
|
|
|
|
|
|
|
public void fireConnectionStateChanged(Boolean connected){
|
|
|
|
for (PulsCounterApplicationListener listener: applicationListeners){
|
|
|
|
listener.connectionStateChanged(connected);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void message(String message){
|
|
|
|
|
|
|
|
if (applicationListeners.size() == 0){
|
|
|
|
unseenMessages.addElement(message);
|
|
|
|
} else {
|
|
|
|
while (!unseenMessages.isEmpty()){
|
|
|
|
String msg = unseenMessages.remove(0);
|
|
|
|
for (PulsCounterApplicationListener listener: applicationListeners){
|
|
|
|
listener.messageArrived(msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (PulsCounterApplicationListener listener: applicationListeners){
|
|
|
|
listener.messageArrived(message);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
public synchronized NewSerialPort getSerialPort() {
|
|
|
|
if (serialPort == null){
|
|
|
|
serialPort = new NewSerialPort("COM1:");
|
|
|
|
}
|
2015-05-05 16:05:22 +02:00
|
|
|
return serialPort;
|
|
|
|
}
|
2016-09-06 16:49:11 +02:00
|
|
|
public synchronized void setSerialPort(NewSerialPort serialPort) {
|
2015-05-05 16:05:22 +02:00
|
|
|
if (serviceLink != null){
|
|
|
|
serviceLink.close();
|
|
|
|
serviceLink = null;
|
|
|
|
}
|
|
|
|
this.serialPort = serialPort;
|
|
|
|
|
2016-09-06 16:49:11 +02:00
|
|
|
getServiceLink();
|
|
|
|
|
2015-05-05 16:05:22 +02:00
|
|
|
fireServiceLinkChanged();
|
2016-09-06 16:49:11 +02:00
|
|
|
fireSerialPortChanged();
|
2015-05-05 16:05:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public synchronized ServiceLink getServiceLink() {
|
|
|
|
if (serviceLink == null){
|
2016-09-06 16:49:11 +02:00
|
|
|
serviceLink = new ServiceLink(getSerialPort());
|
|
|
|
serviceLink.getSerialPort().setTimeOut(200);
|
|
|
|
serviceLink.addServiceLinkListener(this);
|
2015-05-05 16:05:22 +02:00
|
|
|
}
|
|
|
|
return serviceLink;
|
|
|
|
}
|
|
|
|
public synchronized void setServiceLink(ServiceLink serviceLink) {
|
|
|
|
if (serviceLink != null){
|
|
|
|
serviceLink.close();
|
|
|
|
}
|
|
|
|
this.serviceLink = serviceLink;
|
|
|
|
|
|
|
|
fireServiceLinkChanged();
|
|
|
|
}
|
|
|
|
|
2016-09-06 16:49:11 +02:00
|
|
|
private Preferences getPreferencesNode(){
|
|
|
|
return Preferences.userNodeForPackage(getClass());
|
|
|
|
}
|
|
|
|
|
|
|
|
public void savePrefs(){
|
|
|
|
Preferences prefs = getPreferencesNode();
|
|
|
|
|
|
|
|
if (serialPort != null)
|
|
|
|
prefs.put("io.port", serialPort.getPortName());
|
|
|
|
|
|
|
|
System.out.println(String.format("savePrefs(): %d exportSettings werden gesichert.", exportSettings.size()));
|
|
|
|
if (exportSettings.size()>0)
|
|
|
|
{
|
|
|
|
for (int n=0;n<exportSettings.size();n++){
|
|
|
|
prefs.put(String.format("export.configuration.%d", n), ConfigurableObjects.getConfiguration(exportSettings.get(n)));
|
|
|
|
}
|
|
|
|
prefs.putInt("export.configurations", exportSettings.size());
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
prefs.flush();
|
|
|
|
} catch (BackingStoreException e)
|
|
|
|
{
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void loadPrefs(){
|
|
|
|
Preferences prefs = getPreferencesNode();
|
|
|
|
|
|
|
|
String sn = prefs.get("io.port", null);
|
|
|
|
|
|
|
|
if (sn != null){
|
|
|
|
NewSerialPort nsp = new NewSerialPort(sn);
|
|
|
|
setSerialPort(nsp);
|
|
|
|
}
|
|
|
|
|
|
|
|
exportSettings.clear();
|
|
|
|
int nESC = prefs.getInt("export.configurations", 0);
|
|
|
|
for (int n=0;n<nESC;n++){
|
|
|
|
ExportSetting es = new ExportSetting();
|
|
|
|
ConfigurableObjects.setConfiguration(es, prefs.get(String.format("export.configuration.%d", n), ""));
|
|
|
|
exportSettings.add(es);
|
|
|
|
}
|
|
|
|
System.out.println(String.format("loadPrefs(): %d exportSettings geladen.", nESC));
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void connectionStateChanged(Boolean connected) {
|
|
|
|
fireConnectionStateChanged(connected);
|
|
|
|
}
|
|
|
|
|
|
|
|
public SnapshotManager getSnapshotManager() {
|
|
|
|
return snapshotManager;
|
|
|
|
}
|
|
|
|
|
|
|
|
public List<ExportSetting> getExportSettings() {
|
|
|
|
return exportSettings;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void shutdown(){
|
|
|
|
System.err.println("Shutting down...");
|
|
|
|
this.scheduler.shutdown();
|
|
|
|
this.snapshotManager.doShutdown();
|
|
|
|
TaskletManager.instance().shutdown();
|
|
|
|
|
|
|
|
this.getServiceLink().close();
|
|
|
|
}
|
2015-05-05 16:05:22 +02:00
|
|
|
|
|
|
|
|
|
|
|
}
|