]> git.basschouten.com Git - openhab-addons.git/blob
957ad7ba53bb6bcf576826e18bef27cf5b4e9bfc
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.dsmr.internal.meter;
14
15 import java.util.ArrayList;
16 import java.util.List;
17 import java.util.stream.Collectors;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.openhab.binding.dsmr.internal.device.cosem.CosemObject;
21 import org.openhab.binding.dsmr.internal.device.cosem.CosemObjectType;
22 import org.openhab.binding.dsmr.internal.device.cosem.OBISIdentifier;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 /**
27  * DSMR Meter represents a meter for this binding.
28  *
29  * A physical meter is a certain {@link DSMRMeterType} on a M-Bus channel. This is the {@link DSMRMeterDescriptor}
30  * and is a private member of the {@link DSMRMeter}.
31  *
32  * @author M. Volaart - Initial contribution
33  * @author Hilbrand Bouwkamp - Refactored class, removed actual handling and moved to handler class
34  */
35 @NonNullByDefault
36 public class DSMRMeter {
37     private final Logger logger = LoggerFactory.getLogger(DSMRMeter.class);
38
39     /**
40      * Meter identification.
41      */
42     private final DSMRMeterDescriptor meterDescriptor;
43
44     /**
45      * List of supported message identifiers for this meter
46      */
47     private List<OBISIdentifier> supportedIdentifiers = new ArrayList<>();
48
49     /**
50      * Creates a new DSMRMeter
51      *
52      * @param meterDescriptor {@link DSMRMeterDescriptor} containing the description of the new meter
53      */
54     public DSMRMeter(DSMRMeterDescriptor meterDescriptor) {
55         this.meterDescriptor = meterDescriptor;
56
57         for (CosemObjectType msgType : meterDescriptor.getMeterType().supportedCosemObjects) {
58             OBISIdentifier obisId = msgType.obisId;
59             if (msgType.obisId.getChannel() == null) {
60                 supportedIdentifiers.add(new OBISIdentifier(obisId.getGroupA(), obisId.getGroupC(), obisId.getGroupD(),
61                         obisId.getGroupE()));
62             } else {
63                 supportedIdentifiers.add(msgType.obisId);
64             }
65         }
66     }
67
68     /**
69      * Returns a list of Cosem Objects this meter will handle and removed them from the passed {@link CosemObject} list.
70      *
71      * @param cosemObjects list of CosemObject that must be processed and where the objects of this meter are removed
72      * @return List of CosemObject that this meter can process
73      */
74     public List<CosemObject> filterMeterValues(List<CosemObject> cosemObjects, int channel) {
75         logger.trace("supported identifiers: {}, searching for objects {}", supportedIdentifiers, cosemObjects);
76         List<CosemObject> filteredValues = cosemObjects.stream()
77                 .filter(cosemObject -> (DSMRMeterConstants.UNKNOWN_CHANNEL == channel
78                         || cosemObject.getObisIdentifier().getChannel() == channel)
79                         && supportedIdentifiers.contains(cosemObject.getObisIdentifier().getReducedOBISIdentifier()))
80                 .collect(Collectors.toList());
81         return filteredValues;
82     }
83
84     /**
85      * @return Returns the {@link DSMRMeterDescriptor} this object is configured with
86      */
87     public DSMRMeterDescriptor getMeterDescriptor() {
88         return meterDescriptor;
89     }
90
91     @Override
92     public String toString() {
93         return meterDescriptor.toString();
94     }
95 }