ServiceLink: Update, Cleanup

thobaben_serialize
Harald Wolff 2014-07-26 01:34:32 +02:00
parent 469861ed44
commit 44f8ca41de
5 changed files with 25 additions and 281 deletions

View File

@ -9,6 +9,7 @@ import java.util.Hashtable;
import java.util.List;
import org.hwo.ChkSum;
import org.hwo.Smoother;
import org.hwo.bitfields.BitField;
import org.hwo.interactiveobjects.InteractiveObject;
import org.hwo.io.SerialPort;
@ -172,275 +173,27 @@ public class ServiceLink {
}
}
public class ServiceNode
{
public class ServiceNodeRegister
{
@TableColumn(label="Register",readonly=true,width=60)
Integer register;
@TableColumn(label="Bezeichnung",readonly=true,width=150)
String label;
private Float floatValue;
private Integer intValue;
boolean floatType;
private BitField bitField;
public ServiceNodeRegister(int register,boolean floatType)
{
this.label = String.format("%d", register);
this.register = register;
this.floatType = floatType;
}
public ServiceNodeRegister(int register,String label,boolean floatType)
{
this.label = label;
this.register = register;
this.floatType = floatType;
}
public boolean isBitField()
{
return (bitField != null);
}
public boolean isFloat()
{
return floatType;
}
public boolean isInteger()
{
return (!isBitField() && !isFloat());
}
@TableColumn(label="Wert",width=500)
public String value()
{
if (bitField != null)
return bitField.toText(intValue);
if (intValue != null)
return String.format("%d", intValue);
if (floatValue != null)
return String.format("%f", floatValue);
return "N/A";
}
public void setValue(String value)
{
if (floatType)
floatValue = new Float(value);
else
intValue = new Integer(value);
}
public void read() throws ServiceLinkRequestFailedException
{
try {
if (floatType)
floatValue = ServiceNode.this.readFloat(register);
else
intValue = ServiceNode.this.readInt(register);
} catch (ServiceLinkRequestFailedException slex)
{
if (floatType)
{
floatValue = 0.0f;
intValue = null;
} else
{
floatValue = null;
intValue = 0;
}
throw slex;
} catch (Exception e) {
if (floatType)
{
floatValue = 0.0f;
intValue = null;
} else
{
floatValue = null;
intValue = 0;
}
};
}
public void write()
{
try
{
if (floatType)
ServiceNode.this.writeFloat(register, floatValue);
else
ServiceNode.this.writeInt(register, intValue);
} catch (Exception e)
{
e.printStackTrace();
}
}
public BitField getBitField() {
return bitField;
}
public void setBitField(BitField bitField) {
this.bitField = bitField;
}
public Integer getIntValue() {
return intValue;
}
public void setIntValue(Integer intValue) {
this.intValue = intValue;
}
public Float getFloatValue() {
return floatValue;
}
public void setFloatValue(Float floatValue) {
this.floatValue = floatValue;
}
}
private byte achse;
private byte knoten;
List<ServiceNodeRegister> registers;
public ServiceNode(byte achse,byte knoten)
{
this.achse = achse;
this.knoten = knoten;
this.registers = new ArrayList<ServiceLink.ServiceNode.ServiceNodeRegister>();
}
public ServiceNodeRegister createRegister(int register,String label,boolean floatType)
{
return new ServiceNodeRegister(register, label, floatType);
}
public List<ServiceNodeRegister> getRegisters()
{
return registers;
}
public void read() throws ServiceLinkRequestFailedException
{
for (ServiceNodeRegister r:registers)
{
try
{
r.read();
} catch (ServiceLinkRequestFailedException slex)
{
System.err.println("ServiceNode.read(): "+ slex);
}
}
}
public ServiceLink getServiceLink()
{
return ServiceLink.this;
}
public int readInt(int register) throws IOException, ServiceLinkException
{
for (int n=0;n<retries;n++)
{
try
{
return ServiceLink.this.readInt(achse, knoten, register);
} catch (ServiceLinkException e)
{
if (n == (retries -1))
throw e;
}
}
throw new ServiceLinkException();
}
public float readFloat(int register) throws IOException, ServiceLinkException
{
for (int n=0;n<retries;n++)
{
try
{
return ServiceLink.this.readFloat(achse, knoten, register);
} catch (ServiceLinkException e)
{
if (n == (retries -1))
throw e;
}
}
throw new ServiceLinkException();
}
public void writeInt(int register,int value) throws IOException, ServiceLinkException
{
for (int n=0;n<retries;n++)
{
try
{
ServiceLink.this.writeInt(achse, knoten, register, value);
return;
} catch (ServiceLinkException e)
{
if (n == (retries -1))
throw e;
}
}
throw new ServiceLinkException();
}
public void writeFloat(int register,float value) throws IOException, ServiceLinkException
{
for (int n=0;n<retries;n++)
{
try
{
ServiceLink.this.writeFloat(achse, knoten, register,value);
return;
} catch (ServiceLinkException e)
{
if (n == (retries -1))
throw e;
}
}
throw new ServiceLinkException();
}
public byte getAchse() {
return achse;
}
public void setAchse(byte achse) {
this.achse = achse;
}
public byte getKnoten() {
return knoten;
}
public void setKnoten(byte knoten) {
this.knoten = knoten;
}
}
private SerialPort serialPort;
int retries;
private ServiceRegisterCache
serviceRegisterCache;
Smoother requestTime;
public ServiceLink(SerialPort serialPort)
{
this.retries = 3;
this.serialPort = serialPort;
this.serialPort.setTimeout(250);
this.serviceRegisterCache = new ServiceRegisterCache(this);
this.requestTime = new Smoother();
this.requestTime.setTn(16);
}
public int getAverageRequestTime()
{
return requestTime.getWert();
}
public ServiceRegisterCache getServiceRegisterCache()
@ -466,12 +219,7 @@ public class ServiceLink {
if (serialPort.isOpen())
serialPort.close();
}
public ServiceNode getServiceNode(byte achse,byte knoten)
{
return new ServiceNode(achse, knoten);
}
public Integer readInt(byte achse,byte knoten,int register) throws IOException, ServiceLinkException, ServiceLinkRequestFailedException
{
throwNotOpen();
@ -512,6 +260,8 @@ public class ServiceLink {
}
private synchronized ServiceTelegram request(byte request,byte achse,byte knoten,int register,byte[] value) throws IOException, ServiceLinkException, ServiceLinkRequestFailedException
{
long rstart,rend;
ServiceTelegram telegram = new ServiceTelegram();
telegram.setRequest(request);
@ -524,7 +274,7 @@ public class ServiceLink {
telegram.send();
rstart = System.currentTimeMillis();
for (int n = 0; n<3; n++)
{
telegram.recv();
@ -535,6 +285,9 @@ public class ServiceLink {
)
break;
};
rend = System.currentTimeMillis();
requestTime.cycle((int)(rend - rstart));
if ((telegram.getRequest() & REQ_ACK)==0)
throw new ServiceLinkRequestFailedException(telegram);

View File

@ -8,14 +8,11 @@ import javax.swing.JPanel;
import javax.swing.JTextField;
import org.hwo.io.servicelink.ServiceLink;
import org.hwo.io.servicelink.ServiceLink.ServiceNode;
import org.hwo.io.servicelink.ServiceLink.ServiceNode.ServiceNodeRegister;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
public class FloatRegisterEditor extends JPanel implements ServiceRegisterControl {
private ServiceNodeRegister serviceNodeRegister;
private ServiceRegister serviceRegister;
private JTextField tfValue;
@ -37,14 +34,6 @@ public class FloatRegisterEditor extends JPanel implements ServiceRegisterContro
tfValue.setColumns(10);
}
public ServiceNodeRegister getServiceNodeRegister() {
return serviceNodeRegister;
}
public void setServiceNodeRegister(ServiceNodeRegister serviceNodeRegister) {
this.serviceNodeRegister = serviceNodeRegister;
}
@Override
public Component getComponent() {
return this;

View File

@ -9,8 +9,6 @@ import javax.swing.JPanel;
import javax.swing.JTextField;
import org.hwo.io.servicelink.ServiceLink;
import org.hwo.io.servicelink.ServiceLink.ServiceNode;
import org.hwo.io.servicelink.ServiceLink.ServiceNode.ServiceNodeRegister;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;

View File

@ -5,8 +5,6 @@ import java.awt.Component;
import javax.tools.JavaCompiler;
import org.hwo.io.servicelink.ServiceLink;
import org.hwo.io.servicelink.ServiceLink.ServiceNode;
import org.hwo.io.servicelink.ServiceLink.ServiceNode.ServiceNodeRegister;
public interface ServiceRegisterControl {

View File

@ -27,6 +27,7 @@ import javax.swing.BoxLayout;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import javax.swing.JScrollPane;
public class ServiceRegisterEditorDialog extends JDialog implements IInteractiveObjectEditor{
@ -40,6 +41,7 @@ public class ServiceRegisterEditorDialog extends JDialog implements IInteractive
private JTextField tfRegname;
private KeyStrokeHelper keyStrokeHelper;
private JScrollPane scrollPane;
/**
* Create the dialog.
@ -165,6 +167,10 @@ public class ServiceRegisterEditorDialog extends JDialog implements IInteractive
gbc_panelEditorControl.gridy = 1;
contentPanel.add(panelEditorControl, gbc_panelEditorControl);
panelEditorControl.setLayout(new BoxLayout(panelEditorControl, BoxLayout.X_AXIS));
{
scrollPane = new JScrollPane();
panelEditorControl.add(scrollPane);
}
}
{
JPanel buttonPane = new JPanel();
@ -227,14 +233,14 @@ public class ServiceRegisterEditorDialog extends JDialog implements IInteractive
private void initializeView()
{
panelEditorControl.removeAll();
scrollPane.setViewportView(null);
if (serviceRegister != null)
{
tfAx.setText(serviceRegister.getAx().toString());
tfNode.setText(serviceRegister.getNode().toString());
tfRegister.setText(serviceRegister.getRegister().toString());
tfRegname.setText(serviceRegister.getRegisterName());
panelEditorControl.add( serviceRegister.getEditorComponent());
scrollPane.setViewportView(serviceRegister.getEditorComponent());
serviceRegister.updateEditorValue();
}
doLayout();