Skip to content

Commit

Permalink
feat(plc4j/iec-60870): Made the Tag-informaiton available in subscrip…
Browse files Browse the repository at this point in the history
…tion events.
  • Loading branch information
chrisdutz committed Sep 4, 2023
1 parent 917b5fc commit 80d759d
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.plc4x.java.iec608705104.readwrite.messages;

import org.apache.plc4x.java.api.model.PlcTag;
import org.apache.plc4x.java.api.value.PlcValue;
import org.apache.plc4x.java.spi.messages.DefaultPlcSubscriptionEvent;
import org.apache.plc4x.java.spi.messages.utils.ResponseItem;

import java.time.Instant;
import java.util.Map;

public class Iec608705104PlcSubscriptionEvent extends DefaultPlcSubscriptionEvent {

private final Map<String, PlcTag> tags;
public Iec608705104PlcSubscriptionEvent(Instant timestamp, Map<String, PlcTag> tags, Map<String, ResponseItem<PlcValue>> values) {
super(timestamp, values);
this.tags = tags;
}

@Override
public PlcTag getTag(String name) {
return tags.get(name);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.apache.plc4x.java.api.value.PlcValue;
import org.apache.plc4x.java.iec608705104.readwrite.*;
import org.apache.plc4x.java.iec608705104.readwrite.configuration.Iec608705014Configuration;
import org.apache.plc4x.java.iec608705104.readwrite.messages.Iec608705104PlcSubscriptionEvent;
import org.apache.plc4x.java.iec608705104.readwrite.model.Iec608705104SubscriptionHandle;
import org.apache.plc4x.java.iec608705104.readwrite.tag.Iec608705104Tag;
import org.apache.plc4x.java.spi.ConversationContext;
Expand Down Expand Up @@ -206,8 +207,9 @@ protected LocalDateTime convertCp56Time2aToCalendar(SevenOctetBinaryTime cp56Tim

protected void publishEvent(LocalDateTime timeStamp, Iec608705104Tag tag, PlcValue plcValue) {
// Create a subscription event from the input.
final PlcSubscriptionEvent event = new DefaultPlcSubscriptionEvent(
final PlcSubscriptionEvent event = new Iec608705104PlcSubscriptionEvent(
timeStamp.atZone(ZoneId.systemDefault()).toInstant(),
Collections.singletonMap(tag.toString(), tag),
Collections.singletonMap(tag.toString(),
new ResponseItem<>(PlcResponseCode.OK, plcValue)));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

import org.apache.plc4x.java.api.PlcConnection;
import org.apache.plc4x.java.api.PlcDriverManager;
import org.apache.plc4x.java.api.model.PlcTag;
import org.apache.plc4x.java.iec608705104.readwrite.tag.Iec608705104Tag;

import java.util.concurrent.CompletableFuture;

Expand All @@ -38,7 +40,8 @@ public static void main(String[] args) throws Exception {

plcConnection.subscriptionRequestBuilder().addChangeOfStateTagAddress("all", "*").addPreRegisteredConsumer("all", plcSubscriptionEvent -> {
for (String tagName : plcSubscriptionEvent.getTagNames()) {
System.out.println(String.format("TS: %s, Addr: %s, Value; %s", plcSubscriptionEvent.getTimestamp().toString(), tagName, plcSubscriptionEvent.getPlcValue(tagName).toString()));
Iec608705104Tag tag = (Iec608705104Tag) plcSubscriptionEvent.getTag(tagName);
System.out.println(String.format("TS: %s, Addr: %d:%d, Value; %s", plcSubscriptionEvent.getTimestamp().toString(), tag.getAdsuAddress(), tag.getObjectAddress(), plcSubscriptionEvent.getPlcValue(tagName).toString()));
}
}).build().execute();

Expand Down

0 comments on commit 80d759d

Please sign in to comment.