Closed aussichicken closed 4 years ago
Hey Dominik,
There are two questions, I will try to address both as much as I can. Converters in get/set methods of client are responsible to transmit java value to bacnet4j (bacnet) primitive type. There is no default implementation for second as there might be additional logic necessary for specific devices or applications. JavaToBacnetConverter
is parametrized type and takes a Java type name as type argument, meaning that you can create variants of converters for floats, decimals and so on, or one generic one handling all java.lang.Object
instances.
FYI, the converter should not return a null
, but a new com.serotonin.bacnet4j.type.primitive.Null
. Wrapper doesn't do any additional wrapping or transformation thus it is shifted to a caller.
For second question - there is a org.code_house.bacnet4j.wrapper.ip.IpDevice#IpDevice(int, byte[], int)
constructor which allows you to pass instanceId
, mac
and networkId
. With this you will be able to work with pre-defined address without the need for calling discovery.
Best, Łukasz
Hi Lukasz, thank you for your quick response. I could solve the problem with the pre-defined address.
But I'm still struggeling with the converter. I want to change a Analog Value to a new value (integer). I created a class "JavaDoubleConverter" similar to the class "BypassBacnetConverter". When i use it, i still get an error:
Exception in thread "main" java.lang.ClassCastException: class java.lang.Integer cannot be cast to class com.serotonin.bacnet4j.type.Encodable (java.lang.Integer is in module java.base of loader 'bootstrap'; com.serotonin.bacnet4j.type.Encodable is in unnamed module of loader 'app')
The class looks like this:
package BACnet;
import org.code_house.bacnet4j.wrapper.api.JavaToBacNetConverter;
import com.serotonin.bacnet4j.type.Encodable;
public class JavaDoubleConverter<T> implements JavaToBacNetConverter<Encodable> {
@Override
public Encodable toBacNet(Encodable value) {
return value;
}
}
If i don't misunderstand the error message, there's a problem to cast an integer in an encodable. Do you have an idea? Maybe an example from an other project.
Thank you very much.
Best Dominik
In your case, when you write the conversion is not a straight task (let say). You might have a Java type which can be mapped to couple of bacnet types (real, unsigned, signed etc) depending on object type you are trying to write to.
That's why when you have an Integer
you have to convert it to an encodable instance. You can use "bypass" converter on the write path only if you create Encodable yourself (client.setProperty(prop, <encodable>, new BypassEncodableEncoder()
), otherwise your encoder should implement necessary logic to map Java to bacnet4j primitive.
So your code should look more like this:
package BACnet;
import org.code_house.bacnet4j.wrapper.api.JavaToBacNetConverter;
import com.serotonin.bacnet4j.type.Encodable;
import com.serotonin.bacnet4j.type.primitive.Null;
public class JavaDoubleConverter implements JavaToBacNetConverter<Double> {
@Override
public Encodable toBacNet(Double value) {
if (value == null) return new Null();
return new Real(value.floatValue());
}
}
public class JavaIntegerConverter implements JavaToBacNetConverter<Integer> {
@Override
public Encodable toBacNet(Integer value) {
if (value == null) return new Null();
return new SignedInteger(value);
}
}
There are variants of primitives on bacnet4j such as Unsigned8
, Unsigned16
, Unsigned32
, SignedInteger
.
Let me know if this helps. Best luck, Łukasz
Hi Lukasz, the code for double works, the integer one not, but that's no problem.
Thank you very much for your help :)
Best Dominik
I believe this issue can be marked as solved. Please re-open it if you encounter any problems with writing values.
Hi, i'm new to programming and try to work with bacnet4j-wrapper for my master thesis. I want to set a value (Analog Output) to a new value. For setPropertyValue i have to set a property and value, this works. But i don't know, what i have to do with the JavaToBacNetConverter. If I set 'null' i get following error:
My complete code is:
Can someone please help me?
My second question would be, how can I set a specific MAC address instead of using .getAddress()?
Thank you very much for your help.
Have a nice weekend
Dominik