98 lines
2.1 KiB
Java
98 lines
2.1 KiB
Java
package org.hwo.servicelink;
|
|
|
|
import java.nio.ByteBuffer;
|
|
import java.nio.ByteOrder;
|
|
import java.lang.Thread.State;
|
|
import static org.hwo.logging.LogLevel.*;
|
|
import static org.hwo.logging.Logging.*;
|
|
|
|
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 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) {
|
|
log(DEBUGDETAIL,"setValue( %s )",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;
|
|
}
|
|
}
|
|
|