package org.hwo.pulscounter.device; import java.io.IOException; import org.hwo.io.NewSerialPort.NewSerialPort; import org.hwo.pulscounter.SnapShot; import org.hwo.pulscounter.ui.DeviceConfiguration; import org.hwo.servicelink.ServiceLink; import org.hwo.servicelink.ServiceLinkException; import org.hwo.servicelink.ServiceLinkRequestFailedException; import org.hwo.ui.dialog.SerialPortChooser; import static org.hwo.logging.Logging.*; import static org.hwo.logging.LogLevel.*; public class ServiceLinkDeviceConnector implements IDeviceConnector { private ServiceLink serviceLink; private Integer deviceSerial; private Integer indSnapshotOldest; public ServiceLinkDeviceConnector() { serviceLink = new ServiceLink(new NewSerialPort("COM1:")); } private void checkOpen(){ if (!serviceLink.isOpen()){ try { serviceLink.open(); serviceLink.getSerialPort().setTimeOut(250); } catch (ServiceLinkException e) { throw new NoDeviceConnectionException(); } } } @Override public String toString() { return String.format("Serial [%s]", this.serviceLink.getSerialPort().getPortName()); } private Integer readDeviceSerial(){ checkOpen(); try { Integer v = serviceLink.readInt(13, 0, 0x0001 ); return v; } catch (IOException | ServiceLinkException e) { throw new NoDeviceConnectionException(); } } @Override public Integer getDeviceSerial() { Integer serial = readDeviceSerial(); deviceSerial = serial; return serial; } public void setDeviceSerial(int serial) { checkOpen(); try { serviceLink.writeInt(13, 0, 0x0004, -1895890944); serviceLink.writeInt(13, 0, 0x0001, serial ); serviceLink.writeInt(13, 0, 0x0004, 0x0); } catch (IOException | ServiceLinkException e) { throw new NoDeviceConnectionException(); } } @Override public boolean showConnctionSetup() { NewSerialPort newSerialPort = SerialPortChooser.execute(serviceLink.getSerialPort().getPortName()); if (newSerialPort != null){ serviceLink.close(); serviceLink.getSerialPort().setPortName(newSerialPort.getPortName()); return true; } return false; } @Override public String getConnectionSettings() { return serviceLink.getSerialPort().getPortName(); } @Override public void setConnectionSettings(String connectionSettings) { serviceLink.close(); serviceLink.getSerialPort().setPortName(connectionSettings); } @Override public String getConnectionSettingsText() { return String.format("Port: %s",getConnectionSettings()); } @Override public int[] getCounters() { int[] values = new int[32]; for (int n=0;n<32;n++){ values[n] = getCounter(n); } return values; } @Override public void setCounters(int[] values) { for (int n=0;n<32;n++){ setCounter(n, values[n]); } } @Override public int getCounter(int channel) { Integer v = null; checkOpen(); try { v = serviceLink.readInt(13, 0, 0x600 + channel ); } catch (Exception e) { throw new NoDeviceConnectionException(); } if (v != null){ return v; } else { return 0; } } @Override public void setCounter(int channel, int counter) { checkOpen(); try { serviceLink.writeInt(13, 0, 0x0600 + channel, counter); } catch (IOException | ServiceLinkException e) { e.printStackTrace(); } } @Override public int[] getSimpleScript() { // TODO Auto-generated method stub return null; } @Override public void setSimpleScript(int[] simpleScript) { // TODO Auto-generated method stub } @Override public int getInputs() { checkOpen(); try { Integer v = serviceLink.readInt(13, 0, 0x0681 ); return v; } catch (IOException | ServiceLinkException e) { throw new NoDeviceConnectionException(); } } @Override public int getOutputs() { checkOpen(); try { Integer v = serviceLink.readInt(13, 0, 0x0682 ); return v; } catch (IOException | ServiceLinkException e) { throw new NoDeviceConnectionException(); } } @Override public void setOutputs(int outputs) { try { serviceLink.writeInt(13, 0, 0x0682, outputs); } catch (IOException | ServiceLinkException e) { log(e); } } @Override public int getPullups() { checkOpen(); try { Integer v = serviceLink.readInt(13, 0, 0x0683 ); return v; } catch (IOException | ServiceLinkException e) { throw new NoDeviceConnectionException(); } } @Override public void setPullups(int pullups) { try { serviceLink.writeInt(13, 0, 0x0683, pullups); } catch (IOException | ServiceLinkException e) { log(e); } } @Override public int getInverts() { // TODO Auto-generated method stub return 0; } @Override public void setInverts(int inverts) { // TODO Auto-generated method stub } @Override public float[] getAnalogs() { float[] values = new float[32]; for (int n=0;n<8;n++){ values[n] = getAnalog(n); } return values; } @Override public float getAnalog(int channel) { Integer v = null; checkOpen(); try { v = serviceLink.readInt(13, 0, 0x8000 + channel ); } catch (Exception e) { throw new NoDeviceConnectionException(); } if (v != null){ return (v / 6553.60f); } else { return 0.0f; } } @Override public int getAvailableSnapshots() { Integer s = readDeviceSerial(); if (s != null){ Integer oldest,newest,sssize; try { if (s.equals(deviceSerial) && (indSnapshotOldest != null)){ oldest = indSnapshotOldest; } else { oldest = serviceLink.readInt(13, 0, 0x0580); } newest = serviceLink.readInt(13, 0, 0x0581); sssize = serviceLink.readInt(13, 0, 0x0582); Integer avail = newest - oldest; if (avail < 0){ avail += sssize; } return avail; } catch (IOException | ServiceLinkException e) { log(e); } } return 0; } @Override public SnapShot[] readSnapShots() { Integer s = readDeviceSerial(); if (s != null){ Integer oldest,newest,sssize; try { if (s.equals(deviceSerial) && (indSnapshotOldest != null)){ oldest = indSnapshotOldest; } else { oldest = serviceLink.readInt(13, 0, 0x0580); } newest = serviceLink.readInt(13, 0, 0x0581); sssize = serviceLink.readInt(13, 0, 0x0582); while (!oldest.equals(newest)){ Integer id; try { serviceLink.writeInt(13, 0, 0x0500, oldest); id = serviceLink.readInt(13, 0, 0x0500); if (!id.equals(oldest)){ log(WARN,"Snapshot could not be selected [%d]",oldest); } else { Integer timestamp, flags, in, out, pu, inv, trigger; Integer[] counters, analogs; timestamp = serviceLink.readInt(13, 0, 0x0501); flags = serviceLink.readInt(13, 0, 0x0502); in = serviceLink.readInt(13, 0, 0x0503); out = serviceLink.readInt(13, 0, 0x0504); pu = serviceLink.readInt(13, 0, 0x0505); inv = serviceLink.readInt(13, 0, 0x0506); trigger = serviceLink.readInt(13, 0, 0x0507); counters = new Integer[32]; analogs = new Integer[8]; for (int n=0;n<32;n++){ counters[n] = serviceLink.readInt(13, 0, 0x0510 + n); } for (int n=0;n<8;n++){ analogs[n] = serviceLink.readInt(13, 0, 0x0508 + n); } SnapShot ss = new SnapShot(s); ss.setTimestamp(timestamp); }; } catch (ServiceLinkRequestFailedException e){ log(e); } } } catch (IOException | ServiceLinkException e) { log(e); } } return null; } @Override public SnapShot readSnapShot() { // TODO Auto-generated method stub return null; } }