Compare commits
2 Commits
Author | SHA1 | Date |
---|---|---|
Harald Wolff | 817abe6146 | |
Harald Wolff | 70e68bdda8 |
|
@ -2,7 +2,7 @@
|
|||
<classpath>
|
||||
<classpathentry kind="src" path="src"/>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
|
||||
<classpathentry combineaccessrules="false" kind="src" path="/org.hwo"/>
|
||||
<classpathentry combineaccessrules="false" kind="src" path="/org.hwo.ui"/>
|
||||
<classpathentry combineaccessrules="false" kind="src" path="/bootstrapper"/>
|
||||
<classpathentry combineaccessrules="false" kind="src" path="/org.hwo.io"/>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
</classpath>
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
Manifest-Version: 1.0
|
||||
Sealed: true
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
||||
<jardesc>
|
||||
<jar path="bootstrapper/lib/org.hwo.servicelink.jar"/>
|
||||
<options buildIfNeeded="true" compress="true" descriptionLocation="/org.hwo.servicelink/org.hwo.servicelink.jardesc" exportErrors="false" exportWarnings="true" includeDirectoryEntries="false" overwrite="true" saveDescription="true" storeRefactorings="false" useSourceFolders="false"/>
|
||||
<storedRefactorings deprecationInfo="true" structuralOnly="false"/>
|
||||
<selectedProjects/>
|
||||
<manifest generateManifest="false" manifestLocation="/org.hwo.servicelink/MANIFEST.MF" manifestVersion="1.0" reuseManifest="true" saveManifest="true" usesManifest="true">
|
||||
<sealing sealJar="true">
|
||||
<packagesToSeal/>
|
||||
<packagesToUnSeal/>
|
||||
</sealing>
|
||||
</manifest>
|
||||
<selectedElements exportClassFiles="true" exportJavaFiles="false" exportOutputFolder="false">
|
||||
<javaElement handleIdentifier="=org.hwo.servicelink/src"/>
|
||||
</selectedElements>
|
||||
</jardesc>
|
|
@ -1,99 +0,0 @@
|
|||
package org.hwo.servicelink;
|
||||
|
||||
import java.util.Hashtable;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
public class AsynchronServiceLinkProvider {
|
||||
|
||||
ServiceRegisterCache cache;
|
||||
Hashtable<Integer, List<ServiceRegisterListener>> serviceRegisterListeners;
|
||||
|
||||
public AsynchronServiceLinkProvider(ServiceRegisterCache cache){
|
||||
this.cache = cache;
|
||||
this.serviceRegisterListeners = new Hashtable<Integer, List<ServiceRegisterListener>>();
|
||||
}
|
||||
|
||||
|
||||
private List<ServiceRegisterListener> getListeners(Integer hash){
|
||||
if (!serviceRegisterListeners.containsKey(hash))
|
||||
serviceRegisterListeners.put(hash, new LinkedList<ServiceRegisterListener>());
|
||||
|
||||
return serviceRegisterListeners.get(hash);
|
||||
}
|
||||
|
||||
public void addServiceRegisterListener(int ax,int node,int registerno,boolean asFloat,ServiceRegisterListener listener){
|
||||
getListeners(cache.calcHash(ax, node, registerno,asFloat)).add(listener);
|
||||
}
|
||||
public void removeServiceRegisterListener(int ax,int node,int registerno,boolean asFloat,ServiceRegisterListener listener){
|
||||
getListeners(cache.calcHash(ax, node, registerno,asFloat)).remove(listener);
|
||||
}
|
||||
|
||||
public Hashtable<Integer, List<ServiceRegisterListener>> getServiceRegisterListeners() {
|
||||
return serviceRegisterListeners;
|
||||
}
|
||||
public void setServiceRegisterListeners(
|
||||
Hashtable<Integer, List<ServiceRegisterListener>> serviceRegisterListeners) {
|
||||
this.serviceRegisterListeners = serviceRegisterListeners;
|
||||
}
|
||||
|
||||
public void telegramReceived(ServiceLinkTelegram telegram){
|
||||
|
||||
if ((telegram.getOpcode() & ServiceLinkTelegram.REQ_READ) != 0){
|
||||
int hash = cache.calcHash(telegram.getAddress(), ((telegram.getOpcode() & ServiceLinkTelegram.REQ_FLOAT) == ServiceLinkTelegram.REQ_FLOAT));
|
||||
|
||||
cache.updateValue(hash, telegram.getValue());
|
||||
|
||||
//fireUpdates(hash);
|
||||
}
|
||||
}
|
||||
|
||||
public void fireUpdates(int hash){
|
||||
ServiceLinkAddress sla = cache.getAddress(hash);
|
||||
Object v = cache.getCachedValue(hash);
|
||||
|
||||
if (Integer.class.isInstance(v)){
|
||||
Integer i = (Integer)v;
|
||||
|
||||
for (ServiceRegisterListener listener: getListeners(hash)){
|
||||
listener.ServiceRegisterValueUpdated(sla.getAx(),sla.getNode(),sla.getRegister(),i);
|
||||
}
|
||||
}
|
||||
if (Float.class.isInstance(v)){
|
||||
Float f = (Float)v;
|
||||
|
||||
for (ServiceRegisterListener listener: getListeners(hash)){
|
||||
listener.ServiceRegisterValueUpdated(sla.getAx(),sla.getNode(),sla.getRegister(),f);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void update(){
|
||||
|
||||
for (Integer hash: serviceRegisterListeners.keySet()){
|
||||
if (!getListeners(hash).isEmpty()){
|
||||
|
||||
cache.updateValue(hash);
|
||||
fireUpdates(hash);
|
||||
|
||||
/*
|
||||
int opcode = ServiceLinkTelegram.REQ_READ;
|
||||
if ((hash & 0x40000000)!=0){
|
||||
opcode |= ServiceLinkTelegram.REQ_FLOAT;
|
||||
}
|
||||
|
||||
ServiceLinkTelegram telegram = cache.getServiceLink().createTelegram(cache.axFromHash(hash), cache.nodeFromHash(hash), cache.regFromHash(hash), opcode, null);
|
||||
|
||||
log(DEBUGDETAIL,"non-pending request: %s", telegram.getAddress() );
|
||||
|
||||
cache.getServiceLink().queueRequestNonPending(telegram);
|
||||
|
||||
cache.getServiceLink().queueRequest(telegram);
|
||||
*/
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -1,509 +0,0 @@
|
|||
package org.hwo.servicelink;
|
||||
|
||||
import static org.hwo.logging.Logging.log;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Hashtable;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import org.hwo.ByteArrayHexlifier;
|
||||
import org.hwo.Smoother;
|
||||
import org.hwo.io.NewSerialPort.NewSerialPort;
|
||||
import org.hwo.io.NewSerialPort.NewSerialPortListener;
|
||||
import org.hwo.servicelink.exceptions.ServiceLinkException;
|
||||
import org.hwo.servicelink.exceptions.ServiceLinkRequestFailedException;
|
||||
|
||||
import static org.hwo.servicelink.ServiceLinkTelegram.*;
|
||||
import static org.hwo.logging.LogLevel.*;
|
||||
|
||||
/* ServiceLink
|
||||
*
|
||||
* Kommunikation via USB mit RegBus f<EFBFBD>higem System
|
||||
*
|
||||
* Übertragungssicherheit wird durch USB sichergestellt
|
||||
*
|
||||
* Übertragen werden Telegramme mit folgendem Aufbau:
|
||||
*
|
||||
* Offset Type Inhalt
|
||||
* 0 Byte RequestTyp ( 0 = NoOP, 1 = Wert lesen, 2 = Wert schreiben, 3 = Ereignis senden )
|
||||
* 1 Byte Achse
|
||||
* 2 Byte Knoten
|
||||
* 3 Short Register Nummer
|
||||
* 5 Int32/Float Wert (nur bei schreiben oder lesen antwort)
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
public class ServiceLink implements NewSerialPortListener {
|
||||
private NewSerialPort
|
||||
serialPort;
|
||||
|
||||
int retries;
|
||||
|
||||
private ServiceRegisterCache
|
||||
serviceRegisterCache;
|
||||
private AsynchronServiceLinkProvider
|
||||
asynchronServiceLinkProvider;
|
||||
|
||||
Smoother requestTime;
|
||||
int requestTimeOut;
|
||||
|
||||
boolean useV2 = true;
|
||||
boolean forceSynchronousRequests;
|
||||
|
||||
LinkedList<ServiceLinkTelegram> txQueue;
|
||||
Hashtable<ServiceLinkAddress, List<ServiceLinkTelegram>>
|
||||
pendingRequests;
|
||||
int pendingRequestsCount;
|
||||
|
||||
RxWorker rxWorker;
|
||||
TxWorker txWorker;
|
||||
|
||||
boolean closing;
|
||||
|
||||
private List<ServiceLinkListener> serviceLinkListeners;
|
||||
|
||||
public ServiceLink(NewSerialPort serialPort)
|
||||
{
|
||||
this.requestTimeOut = 100;
|
||||
|
||||
this.serviceLinkListeners = new LinkedList<ServiceLinkListener>();
|
||||
this.retries = 3;
|
||||
this.serialPort = serialPort;
|
||||
this.serialPort.setTimeOut(25);
|
||||
this.serialPort.addNewSerialPortListener(this);
|
||||
this.serviceRegisterCache = new ServiceRegisterCache(this);
|
||||
this.asynchronServiceLinkProvider = new AsynchronServiceLinkProvider(serviceRegisterCache);
|
||||
this.requestTime = new Smoother();
|
||||
this.requestTime.setTn(16);
|
||||
|
||||
this.txWorker = null;
|
||||
this.rxWorker = null;
|
||||
|
||||
this.txQueue = new LinkedList<>();
|
||||
this.pendingRequests = new Hashtable<>();
|
||||
|
||||
}
|
||||
|
||||
/** Handling der Seriellen Verbindung **/
|
||||
|
||||
private void _open(){
|
||||
this.txWorker = new TxWorker();
|
||||
this.rxWorker = new RxWorker();
|
||||
|
||||
this.serialPort.setTimeOut(25);
|
||||
this.serialPort.open();
|
||||
|
||||
this.txWorker.start();
|
||||
this.rxWorker.start();
|
||||
}
|
||||
private void _close(){
|
||||
log(DEBUG,"servicelink closing connection...");
|
||||
log(DEBUG,"txWorker...");
|
||||
this.txWorker.exit();
|
||||
log(DEBUG,"rxWorker...");
|
||||
this.rxWorker.exit();
|
||||
|
||||
this.txWorker = null;
|
||||
this.rxWorker = null;
|
||||
|
||||
log(DEBUG,"serialPort...");
|
||||
this.serialPort.close();
|
||||
}
|
||||
|
||||
public void open() throws ServiceLinkException
|
||||
{
|
||||
if (!closing){
|
||||
close();
|
||||
|
||||
if (serialPort != null){
|
||||
_open();
|
||||
} else {
|
||||
throwNotOpen();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void close()
|
||||
{
|
||||
if (!closing){
|
||||
if (txWorker != null){
|
||||
_close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void throwNotOpen() throws ServiceLinkException
|
||||
{
|
||||
if (!isOpen())
|
||||
throw new ServiceLinkException("serial port not opened!");
|
||||
}
|
||||
|
||||
public boolean isOpen()
|
||||
{
|
||||
return (this.serialPort != null) && (this.serialPort.isOpen()) && (this.txWorker != null);
|
||||
}
|
||||
|
||||
/** -- ENDE: Handling der seriellen Verbindung -- **/
|
||||
|
||||
/** Handling der Queues **/
|
||||
|
||||
public boolean isUseV2() {
|
||||
return useV2;
|
||||
}
|
||||
public void setUseV2(boolean useV2) {
|
||||
this.useV2 = useV2;
|
||||
}
|
||||
|
||||
public boolean isForceSynchronousRequests() {
|
||||
return forceSynchronousRequests;
|
||||
}
|
||||
public void setForceSynchronousRequests(boolean forceSynchronousRequests) {
|
||||
this.forceSynchronousRequests = forceSynchronousRequests;
|
||||
}
|
||||
|
||||
private void addPendingRequest(ServiceLinkTelegram request){
|
||||
synchronized (pendingRequests) {
|
||||
if (!pendingRequests.containsKey(request.getAddress())){
|
||||
pendingRequests.put(request.getAddress(),new LinkedList<ServiceLinkTelegram>());
|
||||
}
|
||||
List<ServiceLinkTelegram> ll = pendingRequests.get(request.getAddress());
|
||||
ll.add(request);
|
||||
pendingRequestsCount++;
|
||||
}
|
||||
}
|
||||
private void removePendingRequest(ServiceLinkTelegram request){
|
||||
synchronized (pendingRequests) {
|
||||
if (pendingRequests.containsKey(request.getAddress())){
|
||||
pendingRequests.get(request.getAddress()).remove(request);
|
||||
pendingRequestsCount--;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void appendRequest(ServiceLinkTelegram request){
|
||||
|
||||
addPendingRequest(request);
|
||||
|
||||
synchronized (txQueue) {
|
||||
txQueue.add(request);
|
||||
txQueue.notify();
|
||||
}
|
||||
}
|
||||
public void removeRequest(ServiceLinkTelegram request){
|
||||
synchronized (txQueue) {
|
||||
txQueue.remove(request);
|
||||
}
|
||||
removePendingRequest(request);
|
||||
}
|
||||
|
||||
public void queueRequestNonPending(ServiceLinkTelegram request){
|
||||
synchronized (txQueue) {
|
||||
txQueue.add(request);
|
||||
txQueue.notify();
|
||||
}
|
||||
}
|
||||
|
||||
public void queueRequest(ServiceLinkTelegram request){
|
||||
long ts_start = System.currentTimeMillis();
|
||||
|
||||
log(DEBUG,"QUEUE: %s",request);
|
||||
|
||||
if (forceSynchronousRequests){
|
||||
log(DEBUGDETAIL,"SYNCH.");
|
||||
while (true){
|
||||
synchronized (pendingRequests) {
|
||||
if (pendingRequestsCount == 0){
|
||||
appendRequest(request);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
appendRequest(request);
|
||||
}
|
||||
|
||||
log(DEBUGDETAIL,"WAIT: %s",request);
|
||||
|
||||
synchronized (request) {
|
||||
try {
|
||||
request.wait(requestTimeOut);
|
||||
} catch (InterruptedException e) {
|
||||
log(e);
|
||||
}
|
||||
}
|
||||
|
||||
removeRequest(request);
|
||||
|
||||
requestTime.cycle( (int)(System.currentTimeMillis() - ts_start) );
|
||||
|
||||
log(DEBUG,"DONE: %s",request);
|
||||
|
||||
}
|
||||
|
||||
public void received(ServiceLinkTelegram rx){
|
||||
|
||||
synchronized (pendingRequests) {
|
||||
if (pendingRequests.containsKey(rx.getAddress())){
|
||||
for (ServiceLinkTelegram pending: pendingRequests.get(rx.getAddress())){
|
||||
|
||||
if ((pending.getOpcode() & (REQ_INT | REQ_FLOAT))==(rx.getOpcode() & (REQ_INT | REQ_FLOAT))){
|
||||
synchronized (pending) {
|
||||
pending.setValue(rx.getValue());
|
||||
pending.notifyAll();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
asynchronServiceLinkProvider.telegramReceived(rx);
|
||||
}
|
||||
|
||||
/** ENDE: Handling der Queues **/
|
||||
|
||||
public int getRequestTimeOut() {
|
||||
return requestTimeOut;
|
||||
}
|
||||
public void setRequestTimeOut(int requestTimeOut) {
|
||||
this.requestTimeOut = requestTimeOut;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void addServiceLinkListener(ServiceLinkListener listener){
|
||||
serviceLinkListeners.add(listener);
|
||||
}
|
||||
public void removeServiceLinkListener(ServiceLinkListener listener){
|
||||
serviceLinkListeners.remove(listener);
|
||||
}
|
||||
private void fireConnectionStateChanged(Boolean connected){
|
||||
for (ServiceLinkListener l: serviceLinkListeners)
|
||||
l.connectionStateChanged(connected);
|
||||
}
|
||||
|
||||
|
||||
public int getAverageRequestTime()
|
||||
{
|
||||
return requestTime.getWert();
|
||||
}
|
||||
|
||||
public ServiceRegisterCache getServiceRegisterCache()
|
||||
{
|
||||
return this.serviceRegisterCache;
|
||||
}
|
||||
|
||||
public AsynchronServiceLinkProvider getAsynchronServiceLinkProvider() {
|
||||
return asynchronServiceLinkProvider;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
protected void finalize() throws Throwable {
|
||||
if (serialPort.isOpen())
|
||||
serialPort.close();
|
||||
}
|
||||
|
||||
public ServiceLinkTelegram createTelegram(int achse,int knoten,int register,int opcode,Object value){
|
||||
|
||||
ServiceLinkTelegram request = useV2 ?
|
||||
new ServiceLinkV2Telegram(opcode, achse, knoten, register, value) :
|
||||
new ServiceLinkV1Telegram(opcode, achse, knoten, register, value);
|
||||
|
||||
return request;
|
||||
}
|
||||
|
||||
public Integer readInt(int achse,int knoten,int register) throws IOException, ServiceLinkException, ServiceLinkRequestFailedException
|
||||
{
|
||||
ServiceLinkTelegram request = useV2 ?
|
||||
new ServiceLinkV2Telegram(REQ_INT | REQ_READ, achse, knoten, register, null) :
|
||||
new ServiceLinkV1Telegram(REQ_INT | REQ_READ, achse, knoten, register, null);
|
||||
queueRequest(request);
|
||||
return (Integer)request.getValue();
|
||||
}
|
||||
public Float readFloat(int achse,int knoten,int register) throws IOException, ServiceLinkException, ServiceLinkRequestFailedException
|
||||
{
|
||||
ServiceLinkTelegram request = useV2 ?
|
||||
new ServiceLinkV2Telegram(REQ_FLOAT | REQ_READ, achse, knoten, register, null) :
|
||||
new ServiceLinkV1Telegram(REQ_FLOAT | REQ_READ, achse, knoten, register, null);
|
||||
queueRequest(request);
|
||||
return (Float)request.getValue();
|
||||
}
|
||||
|
||||
public void writeInt(int achse,int knoten,int register,int value) throws IOException, ServiceLinkException, ServiceLinkRequestFailedException
|
||||
{
|
||||
ServiceLinkTelegram request = useV2 ?
|
||||
new ServiceLinkV2Telegram(REQ_INT | REQ_WRITE, achse, knoten, register, value) :
|
||||
new ServiceLinkV1Telegram(REQ_INT | REQ_WRITE, achse, knoten, register, value);
|
||||
queueRequest(request);
|
||||
}
|
||||
public void writeFloat(int achse,int knoten,int register,float value) throws IOException, ServiceLinkException, ServiceLinkRequestFailedException{
|
||||
ServiceLinkTelegram request = useV2 ?
|
||||
new ServiceLinkV2Telegram(REQ_FLOAT | REQ_WRITE, achse, knoten, register, value) :
|
||||
new ServiceLinkV1Telegram(REQ_FLOAT | REQ_WRITE, achse, knoten, register, value);
|
||||
queueRequest(request);
|
||||
}
|
||||
|
||||
public NewSerialPort getSerialPort() {
|
||||
return serialPort;
|
||||
}
|
||||
|
||||
public void setSerialPort(NewSerialPort serialPort) {
|
||||
if (isOpen()){
|
||||
this.close();
|
||||
this.serialPort.removeNewSerialPortListener(this);
|
||||
}
|
||||
|
||||
this.serialPort = serialPort;
|
||||
this.serialPort.addNewSerialPortListener(this);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public void connectionStateChanged(NewSerialPort port, boolean connected) {
|
||||
fireConnectionStateChanged(connected);
|
||||
}
|
||||
|
||||
|
||||
private class RxWorker extends Thread {
|
||||
private boolean exit;
|
||||
|
||||
public synchronized void exit(){
|
||||
if (!isAlive())
|
||||
return;
|
||||
|
||||
if (exit)
|
||||
return;
|
||||
|
||||
exit = true;
|
||||
this.notifyAll();
|
||||
try {
|
||||
this.wait(getSerialPort().getTimeOut());
|
||||
|
||||
if (this.isAlive()){
|
||||
this.stop();
|
||||
}
|
||||
|
||||
} catch (InterruptedException e) {
|
||||
log(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
ServiceLinkTelegram rx;
|
||||
|
||||
while (true){
|
||||
synchronized (this) {
|
||||
if (exit){
|
||||
this.notifyAll();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (!isOpen())
|
||||
break;
|
||||
|
||||
try {
|
||||
rx = useV2 ?
|
||||
new ServiceLinkV2Telegram() :
|
||||
new ServiceLinkV1Telegram() ;
|
||||
|
||||
if (rx.read(getSerialPort().getInputStream())){
|
||||
received(rx);
|
||||
};
|
||||
|
||||
} catch (Exception io){
|
||||
log(io);
|
||||
exit = true;
|
||||
ServiceLink.this.close();
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private class TxWorker extends Thread{
|
||||
private boolean exit;
|
||||
|
||||
public TxWorker(){
|
||||
exit = false;
|
||||
}
|
||||
|
||||
public synchronized void exit(){
|
||||
if (!isAlive())
|
||||
return;
|
||||
|
||||
if (exit)
|
||||
return;
|
||||
|
||||
exit = true;
|
||||
try {
|
||||
this.notifyAll();
|
||||
this.wait(250);
|
||||
|
||||
if (this.isAlive())
|
||||
this.stop();
|
||||
|
||||
} catch (InterruptedException e) {
|
||||
log(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
ServiceLinkTelegram tx;
|
||||
|
||||
while (true){
|
||||
synchronized (txQueue) {
|
||||
try {
|
||||
txQueue.wait(100);
|
||||
} catch (InterruptedException e) {
|
||||
log(e);
|
||||
}
|
||||
|
||||
synchronized (this) {
|
||||
if (exit){
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!isOpen())
|
||||
break;
|
||||
|
||||
synchronized (txQueue) {
|
||||
tx = txQueue.pollFirst();
|
||||
}
|
||||
|
||||
while ((tx != null)&&(!exit)&&(ServiceLink.this.isOpen())){
|
||||
byte[] txbytes = tx.toByteArray();
|
||||
try {
|
||||
log(DEBUGDETAIL,"TX BYTES: %s",ByteArrayHexlifier.byteArrayToString(txbytes));
|
||||
serialPort.getOutputStream().write(txbytes);
|
||||
} catch (IOException io) {
|
||||
log(io);
|
||||
exit = true;
|
||||
ServiceLink.this.close();
|
||||
}
|
||||
|
||||
synchronized (txQueue) {
|
||||
tx = txQueue.pollFirst();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
synchronized (this) {
|
||||
this.notifyAll();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -1,57 +0,0 @@
|
|||
package org.hwo.servicelink;
|
||||
|
||||
public class ServiceLinkAddress {
|
||||
|
||||
private int ax,
|
||||
node;
|
||||
private int register;
|
||||
|
||||
public ServiceLinkAddress(){
|
||||
}
|
||||
public ServiceLinkAddress(int ax,int node,int register){
|
||||
this.ax = ax;
|
||||
this.node = node;
|
||||
this.register = register;
|
||||
|
||||
if (this.register < 0){
|
||||
this.register += 65536;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return ((ax & 0x0F) << 20) | ((node & 0x0F) << 16) | ((register & 0xFFFF) << 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
return (ax == ((ServiceLinkAddress)obj).ax) &&
|
||||
(node == ((ServiceLinkAddress)obj).node) &&
|
||||
(register == ((ServiceLinkAddress)obj).register);
|
||||
}
|
||||
|
||||
public int getAx() {
|
||||
return ax;
|
||||
}
|
||||
public void setAx(int ax) {
|
||||
this.ax = ax;
|
||||
}
|
||||
public int getNode() {
|
||||
return node;
|
||||
}
|
||||
public void setNode(int node) {
|
||||
this.node = node;
|
||||
}
|
||||
public int getRegister() {
|
||||
return register;
|
||||
}
|
||||
public void setRegister(int register) {
|
||||
this.register = register;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("SLA: [%d:%d:%d]",ax,node,register);
|
||||
}
|
||||
|
||||
}
|
|
@ -1,7 +0,0 @@
|
|||
package org.hwo.servicelink;
|
||||
|
||||
public interface ServiceLinkListener {
|
||||
|
||||
void connectionStateChanged(Boolean connected);
|
||||
|
||||
}
|
|
@ -1,103 +0,0 @@
|
|||
package org.hwo.servicelink;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import java.io.InputStream;
|
||||
|
||||
public abstract class ServiceLinkTelegram {
|
||||
|
||||
static int REQ_READ = 0x01;
|
||||
static int REQ_WRITE = 0x02;
|
||||
static int REQ_EVENT = 0x04;
|
||||
static int REQ_FAIL = 0x08;
|
||||
|
||||
static int REQ_INT = 0x10;
|
||||
static int REQ_FLOAT = 0x20;
|
||||
static int REQ_ACK = 0x40;
|
||||
|
||||
static int SL_MAGIC = 0x66;
|
||||
|
||||
int opcode;
|
||||
ServiceLinkAddress
|
||||
address;
|
||||
Object
|
||||
value;
|
||||
|
||||
long timestamp;
|
||||
|
||||
protected ServiceLinkTelegram(){
|
||||
timestamp = System.currentTimeMillis();
|
||||
}
|
||||
|
||||
protected ServiceLinkTelegram(int operation,int achse,int knoten,int register,Object value){
|
||||
this.opcode = operation;
|
||||
this.address = new ServiceLinkAddress(achse, knoten, register);
|
||||
this.value = value;
|
||||
|
||||
this.checkPlausible();
|
||||
|
||||
timestamp = System.currentTimeMillis();
|
||||
}
|
||||
|
||||
private void checkPlausible(){
|
||||
if ((opcode & REQ_WRITE)!=0){
|
||||
if (value == null){
|
||||
throw new NullPointerException("Need a value to create write-request");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected byte[] valueAsByteArray(){
|
||||
byte[] v = new byte[4];
|
||||
|
||||
if (value == null)
|
||||
return new byte[]{0,0,0,0};
|
||||
if (Integer.class.isInstance(value)){
|
||||
ByteBuffer.wrap(v).order(ByteOrder.LITTLE_ENDIAN).asIntBuffer().put((Integer)value);
|
||||
} else {
|
||||
ByteBuffer.wrap(v).order(ByteOrder.LITTLE_ENDIAN).asIntBuffer().put(Float.floatToIntBits((Float)value));
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
public abstract byte[] toByteArray();
|
||||
public abstract boolean read(InputStream in);
|
||||
|
||||
public ServiceLinkAddress getAddress() {
|
||||
return address;
|
||||
}
|
||||
public void setAddress(ServiceLinkAddress address) {
|
||||
this.address = address;
|
||||
}
|
||||
|
||||
public int getOpcode() {
|
||||
return opcode;
|
||||
}
|
||||
public void setOpcode(int opcode) {
|
||||
this.opcode = opcode;
|
||||
}
|
||||
|
||||
public Object getValue() {
|
||||
return value;
|
||||
}
|
||||
public void setValue(Object value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public boolean isNull(){
|
||||
return (getAddress().getAx()==0) && (getAddress().getNode()==0) && (getAddress().getRegister()==0) && (opcode == 0) && value.equals(0);
|
||||
}
|
||||
|
||||
public long getTimestamp() {
|
||||
return timestamp;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("%s %s %s",
|
||||
getAddress().toString(),
|
||||
((opcode & REQ_WRITE)==REQ_WRITE) ? "=" : "?",
|
||||
value);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,118 +0,0 @@
|
|||
package org.hwo.servicelink;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import org.hwo.ByteArrayHexlifier;
|
||||
import org.hwo.ChkSum;
|
||||
import org.hwo.servicelink.exceptions.CRCFailedException;
|
||||
|
||||
import static org.hwo.logging.Logging.*;
|
||||
import static org.hwo.logging.LogLevel.*;
|
||||
|
||||
public class ServiceLinkV1Telegram extends ServiceLinkTelegram {
|
||||
|
||||
byte buffer[];
|
||||
|
||||
public ServiceLinkV1Telegram(int operation,int achse,int knoten,int register,Object value){
|
||||
super(operation,achse,knoten,register,value);
|
||||
}
|
||||
|
||||
public ServiceLinkV1Telegram(){
|
||||
}
|
||||
|
||||
public boolean read(InputStream in){
|
||||
int l,
|
||||
sync,
|
||||
opcode;
|
||||
|
||||
byte[] rxbuffer;
|
||||
|
||||
try {
|
||||
int ch = in.read();
|
||||
|
||||
while ((ch != SL_MAGIC)) {
|
||||
ch = in.read();
|
||||
if (ch != SL_MAGIC){
|
||||
if (ch == -1){
|
||||
return false;
|
||||
}
|
||||
log(DEBUGDETAIL,"readTelegramBytes(): junk byte in the line: %d [ 0x%02x ]",ch,ch);
|
||||
}
|
||||
}
|
||||
sync = (byte)ch;
|
||||
opcode = (byte)in.read();
|
||||
|
||||
l = ((opcode & (REQ_READ | REQ_ACK)) == (REQ_READ | REQ_ACK)) ? 10 : 6;
|
||||
|
||||
rxbuffer = new byte[ l + 2 ];
|
||||
|
||||
rxbuffer[0] = (byte)sync;
|
||||
rxbuffer[1] = (byte)opcode;
|
||||
|
||||
l = in.read(rxbuffer,2,l);
|
||||
log(DEBUGDETAIL,"RX BYTES: %2d: %s",rxbuffer.length,ByteArrayHexlifier.byteArrayToString(rxbuffer));
|
||||
|
||||
try {
|
||||
fromBytes(rxbuffer);
|
||||
return true;
|
||||
} catch (CRCFailedException e){
|
||||
log(e);
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
log(e);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
protected void fromBytes(byte[] buffer) throws CRCFailedException {
|
||||
if ((buffer.length != 12) && (buffer.length != 8) ){
|
||||
throw new ArrayIndexOutOfBoundsException(String.format("ServiceLinkTelegram needs to be constructed from 8 or 12 bytes! But got %d bytes.",buffer.length));
|
||||
}
|
||||
|
||||
ByteBuffer bb = ByteBuffer.wrap(buffer).order(ByteOrder.LITTLE_ENDIAN);
|
||||
short chk = ChkSum.chksum(buffer, 1, buffer.length - 3);
|
||||
|
||||
|
||||
if (bb.getShort(buffer.length - 2) != chk) {
|
||||
log(DEBUG,"CRC failed: 0x%04x != 0x%04x",bb.getShort(buffer.length - 2),chk);
|
||||
throw new CRCFailedException(buffer, "ServiceLinkTelegram bytes with wrong crc");
|
||||
}
|
||||
|
||||
this.opcode = bb.get(1);
|
||||
this.address = new ServiceLinkAddress(bb.get(2),bb.get(3),bb.getShort(4));
|
||||
if (buffer.length == 12){
|
||||
int v = bb.getInt(6);
|
||||
if ((opcode & REQ_FLOAT)==REQ_FLOAT){
|
||||
this.value = Float.intBitsToFloat(v);
|
||||
} else if ((opcode & REQ_INT)==REQ_INT){
|
||||
this.value = v;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public byte[] toByteArray(){
|
||||
byte[] txbuffer = new byte[ ((opcode & REQ_WRITE) == REQ_WRITE) ? 12 : 8 ];
|
||||
ByteBuffer bb = ByteBuffer.wrap(txbuffer).order(ByteOrder.LITTLE_ENDIAN);
|
||||
|
||||
bb.put( (byte)SL_MAGIC );
|
||||
bb.put( (byte)opcode );
|
||||
bb.put( (byte)address.getAx());
|
||||
bb.put( (byte)address.getNode());
|
||||
bb.putShort((short)(address.getRegister()));
|
||||
|
||||
|
||||
if ((opcode & REQ_WRITE) == REQ_WRITE){
|
||||
bb.put(valueAsByteArray());
|
||||
}
|
||||
|
||||
short chk = ChkSum.chksum(txbuffer, 1, bb.position()-1);
|
||||
bb.putShort(chk);
|
||||
|
||||
return txbuffer;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,102 +0,0 @@
|
|||
package org.hwo.servicelink;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import org.hwo.ByteArrayHexlifier;
|
||||
import static org.hwo.logging.Logging.*;
|
||||
import static org.hwo.logging.LogLevel.*;
|
||||
|
||||
public class ServiceLinkV2Telegram extends ServiceLinkTelegram {
|
||||
static int REQ_READ = 0x01;
|
||||
static int REQ_WRITE = 0x02;
|
||||
static int REQ_EVENT = 0x04;
|
||||
static int REQ_FAIL = 0x08;
|
||||
|
||||
static int REQ_INT = 0x10;
|
||||
static int REQ_FLOAT = 0x20;
|
||||
static int REQ_ACK = 0x40;
|
||||
|
||||
|
||||
byte[] buffer = new byte[8];
|
||||
int bufferUsed = 0;
|
||||
|
||||
public ServiceLinkV2Telegram(int operation,int achse,int knoten,int register,Object value){
|
||||
super(operation,achse,knoten,register,value);
|
||||
}
|
||||
|
||||
public ServiceLinkV2Telegram(){
|
||||
}
|
||||
|
||||
public boolean read(InputStream in){
|
||||
try {
|
||||
int n = in.read(buffer, bufferUsed, 8 - bufferUsed);
|
||||
if (n > 0){
|
||||
bufferUsed += n;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
log(e);
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
if (bufferUsed != 8){
|
||||
//log(DEBUG,"ServiceLinkV2Telegram: bufferUsed == %d !!",bufferUsed);
|
||||
return false;
|
||||
}
|
||||
log(DEBUGDETAIL,"RX BYTES: %s",ByteArrayHexlifier.byteArrayToString(buffer));
|
||||
|
||||
setAddress(new ServiceLinkAddress((buffer[1]>>4) & 0x0F, buffer[1] & 0x0F, (((int)buffer[2])&0xff) | (((int)buffer[3])<<8)&0xFF00 ));
|
||||
|
||||
int op = 0;
|
||||
|
||||
op |= ( buffer[0] & 0x01 ) == 0 ? REQ_WRITE : REQ_READ;
|
||||
op |= ( buffer[0] & 0x02 ) == 0 ? REQ_INT : REQ_FLOAT;
|
||||
op |= ( buffer[0] & 0x04 ) == 0 ? REQ_ACK : REQ_FAIL;
|
||||
|
||||
setOpcode(op);
|
||||
|
||||
if ((op & (REQ_READ | REQ_ACK))==(REQ_READ | REQ_ACK)){
|
||||
ByteBuffer bb = ByteBuffer.wrap(buffer);
|
||||
|
||||
if ((op & REQ_FLOAT)==REQ_FLOAT){
|
||||
setValue( bb.order(ByteOrder.LITTLE_ENDIAN).asFloatBuffer().get(1) );
|
||||
} else {
|
||||
setValue( bb.order(ByteOrder.LITTLE_ENDIAN).asIntBuffer().get(1) );
|
||||
}
|
||||
|
||||
} else {
|
||||
setValue(null);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public byte[] toByteArray() {
|
||||
byte[] buffer = new byte[8];
|
||||
byte[] bv;
|
||||
int i;
|
||||
|
||||
i = (getOpcode() & REQ_WRITE) == REQ_WRITE ? 0x01 : 0x00;
|
||||
i |= (getOpcode() & REQ_FLOAT) == REQ_FLOAT ? 0x02 : 0x00;
|
||||
|
||||
buffer[0] = (byte)i;
|
||||
buffer[1] = (byte)( ((getAddress().getAx()&0x0F)<<4) | (getAddress().getNode() & 0x0F) );
|
||||
buffer[2] = (byte)(getAddress().getRegister() & 0xFF);
|
||||
buffer[3] = (byte)((getAddress().getRegister()>>8) & 0xFF);
|
||||
|
||||
bv = valueAsByteArray();
|
||||
buffer[4] = bv[0];
|
||||
buffer[5] = bv[1];
|
||||
buffer[6] = bv[2];
|
||||
buffer[7] = bv[3];
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -1,170 +0,0 @@
|
|||
package org.hwo.servicelink;
|
||||
|
||||
import java.util.Hashtable;
|
||||
import static org.hwo.logging.Logging.*;
|
||||
import static org.hwo.logging.LogLevel.*;
|
||||
|
||||
public class ServiceRegisterCache {
|
||||
ServiceLink serviceLink;
|
||||
Hashtable<Integer, BaseCacheItem>
|
||||
cache;
|
||||
|
||||
public class BaseCacheItem
|
||||
{
|
||||
Integer ax,
|
||||
node,
|
||||
register;
|
||||
|
||||
boolean isFloat;
|
||||
|
||||
long lastReadTime;
|
||||
|
||||
Object value;
|
||||
|
||||
public BaseCacheItem(int hash){
|
||||
this.ax = axFromHash(hash);
|
||||
this.node = nodeFromHash(hash);
|
||||
this.register = regFromHash(hash);
|
||||
this.isFloat = ((hash & 0x40000000) != 0);
|
||||
this.value = null;
|
||||
}
|
||||
|
||||
public BaseCacheItem(int ax,int node,int register, boolean isFloat)
|
||||
{
|
||||
this.ax = ax;
|
||||
this.node = node;
|
||||
this.register = register;
|
||||
this.isFloat = isFloat;
|
||||
this.value = null;
|
||||
}
|
||||
|
||||
long age()
|
||||
{
|
||||
return System.currentTimeMillis() - lastReadTime;
|
||||
}
|
||||
|
||||
boolean isOld()
|
||||
{
|
||||
return age() > 250;
|
||||
}
|
||||
|
||||
private void fetchValue(){
|
||||
if (isOld() && serviceLink.isOpen())
|
||||
{
|
||||
try {
|
||||
if (isFloat){
|
||||
Float f = serviceLink.readFloat(ax.byteValue(),node.byteValue(),register);
|
||||
} else {
|
||||
Integer i = serviceLink.readInt(ax.byteValue(),node.byteValue(),register);
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Object getValue(){
|
||||
if (isOld()){
|
||||
log(DEBUGDETAIL,"OLDCACHE:SLA: [%d:%d:%d:%s] = %s", ax,node,register,isFloat ? "F" : "I",value);
|
||||
fetchValue();
|
||||
log(DEBUGDETAIL,"NEWCACHE:SLA: [%d:%d:%d:%s] = %s", ax,node,register,isFloat ? "F" : "I",value);
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setValue(Object value){
|
||||
this.value = value;
|
||||
this.lastReadTime = System.currentTimeMillis();
|
||||
log(DEBUGDETAIL,"CACHE: SLA: [%d:%d:%d:%s] = %s", ax,node,register,isFloat ? "F" : "I",value);
|
||||
}
|
||||
|
||||
public void invalidate(){
|
||||
this.lastReadTime = 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public Integer axFromHash(Integer hash){
|
||||
return (hash >> 20) & 0x0F;
|
||||
}
|
||||
public Integer nodeFromHash(Integer hash){
|
||||
return (hash >> 16) & 0x0F;
|
||||
}
|
||||
public Integer regFromHash(Integer hash){
|
||||
return (hash) & 0xFFFF;
|
||||
}
|
||||
|
||||
public int calcHash(int ax,int node,int register,boolean isFloat)
|
||||
{
|
||||
return (isFloat ? 0x40000000 : 0) | (ax << 20) | (node << 16) | register;
|
||||
}
|
||||
|
||||
public int calcHash(ServiceLinkAddress sla,boolean isFloat){
|
||||
Integer hash = (sla.getAx() << 20) | (sla.getNode() << 16) | sla.getRegister();
|
||||
if (isFloat)
|
||||
hash |= 0x40000000;
|
||||
|
||||
return hash;
|
||||
}
|
||||
|
||||
public ServiceLinkAddress getAddress(int hash){
|
||||
return new ServiceLinkAddress( axFromHash(hash), nodeFromHash(hash), regFromHash(hash) );
|
||||
}
|
||||
|
||||
|
||||
ServiceRegisterCache(ServiceLink serviceLink)
|
||||
{
|
||||
this.serviceLink = serviceLink;
|
||||
this.cache = new Hashtable<>();
|
||||
}
|
||||
|
||||
public ServiceLink getServiceLink() {
|
||||
return serviceLink;
|
||||
}
|
||||
|
||||
private BaseCacheItem getCacheItem(int hash){
|
||||
if (!cache.containsKey(hash)){
|
||||
BaseCacheItem bci = new BaseCacheItem(hash);
|
||||
cache.put(new Integer(hash), bci);
|
||||
log( DEBUGDETAIL, "new cache-item: 0x%08x = %s", hash, bci);
|
||||
}
|
||||
return cache.get(hash);
|
||||
}
|
||||
|
||||
public void updateValue(int hash){
|
||||
BaseCacheItem bci = getCacheItem(hash);
|
||||
bci.invalidate();
|
||||
bci.getValue();
|
||||
}
|
||||
|
||||
public void updateValue(int hash,Object value){
|
||||
getCacheItem(hash).setValue(value);
|
||||
}
|
||||
|
||||
public synchronized Object getCachedValue(int hash){
|
||||
return getCacheItem(hash).getValue();
|
||||
}
|
||||
public synchronized Object getCachedValue(int ax,int node,int register,boolean isFloat){
|
||||
int hash = calcHash(ax, node, register, isFloat);
|
||||
return getCacheItem(hash).getValue();
|
||||
}
|
||||
|
||||
public synchronized Integer getCachedInteger(int ax,int node,int register)
|
||||
{
|
||||
return (Integer)getCachedValue(ax, node, register, false);
|
||||
}
|
||||
|
||||
public synchronized Float getCachedFloat(int ax,int node,int register)
|
||||
{
|
||||
return (Float)getCachedValue(ax, node, register, true);
|
||||
}
|
||||
|
||||
public synchronized void invalidate(int ax,int node,int register,boolean isFloat)
|
||||
{
|
||||
cache.remove(calcHash(ax, node, register,isFloat));
|
||||
}
|
||||
|
||||
}
|
|
@ -1,8 +0,0 @@
|
|||
package org.hwo.servicelink;
|
||||
|
||||
public interface ServiceRegisterListener {
|
||||
|
||||
void ServiceRegisterValueUpdated(int ax,int node,int registervalue,Integer value);
|
||||
void ServiceRegisterValueUpdated(int ax,int node,int registervalue,Float value);
|
||||
|
||||
}
|
|
@ -1,235 +0,0 @@
|
|||
package org.hwo.servicelink;
|
||||
|
||||
import java.awt.TexturePaint;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import org.hwo.ByteArrayHexlifier;
|
||||
import java.io.InputStreamReader;
|
||||
|
||||
import org.hwo.io.NewSerialPort.BaudRate;
|
||||
import org.hwo.io.NewSerialPort.NewSerialPort;
|
||||
import org.hwo.servicelink.exceptions.ServiceLinkException;
|
||||
import static org.hwo.logging.Logging.*;
|
||||
import static org.hwo.logging.LogLevel.*;
|
||||
|
||||
|
||||
public class SimpleLink {
|
||||
|
||||
NewSerialPort port;
|
||||
//BufferedReader br;
|
||||
SimpleLineReader br;
|
||||
|
||||
public SimpleLink(NewSerialPort port){
|
||||
this.port = port;
|
||||
//this.br = new BufferedReader(new InputStreamReader(this.port.getInputStream()));
|
||||
this.br = new SimpleLineReader(this.port.getInputStream());
|
||||
}
|
||||
|
||||
public void close(){
|
||||
this.port.close();
|
||||
}
|
||||
|
||||
public boolean isOpen(){
|
||||
return this.port.isOpen();
|
||||
}
|
||||
|
||||
public void open() throws IOException, ServiceLinkException{
|
||||
//this.port.setBaudRate(BaudRate.B57600);
|
||||
this.port.open();
|
||||
}
|
||||
|
||||
public NewSerialPort getSerialPort(){
|
||||
return this.port;
|
||||
}
|
||||
|
||||
public void setTimeOut(int timeout) throws IOException, ServiceLinkException{
|
||||
this.port.setTimeOut(timeout);
|
||||
}
|
||||
|
||||
public String fixhexstring(String hs,int digits){
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (int n=0;n<digits-hs.length();n++)
|
||||
{
|
||||
sb.append("0");
|
||||
}
|
||||
sb.append(hs);
|
||||
return sb.toString().substring(0,digits);
|
||||
}
|
||||
|
||||
|
||||
public float readFloat(int reg) throws IOException, ServiceLinkException{
|
||||
String sreg = fixhexstring(Integer.toHexString(reg),4);
|
||||
|
||||
String ol = String.format("R%s\n\r", sreg);
|
||||
|
||||
String reply = request( ol );
|
||||
|
||||
if (reply.length() != 9){
|
||||
throw new ServiceLinkException();
|
||||
}
|
||||
|
||||
return Float.intBitsToFloat(Integer.parseUnsignedInt(reply.substring(1), 16));
|
||||
}
|
||||
public int readInt(int reg) throws IOException, ServiceLinkException{
|
||||
return readInt(new int[]{ reg })[0];
|
||||
}
|
||||
|
||||
public void writeInt(int reg,int value) throws IOException, ServiceLinkException{
|
||||
writeInt(new int[]{ reg },new int[]{value});
|
||||
}
|
||||
|
||||
public void writeFloat(int reg,float value) throws IOException, ServiceLinkException{
|
||||
|
||||
}
|
||||
|
||||
public Integer[] readInt(int[] reg) throws IOException, ServiceLinkException{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
Integer[] values = new Integer[reg.length];
|
||||
|
||||
sb.append("R");
|
||||
|
||||
for (int i=0;i<reg.length;i++){
|
||||
sb.append(fixhexstring(Integer.toHexString(reg[i]),4));
|
||||
}
|
||||
|
||||
sb.append("\n\r");
|
||||
|
||||
String reply = request(sb.toString());
|
||||
|
||||
for (int i=0;i<reg.length;i++){
|
||||
if (reply.charAt(1 + (i*9)) == 'V'){
|
||||
values[i] = Integer.parseUnsignedInt(reply.substring(2 + (i*9),10 + (i*9)), 16);
|
||||
};
|
||||
}
|
||||
|
||||
return values;
|
||||
}
|
||||
public Integer[] readInt(int startReg,int no) throws IOException, ServiceLinkException{
|
||||
Integer[] values = new Integer[no];
|
||||
String tx = String.format("R+%s%s\n\r",fixhexstring(Integer.toHexString(no),2),fixhexstring(Integer.toHexString(startReg),4));
|
||||
|
||||
String reply = request(tx);
|
||||
|
||||
for (int i=0;i<no;i++){
|
||||
if (reply.charAt(1 + (i*9)) == 'V'){
|
||||
values[i] = Integer.parseUnsignedInt(reply.substring(2 + (i*9),10 + (i*9)), 16);
|
||||
}
|
||||
}
|
||||
|
||||
return values;
|
||||
}
|
||||
|
||||
public boolean[] writeInt(int[] reg,int[] values) throws IOException, ServiceLinkException{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
if (reg.length != values.length){
|
||||
throw new IllegalArgumentException("reg.length != values.length");
|
||||
};
|
||||
|
||||
sb.append("W");
|
||||
|
||||
for (int i=0;i<reg.length;i++){
|
||||
sb.append(fixhexstring(Integer.toHexString(reg[i]),4));
|
||||
sb.append(fixhexstring(Integer.toHexString(values[i]),8));
|
||||
}
|
||||
|
||||
sb.append("\n\r");
|
||||
|
||||
String reply = request(sb.toString());
|
||||
boolean[] rb = new boolean[reg.length];
|
||||
|
||||
for (int i=0;i<reg.length;i++){
|
||||
if (reply.charAt(1 + (i*5)) == 'V'){
|
||||
int rreg = Integer.parseUnsignedInt(reply.substring(2 + (i*5),6 + (i*5)), 16);
|
||||
if (reg[i] == rreg){
|
||||
rb[i] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return rb;
|
||||
}
|
||||
|
||||
public boolean[] writeInt(int startReg,int[] values) throws IOException, ServiceLinkException{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("W+");
|
||||
sb.append(fixhexstring(Integer.toHexString(values.length),2));
|
||||
sb.append(fixhexstring(Integer.toHexString(startReg),4));
|
||||
|
||||
for (int i=0;i<values.length;i++){
|
||||
sb.append(fixhexstring(Integer.toHexString(values[i]),8));
|
||||
}
|
||||
|
||||
sb.append("\n\r");
|
||||
|
||||
String reply = request(sb.toString());
|
||||
boolean[] rb = new boolean[values.length];
|
||||
|
||||
for (int i=0;i<values.length;i++){
|
||||
if (reply.charAt(1 + (i*5)) == 'V'){
|
||||
int rreg = Integer.parseUnsignedInt(reply.substring(2 + (i*5),6 + (i*5)), 16);
|
||||
if ((startReg+i) == rreg){
|
||||
rb[i] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return rb;
|
||||
}
|
||||
public synchronized String request(String request) throws IOException{
|
||||
long st = System.currentTimeMillis();
|
||||
log(DEBUGDETAIL,"request(): tx [%d] %s",st,request);
|
||||
port.getOutputStream().write(request.getBytes());
|
||||
log(DEBUGDETAIL,"request(): rx [+%d]",(System.currentTimeMillis()-st));
|
||||
String reply = br.readLine();
|
||||
log(DEBUGDETAIL,"request(): [+%d] %d bytes: %s",(System.currentTimeMillis()-st),reply.length(),reply);
|
||||
return reply;
|
||||
}
|
||||
|
||||
|
||||
|
||||
class SimpleLineReader {
|
||||
InputStream stream;
|
||||
byte[] ibuffer;
|
||||
int ipos,ilen;
|
||||
|
||||
public SimpleLineReader(InputStream stream){
|
||||
this.stream = stream;
|
||||
this.ibuffer = new byte[512];
|
||||
this.ilen = 0;
|
||||
this.ipos = 0;
|
||||
}
|
||||
|
||||
int readByte(){
|
||||
if (ipos >= ilen){
|
||||
ipos = 0;
|
||||
try {
|
||||
ilen = this.stream.read(ibuffer);
|
||||
} catch (IOException e){
|
||||
log(e);
|
||||
ilen = 0;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
return ibuffer[ipos++];
|
||||
}
|
||||
|
||||
String readLine(){
|
||||
byte[] buffer = new byte[512];
|
||||
int p = 0;
|
||||
do {
|
||||
int ch = readByte();
|
||||
if (ch == 0x0d){
|
||||
break;
|
||||
}
|
||||
buffer[p++] = (byte)ch;
|
||||
} while (p < buffer.length);
|
||||
return new String(buffer,0,p);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -1,23 +0,0 @@
|
|||
package org.hwo.servicelink.exceptions;
|
||||
|
||||
import org.hwo.ByteArrayHexlifier;
|
||||
|
||||
public class CRCFailedException extends ServiceLinkException {
|
||||
|
||||
private byte[] buffer;
|
||||
|
||||
public CRCFailedException(byte[] buffer,String message){
|
||||
super(message);
|
||||
this.buffer = buffer;
|
||||
}
|
||||
|
||||
public byte[] getBuffer() {
|
||||
return buffer;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDebugMessage() {
|
||||
return String.format("Failed Buffer is: %s",ByteArrayHexlifier.byteArrayToString(buffer));
|
||||
}
|
||||
|
||||
}
|
|
@ -1,29 +0,0 @@
|
|||
package org.hwo.servicelink.exceptions;
|
||||
|
||||
import org.hwo.exceptions.ExtendedExceptionBase;
|
||||
|
||||
public class ServiceLinkException extends ExtendedExceptionBase {
|
||||
|
||||
public ServiceLinkException()
|
||||
{
|
||||
super("Unspecific ServiceLink Exception");
|
||||
}
|
||||
|
||||
public ServiceLinkException(String message)
|
||||
{
|
||||
super(message);
|
||||
}
|
||||
|
||||
public ServiceLinkException(String message,Throwable cause)
|
||||
{
|
||||
super(message,cause);
|
||||
}
|
||||
|
||||
public ServiceLinkException(Throwable e) {
|
||||
super(e);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -1,30 +0,0 @@
|
|||
package org.hwo.servicelink.exceptions;
|
||||
|
||||
import org.hwo.servicelink.ServiceLinkV1Telegram;
|
||||
|
||||
public class ServiceLinkRequestFailedException extends ServiceLinkException {
|
||||
|
||||
private ServiceLinkV1Telegram serviceTelegram;
|
||||
|
||||
public ServiceLinkRequestFailedException(ServiceLinkV1Telegram telegram)
|
||||
{
|
||||
serviceTelegram = telegram;
|
||||
}
|
||||
|
||||
public ServiceLinkRequestFailedException()
|
||||
{
|
||||
serviceTelegram = null;
|
||||
}
|
||||
|
||||
public ServiceLinkV1Telegram getServiceLinkTelegram()
|
||||
{
|
||||
return this.serviceTelegram;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
if (getServiceLinkTelegram()!=null)
|
||||
return String.format("ServiceLinkRequestFailedException: %d.%d:%d",getServiceLinkTelegram().getAddress().getAx(),getServiceLinkTelegram().getAddress().getNode(),getServiceLinkTelegram().getAddress().getRegister());
|
||||
return "ServiceLinkRequestFailedException";
|
||||
}
|
||||
}
|
|
@ -1,14 +1,9 @@
|
|||
package org.hwo.servicelink.ng;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.util.Hashtable;
|
||||
|
||||
import javax.swing.DebugGraphics;
|
||||
|
||||
import org.hwo.Smoother;
|
||||
|
||||
import static org.hwo.logging.Logging.*;
|
||||
import static org.hwo.logging.LogLevel.*;
|
||||
import static bootstrapper.logging.Logging.*;
|
||||
import static bootstrapper.logging.LogLevel.*;
|
||||
|
||||
public class ServiceLink implements RegBUSInterface {
|
||||
|
||||
|
@ -20,8 +15,6 @@ public class ServiceLink implements RegBUSInterface {
|
|||
ServiceRegisterCache registerCache = new ServiceRegisterCache(this);
|
||||
AsynchronServiceLinkProvider asynchronServiceLinkProvider = new AsynchronServiceLinkProvider(registerCache);
|
||||
|
||||
Smoother turnAroundTime = new Smoother();
|
||||
|
||||
Thread readerThread = new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
|
@ -41,11 +34,11 @@ public class ServiceLink implements RegBUSInterface {
|
|||
}
|
||||
|
||||
private void initialize() {
|
||||
this.turnAroundTime.setTn(10);
|
||||
}
|
||||
|
||||
public void close() {
|
||||
this.stream.close();
|
||||
this.closed = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -86,13 +79,12 @@ public class ServiceLink implements RegBUSInterface {
|
|||
}
|
||||
|
||||
long tLen = System.currentTimeMillis() - tStart;
|
||||
turnAroundTime.cycle((int)tLen);
|
||||
|
||||
return buffer.isSuccess();
|
||||
}
|
||||
|
||||
public int getTurnAroundTime() {
|
||||
return this.turnAroundTime.getLastValue();
|
||||
return -1;
|
||||
}
|
||||
|
||||
public synchronized ServiceLinkStream getServiceLinkStream() {
|
||||
|
@ -181,7 +173,7 @@ public class ServiceLink implements RegBUSInterface {
|
|||
private void reader() {
|
||||
ServiceLinkRequestBuffer buffer = new ServiceLinkRequestBuffer();
|
||||
|
||||
while (!closed) {
|
||||
while (!this.closed) {
|
||||
if ((this.stream == null) || !this.stream.isConnected()) {
|
||||
try {
|
||||
log(DEBUG,"ServiceLink:reader() sleeps...");
|
||||
|
@ -220,7 +212,6 @@ public class ServiceLink implements RegBUSInterface {
|
|||
try {
|
||||
Thread.sleep(250);
|
||||
} catch (InterruptedException e1) {
|
||||
// TODO Auto-generated catch block
|
||||
e1.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
@ -231,7 +222,14 @@ public class ServiceLink implements RegBUSInterface {
|
|||
|
||||
}
|
||||
|
||||
|
||||
public static ServiceLink fromURI(URI uri) {
|
||||
if (uri == null) {
|
||||
return null;
|
||||
}
|
||||
StreamContainer sls = new StreamContainer(uri);
|
||||
ServiceLink serviceLink = new ServiceLink(new TelegramStreamV2(sls));
|
||||
return serviceLink;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@ package org.hwo.servicelink.ng;
|
|||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.hwo.logging.Logging;
|
||||
import bootstrapper.logging.Logging;
|
||||
|
||||
public abstract class ServiceLinkStream {
|
||||
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
package org.hwo.servicelink.ng;
|
||||
|
||||
import java.util.Hashtable;
|
||||
import static org.hwo.logging.Logging.*;
|
||||
import static org.hwo.logging.LogLevel.*;
|
||||
import static bootstrapper.logging.Logging.*;
|
||||
import static bootstrapper.logging.LogLevel.*;
|
||||
|
||||
public class ServiceRegisterCache {
|
||||
ServiceLink serviceLink;
|
||||
|
|
|
@ -4,13 +4,15 @@ import java.io.IOException;
|
|||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.Socket;
|
||||
import java.net.URI;
|
||||
|
||||
import static org.hwo.logging.Logging.*;
|
||||
import static bootstrapper.logging.Logging.*;
|
||||
|
||||
import org.hwo.io.NewSerialPort.NewSerialPort;
|
||||
|
||||
public class StreamContainer {
|
||||
|
||||
URI uri;
|
||||
boolean exceptionCaught = false;
|
||||
|
||||
InputStream inStream;
|
||||
|
@ -19,23 +21,13 @@ public class StreamContainer {
|
|||
Socket socket = null;
|
||||
NewSerialPort newSerialPort = null;
|
||||
|
||||
public StreamContainer(String url) {
|
||||
|
||||
int dots = url.indexOf(':');
|
||||
String proto,target;
|
||||
|
||||
if (dots == -1) {
|
||||
proto = "serial";
|
||||
target = url;
|
||||
} else {
|
||||
proto = url.substring(0, dots);
|
||||
target = url.substring(dots + 1);
|
||||
}
|
||||
|
||||
switch (proto) {
|
||||
public StreamContainer(URI uri) {
|
||||
this.uri = uri;
|
||||
|
||||
switch (this.uri.getScheme()) {
|
||||
case "serial":
|
||||
|
||||
newSerialPort = new NewSerialPort(target);
|
||||
newSerialPort = new NewSerialPort(uri.getPath());
|
||||
newSerialPort.setTimeOut(250);
|
||||
if (newSerialPort.open()) {
|
||||
inStream = newSerialPort.getInputStream();
|
||||
|
@ -46,7 +38,10 @@ public class StreamContainer {
|
|||
case "tcp":
|
||||
|
||||
try {
|
||||
socket = new Socket(target,3009);
|
||||
socket = new Socket(
|
||||
this.uri.getHost(),
|
||||
this.uri.getPort() > 0 ? this.uri.getPort() : 3009
|
||||
);
|
||||
socket.setSoTimeout(150);
|
||||
socket.setTcpNoDelay(true);
|
||||
|
||||
|
@ -63,6 +58,10 @@ public class StreamContainer {
|
|||
|
||||
}
|
||||
|
||||
public URI getURI() {
|
||||
return this.uri;
|
||||
}
|
||||
|
||||
public StreamContainer(InputStream inStream,OutputStream outStream)
|
||||
{
|
||||
this.inStream = inStream;
|
||||
|
|
|
@ -7,9 +7,8 @@ import java.net.Socket;
|
|||
import java.net.SocketTimeoutException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import org.hwo.ByteArrayHexlifier;
|
||||
import static org.hwo.logging.Logging.*;
|
||||
import static org.hwo.logging.LogLevel.*;
|
||||
import static bootstrapper.logging.Logging.*;
|
||||
import static bootstrapper.logging.LogLevel.*;
|
||||
|
||||
public class TelegramStreamV2 extends ServiceLinkStream {
|
||||
|
||||
|
@ -70,7 +69,7 @@ public class TelegramStreamV2 extends ServiceLinkStream {
|
|||
|
||||
try {
|
||||
log(DEBUGDETAIL,"TX BUFFER: %s",rbuffer);
|
||||
log(DEBUGDETAIL,"TX BYTES: %s",ByteArrayHexlifier.byteArrayToString(buffer));
|
||||
//log(DEBUGDETAIL,"TX BYTES: %s",ByteArrayHexlifier.byteArrayToString(buffer));
|
||||
|
||||
synchronized(this.streamContainer.getOutStream()) {
|
||||
this.streamContainer.getOutStream().write(buffer);
|
||||
|
@ -116,7 +115,7 @@ public class TelegramStreamV2 extends ServiceLinkStream {
|
|||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
log(DEBUGDETAIL,"RX BYTES: %s",ByteArrayHexlifier.byteArrayToString(buffer));
|
||||
//log(DEBUGDETAIL,"RX BYTES: %s",ByteArrayHexlifier.byteArrayToString(buffer));
|
||||
|
||||
if (bufferUsed != 8){
|
||||
return false;
|
||||
|
|
|
@ -1,99 +0,0 @@
|
|||
package org.hwo.servicelink.register;
|
||||
|
||||
import javax.xml.xpath.XPath;
|
||||
import javax.xml.xpath.XPathConstants;
|
||||
import javax.xml.xpath.XPathFactory;
|
||||
|
||||
import org.w3c.dom.Node;
|
||||
|
||||
public class RegisterType implements Cloneable{
|
||||
|
||||
XPath xpath;
|
||||
|
||||
String name;
|
||||
String baseName;
|
||||
|
||||
boolean wantsFloat;
|
||||
|
||||
protected RegisterType(String name){
|
||||
this.name = name;
|
||||
this.baseName = "";
|
||||
this.wantsFloat = false;
|
||||
xpath = XPathFactory.newInstance().newXPath();
|
||||
}
|
||||
|
||||
protected XPath getXPath(){
|
||||
return xpath;
|
||||
}
|
||||
|
||||
public String getBaseName() {
|
||||
return baseName;
|
||||
}
|
||||
protected void setBaseName(String baseName) {
|
||||
this.baseName = baseName;
|
||||
}
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
protected void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
public boolean getWantsFloat() {
|
||||
return wantsFloat;
|
||||
}
|
||||
protected void setWantsFloat(boolean wantsFloat) {
|
||||
this.wantsFloat = wantsFloat;
|
||||
}
|
||||
|
||||
public void createFromTypeNode(Node typeNode){
|
||||
};
|
||||
|
||||
public RegisterType inherit(String newName){
|
||||
try {
|
||||
RegisterType inherited = (RegisterType)this.clone();
|
||||
|
||||
inherited.setBaseName(getName());
|
||||
inherited.setName(newName);
|
||||
|
||||
return inherited;
|
||||
} catch (CloneNotSupportedException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public RegisterType createRegisterTypeInstance(ServiceRegisterList serviceRegisterList,Node registerNode){
|
||||
try {
|
||||
Node aNode = (Node)getXPath().evaluate("Address", registerNode, XPathConstants.NODE);
|
||||
RegisterType rt = inherit(String.format("%s/%s/%s",serviceRegisterList.id,getName(),aNode.getFirstChild().getNodeValue()));
|
||||
return rt;
|
||||
} catch (Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public String getDisplayValue(Object regValue){
|
||||
return "-- REGISTER TYPE NOT IMPLEMENTED --";
|
||||
}
|
||||
|
||||
|
||||
protected java.lang.Integer valueAsInteger(Object regValue){
|
||||
if (java.lang.Integer.class.isInstance(regValue)){
|
||||
return (java.lang.Integer)regValue;
|
||||
} else if (java.lang.Float.class.isInstance(regValue)){
|
||||
return ((java.lang.Float)regValue).intValue();
|
||||
};
|
||||
return null;
|
||||
}
|
||||
|
||||
protected java.lang.Float valueAsFloat(Object regValue){
|
||||
if (java.lang.Float.class.isInstance(regValue)){
|
||||
return (java.lang.Float)regValue;
|
||||
} else if (java.lang.Integer.class.isInstance(regValue)){
|
||||
return ((java.lang.Integer)regValue).floatValue();
|
||||
};
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,58 +0,0 @@
|
|||
package org.hwo.servicelink.register;
|
||||
|
||||
import java.util.Hashtable;
|
||||
|
||||
import org.w3c.dom.Element;
|
||||
import org.w3c.dom.Node;
|
||||
|
||||
public class RegisterTypeRegistry {
|
||||
ServiceRegisterRegistry registry;
|
||||
Hashtable<String, RegisterType> registeredTypes;
|
||||
|
||||
|
||||
public RegisterTypeRegistry(ServiceRegisterRegistry registry) {
|
||||
this.registry = registry;
|
||||
this.registeredTypes = new Hashtable<String, RegisterType>();
|
||||
|
||||
|
||||
registerType(new org.hwo.servicelink.register.basetypes.Integer());
|
||||
registerType(new org.hwo.servicelink.register.basetypes.Float());
|
||||
registerType(new org.hwo.servicelink.register.basetypes.BitField());
|
||||
registerType(new org.hwo.servicelink.register.basetypes.IndexedList());
|
||||
}
|
||||
|
||||
public void registerType(RegisterType registerType){
|
||||
System.err.println(String.format("RegisterTypeRegistry: New type: %s", registerType.getName()));
|
||||
registeredTypes.put(registerType.getName(),registerType);
|
||||
}
|
||||
|
||||
void createType(Node typeNode){
|
||||
Element typeElement = (Element)typeNode;
|
||||
|
||||
RegisterType baseType = registeredTypes.get(typeElement.getAttribute("basetype"));
|
||||
if (baseType == null){
|
||||
throw new RuntimeException("BaseType not found: " + typeElement.getAttribute("basetype"));
|
||||
}
|
||||
RegisterType rt = baseType.inherit(typeElement.getAttribute("id"));
|
||||
rt.createFromTypeNode(typeNode);
|
||||
|
||||
registerType(rt);
|
||||
}
|
||||
|
||||
|
||||
public RegisterType createRegisterTypeInstance(ServiceRegisterList serviceRegisterList,Node registerNode){
|
||||
RegisterType baseType;
|
||||
Element registerElement = (Element)registerNode;
|
||||
String id = registerElement.getAttribute("type");
|
||||
|
||||
if (registeredTypes.containsKey(id)){
|
||||
baseType = registeredTypes.get(id);
|
||||
} else {
|
||||
baseType = registeredTypes.get("Integer");
|
||||
}
|
||||
return baseType.createRegisterTypeInstance(serviceRegisterList, registerNode);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -1,45 +0,0 @@
|
|||
package org.hwo.servicelink.register;
|
||||
|
||||
import javax.xml.xpath.XPathConstants;
|
||||
|
||||
import org.w3c.dom.Node;
|
||||
|
||||
public class ServiceRegisterInstance {
|
||||
ServiceRegisterList serviceRegisterList;
|
||||
|
||||
RegisterType registerType;
|
||||
|
||||
String label;
|
||||
Integer registerAddress;
|
||||
|
||||
|
||||
public ServiceRegisterInstance(ServiceRegisterList serviceRegisterList){
|
||||
this.serviceRegisterList = serviceRegisterList;
|
||||
}
|
||||
|
||||
public RegisterType getRegisterType() {
|
||||
return registerType;
|
||||
}
|
||||
public void setRegisterType(RegisterType registerType) {
|
||||
this.registerType = registerType;
|
||||
}
|
||||
|
||||
public static ServiceRegisterInstance fromRegisterNode(ServiceRegisterList serviceRegisterList,Node registerNode){
|
||||
ServiceRegisterInstance sri = new ServiceRegisterInstance(serviceRegisterList);
|
||||
RegisterType rti = serviceRegisterList.getRegistry().getTypeRegistry().createRegisterTypeInstance(serviceRegisterList, registerNode);
|
||||
sri.setRegisterType(rti);
|
||||
|
||||
try {
|
||||
Node labelNode = (Node)serviceRegisterList.getRegistry().getXpath().evaluate("Label", registerNode, XPathConstants.NODE);
|
||||
Node addressNode = (Node)serviceRegisterList.getRegistry().getXpath().evaluate("Address", registerNode, XPathConstants.NODE);
|
||||
sri.label = labelNode.getFirstChild().getNodeValue();
|
||||
sri.registerAddress = Integer.decode(addressNode.getFirstChild().getNodeValue());
|
||||
|
||||
} catch (Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
System.err.println(String.format("ServiceRegisterInstance: created type: %s", rti.getName()));
|
||||
return sri;
|
||||
}
|
||||
}
|
|
@ -1,70 +0,0 @@
|
|||
package org.hwo.servicelink.register;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import javax.xml.xpath.XPath;
|
||||
import javax.xml.xpath.XPathConstants;
|
||||
import javax.xml.xpath.XPathFactory;
|
||||
|
||||
import org.hwo.xml.NodeListIterator;
|
||||
import org.w3c.dom.Element;
|
||||
import org.w3c.dom.Node;
|
||||
import org.w3c.dom.NodeList;
|
||||
|
||||
public class ServiceRegisterList {
|
||||
ServiceRegisterRegistry registry;
|
||||
String label;
|
||||
String id;
|
||||
|
||||
List<ServiceRegisterInstance> serviceRegisterInstances;
|
||||
|
||||
XPath xpath;
|
||||
|
||||
public ServiceRegisterList(ServiceRegisterRegistry registry){
|
||||
this.registry = registry;
|
||||
this.label = "-";
|
||||
this.xpath = XPathFactory.newInstance().newXPath();
|
||||
this.serviceRegisterInstances = new ArrayList<ServiceRegisterInstance>();
|
||||
}
|
||||
|
||||
public ServiceRegisterRegistry getRegistry() {
|
||||
return registry;
|
||||
}
|
||||
|
||||
public String getLabel() {
|
||||
return label;
|
||||
}
|
||||
public void setLabel(String label) {
|
||||
this.label = label;
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public void createFromNode(Node registerListNode){
|
||||
Element registerListElement = (Element)registerListNode;
|
||||
|
||||
setId(registerListElement.getAttribute("id"));
|
||||
setLabel(registerListElement.getAttribute("label"));
|
||||
|
||||
try {
|
||||
NodeList serviceRegisterList = (NodeList)xpath.evaluate("Register", registerListNode, XPathConstants.NODESET);
|
||||
for (Node registerNode: NodeListIterator.create(serviceRegisterList)){
|
||||
ServiceRegisterInstance sri = ServiceRegisterInstance.fromRegisterNode(this, registerNode);
|
||||
this.serviceRegisterInstances.add(sri);
|
||||
}
|
||||
|
||||
|
||||
|
||||
} catch (Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -1,37 +0,0 @@
|
|||
package org.hwo.servicelink.register;
|
||||
|
||||
import java.util.Hashtable;
|
||||
|
||||
import org.w3c.dom.Node;
|
||||
|
||||
public class ServiceRegisterListRegistry {
|
||||
ServiceRegisterRegistry registry;
|
||||
|
||||
Hashtable<String, ServiceRegisterList> serviceRegisterLists;
|
||||
|
||||
public ServiceRegisterListRegistry(ServiceRegisterRegistry registry){
|
||||
this.registry = registry;
|
||||
this.serviceRegisterLists = new Hashtable<String, ServiceRegisterList>();
|
||||
}
|
||||
|
||||
public Hashtable<String, ServiceRegisterList> getServiceRegisterLists() {
|
||||
return serviceRegisterLists;
|
||||
}
|
||||
|
||||
public ServiceRegisterList getServiceRegisterList(String id){
|
||||
return this.serviceRegisterLists.get(id);
|
||||
}
|
||||
|
||||
void addServiceRegisterList(ServiceRegisterList serviceRegisterList){
|
||||
System.err.println(String.format("ServiceRegisterListRegistry: adding %s", serviceRegisterList.getId()));
|
||||
this.serviceRegisterLists.put(serviceRegisterList.getId(), serviceRegisterList);
|
||||
}
|
||||
|
||||
public void createServiceRegisterList(Node serviceListNode){
|
||||
ServiceRegisterList list = new ServiceRegisterList(registry);
|
||||
list.createFromNode(serviceListNode);
|
||||
|
||||
addServiceRegisterList(list);
|
||||
}
|
||||
|
||||
}
|
|
@ -1,104 +0,0 @@
|
|||
package org.hwo.servicelink.register;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import javax.xml.xpath.XPath;
|
||||
import javax.xml.xpath.XPathConstants;
|
||||
import javax.xml.xpath.XPathFactory;
|
||||
|
||||
import org.hwo.xml.NodeListIterator;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Node;
|
||||
import org.w3c.dom.NodeList;
|
||||
|
||||
public class ServiceRegisterRegistry {
|
||||
|
||||
RegisterTypeRegistry typeRegistry;
|
||||
ServiceRegisterListRegistry
|
||||
listRegistry;
|
||||
|
||||
List<String> loadedFiles;
|
||||
XPath xpath;
|
||||
|
||||
|
||||
public ServiceRegisterRegistry(){
|
||||
xpath = XPathFactory.newInstance().newXPath();
|
||||
loadedFiles = new ArrayList<String>();
|
||||
typeRegistry = new RegisterTypeRegistry(this);
|
||||
listRegistry = new ServiceRegisterListRegistry(this);
|
||||
}
|
||||
|
||||
public boolean loadRegistryFile(String filename){
|
||||
return loadRegistryFile(filename,new File(".").getAbsolutePath());
|
||||
}
|
||||
public boolean loadRegistryFile(String filename,String basePath){
|
||||
|
||||
File f = new File(filename);
|
||||
if (!f.exists()){
|
||||
f = new File(basePath,filename);
|
||||
if (!f.exists()){
|
||||
System.err.println(String.format("ServiceRegisterRegistry: file not found: %s",filename));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (loadedFiles.contains(filename)){
|
||||
return true;
|
||||
}
|
||||
loadedFiles.add(filename);
|
||||
|
||||
try {
|
||||
DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
|
||||
Document xmlDoc = db.parse( f );
|
||||
|
||||
NodeList externalRegistryFiles = (NodeList)xpath.evaluate("/ServiceRegisterFile/ExternalRefs/File",xmlDoc, XPathConstants.NODESET);
|
||||
for (Node refFile: NodeListIterator.create(externalRegistryFiles)){
|
||||
if (!loadRegistryFile(refFile.getFirstChild().getNodeValue(),f.getParent())){
|
||||
System.err.println(String.format("ServiceRegisterRegistry: failed to load %s",refFile.getFirstChild().getNodeValue()));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
NodeList typesList = (NodeList)xpath.evaluate("ServiceRegisterFile/Types/Type", xmlDoc, XPathConstants.NODESET);
|
||||
for (Node typeNode: NodeListIterator.create(typesList)){
|
||||
typeRegistry.createType(typeNode);
|
||||
}
|
||||
|
||||
NodeList registerListsList = (NodeList)xpath.evaluate("ServiceRegisterFile/RegisterList", xmlDoc, XPathConstants.NODESET);
|
||||
for (Node registerListNode: NodeListIterator.create(registerListsList)){
|
||||
listRegistry.createServiceRegisterList(registerListNode);
|
||||
}
|
||||
|
||||
} catch (Exception e){
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
System.err.println(String.format("ServiceRegisterRegistry: loaded %s", f.getAbsolutePath()));
|
||||
return true;
|
||||
}
|
||||
|
||||
public XPath getXpath() {
|
||||
return xpath;
|
||||
}
|
||||
|
||||
public RegisterTypeRegistry getTypeRegistry() {
|
||||
return typeRegistry;
|
||||
}
|
||||
public ServiceRegisterListRegistry getListRegistry() {
|
||||
return listRegistry;
|
||||
}
|
||||
|
||||
|
||||
public static void main(String[] args){
|
||||
|
||||
ServiceRegisterRegistry registry = new ServiceRegisterRegistry();
|
||||
registry.loadRegistryFile("/data/src/hf3/service-registers/serviceregisters.xml");
|
||||
|
||||
}
|
||||
}
|
|
@ -1,48 +0,0 @@
|
|||
package org.hwo.servicelink.register.basetypes;
|
||||
|
||||
import javax.xml.xpath.XPathConstants;
|
||||
import org.hwo.servicelink.register.RegisterType;
|
||||
import org.hwo.servicelink.register.ServiceRegisterList;
|
||||
import org.w3c.dom.Node;
|
||||
|
||||
public class BitField extends RegisterType{
|
||||
|
||||
org.hwo.bitfields.BitField bitfield;
|
||||
|
||||
public BitField() {
|
||||
super("BitField");
|
||||
this.bitfield = new org.hwo.bitfields.BitField();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void createFromTypeNode(Node typeNode) {
|
||||
try {
|
||||
Node fieldsNode = (Node)getXPath().evaluate("Fields", typeNode, XPathConstants.NODE);
|
||||
this.bitfield.fieldsFromXML(fieldsNode);
|
||||
} catch (Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDisplayValue(Object regValue) {
|
||||
return this.bitfield.toText(valueAsInteger(regValue));
|
||||
}
|
||||
|
||||
@Override
|
||||
public RegisterType createRegisterTypeInstance(ServiceRegisterList serviceRegisterList, Node registerNode) {
|
||||
RegisterType rt = super.createRegisterTypeInstance(serviceRegisterList, registerNode);
|
||||
try {
|
||||
Node fieldsNode = (Node)getXPath().evaluate("Fields", registerNode, XPathConstants.NODE);
|
||||
if (fieldsNode != null){
|
||||
bitfield.fieldsFromXML(fieldsNode);
|
||||
}
|
||||
} catch (Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return rt;
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -1,17 +0,0 @@
|
|||
package org.hwo.servicelink.register.basetypes;
|
||||
|
||||
import org.hwo.servicelink.register.RegisterType;
|
||||
|
||||
public class Float extends RegisterType {
|
||||
|
||||
public Float(){
|
||||
super("Float");
|
||||
setWantsFloat(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDisplayValue(Object regValue) {
|
||||
return String.format("[0x%08x] %f", java.lang.Float.floatToIntBits(valueAsFloat(regValue)),valueAsFloat(regValue));
|
||||
}
|
||||
|
||||
}
|
|
@ -1,56 +0,0 @@
|
|||
package org.hwo.servicelink.register.basetypes;
|
||||
|
||||
import java.util.Hashtable;
|
||||
|
||||
import javax.xml.xpath.XPathConstants;
|
||||
|
||||
import org.hwo.servicelink.register.RegisterType;
|
||||
import org.hwo.xml.NodeListIterator;
|
||||
import org.w3c.dom.Element;
|
||||
import org.w3c.dom.Node;
|
||||
import org.w3c.dom.NodeList;
|
||||
|
||||
public class IndexedList extends RegisterType {
|
||||
|
||||
Hashtable<java.lang.Integer, String> listItems;
|
||||
|
||||
public IndexedList(){
|
||||
super("IndexedList");
|
||||
listItems = new Hashtable<java.lang.Integer, String>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void createFromTypeNode(Node typeNode) {
|
||||
try {
|
||||
NodeList itemNodes = (NodeList)getXPath().evaluate("List/Item", typeNode, XPathConstants.NODESET);
|
||||
for (Node itemNode: NodeListIterator.create(itemNodes)){
|
||||
Element itemElement = (Element)itemNode;
|
||||
|
||||
java.lang.Integer ind = java.lang.Integer.parseInt( itemElement.getAttribute("index") );
|
||||
listItems.put(ind, itemElement.getFirstChild().getNodeValue());
|
||||
}
|
||||
|
||||
} catch (Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
String getListItem(java.lang.Integer index){
|
||||
return listItems.get(index);
|
||||
}
|
||||
|
||||
Hashtable<java.lang.Integer, String> getListItems(){
|
||||
return listItems;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String getDisplayValue(Object regValue) {
|
||||
if (listItems.containsKey(valueAsInteger(regValue))){
|
||||
return listItems.get(valueAsInteger(regValue));
|
||||
} else {
|
||||
return String.format("(%d)", valueAsInteger(regValue));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -1,15 +0,0 @@
|
|||
package org.hwo.servicelink.register.basetypes;
|
||||
|
||||
import org.hwo.servicelink.register.RegisterType;
|
||||
|
||||
public class Integer extends RegisterType {
|
||||
|
||||
public Integer(){
|
||||
super("Integer");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDisplayValue(Object regValue) {
|
||||
return String.format("[0x%08x] %d", valueAsInteger(regValue),valueAsInteger(regValue));
|
||||
}
|
||||
}
|
|
@ -1,122 +0,0 @@
|
|||
package org.hwo.ui.servicelink;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.hwo.ui.diagram.PlotProvider;
|
||||
import org.hwo.ui.servicelink.register.ServiceRegister;
|
||||
|
||||
public class ServiceLinkPlotProvider implements PlotProvider{
|
||||
|
||||
public class Plot{
|
||||
ServiceRegister serviceRegister;
|
||||
Float[] values;
|
||||
|
||||
public Plot(ServiceRegister serviceRegister){
|
||||
this.serviceRegister = serviceRegister;
|
||||
this.values = new Float[samples];
|
||||
}
|
||||
|
||||
public Float[] getSamples(){
|
||||
return this.values;
|
||||
}
|
||||
|
||||
public void sample(int pos){
|
||||
if (this.serviceRegister.getAsFloat())
|
||||
this.values[pos] = this.serviceRegister.readFloatValueSynchron();
|
||||
else
|
||||
this.values[pos] = this.serviceRegister.readIntegerValue().floatValue();
|
||||
}
|
||||
|
||||
public void resize(){
|
||||
this.values = new Float[samples];
|
||||
}
|
||||
}
|
||||
|
||||
private int samples;
|
||||
private List<Plot> plots;
|
||||
|
||||
private int samplePosition;
|
||||
|
||||
public ServiceLinkPlotProvider() {
|
||||
this.samples = 128;
|
||||
this.samplePosition = 0;
|
||||
this.plots = new ArrayList<ServiceLinkPlotProvider.Plot>();
|
||||
}
|
||||
|
||||
public synchronized void resize(int size){
|
||||
|
||||
this.samples = size;
|
||||
this.samplePosition = 0;
|
||||
|
||||
for (Plot p: this.plots){
|
||||
p.resize();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public synchronized void addPlot(ServiceRegister sr){
|
||||
Plot p = new Plot(sr);
|
||||
|
||||
this.plots.add(p);
|
||||
}
|
||||
|
||||
public synchronized void sample(){
|
||||
for (Plot p: this.plots){
|
||||
p.sample(this.samplePosition);
|
||||
}
|
||||
|
||||
this.samplePosition++;
|
||||
if (this.samplePosition == this.samples)
|
||||
this.samplePosition = 0;
|
||||
|
||||
for (Plot p: this.plots){
|
||||
p.getSamples()[this.samplePosition] = null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized int getPoints() {
|
||||
return this.samples;
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized int getPlots() {
|
||||
return this.plots.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized Float[][] getMatrix() {
|
||||
Float[][] matrix = new Float[this.plots.size()][];
|
||||
|
||||
for (int i=0;i<this.plots.size();i++){
|
||||
matrix[i] = this.plots.get(i).values;
|
||||
}
|
||||
|
||||
return matrix;
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized String getLabel(int plot) {
|
||||
return this.plots.get(plot).serviceRegister.getRegisterName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized String getPointLabel(int point) {
|
||||
return String.format("%d",point);
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized Color[] getColors() {
|
||||
Color[] cl = new Color[this.plots.size()];
|
||||
for (int i=0;i<this.plots.size();i++){
|
||||
cl[i] = Color.BLACK;
|
||||
}
|
||||
return cl;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -1,57 +0,0 @@
|
|||
package org.hwo.ui.servicelink.register;
|
||||
|
||||
import javax.swing.JComponent;
|
||||
|
||||
import org.hwo.bitfields.BitField;
|
||||
import org.hwo.servicelink.ng.ServiceLink;
|
||||
import org.hwo.bitfields.ui.BitFieldEditor;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
public class BitFieldServiceRegister extends ServiceRegister {
|
||||
|
||||
BitField bitField;
|
||||
BitFieldEditor
|
||||
editor;
|
||||
|
||||
|
||||
public BitFieldServiceRegister(ServiceLink serviceLink) {
|
||||
super(serviceLink,false);
|
||||
|
||||
editor = new BitFieldEditor();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setRegisterNode(Element registerNode) {
|
||||
super.setRegisterNode(registerNode);
|
||||
|
||||
Element fieldsNode = (Element)registerNode.getElementsByTagName("Fields").item(0);
|
||||
bitField = new BitField(fieldsNode);
|
||||
|
||||
editor.setBitField(bitField);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String getTextRepresentation() {
|
||||
if (bitField == null)
|
||||
return "-";
|
||||
return bitField.toText(readIntegerValue());
|
||||
}
|
||||
|
||||
@Override
|
||||
public JComponent getEditorComponent() {
|
||||
return editor;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void acceptEditorValue() {
|
||||
writeIntegerValue( editor.getIntValue() );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateEditorValue() {
|
||||
editor.setIntValue( readIntegerValue() );
|
||||
}
|
||||
|
||||
}
|
|
@ -1,58 +0,0 @@
|
|||
package org.hwo.ui.servicelink.register;
|
||||
|
||||
import java.awt.Component;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.JTextField;
|
||||
|
||||
import java.awt.GridBagLayout;
|
||||
import java.awt.GridBagConstraints;
|
||||
|
||||
public class FloatRegisterEditor extends JPanel implements ServiceRegisterControl {
|
||||
|
||||
private ServiceRegister serviceRegister;
|
||||
private JTextField tfValue;
|
||||
|
||||
public FloatRegisterEditor()
|
||||
{
|
||||
GridBagLayout gridBagLayout = new GridBagLayout();
|
||||
gridBagLayout.columnWidths = new int[]{0, 0};
|
||||
gridBagLayout.rowHeights = new int[]{0, 0};
|
||||
gridBagLayout.columnWeights = new double[]{1.0, Double.MIN_VALUE};
|
||||
gridBagLayout.rowWeights = new double[]{0.0, Double.MIN_VALUE};
|
||||
setLayout(gridBagLayout);
|
||||
|
||||
tfValue = new JTextField();
|
||||
GridBagConstraints gbc_tfValue = new GridBagConstraints();
|
||||
gbc_tfValue.fill = GridBagConstraints.HORIZONTAL;
|
||||
gbc_tfValue.gridx = 0;
|
||||
gbc_tfValue.gridy = 0;
|
||||
add(tfValue, gbc_tfValue);
|
||||
tfValue.setColumns(10);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Component getComponent() {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setServiceRegister(ServiceRegister serviceRegister) {
|
||||
this.serviceRegister = serviceRegister;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeValue() {
|
||||
this.serviceRegister.writeFloatValue(Float.parseFloat(tfValue.getText()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readValue() {
|
||||
tfValue.setText(this.serviceRegister.readFloatValue().toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean requestFocusInWindow() {
|
||||
return tfValue.requestFocusInWindow();
|
||||
}
|
||||
|
||||
}
|
|
@ -1,41 +0,0 @@
|
|||
package org.hwo.ui.servicelink.register;
|
||||
|
||||
import javax.swing.JComponent;
|
||||
|
||||
import org.hwo.servicelink.ng.ServiceLink;
|
||||
|
||||
public class FloatServiceRegister extends ServiceRegister {
|
||||
|
||||
FloatRegisterEditor editor;
|
||||
|
||||
public FloatServiceRegister(ServiceLink serviceLink)
|
||||
{
|
||||
super(serviceLink,true);
|
||||
editor = new FloatRegisterEditor();
|
||||
editor.setServiceRegister(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getTextRepresentation() {
|
||||
Float f = readFloatValue();
|
||||
if (f == null)
|
||||
return "";
|
||||
return String.format("0x%08X %f",Float.floatToIntBits(f),f);
|
||||
}
|
||||
|
||||
@Override
|
||||
public JComponent getEditorComponent() {
|
||||
return editor;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void acceptEditorValue() {
|
||||
editor.writeValue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateEditorValue() {
|
||||
editor.readValue();
|
||||
}
|
||||
|
||||
}
|
|
@ -1,117 +0,0 @@
|
|||
package org.hwo.ui.servicelink.register;
|
||||
|
||||
import java.awt.Component;
|
||||
import javax.swing.JPanel;
|
||||
import org.hwo.ui.JComboBoxEx;
|
||||
|
||||
import java.awt.GridBagLayout;
|
||||
import java.awt.GridBagConstraints;
|
||||
|
||||
public class IndexedListRegisterEditor extends JPanel implements ServiceRegisterControl {
|
||||
|
||||
public static class IndexedItem {
|
||||
private Integer index;
|
||||
private String label;
|
||||
|
||||
public IndexedItem(Integer index,String label) {
|
||||
this.index = index;
|
||||
this.label = label;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("%d: %s", this.index, this.label);
|
||||
}
|
||||
|
||||
public Integer getIndex() {
|
||||
return index;
|
||||
}
|
||||
|
||||
public void setIndex(Integer index) {
|
||||
this.index = index;
|
||||
}
|
||||
|
||||
public String getLabel() {
|
||||
return label;
|
||||
}
|
||||
|
||||
public void setLabel(String label) {
|
||||
this.label = label;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
private ServiceRegister serviceRegister;
|
||||
private JComboBoxEx cbValue;
|
||||
|
||||
private IndexedItem[] items;
|
||||
|
||||
|
||||
public IndexedListRegisterEditor()
|
||||
{
|
||||
super();
|
||||
GridBagLayout gridBagLayout = new GridBagLayout();
|
||||
gridBagLayout.columnWidths = new int[]{0, 0};
|
||||
gridBagLayout.rowHeights = new int[]{0, 0};
|
||||
gridBagLayout.columnWeights = new double[]{1.0, Double.MIN_VALUE};
|
||||
gridBagLayout.rowWeights = new double[]{0.0, Double.MIN_VALUE};
|
||||
setLayout(gridBagLayout);
|
||||
|
||||
cbValue = new JComboBoxEx();
|
||||
GridBagConstraints gbc_tfValue = new GridBagConstraints();
|
||||
gbc_tfValue.fill = GridBagConstraints.HORIZONTAL;
|
||||
gbc_tfValue.gridx = 0;
|
||||
gbc_tfValue.gridy = 0;
|
||||
add(cbValue, gbc_tfValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Component getComponent() {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setServiceRegister(ServiceRegister serviceRegister) {
|
||||
this.serviceRegister = serviceRegister;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeValue() {
|
||||
this.serviceRegister.writeIntegerValue(((IndexedItem)cbValue.getSelectedItem()).getIndex());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readValue() {
|
||||
Integer i = this.serviceRegister.readIntegerValue();
|
||||
if ( i == null )
|
||||
i = 0;
|
||||
|
||||
for (IndexedItem item : items) {
|
||||
if (item.getIndex().equals(i)) {
|
||||
cbValue.setSelectedItem(item);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
IndexedItem item = new IndexedItem(i, "Unkown Index");
|
||||
cbValue.setSelectedItem(item);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean requestFocusInWindow() {
|
||||
return cbValue.requestFocusInWindow();
|
||||
}
|
||||
|
||||
public IndexedItem[] getItems() {
|
||||
return items;
|
||||
}
|
||||
|
||||
public void setItems(IndexedItem[] items) {
|
||||
this.items = items;
|
||||
|
||||
cbValue.removeAllItems();
|
||||
for (IndexedItem item: items){
|
||||
cbValue.addItem(item);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,74 +0,0 @@
|
|||
package org.hwo.ui.servicelink.register;
|
||||
|
||||
import java.util.Hashtable;
|
||||
import java.util.LinkedList;
|
||||
|
||||
import javax.swing.JComponent;
|
||||
|
||||
import org.hwo.servicelink.ng.ServiceLink;
|
||||
import org.hwo.ui.servicelink.register.IndexedListRegisterEditor.IndexedItem;
|
||||
import org.hwo.xml.NodeListIterator;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
public class IndexedListServiceRegister extends ServiceRegister {
|
||||
|
||||
IndexedListRegisterEditor editor;
|
||||
Hashtable<Integer, IndexedItem> items;
|
||||
|
||||
public IndexedListServiceRegister(ServiceLink serviceLink)
|
||||
{
|
||||
super(serviceLink,false);
|
||||
editor = new IndexedListRegisterEditor();
|
||||
editor.setServiceRegister(this);
|
||||
|
||||
items = new Hashtable<Integer, IndexedListRegisterEditor.IndexedItem>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setRegisterNode(Element registerNode) {
|
||||
super.setRegisterNode(registerNode);
|
||||
|
||||
Element listNode = (Element)registerNode.getElementsByTagName("List").item(0);
|
||||
|
||||
LinkedList<IndexedItem> items = new LinkedList<IndexedListRegisterEditor.IndexedItem>();
|
||||
|
||||
for (Element listItem: new NodeListIterator(listNode.getElementsByTagName("Item"))){
|
||||
IndexedItem item = new IndexedItem(Integer.parseInt(listItem.getAttribute("index")), listItem.getTextContent());
|
||||
items.add(item);
|
||||
this.items.put(item.getIndex(), item);
|
||||
}
|
||||
editor.setItems(items.toArray(new IndexedItem[0]));
|
||||
}
|
||||
|
||||
private IndexedItem getIndexedItem(Integer index) {
|
||||
if (this.items.containsKey(index)) {
|
||||
return this.items.get(index);
|
||||
}
|
||||
IndexedItem unknown = new IndexedItem(index, "Unkown");
|
||||
return unknown;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getTextRepresentation() {
|
||||
Integer i = readIntegerValue();
|
||||
if (i == null)
|
||||
return "";
|
||||
return getIndexedItem(i).toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public JComponent getEditorComponent() {
|
||||
return editor;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void acceptEditorValue() {
|
||||
editor.writeValue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateEditorValue() {
|
||||
editor.readValue();
|
||||
}
|
||||
|
||||
}
|
|
@ -1,61 +0,0 @@
|
|||
package org.hwo.ui.servicelink.register;
|
||||
|
||||
import java.awt.Component;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.JTextField;
|
||||
|
||||
import java.awt.GridBagLayout;
|
||||
import java.awt.GridBagConstraints;
|
||||
|
||||
public class IntegerRegisterEditor extends JPanel implements ServiceRegisterControl {
|
||||
|
||||
private ServiceRegister serviceRegister;
|
||||
private JTextField tfValue;
|
||||
|
||||
public IntegerRegisterEditor()
|
||||
{
|
||||
super();
|
||||
GridBagLayout gridBagLayout = new GridBagLayout();
|
||||
gridBagLayout.columnWidths = new int[]{0, 0};
|
||||
gridBagLayout.rowHeights = new int[]{0, 0};
|
||||
gridBagLayout.columnWeights = new double[]{1.0, Double.MIN_VALUE};
|
||||
gridBagLayout.rowWeights = new double[]{0.0, Double.MIN_VALUE};
|
||||
setLayout(gridBagLayout);
|
||||
|
||||
tfValue = new JTextField();
|
||||
GridBagConstraints gbc_tfValue = new GridBagConstraints();
|
||||
gbc_tfValue.fill = GridBagConstraints.HORIZONTAL;
|
||||
gbc_tfValue.gridx = 0;
|
||||
gbc_tfValue.gridy = 0;
|
||||
add(tfValue, gbc_tfValue);
|
||||
tfValue.setColumns(10);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Component getComponent() {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setServiceRegister(ServiceRegister serviceRegister) {
|
||||
this.serviceRegister = serviceRegister;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeValue() {
|
||||
this.serviceRegister.writeIntegerValue(Integer.decode(tfValue.getText()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readValue() {
|
||||
Integer i = this.serviceRegister.readIntegerValue();
|
||||
if ( i == null )
|
||||
i = 0;
|
||||
tfValue.setText(i.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean requestFocusInWindow() {
|
||||
return tfValue.requestFocusInWindow();
|
||||
}
|
||||
}
|
|
@ -1,41 +0,0 @@
|
|||
package org.hwo.ui.servicelink.register;
|
||||
|
||||
import javax.swing.JComponent;
|
||||
|
||||
import org.hwo.servicelink.ng.ServiceLink;
|
||||
|
||||
public class IntegerServiceRegister extends ServiceRegister {
|
||||
|
||||
IntegerRegisterEditor editor;
|
||||
|
||||
public IntegerServiceRegister(ServiceLink serviceLink)
|
||||
{
|
||||
super(serviceLink,false);
|
||||
editor = new IntegerRegisterEditor();
|
||||
editor.setServiceRegister(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getTextRepresentation() {
|
||||
Integer i = readIntegerValue();
|
||||
if (i == null)
|
||||
return "";
|
||||
return String.format("0x%08x %d", i, i);
|
||||
}
|
||||
|
||||
@Override
|
||||
public JComponent getEditorComponent() {
|
||||
return editor;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void acceptEditorValue() {
|
||||
editor.writeValue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateEditorValue() {
|
||||
editor.readValue();
|
||||
}
|
||||
|
||||
}
|
|
@ -1,159 +0,0 @@
|
|||
package org.hwo.ui.servicelink.register;
|
||||
|
||||
import javax.swing.JPanel;
|
||||
|
||||
import java.awt.GridBagLayout;
|
||||
|
||||
import javax.swing.JLabel;
|
||||
|
||||
import java.awt.GridBagConstraints;
|
||||
|
||||
import javax.swing.JSpinner;
|
||||
|
||||
import java.awt.Insets;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import javax.swing.SpinnerNumberModel;
|
||||
import javax.swing.event.ChangeListener;
|
||||
import javax.swing.event.ChangeEvent;
|
||||
|
||||
public class JServiceRegisterSourceEditor extends JPanel {
|
||||
private JSpinner spRegister;
|
||||
private JSpinner spNode;
|
||||
private JSpinner spAx;
|
||||
|
||||
private List<ActionListener> actionListeners;
|
||||
|
||||
private ServiceRegister serviceRegister;
|
||||
|
||||
/**
|
||||
* Create the panel.
|
||||
*/
|
||||
public JServiceRegisterSourceEditor() {
|
||||
GridBagLayout gridBagLayout = new GridBagLayout();
|
||||
gridBagLayout.columnWidths = new int[]{0, 0, 0, 0, 0, 0, 0};
|
||||
gridBagLayout.rowHeights = new int[]{0, 0};
|
||||
gridBagLayout.columnWeights = new double[]{0.0, 1.0, 0.0, 1.0, 0.0, 1.0, Double.MIN_VALUE};
|
||||
gridBagLayout.rowWeights = new double[]{1.0, Double.MIN_VALUE};
|
||||
setLayout(gridBagLayout);
|
||||
|
||||
JLabel lblAx = new JLabel("AX:");
|
||||
GridBagConstraints gbc_lblAx = new GridBagConstraints();
|
||||
gbc_lblAx.insets = new Insets(0, 0, 0, 5);
|
||||
gbc_lblAx.gridx = 0;
|
||||
gbc_lblAx.gridy = 0;
|
||||
add(lblAx, gbc_lblAx);
|
||||
|
||||
spAx = new JSpinner();
|
||||
spAx.addChangeListener(new ChangeListener() {
|
||||
public void stateChanged(ChangeEvent e) {
|
||||
fireAction();
|
||||
}
|
||||
});
|
||||
spAx.setModel(new SpinnerNumberModel(0, 0, 14, 1));
|
||||
GridBagConstraints gbc_spAx = new GridBagConstraints();
|
||||
gbc_spAx.fill = GridBagConstraints.HORIZONTAL;
|
||||
gbc_spAx.insets = new Insets(0, 0, 0, 5);
|
||||
gbc_spAx.gridx = 1;
|
||||
gbc_spAx.gridy = 0;
|
||||
add(spAx, gbc_spAx);
|
||||
|
||||
JLabel lblNode = new JLabel("NODE:");
|
||||
GridBagConstraints gbc_lblNode = new GridBagConstraints();
|
||||
gbc_lblNode.insets = new Insets(0, 0, 0, 5);
|
||||
gbc_lblNode.gridx = 2;
|
||||
gbc_lblNode.gridy = 0;
|
||||
add(lblNode, gbc_lblNode);
|
||||
|
||||
spNode = new JSpinner();
|
||||
spNode.addChangeListener(new ChangeListener() {
|
||||
public void stateChanged(ChangeEvent e) {
|
||||
fireAction();
|
||||
}
|
||||
});
|
||||
spNode.setModel(new SpinnerNumberModel(0, 0, 15, 1));
|
||||
GridBagConstraints gbc_spNode = new GridBagConstraints();
|
||||
gbc_spNode.fill = GridBagConstraints.HORIZONTAL;
|
||||
gbc_spNode.insets = new Insets(0, 0, 0, 5);
|
||||
gbc_spNode.gridx = 3;
|
||||
gbc_spNode.gridy = 0;
|
||||
add(spNode, gbc_spNode);
|
||||
|
||||
JLabel lblRegister = new JLabel("REGISTER:");
|
||||
GridBagConstraints gbc_lblRegister = new GridBagConstraints();
|
||||
gbc_lblRegister.insets = new Insets(0, 0, 0, 5);
|
||||
gbc_lblRegister.gridx = 4;
|
||||
gbc_lblRegister.gridy = 0;
|
||||
add(lblRegister, gbc_lblRegister);
|
||||
|
||||
spRegister = new JSpinner();
|
||||
spRegister.addChangeListener(new ChangeListener() {
|
||||
public void stateChanged(ChangeEvent e) {
|
||||
fireAction();
|
||||
}
|
||||
});
|
||||
spRegister.setModel(new SpinnerNumberModel(0, 0, 65535, 1));
|
||||
GridBagConstraints gbc_spRegister = new GridBagConstraints();
|
||||
gbc_spRegister.fill = GridBagConstraints.HORIZONTAL;
|
||||
gbc_spRegister.gridx = 5;
|
||||
gbc_spRegister.gridy = 0;
|
||||
add(spRegister, gbc_spRegister);
|
||||
|
||||
this.initialize();
|
||||
}
|
||||
|
||||
private void initialize(){
|
||||
actionListeners = new LinkedList<ActionListener>();
|
||||
}
|
||||
|
||||
public void addActionListener(ActionListener actionListener){
|
||||
actionListeners.add(actionListener);
|
||||
}
|
||||
public void removeActionListener(ActionListener actionListener){
|
||||
actionListeners.remove(actionListener);
|
||||
}
|
||||
|
||||
public ServiceRegister getServiceRegister(){
|
||||
return this.serviceRegister;
|
||||
}
|
||||
public void setServiceRegister(ServiceRegister serviceRegister){
|
||||
this.serviceRegister = serviceRegister;
|
||||
}
|
||||
|
||||
void fireAction(){
|
||||
|
||||
if (serviceRegister != null){
|
||||
serviceRegister.setAx(getAx());
|
||||
serviceRegister.setNode(getNode());
|
||||
serviceRegister.setRegister(getRegister());
|
||||
}
|
||||
|
||||
for (ActionListener l:actionListeners)
|
||||
l.actionPerformed(new ActionEvent(this, 0, ""));
|
||||
}
|
||||
|
||||
public Integer getAx(){
|
||||
return (Integer)spAx.getValue();
|
||||
}
|
||||
public void setAx(Integer value){
|
||||
spAx.setValue(value);
|
||||
}
|
||||
|
||||
public Integer getNode(){
|
||||
return (Integer)spNode.getValue();
|
||||
}
|
||||
public void setNode(Integer value){
|
||||
spNode.setValue(value);
|
||||
}
|
||||
|
||||
public Integer getRegister(){
|
||||
return (Integer)spRegister.getValue();
|
||||
}
|
||||
public void setRegister(Integer value){
|
||||
spAx.setValue(value);
|
||||
}
|
||||
|
||||
}
|
|
@ -1,232 +0,0 @@
|
|||
package org.hwo.ui.servicelink.register;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.HashMap;
|
||||
import javax.swing.JComponent;
|
||||
|
||||
import org.hwo.i18n.Messages;
|
||||
import org.hwo.interactiveobjects.InteractiveObject;
|
||||
import org.hwo.servicelink.ng.ServiceLink;
|
||||
import org.hwo.servicelink.ng.ServiceRegisterListener;
|
||||
import org.hwo.models.TableMapper.TableColumn;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
@InteractiveObject(editor=ServiceRegisterEditorDialog.class)
|
||||
public abstract class ServiceRegister implements ServiceRegisterListener {
|
||||
|
||||
static HashMap<String, Class<? extends ServiceRegister>> serviceRegisterTypes;
|
||||
public static void addServiceRegisterType(String typename,Class<? extends ServiceRegister> clazz)
|
||||
{
|
||||
serviceRegisterTypes.put(typename, clazz);
|
||||
}
|
||||
|
||||
public static ServiceRegister createServiceRegister(ServiceLink serviceLink,String typeName)
|
||||
{
|
||||
ServiceRegister sr = null;
|
||||
|
||||
Class<? extends ServiceRegister> clazz = serviceRegisterTypes.get(typeName);
|
||||
if (clazz != null)
|
||||
{
|
||||
try {
|
||||
sr = clazz.getDeclaredConstructor(ServiceLink.class).newInstance(serviceLink);
|
||||
} catch (InstantiationException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
} catch (InvocationTargetException e) {
|
||||
e.printStackTrace();
|
||||
} catch (NoSuchMethodException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return sr;
|
||||
}
|
||||
public static ServiceRegister createServiceRegister(ServiceLink serviceLink,Element registerNode)
|
||||
{
|
||||
ServiceRegister sr = null;
|
||||
String registerType = registerNode.getAttribute("type");
|
||||
|
||||
if (registerType == null || registerType.equals(""))
|
||||
registerType = "Integer";
|
||||
|
||||
sr = createServiceRegister(serviceLink, registerType);
|
||||
if (sr != null)
|
||||
{
|
||||
sr.setRegisterNode(registerNode);
|
||||
}
|
||||
return sr;
|
||||
}
|
||||
|
||||
private Element registerNode;
|
||||
|
||||
private ServiceLink serviceLink;
|
||||
private Integer ax,
|
||||
node,
|
||||
register;
|
||||
|
||||
private boolean asFloat;
|
||||
|
||||
private String registerName;
|
||||
|
||||
protected Integer lastAsyncIntValue;
|
||||
protected Float lastAsyncFloatValue;
|
||||
|
||||
public ServiceRegister(ServiceLink serviceLink,boolean asFloat)
|
||||
{
|
||||
this.serviceLink = serviceLink;
|
||||
this.asFloat = asFloat;
|
||||
}
|
||||
|
||||
public void dispose(){
|
||||
removeServiceRegisterListener();
|
||||
}
|
||||
|
||||
private void removeServiceRegisterListener(){
|
||||
if ((this.serviceLink != null)&&(ax != null)&&(node != null)&&(register != null))
|
||||
this.serviceLink.getAsynchronServiceLinkProvider().removeServiceRegisterListener(this.ax, this.node, this.register, this.asFloat, this);
|
||||
}
|
||||
private void addServiceRegisterListener(){
|
||||
if ((this.serviceLink != null) &&(ax != null)&&(node != null)&&(register != null))
|
||||
this.serviceLink.getAsynchronServiceLinkProvider().addServiceRegisterListener(this.ax, this.node, this.register, this.asFloat, this);
|
||||
}
|
||||
|
||||
|
||||
@TableColumn(label="Bezeichnung",width=400,order=5)
|
||||
public String getRegisterName() {
|
||||
return registerName;
|
||||
}
|
||||
public void setRegisterName(String registerName) {
|
||||
this.registerName = registerName;
|
||||
}
|
||||
|
||||
@TableColumn(label="Register",firstColumn=true,width=80)
|
||||
public Integer getRegister() {
|
||||
return register;
|
||||
}
|
||||
public void setRegister(Integer register) {
|
||||
removeServiceRegisterListener();
|
||||
this.register = register;
|
||||
addServiceRegisterListener();
|
||||
}
|
||||
|
||||
@TableColumn(label="Wert",order=10,width=250)
|
||||
abstract public String getTextRepresentation();
|
||||
|
||||
abstract public JComponent getEditorComponent();
|
||||
abstract public void acceptEditorValue();
|
||||
abstract public void updateEditorValue();
|
||||
|
||||
public Element getRegisterNode()
|
||||
{
|
||||
return registerNode;
|
||||
};
|
||||
public void setRegisterNode(Element registerNode)
|
||||
{
|
||||
this.registerNode = registerNode;
|
||||
|
||||
if (this.registerNode != null)
|
||||
{
|
||||
Element labelNode = (Element)this.registerNode.getElementsByTagName("Label").item(0);
|
||||
Element addressNode = (Element)this.registerNode.getElementsByTagName("Address").item(0);
|
||||
|
||||
register = Integer.decode(addressNode.getTextContent());
|
||||
registerName = Messages.getString(labelNode.getTextContent());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void setAsFloat(boolean asFloat) {
|
||||
removeServiceRegisterListener();
|
||||
this.asFloat = asFloat;
|
||||
addServiceRegisterListener();
|
||||
}
|
||||
public boolean getAsFloat() {
|
||||
return this.asFloat;
|
||||
}
|
||||
|
||||
|
||||
public ServiceLink getServiceLink() {
|
||||
return serviceLink;
|
||||
}
|
||||
public void setServiceLink(ServiceLink serviceLink) {
|
||||
this.serviceLink = serviceLink;
|
||||
}
|
||||
|
||||
|
||||
public Integer getAx() {
|
||||
return ax;
|
||||
}
|
||||
public void setAx(Integer ax) {
|
||||
removeServiceRegisterListener();
|
||||
this.ax = ax;
|
||||
addServiceRegisterListener();
|
||||
}
|
||||
|
||||
|
||||
public Integer getNode() {
|
||||
return node;
|
||||
}
|
||||
public void setNode(Integer node) {
|
||||
removeServiceRegisterListener();
|
||||
this.node = node;
|
||||
addServiceRegisterListener();
|
||||
}
|
||||
|
||||
public Float readFloatValue()
|
||||
{
|
||||
if (lastAsyncFloatValue != null)
|
||||
return lastAsyncFloatValue;
|
||||
return 0.0f;
|
||||
//return serviceLink.getServiceRegisterCache().getCachedFloat(ax, node, register);
|
||||
}
|
||||
public void writeFloatValue(Float value)
|
||||
{
|
||||
try
|
||||
{
|
||||
this.serviceLink.writeFloat(ax.byteValue(), node.byteValue(), register,value);
|
||||
} catch (Exception e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public Float readFloatValueSynchron()
|
||||
{
|
||||
return serviceLink.readFloat(ax.byteValue(), node.byteValue(), register);
|
||||
}
|
||||
|
||||
|
||||
public Integer readIntegerValue()
|
||||
{
|
||||
if (lastAsyncIntValue != null)
|
||||
return lastAsyncIntValue;
|
||||
return 0;
|
||||
//return serviceLink.getServiceRegisterCache().getCachedInteger(ax, node, register);
|
||||
}
|
||||
public void writeIntegerValue(Integer value)
|
||||
{
|
||||
this.serviceLink.writeInteger(ax.byteValue(), node.byteValue(), register,value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void ServiceRegisterValueUpdated(int ax, int node,
|
||||
int registervalue, Float value) {
|
||||
this.lastAsyncFloatValue = value;
|
||||
}
|
||||
@Override
|
||||
public void ServiceRegisterValueUpdated(int ax, int node,
|
||||
int registervalue, Integer value) {
|
||||
this.lastAsyncIntValue = value;
|
||||
}
|
||||
|
||||
|
||||
static {
|
||||
serviceRegisterTypes = new HashMap<String, Class<? extends ServiceRegister>>();
|
||||
|
||||
serviceRegisterTypes.put("Integer", IntegerServiceRegister.class);
|
||||
serviceRegisterTypes.put("Float", FloatServiceRegister.class);
|
||||
serviceRegisterTypes.put("BitField", BitFieldServiceRegister.class);
|
||||
serviceRegisterTypes.put("IndexedList", IndexedListServiceRegister.class);
|
||||
}
|
||||
|
||||
}
|
|
@ -1,16 +0,0 @@
|
|||
package org.hwo.ui.servicelink.register;
|
||||
|
||||
import java.awt.Component;
|
||||
|
||||
public interface ServiceRegisterControl {
|
||||
|
||||
public boolean requestFocusInWindow();
|
||||
|
||||
public Component getComponent();
|
||||
|
||||
public void setServiceRegister(ServiceRegister serviceRegister);
|
||||
|
||||
public void writeValue();
|
||||
public void readValue();
|
||||
|
||||
}
|
|
@ -1,260 +0,0 @@
|
|||
package org.hwo.ui.servicelink.register;
|
||||
|
||||
import java.awt.BorderLayout;
|
||||
import java.awt.FlowLayout;
|
||||
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.JDialog;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.border.EmptyBorder;
|
||||
|
||||
import org.hwo.interactiveobjects.IInteractiveObjectEditor;
|
||||
import org.hwo.ui.KeyStrokeHelper;
|
||||
import org.hwo.ui.KeyStrokeListener;
|
||||
import org.hwo.ui.KeyStrokeParameters;
|
||||
|
||||
import java.awt.event.ActionListener;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.awt.GridBagLayout;
|
||||
import java.awt.GridBagConstraints;
|
||||
import java.awt.Insets;
|
||||
|
||||
import javax.swing.border.TitledBorder;
|
||||
import javax.swing.JLabel;
|
||||
import javax.swing.JTextField;
|
||||
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{
|
||||
|
||||
private final JPanel contentPanel = new JPanel();
|
||||
|
||||
private ServiceRegister serviceRegister;
|
||||
private JTextField tfAx;
|
||||
private JTextField tfNode;
|
||||
private JTextField tfRegister;
|
||||
private JPanel panelEditorControl;
|
||||
private JTextField tfRegname;
|
||||
|
||||
private KeyStrokeHelper keyStrokeHelper;
|
||||
private JScrollPane scrollPane;
|
||||
|
||||
/**
|
||||
* Create the dialog.
|
||||
*/
|
||||
public ServiceRegisterEditorDialog() {
|
||||
addComponentListener(new ComponentAdapter() {
|
||||
@Override
|
||||
public void componentShown(ComponentEvent e) {
|
||||
if (serviceRegister != null)
|
||||
serviceRegister.getEditorComponent().requestFocusInWindow();
|
||||
}
|
||||
});
|
||||
setBounds(100, 100, 327, 318);
|
||||
getContentPane().setLayout(new BorderLayout());
|
||||
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
|
||||
getContentPane().add(contentPanel, BorderLayout.CENTER);
|
||||
GridBagLayout gbl_contentPanel = new GridBagLayout();
|
||||
gbl_contentPanel.columnWidths = new int[]{0, 0};
|
||||
gbl_contentPanel.rowHeights = new int[]{0, 0, 0};
|
||||
gbl_contentPanel.columnWeights = new double[]{1.0, Double.MIN_VALUE};
|
||||
gbl_contentPanel.rowWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
|
||||
contentPanel.setLayout(gbl_contentPanel);
|
||||
{
|
||||
JPanel panel = new JPanel();
|
||||
panel.setBorder(new TitledBorder(null, "Service Register", TitledBorder.LEADING, TitledBorder.TOP, null, null));
|
||||
GridBagConstraints gbc_panel = new GridBagConstraints();
|
||||
gbc_panel.insets = new Insets(0, 0, 5, 0);
|
||||
gbc_panel.fill = GridBagConstraints.BOTH;
|
||||
gbc_panel.gridx = 0;
|
||||
gbc_panel.gridy = 0;
|
||||
contentPanel.add(panel, gbc_panel);
|
||||
GridBagLayout gbl_panel = new GridBagLayout();
|
||||
gbl_panel.columnWidths = new int[]{0, 0, 0};
|
||||
gbl_panel.rowHeights = new int[]{0, 0, 0, 0, 0};
|
||||
gbl_panel.columnWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
|
||||
gbl_panel.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
|
||||
panel.setLayout(gbl_panel);
|
||||
{
|
||||
JLabel lblAchse = new JLabel("Achse:");
|
||||
GridBagConstraints gbc_lblAchse = new GridBagConstraints();
|
||||
gbc_lblAchse.insets = new Insets(0, 0, 5, 5);
|
||||
gbc_lblAchse.anchor = GridBagConstraints.EAST;
|
||||
gbc_lblAchse.gridx = 0;
|
||||
gbc_lblAchse.gridy = 0;
|
||||
panel.add(lblAchse, gbc_lblAchse);
|
||||
}
|
||||
{
|
||||
tfAx = new JTextField();
|
||||
tfAx.setEditable(false);
|
||||
GridBagConstraints gbc_tfAx = new GridBagConstraints();
|
||||
gbc_tfAx.insets = new Insets(0, 0, 5, 0);
|
||||
gbc_tfAx.fill = GridBagConstraints.HORIZONTAL;
|
||||
gbc_tfAx.gridx = 1;
|
||||
gbc_tfAx.gridy = 0;
|
||||
panel.add(tfAx, gbc_tfAx);
|
||||
tfAx.setColumns(10);
|
||||
}
|
||||
{
|
||||
JLabel lblKnoten = new JLabel("Knoten:");
|
||||
GridBagConstraints gbc_lblKnoten = new GridBagConstraints();
|
||||
gbc_lblKnoten.anchor = GridBagConstraints.EAST;
|
||||
gbc_lblKnoten.insets = new Insets(0, 0, 5, 5);
|
||||
gbc_lblKnoten.gridx = 0;
|
||||
gbc_lblKnoten.gridy = 1;
|
||||
panel.add(lblKnoten, gbc_lblKnoten);
|
||||
}
|
||||
{
|
||||
tfNode = new JTextField();
|
||||
tfNode.setEditable(false);
|
||||
GridBagConstraints gbc_tfNode = new GridBagConstraints();
|
||||
gbc_tfNode.insets = new Insets(0, 0, 5, 0);
|
||||
gbc_tfNode.fill = GridBagConstraints.HORIZONTAL;
|
||||
gbc_tfNode.gridx = 1;
|
||||
gbc_tfNode.gridy = 1;
|
||||
panel.add(tfNode, gbc_tfNode);
|
||||
tfNode.setColumns(10);
|
||||
}
|
||||
{
|
||||
JLabel lblRegister = new JLabel("Register:");
|
||||
GridBagConstraints gbc_lblRegister = new GridBagConstraints();
|
||||
gbc_lblRegister.anchor = GridBagConstraints.EAST;
|
||||
gbc_lblRegister.insets = new Insets(0, 0, 5, 5);
|
||||
gbc_lblRegister.gridx = 0;
|
||||
gbc_lblRegister.gridy = 2;
|
||||
panel.add(lblRegister, gbc_lblRegister);
|
||||
}
|
||||
{
|
||||
tfRegister = new JTextField();
|
||||
tfRegister.setEditable(false);
|
||||
GridBagConstraints gbc_tfRegister = new GridBagConstraints();
|
||||
gbc_tfRegister.insets = new Insets(0, 0, 5, 0);
|
||||
gbc_tfRegister.fill = GridBagConstraints.HORIZONTAL;
|
||||
gbc_tfRegister.gridx = 1;
|
||||
gbc_tfRegister.gridy = 2;
|
||||
panel.add(tfRegister, gbc_tfRegister);
|
||||
tfRegister.setColumns(10);
|
||||
}
|
||||
{
|
||||
JLabel lblNewLabel = new JLabel("Bezeichnung:");
|
||||
GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
|
||||
gbc_lblNewLabel.anchor = GridBagConstraints.EAST;
|
||||
gbc_lblNewLabel.insets = new Insets(0, 0, 0, 5);
|
||||
gbc_lblNewLabel.gridx = 0;
|
||||
gbc_lblNewLabel.gridy = 3;
|
||||
panel.add(lblNewLabel, gbc_lblNewLabel);
|
||||
}
|
||||
{
|
||||
tfRegname = new JTextField();
|
||||
tfRegname.setEditable(false);
|
||||
GridBagConstraints gbc_tfRegname = new GridBagConstraints();
|
||||
gbc_tfRegname.fill = GridBagConstraints.HORIZONTAL;
|
||||
gbc_tfRegname.gridx = 1;
|
||||
gbc_tfRegname.gridy = 3;
|
||||
panel.add(tfRegname, gbc_tfRegname);
|
||||
tfRegname.setColumns(10);
|
||||
}
|
||||
}
|
||||
{
|
||||
panelEditorControl = new JPanel();
|
||||
GridBagConstraints gbc_panelEditorControl = new GridBagConstraints();
|
||||
gbc_panelEditorControl.fill = GridBagConstraints.BOTH;
|
||||
gbc_panelEditorControl.gridx = 0;
|
||||
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();
|
||||
buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
|
||||
getContentPane().add(buttonPane, BorderLayout.SOUTH);
|
||||
{
|
||||
JButton okButton = new JButton("OK");
|
||||
okButton.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent arg0) {
|
||||
accept();
|
||||
}
|
||||
});
|
||||
okButton.setActionCommand("OK");
|
||||
buttonPane.add(okButton);
|
||||
getRootPane().setDefaultButton(okButton);
|
||||
}
|
||||
{
|
||||
JButton cancelButton = new JButton("Cancel");
|
||||
cancelButton.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
cancel();
|
||||
}
|
||||
});
|
||||
cancelButton.setActionCommand("Cancel");
|
||||
buttonPane.add(cancelButton);
|
||||
}
|
||||
}
|
||||
initialize();
|
||||
}
|
||||
|
||||
private void initialize()
|
||||
{
|
||||
keyStrokeHelper = new KeyStrokeHelper(this.getRootPane());
|
||||
keyStrokeHelper.setFocusedWindow(true);
|
||||
keyStrokeHelper.registerKeyStrokeListener(new KeyStrokeListener() {
|
||||
|
||||
@Override
|
||||
public void keyStroke(KeyStrokeParameters parameters) {
|
||||
switch (parameters.getKeyCode())
|
||||
{
|
||||
case KeyEvent.VK_ESCAPE:
|
||||
cancel();
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
keyStrokeHelper.addKeyCode(KeyEvent.VK_ESCAPE);
|
||||
}
|
||||
|
||||
private void accept()
|
||||
{
|
||||
serviceRegister.acceptEditorValue();
|
||||
setVisible(false);
|
||||
}
|
||||
|
||||
private void cancel()
|
||||
{
|
||||
setVisible(false);
|
||||
}
|
||||
|
||||
private void initializeView()
|
||||
{
|
||||
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());
|
||||
scrollPane.setViewportView(serviceRegister.getEditorComponent());
|
||||
serviceRegister.updateEditorValue();
|
||||
};
|
||||
doLayout();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setInteractiveObject(Object o) {
|
||||
serviceRegister = (ServiceRegister)o;
|
||||
initializeView();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getInteractiveObject() {
|
||||
return serviceRegister;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,180 +0,0 @@
|
|||
package org.hwo.ui.servicelink.register.bitfield;
|
||||
|
||||
import java.awt.Component;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import javax.swing.JCheckBox;
|
||||
import javax.swing.JPanel;
|
||||
|
||||
import org.hwo.ui.servicelink.register.ServiceRegister;
|
||||
import org.hwo.ui.servicelink.register.ServiceRegisterControl;
|
||||
|
||||
import javax.swing.JScrollPane;
|
||||
|
||||
import java.awt.GridBagLayout;
|
||||
import java.awt.GridBagConstraints;
|
||||
|
||||
import javax.swing.BoxLayout;
|
||||
|
||||
public class BitFieldEditor extends JPanel implements ServiceRegisterControl {
|
||||
|
||||
private ServiceRegister serviceRegister;
|
||||
|
||||
private JCheckBox[] checkboxes;
|
||||
private JScrollPane scrollPane;
|
||||
private JPanel pBits;
|
||||
|
||||
private Integer intValue;
|
||||
|
||||
private List<Field> fields;
|
||||
|
||||
public class Field
|
||||
{
|
||||
int start,
|
||||
len;
|
||||
|
||||
String text;
|
||||
|
||||
JCheckBox checkbox;
|
||||
|
||||
public Field(int start,int len)
|
||||
{
|
||||
this.start = start;
|
||||
this.len = len;
|
||||
this.text = String.format("B%d",start);
|
||||
}
|
||||
|
||||
public Field(int start)
|
||||
{
|
||||
this.start = start;
|
||||
this.len = 1;
|
||||
this.text = String.format("B%d",start);
|
||||
}
|
||||
|
||||
public void setText(String text)
|
||||
{
|
||||
this.text = text;
|
||||
}
|
||||
public String getText()
|
||||
{
|
||||
return text;
|
||||
}
|
||||
|
||||
public String getTextRepresentation()
|
||||
{
|
||||
if (BitFieldEditor.this.getIntValue() != null)
|
||||
{
|
||||
if ((BitFieldEditor.this.getIntValue() & (1<<start)) != 0)
|
||||
{
|
||||
return getText();
|
||||
}
|
||||
}
|
||||
return "";
|
||||
}
|
||||
public Component getComponent()
|
||||
{
|
||||
if (checkbox == null)
|
||||
{
|
||||
checkbox = new JCheckBox();
|
||||
checkbox.setText(getText());
|
||||
}
|
||||
return checkbox;
|
||||
}
|
||||
public void sync()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public BitFieldEditor() {
|
||||
GridBagLayout gridBagLayout = new GridBagLayout();
|
||||
gridBagLayout.columnWidths = new int[]{0, 0};
|
||||
gridBagLayout.rowHeights = new int[]{0, 0};
|
||||
gridBagLayout.columnWeights = new double[]{1.0, Double.MIN_VALUE};
|
||||
gridBagLayout.rowWeights = new double[]{1.0, Double.MIN_VALUE};
|
||||
setLayout(gridBagLayout);
|
||||
|
||||
scrollPane = new JScrollPane();
|
||||
GridBagConstraints gbc_scrollPane = new GridBagConstraints();
|
||||
gbc_scrollPane.fill = GridBagConstraints.BOTH;
|
||||
gbc_scrollPane.gridx = 0;
|
||||
gbc_scrollPane.gridy = 0;
|
||||
add(scrollPane, gbc_scrollPane);
|
||||
|
||||
pBits = new JPanel();
|
||||
scrollPane.setViewportView(pBits);
|
||||
pBits.setLayout(new BoxLayout(pBits, BoxLayout.Y_AXIS));
|
||||
|
||||
checkboxes = new JCheckBox[0];
|
||||
|
||||
initialize();
|
||||
}
|
||||
|
||||
private void initialize()
|
||||
{
|
||||
removeAllFieldComponents();
|
||||
createFieldControls();
|
||||
|
||||
doLayout();
|
||||
invalidate();
|
||||
}
|
||||
|
||||
protected void createFieldControls()
|
||||
{
|
||||
fields = new ArrayList<BitFieldEditor.Field>();
|
||||
for (int i=0;i<32;i++)
|
||||
{
|
||||
Field field = new Field(i);
|
||||
fields.add(field);
|
||||
addFieldComponent(field.getComponent());
|
||||
}
|
||||
}
|
||||
|
||||
protected void removeAllFieldComponents()
|
||||
{
|
||||
pBits.removeAll();
|
||||
}
|
||||
protected void addFieldComponent(Component component)
|
||||
{
|
||||
pBits.add(component);
|
||||
}
|
||||
|
||||
public void synchronize()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public Component getComponent() {
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void setServiceRegister(ServiceRegister serviceRegister) {
|
||||
this.serviceRegister = serviceRegister;
|
||||
}
|
||||
|
||||
public Integer getIntValue()
|
||||
{
|
||||
return this.intValue;
|
||||
}
|
||||
public void setIntValue(Integer value)
|
||||
{
|
||||
this.intValue = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeValue() {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readValue() {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue