org.hwo.pulscounter/src/org/hwo/pulscounter/device/ServiceLinkDeviceConnector....

403 lines
8.3 KiB
Java
Raw Normal View History

2016-09-08 18:47:31 +02:00
package org.hwo.pulscounter.device;
2016-09-09 00:57:02 +02:00
import java.io.IOException;
2016-09-13 12:07:01 +02:00
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
2016-09-09 00:57:02 +02:00
2016-09-08 18:47:31 +02:00
import org.hwo.io.NewSerialPort.NewSerialPort;
2016-09-13 10:53:46 +02:00
import org.hwo.pulscounter.SnapShot;
2016-09-09 00:57:02 +02:00
import org.hwo.pulscounter.ui.DeviceConfiguration;
2016-09-08 18:47:31 +02:00
import org.hwo.servicelink.ServiceLink;
2016-09-09 00:57:02 +02:00
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.*;
2016-09-08 18:47:31 +02:00
public class ServiceLinkDeviceConnector implements IDeviceConnector {
private ServiceLink serviceLink;
2016-09-13 10:53:46 +02:00
private Integer deviceSerial;
private Integer indSnapshotOldest;
2016-09-08 18:47:31 +02:00
public ServiceLinkDeviceConnector() {
serviceLink = new ServiceLink(new NewSerialPort("COM1:"));
2016-09-09 00:57:02 +02:00
2016-09-08 18:47:31 +02:00
}
2016-09-09 00:57:02 +02:00
private void checkOpen(){
if (!serviceLink.isOpen()){
try {
serviceLink.open();
serviceLink.getSerialPort().setTimeOut(250);
} catch (ServiceLinkException e) {
throw new NoDeviceConnectionException();
}
}
}
2016-09-08 18:47:31 +02:00
@Override
public String toString() {
return String.format("Serial [%s]", this.serviceLink.getSerialPort().getPortName());
}
2016-09-13 10:53:46 +02:00
private Integer readDeviceSerial(){
checkOpen();
try {
Integer v = serviceLink.readInt(13, 0, 0x0001 );
return v;
} catch (IOException | ServiceLinkException e) {
throw new NoDeviceConnectionException();
}
}
2016-09-08 18:47:31 +02:00
@Override
2016-09-13 10:53:46 +02:00
public Integer getDeviceSerial() {
Integer serial = readDeviceSerial();
deviceSerial = serial;
return serial;
2016-09-08 18:47:31 +02:00
}
2016-09-09 00:57:02 +02:00
public void setDeviceSerial(int serial) {
2016-09-13 10:53:46 +02:00
checkOpen();
2016-09-08 18:47:31 +02:00
2016-09-13 10:53:46 +02:00
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();
}
2016-09-08 18:47:31 +02:00
}
@Override
public boolean showConnctionSetup() {
2016-09-09 00:57:02 +02:00
NewSerialPort newSerialPort = SerialPortChooser.execute(serviceLink.getSerialPort().getPortName());
if (newSerialPort != null){
serviceLink.close();
serviceLink.getSerialPort().setPortName(newSerialPort.getPortName());
return true;
}
2016-09-08 18:47:31 +02:00
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() {
2016-09-09 00:57:02 +02:00
int[] values = new int[32];
for (int n=0;n<32;n++){
2016-09-13 10:53:46 +02:00
values[n] = getCounter(n);
2016-09-09 00:57:02 +02:00
}
return values;
2016-09-08 18:47:31 +02:00
}
@Override
public void setCounters(int[] values) {
2016-09-13 10:53:46 +02:00
for (int n=0;n<32;n++){
setCounter(n, values[n]);
}
2016-09-08 18:47:31 +02:00
}
@Override
public int getCounter(int channel) {
2016-09-13 10:53:46 +02:00
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;
}
2016-09-08 18:47:31 +02:00
}
@Override
public void setCounter(int channel, int counter) {
2016-09-13 10:53:46 +02:00
checkOpen();
try {
serviceLink.writeInt(13, 0, 0x0600 + channel, counter);
} catch (IOException | ServiceLinkException e) {
e.printStackTrace();
}
2016-09-08 18:47:31 +02:00
}
@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() {
2016-09-13 10:53:46 +02:00
checkOpen();
try {
Integer v = serviceLink.readInt(13, 0, 0x0681 );
return v;
} catch (IOException | ServiceLinkException e) {
throw new NoDeviceConnectionException();
}
2016-09-08 18:47:31 +02:00
}
@Override
public int getOutputs() {
2016-09-13 10:53:46 +02:00
checkOpen();
try {
Integer v = serviceLink.readInt(13, 0, 0x0682 );
return v;
} catch (IOException | ServiceLinkException e) {
throw new NoDeviceConnectionException();
}
2016-09-08 18:47:31 +02:00
}
@Override
public void setOutputs(int outputs) {
2016-09-13 10:53:46 +02:00
try {
serviceLink.writeInt(13, 0, 0x0682, outputs);
} catch (IOException | ServiceLinkException e) {
log(e);
}
2016-09-08 18:47:31 +02:00
}
@Override
public int getPullups() {
2016-09-13 10:53:46 +02:00
checkOpen();
try {
Integer v = serviceLink.readInt(13, 0, 0x0683 );
return v;
} catch (IOException | ServiceLinkException e) {
throw new NoDeviceConnectionException();
}
2016-09-08 18:47:31 +02:00
}
@Override
public void setPullups(int pullups) {
2016-09-13 10:53:46 +02:00
try {
serviceLink.writeInt(13, 0, 0x0683, pullups);
} catch (IOException | ServiceLinkException e) {
log(e);
}
2016-09-08 18:47:31 +02:00
}
@Override
public int getInverts() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void setInverts(int inverts) {
// TODO Auto-generated method stub
}
2016-09-09 00:57:02 +02:00
@Override
public float[] getAnalogs() {
2016-09-13 10:53:46 +02:00
float[] values = new float[32];
for (int n=0;n<8;n++){
values[n] = getAnalog(n);
}
return values;
2016-09-09 00:57:02 +02:00
}
@Override
public float getAnalog(int channel) {
2016-09-13 10:53:46 +02:00
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);
}
}
2016-09-09 00:57:02 +02:00
return 0;
}
2016-09-13 10:53:46 +02:00
@Override
public SnapShot[] readSnapShots() {
Integer s = readDeviceSerial();
2016-09-13 12:07:01 +02:00
List<SnapShot> snapshots = new LinkedList<>();
2016-09-13 10:53:46 +02:00
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);
2016-09-13 12:07:01 +02:00
if (newest == null)
return null;
2016-09-13 10:53:46 +02:00
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);
2016-09-13 12:07:01 +02:00
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);
2016-09-13 10:53:46 +02:00
};
} catch (ServiceLinkRequestFailedException e){
log(e);
}
2016-09-13 12:07:01 +02:00
oldest++;
if (oldest >= sssize){
oldest = 0;
}
2016-09-13 10:53:46 +02:00
}
2016-09-13 12:07:01 +02:00
indSnapshotOldest = oldest;
2016-09-13 10:53:46 +02:00
} catch (IOException | ServiceLinkException e) {
log(e);
}
}
2016-09-13 12:07:01 +02:00
return snapshots.toArray(new SnapShot[0]);
2016-09-13 10:53:46 +02:00
}
@Override
public SnapShot readSnapShot() {
// TODO Auto-generated method stub
return null;
}
2016-09-08 18:47:31 +02:00
}