use of io.openems.core.utilities.api.WritePOJO in project openems by OpenEMS.
the class ChannelRegisterMap method setValue.
protected void setValue(MyRegister register, byte b1, byte b2) throws OpenemsException {
int registerNo = register.getRegisterNo();
this.setBuffer[registerNo * 2] = b1;
this.setBuffer[registerNo * 2 + 1] = b2;
// is the buffer full?
for (int i = 0; i < this.setBuffer.length; i++) {
if (this.setBuffer[i] == null) {
// no, it is not full
return;
}
}
// yes, it is full -> parse value to Object
byte[] value = new byte[this.setBuffer.length];
boolean isPositive = true;
for (int i = this.setBuffer.length - 1; i > 0; i -= 2) {
if (((this.setBuffer[i - 1] >>> 8) & 1) != 0) {
// test if the 'negative-bit' is set
isPositive = false;
}
value[i] = this.setBuffer[i];
value[i - 1] = this.setBuffer[i - 1];
}
if (!isPositive) {
// fill leading bytes with 0xFF if the value is negative
for (int i = 0; i < value.length; i++) {
if (value[i] != 0) {
break;
}
value[i] = (byte) 0xff;
}
}
// int bitLength = getBitLength(type);
// System.out.println(bitLength + " - " + bytesToHex(value));
Object valueObj = BitUtils.toObject(this.channelDoc.getTypeOpt().get(), value);
Channel channel = this.getChannel();
if (channel instanceof ConfigChannel<?>) {
// it is a ConfigChannel -> write directly
ConfigChannel<?> configChannel = (ConfigChannel<?>) channel;
configChannel.updateValue(valueObj, true);
log.info("Updated [" + this.channelAddress + "] to [" + valueObj + "] via Modbus/TCP.");
} else if (channel instanceof WriteChannel<?>) {
// it is a WriteChannel -> handle by ApiWorker
WriteChannel<?> writeChannel = (WriteChannel<?>) channel;
WritePOJO writeObject = new WritePOJO(valueObj);
apiWorker.addValue(writeChannel, writeObject);
}
}
Aggregations