Filter fields for JBBPOut
thwint opened this issue · 3 comments
Hello
Thank you very much for your library. I just discovered it and it might help for my project. Up to now I was able to fully read the bytes from my device. Now I want to try to send data back to the device. So far everything works as designed.
Unfortunately the device has some packages containing a version when reading but we must not send them when writing to the device.
Example Bin annotated class:
public class Foo {
@Bin(order = 0, type = BinType.BYTE)
protected int startByte = 0x02;
@Bin(order = 1, type = BinType.BYTE)
protected int length;
@Bin(order = 2, type = BinType.BYTE)
protected int key;
@Bin(order = 3, type = BinType.BYTE) // must be removed when sending
protected int packageVersion;
@Bin(order = 4, type = BinType.BYTE)
// more fields here
}
When sending to the device I must remove the field packageVersion. Is there a way to have something like the following?
BeginBin().Bin(new Foo()).RemoveFieldByName("packageVersion").End();
BeginBin().Bin(new Foo(), filterDefinition).End();
Or at class level using Bin annotation:
@Bin(process = "read")
@Bin(process = "write")
@Bin(process = "always") // default
Is there already another way achieving this, or do I need to write a custom writer?
Best regards,
Tom
directly JBBP doesn't have mechanisms for such tricks now, it is possible to make some custom type for instance which will be ignored during write but may be it would be much easier just split the class for two ones and provide read object in the output object constructor
public class FooIn {
@Bin(order = 0, type = BinType.BYTE)
protected int startByte = 0x02;
@Bin(order = 1, type = BinType.BYTE)
protected int length;
@Bin(order = 2, type = BinType.BYTE)
protected int key;
@Bin(order = 3, type = BinType.BYTE) // must be removed when sending
protected int packageVersion;
...
}
public class FooOut {
@Bin(order = 0, type = BinType.BYTE)
protected int startByte = 0x02;
@Bin(order = 1, type = BinType.BYTE)
protected int length;
@Bin(order = 2, type = BinType.BYTE)
protected int key;
...
public FooOut(){
}
public FooOut(FooIn in){
this.startByte = in.startByte;
this.length = in.length;
this.key = in.key;
...
}
}I have added into 2.0.4-SNAPSHOT some way to support filtration of fields, in your case it will look like
BeginBin().Bin(new Foo(), (bin, field) -> field == null || !"packageVersion".equals(field.getName()).End();it will writes all fields excluding packageVersion field
Thanks for this enhancement. It turned out that it is far more complicated than just the package version to be filtered. Depending on the package version there are more or less elements to be added. For reading data I can easily define multiple scripts and ignore missing fields. For sending data I ended writing something like:
JBBPOut jbbpOut = BeginBin().Short(...);
if (packageVersion == 4) {
jbbOut.Short(...);
}
Now Im struggling with a Var field containing a calculated checksum for all fields in the stream.