|
16 | 16 | package com.hivemq.edge.adapters.s7; |
17 | 17 |
|
18 | 18 | import com.github.xingshuangs.iot.protocol.s7.enums.EPlcType; |
19 | | -import com.github.xingshuangs.iot.protocol.s7.service.S7PLC; |
20 | 19 | import com.hivemq.adapter.sdk.api.ProtocolAdapterInformation; |
21 | | -import com.hivemq.adapter.sdk.api.config.PollingContext; |
| 20 | +import com.hivemq.adapter.sdk.api.data.DataPoint; |
22 | 21 | import com.hivemq.adapter.sdk.api.model.ProtocolAdapterInput; |
23 | 22 | import com.hivemq.adapter.sdk.api.model.ProtocolAdapterStartInput; |
24 | 23 | import com.hivemq.adapter.sdk.api.model.ProtocolAdapterStartOutput; |
|
29 | 28 | import com.hivemq.adapter.sdk.api.polling.PollingProtocolAdapter; |
30 | 29 | import com.hivemq.adapter.sdk.api.state.ProtocolAdapterState; |
31 | 30 | import com.hivemq.edge.adapters.s7.config.S7AdapterConfig; |
32 | | -import com.hivemq.edge.adapters.plc4x.config.Plc4xDataType; |
33 | | -import com.hivemq.edge.adapters.plc4x.config.Plc4xToMqttMapping; |
34 | | -import com.hivemq.edge.adapters.plc4x.impl.AbstractPlc4xAdapter; |
35 | | -import com.hivemq.edge.adapters.plc4x.types.siemens.config.S7AdapterConfig; |
| 31 | +import com.hivemq.edge.adapters.s7.config.S7DataType; |
| 32 | +import com.hivemq.edge.adapters.s7.config.S7ToMqttConfig; |
36 | 33 | import org.jetbrains.annotations.NotNull; |
37 | 34 | import org.slf4j.Logger; |
38 | 35 | import org.slf4j.LoggerFactory; |
39 | 36 |
|
40 | | -import java.util.HashMap; |
41 | 37 | import java.util.List; |
42 | 38 | import java.util.Map; |
43 | | -import java.util.Set; |
44 | | -import java.util.regex.Matcher; |
45 | | -import java.util.regex.Pattern; |
46 | | - |
47 | | -import static com.hivemq.edge.adapters.plc4x.config.Plc4xDataType.DATA_TYPE.*; |
| 39 | +import java.util.Objects; |
| 40 | +import java.util.concurrent.ConcurrentHashMap; |
48 | 41 |
|
49 | 42 | /** |
50 | 43 | * @author HiveMQ Adapter Generator |
51 | 44 | */ |
52 | | -public class S7ProtocolAdapter implements PollingProtocolAdapter<S7AdapterConfig> { |
| 45 | +public class S7ProtocolAdapter implements PollingProtocolAdapter<S7ToMqttConfig> { |
53 | 46 |
|
54 | 47 | private static final Logger log = LoggerFactory.getLogger(S7ProtocolAdapter.class); |
55 | 48 |
|
56 | 49 | private final ProtocolAdapterInformation adapterInformation; |
57 | 50 | private final S7AdapterConfig adapterConfig; |
58 | 51 | private final ProtocolAdapterState protocolAdapterState; |
59 | | - private final PollingContext |
| 52 | + private final S7Client s7Client; |
| 53 | + |
| 54 | + private final Map<String, DataPoint> dataPoints; |
60 | 55 |
|
61 | 56 | public S7ProtocolAdapter( |
62 | 57 | final @NotNull ProtocolAdapterInformation adapterInformation, |
63 | 58 | final @NotNull ProtocolAdapterInput<S7AdapterConfig> input) { |
64 | 59 | this.adapterInformation = adapterInformation; |
65 | 60 | this.adapterConfig = input.getConfig(); |
66 | 61 | this.protocolAdapterState = input.getProtocolAdapterState(); |
67 | | - this.pollingContext = adapterConfig.getFileToMqttConfig().getMappings(); |
68 | | - } |
69 | | - |
70 | | - @Override |
71 | | - public void poll( |
72 | | - @NotNull final PollingInput<S7AdapterConfig> pollingInput, |
73 | | - @NotNull final PollingOutput pollingOutput) { |
74 | | - S7PLC s7PLC = new S7PLC(EPlcType.S1200, "127.0.0.1"); |
75 | | - s7PLC.writeByte("DB2.1", (byte) 0x11); |
76 | | - s7PLC.readByte("DB2.1"); |
77 | | - // close it manually, if you want to use it all the time, you do not need to close it |
78 | | - s7PLC.close(); |
| 62 | + final EPlcType eplcType = S7Client.getEplcType(adapterConfig.getControllerType()); |
| 63 | + s7Client = new S7Client( |
| 64 | + eplcType, |
| 65 | + adapterConfig.getHost(), |
| 66 | + adapterConfig.getPort(), |
| 67 | + Objects.requireNonNullElse(adapterConfig.getRemoteRack(), eplcType.getRack()), |
| 68 | + Objects.requireNonNullElse(adapterConfig.getRemoteSlot(), eplcType.getSlot()), |
| 69 | + Objects.requireNonNullElse(adapterConfig.getPduLength(), eplcType.getPduLength()), |
| 70 | + input.adapterFactories().dataPointFactory()); |
| 71 | + this.dataPoints = new ConcurrentHashMap<>(); |
79 | 72 | } |
80 | 73 |
|
81 | 74 | @Override |
82 | | - public @NotNull List<S7AdapterConfig> getPollingContexts() { |
83 | | - return List.of(); |
| 75 | + public @NotNull List<S7ToMqttConfig> getPollingContexts() { |
| 76 | + return adapterConfig.getS7ToMqttMappings(); |
84 | 77 | } |
85 | 78 |
|
86 | 79 | @Override |
87 | 80 | public int getPollingIntervalMillis() { |
88 | | - return adapterInformation; |
| 81 | + return adapterConfig.getPollingIntervalMillis(); |
89 | 82 | } |
90 | 83 |
|
91 | 84 | @Override |
92 | 85 | public int getMaxPollingErrorsBeforeRemoval() { |
93 | | - return 0; |
| 86 | + return adapterConfig.getMaxPollingErrorsBeforeRemoval(); |
94 | 87 | } |
95 | 88 |
|
96 | 89 | @Override |
97 | 90 | public @NotNull String getId() { |
98 | | - return "s7-new"; |
| 91 | + return adapterConfig.getId(); |
99 | 92 | } |
100 | 93 |
|
101 | 94 | @Override |
102 | 95 | public void start( |
103 | 96 | @NotNull final ProtocolAdapterStartInput input, |
104 | 97 | @NotNull final ProtocolAdapterStartOutput output) { |
105 | | - log.error("REPLACE WITH AN ACTUAL IMPLEMENTATION!"); |
106 | | - output.startedSuccessfully(); |
| 98 | + log.info("Connecting to {}@{}:{}", adapterConfig.getControllerType(), adapterConfig.getHost(), adapterConfig.getPort()); |
| 99 | + try { |
| 100 | + s7Client.connect(); |
| 101 | + protocolAdapterState.setConnectionStatus(ProtocolAdapterState.ConnectionStatus.CONNECTED); |
| 102 | + output.startedSuccessfully(); |
| 103 | + } catch (final Exception e) { |
| 104 | + String msg = "Unable to connect to " + adapterConfig.getControllerType() + "@" + adapterConfig.getHost() + ":" + adapterConfig.getPort(); |
| 105 | + protocolAdapterState.setErrorConnectionStatus(e, msg); |
| 106 | + output.failStart(e, msg); |
| 107 | + } |
107 | 108 | } |
108 | 109 |
|
109 | 110 | @Override |
110 | 111 | public void stop(@NotNull final ProtocolAdapterStopInput input, @NotNull final ProtocolAdapterStopOutput output) { |
111 | | - log.error("REPLACE WITH AN ACTUAL IMPLEMENTATION!"); |
112 | | - output.stoppedSuccessfully(); |
| 112 | + log.info("Closing connection to {}@{}:{}", adapterConfig.getControllerType(), adapterConfig.getHost(), adapterConfig.getPort()); |
| 113 | + try { |
| 114 | + s7Client.disconnect(); |
| 115 | + protocolAdapterState.setConnectionStatus(ProtocolAdapterState.ConnectionStatus.DISCONNECTED); |
| 116 | + output.stoppedSuccessfully(); |
| 117 | + } catch (final Exception e) { |
| 118 | + final String msg = "Unable to disconnect from " + adapterConfig.getControllerType() + "@" + adapterConfig.getHost() + ":" + adapterConfig.getPort(); |
| 119 | + protocolAdapterState.setErrorConnectionStatus(e, msg); |
| 120 | + output.failStop(e, msg); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + @Override |
| 125 | + public void poll( |
| 126 | + @NotNull final PollingInput<S7ToMqttConfig> pollingInput, |
| 127 | + @NotNull final PollingOutput pollingOutput) { |
| 128 | + final S7ToMqttConfig s7ToMqtt = pollingInput.getPollingContext(); |
| 129 | + //Every S7 address starts with a % but the iot-communications lib doesn't like it so we are stripping it. |
| 130 | + final String tagAddress = s7ToMqtt.getTagAddress().replace("%",""); |
| 131 | + final DataPoint dataPoint; |
| 132 | + |
| 133 | + if(s7ToMqtt.getDataType() == S7DataType.BYTE) { |
| 134 | + dataPoint = s7Client.readByte(tagAddress); |
| 135 | + } else { |
| 136 | + dataPoint = s7Client.read(s7ToMqtt.getDataType(), List.of(tagAddress)).get(0); |
| 137 | + } |
| 138 | + |
| 139 | + if(adapterConfig.getPublishChangedDataOnly() && dataPoints.containsKey(tagAddress)) { |
| 140 | + final DataPoint existingDataPoint = dataPoints.get(tagAddress); |
| 141 | + if(existingDataPoint != null && !existingDataPoint.equals(dataPoint)) { |
| 142 | + dataPoints.put(tagAddress, dataPoint); |
| 143 | + pollingOutput.addDataPoint(dataPoint); |
| 144 | + } else { |
| 145 | + log.debug("Skipping sending for {} because publishChangedDataOnly=true", tagAddress); |
| 146 | + } |
| 147 | + } else { |
| 148 | + pollingOutput.addDataPoint(dataPoint); |
| 149 | + } |
| 150 | + |
| 151 | + pollingOutput.finish(); |
113 | 152 | } |
114 | 153 |
|
115 | 154 | @Override |
|
0 commit comments