该方法需要的 CustomDataType:
<UADataType NodeId="ns=1;i=3010" BrowseName="1:ScanSettings">
<DisplayName>ScanSettings</DisplayName>
<References>
<Reference ReferenceType="HasEncoding">ns=1;i=5015</Reference>
<Reference ReferenceType="HasEncoding">ns=1;i=5016</Reference>
<Reference ReferenceType="HasSubtype" IsForward="false">i=22</Reference>
</References>
<Definition Name="1:ScanSettings">
<Field DataType="Duration" Name="Duration"/>
<Field DataType="Int32" Name="Cycles"/>
<Field DataType="Boolean" Name="DataAvailable"/>
<Field IsOptional="true" DataType="LocationTypeEnumeration" Name="LocationType"/>
</Definition>
</UADataType>
方法调用:
CallMethodRequest tCallMethodRequest = new CallMethodRequest(tObjectId, tMethodId, new Variant[]{});
CallMethodResult tCallMethodResult = pOpcUaClient.call(tCallMethodRequest).get();
System.out.println(tCallMethodResult.getStatusCode());
如何调用需要 ScanSettings 的方法?我是否需要通过包含 Duration、Cycles 和 DataAvailable 的三个 Variant 来传递 Variant-array?
或者
我需要做这样的事情吗?
编辑:
用 ScanSettings-class 试了一下,得到了错误:
10:08:52.655 [ua-shared-pool-2] WARN org.eclipse.milo.opcua.stack.core.serialization.OpcUaBinaryStreamEncoder - Not a built-in type: class ScanSettings
我的 ScanSettings 类:
public class ScanSettings {
private final double duration;
private final int cycles;
private final boolean dataAvailable;
public ScanSettings() {
this(1000.0, 1, true);
}
public ScanSettings(double pDuration, int pCycles, boolean pDataAvailable) {
duration = pDuration;
cycles = pCycles;
dataAvailable = pDataAvailable;
}
public double getDuration() {
return duration;
}
public int getCycles() {
return cycles;
}
public boolean isDataAvailable() {
return dataAvailable;
}
@Override
public int hashCode() {
return Objects.hashCode(duration);
}
@Override
public boolean equals(final Object obj) {
return super.equals(obj);
}
@Override
public String toString() {
return duration + " " + cycles + " " + dataAvailable;
}
public static class Codec extends GenericDataTypeCodec<ScanSettings> {
@Override
public Class<ScanSettings> getType() {
return ScanSettings.class;
}
@Override
public ScanSettings decode(final SerializationContext context, final UaDecoder reader) throws UaSerializationException {
double tDuration = reader.readDouble("Duration");
int tCycle = reader.readInt32("Cycle");
boolean tDataAvalible = reader.readBoolean("DataAvailable");
return new ScanSettings(tDuration, tCycle, tDataAvalible);
}
@Override
public void encode(final SerializationContext context, final ScanSettings pScanSettings, final UaEncoder writer) throws UaSerializationException {
writer.writeDouble("Duration", pScanSettings.duration);
writer.writeInt32("Cycle", pScanSettings.cycles);
writer.writeBoolean("DataAvailable", pScanSettings.dataAvailable);
}
}
}
注册它:
OpcUaBinaryDataTypeDictionary tOpcUaBinaryDataTypeDictionary = new OpcUaBinaryDataTypeDictionary("urn:ScanSettings");
NodeId binaryEncodingId = new NodeId(2, "DataType.ScanSettings.BinaryEncoding");
tOpcUaBinaryDataTypeDictionary.registerStructCodec(new ScanSettings.Codec().asBinaryCodec(), "ScanSettings", binaryEncodingId);
OpcUaDataTypeManager.getInstance().registerTypeDictionary(tOpcUaBinaryDataTypeDictionary);