package org.hwo.pulscounter.device; import java.io.IOException; import java.util.ArrayList; import java.util.Calendar; import java.util.LinkedList; import java.util.List; 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; 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); } checkRealTimeClock(); 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() { checkOpen(); try { Integer v = serviceLink.readInt(13, 0, 0x0684 ); return v; } catch (IOException | ServiceLinkException e) { throw new NoDeviceConnectionException(); } } @Override public void setInverts(int inverts) { try { serviceLink.writeInt(13, 0, 0x0684, inverts); } catch (IOException | ServiceLinkException e) { log(e); } } @Override public int getTriggers(){ checkOpen(); try { Integer v = serviceLink.readInt(13, 0, 0x0685 ); return v; } catch (IOException | ServiceLinkException e) { throw new NoDeviceConnectionException(); } }; @Override public void setTriggers(int triggers){ try { serviceLink.writeInt(13, 0, 0x0685, triggers); } catch (IOException | ServiceLinkException e) { log(e); } }; @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 getHighestSnapShotIndex() { Integer s = readDeviceSerial(); if (s != null){ try { return serviceLink.readInt(13, 0, 0x0581); } catch (IOException | ServiceLinkException e) { log(ERROR,"ServiceLinkDeviceConnector: getHighestSnapShotIndex(): Exception: %s",e.toString()); } } return -1; } @Override public SnapShot[] readSnapShots(int startIndex) { Integer s = readDeviceSerial(); List snapshots = new LinkedList<>(); if (s != null){ Integer newest,sssize; int ind; Integer id; try { newest = serviceLink.readInt(13, 0, 0x0581); sssize = serviceLink.readInt(13, 0, 0x0582); if ((newest == null) || (sssize == null)){ return null; } if (startIndex < (newest - sssize)){ startIndex = newest - sssize; } if (startIndex < 0){ startIndex = 0; } log(INFO,"ServiceLinkDeviceConnector: reading snapshots [%d ... %d]",startIndex,newest); for (ind = startIndex; ind <= newest; ind++){ try { serviceLink.writeInt(13, 0, 0x0500, ind); id = serviceLink.readInt(13, 0, 0x0500); if (!id.equals(ind)){ log(WARN,"Snapshot could not be selected [%d != %d]",ind,id); } 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); id = serviceLink.readInt(13, 0, 0x0530); 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); ss.setIndex(id); ss.setField0(flags); ss.setInputmask(in); ss.setOutputmask(out); ss.setInvertmask(inv); ss.setTriggermask(trigger); ss.setPullupmask(pu); for (int i=0;i<32;i++){ ss.setValue(i, counters[i]); } for (int i=0;i<8;i++){ ss.setAnalog(i, analogs[i]); } log(INFO,"Snapshot read: %s",id); snapshots.add(ss); }; } catch (ServiceLinkRequestFailedException e){ log(e); } } } catch (IOException | ServiceLinkException e) { log(e); } } return snapshots.toArray(new SnapShot[0]); } @Override public int getInterval(int iNo) { checkOpen(); iNo %= 4; try { Integer v = serviceLink.readInt(13, 0, 0x1010 + iNo ); return v; } catch (IOException | ServiceLinkException e) { throw new NoDeviceConnectionException(); } } @Override public boolean isIntervalDailyBased(int iNo) { iNo %= 4; return ((getDailyBaseMask() & (1< 1)){ log(INFO,"realtime clock has a %d seconds shift",delta); serviceLink.writeInt((byte)13, (byte)0, 0x001C, (int)(calendar.getTimeInMillis() / 1000L)); log(INFO,"realtime clock has been corrected."); } } catch (ServiceLinkRequestFailedException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ServiceLinkException e) { e.printStackTrace(); } } }