use of org.apache.plc4x.java.spi.messages.DefaultPlcSubscriptionRequest in project plc4x by apache.
the class S7ProtocolLogic method subscribe.
@Override
public CompletableFuture<PlcSubscriptionResponse> subscribe(PlcSubscriptionRequest subscriptionRequest) {
CompletableFuture<PlcSubscriptionResponse> future = new CompletableFuture<>();
DefaultPlcSubscriptionRequest request = (DefaultPlcSubscriptionRequest) subscriptionRequest;
List<S7ParameterUserDataItem> parameterItems = new ArrayList<>(request.getNumberOfFields());
List<S7PayloadUserDataItem> payloadItems = new ArrayList<>(request.getNumberOfFields());
for (String fieldName : request.getFieldNames()) {
final DefaultPlcSubscriptionField sf = (DefaultPlcSubscriptionField) request.getField(fieldName);
final S7SubscriptionField field = (S7SubscriptionField) sf.getPlcField();
switch(field.getFieldType()) {
case EVENT_SUBSCRIPTION:
;
encodeEventSubcriptionRequest(request, parameterItems, payloadItems);
break;
case EVENT_UNSUBSCRIPTION:
;
// encodeEventUnSubcriptionRequest(msg, out);
break;
case ALARM_ACK:
;
// encodeAlarmAckRequest(msg, out);
break;
case ALARM_QUERY:
;
// encodeAlarmQueryRequest(msg, out);
break;
case CYCLIC_SUBSCRIPTION:
;
// encodeCycledSubscriptionRequest(msg, out);
break;
case CYCLIC_UNSUBSCRIPTION:
;
// encodeCycledUnSubscriptionRequest(msg, out);
break;
default:
;
}
;
// final PlcValue plcValue = request.getPlcValue(fieldName);
// parameterItems.add(new S7VarRequestParameterItemAddress(encodeS7Address(field)));
// payloadItems.add(serializePlcValue(field, plcValue));
}
final int tpduId = tpduGenerator.getAndIncrement();
// If we've reached the max value for a 16 bit transaction identifier, reset back to 1
if (tpduGenerator.get() == 0xFFFF) {
tpduGenerator.set(1);
}
TPKTPacket tpktPacket = new TPKTPacket(new COTPPacketData(null, new S7MessageUserData(tpduId, new S7ParameterUserData(parameterItems), new S7PayloadUserData(payloadItems, null)), true, (short) tpduId, null));
// Start a new request-transaction (Is ended in the response-handler)
RequestTransactionManager.RequestTransaction transaction = tm.startRequest();
transaction.submit(() -> context.sendRequest(tpktPacket).onTimeout(new TransactionErrorCallback<>(future, transaction)).onError(new TransactionErrorCallback<>(future, transaction)).expectResponse(TPKTPacket.class, REQUEST_TIMEOUT).check(p -> p.getPayload() instanceof COTPPacketData).unwrap(p -> ((COTPPacketData) p.getPayload())).unwrap(COTPPacket::getPayload).check(p -> p.getTpduReference() == tpduId).handle(p -> {
try {
future.complete(((PlcSubscriptionResponse) decodeEventSubcriptionRequest(p, subscriptionRequest)));
} catch (PlcProtocolException e) {
logger.warn("Error sending 'write' message: '{}'", e.getMessage(), e);
}
// Finish the request-transaction.
transaction.endRequest();
}));
return future;
}
use of org.apache.plc4x.java.spi.messages.DefaultPlcSubscriptionRequest in project plc4x by apache.
the class CANOpenProtocolLogic method subscribe.
@Override
public CompletableFuture<PlcSubscriptionResponse> subscribe(PlcSubscriptionRequest request) {
DefaultPlcSubscriptionRequest rq = (DefaultPlcSubscriptionRequest) request;
Map<String, ResponseItem<PlcSubscriptionHandle>> answers = new LinkedHashMap<>();
DefaultPlcSubscriptionResponse response = new DefaultPlcSubscriptionResponse(rq, answers);
for (String key : rq.getFieldNames()) {
DefaultPlcSubscriptionField subscription = (DefaultPlcSubscriptionField) rq.getField(key);
if (subscription.getPlcSubscriptionType() != PlcSubscriptionType.EVENT) {
answers.put(key, new ResponseItem<>(PlcResponseCode.UNSUPPORTED, null));
} else if ((subscription.getPlcField() instanceof CANOpenPDOField)) {
answers.put(key, new ResponseItem<>(PlcResponseCode.OK, new CANOpenSubscriptionHandle(this, key, (CANOpenPDOField) subscription.getPlcField())));
} else if ((subscription.getPlcField() instanceof CANOpenNMTField)) {
answers.put(key, new ResponseItem<>(PlcResponseCode.OK, new CANOpenSubscriptionHandle(this, key, (CANOpenNMTField) subscription.getPlcField())));
} else if ((subscription.getPlcField() instanceof CANOpenHeartbeatField)) {
answers.put(key, new ResponseItem<>(PlcResponseCode.OK, new CANOpenSubscriptionHandle(this, key, (CANOpenHeartbeatField) subscription.getPlcField())));
} else {
answers.put(key, new ResponseItem<>(PlcResponseCode.INVALID_ADDRESS, null));
}
}
return CompletableFuture.completedFuture(response);
}
Aggregations