|
| 1 | +package org.astarteplatform.devicesdk; |
| 2 | + |
| 3 | +import java.util.Collection; |
| 4 | +import java.util.HashMap; |
| 5 | +import java.util.Map; |
| 6 | +import org.astarteplatform.devicesdk.crypto.AstarteCryptoException; |
| 7 | +import org.astarteplatform.devicesdk.protocol.AstarteGlobalEventListener; |
| 8 | +import org.astarteplatform.devicesdk.protocol.AstarteInterface; |
| 9 | +import org.astarteplatform.devicesdk.protocol.AstarteInvalidInterfaceException; |
| 10 | +import org.astarteplatform.devicesdk.protocol.AstarteServerAggregateDatastreamInterface; |
| 11 | +import org.astarteplatform.devicesdk.protocol.AstarteServerDatastreamInterface; |
| 12 | +import org.astarteplatform.devicesdk.protocol.AstarteServerPropertyInterface; |
| 13 | +import org.astarteplatform.devicesdk.transport.AstarteFailedMessageStorage; |
| 14 | +import org.astarteplatform.devicesdk.transport.AstarteTransportException; |
| 15 | +import org.json.JSONException; |
| 16 | +import org.json.JSONObject; |
| 17 | + |
| 18 | +public abstract class AstarteDevice { |
| 19 | + private final Map<String, AstarteInterface> mAstarteInterfaces; |
| 20 | + protected final AstartePropertyStorage mPropertyStorage; |
| 21 | + protected final AstarteFailedMessageStorage mFailedMessageStorage; |
| 22 | + protected boolean mAlwaysReconnect = false; |
| 23 | + |
| 24 | + AstarteDevice( |
| 25 | + AstarteInterfaceProvider astarteInterfaceProvider, |
| 26 | + AstartePropertyStorage propertyStorage, |
| 27 | + AstarteFailedMessageStorage failedMessageStorage) |
| 28 | + throws JSONException, AstarteInvalidInterfaceException { |
| 29 | + mPropertyStorage = propertyStorage; |
| 30 | + mFailedMessageStorage = failedMessageStorage; |
| 31 | + Collection<JSONObject> astarteInterfaces = astarteInterfaceProvider.loadAllInterfaces(); |
| 32 | + mAstarteInterfaces = new HashMap<>(); |
| 33 | + for (JSONObject astarteInterface : astarteInterfaces) { |
| 34 | + AstarteInterface theInterface = AstarteInterface.fromJSON(astarteInterface, mPropertyStorage); |
| 35 | + mAstarteInterfaces.put(theInterface.getInterfaceName(), theInterface); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + public boolean hasInterface(String interfaceName) { |
| 40 | + return mAstarteInterfaces.containsKey(interfaceName); |
| 41 | + } |
| 42 | + |
| 43 | + public Collection<String> getAllInterfaceNames() { |
| 44 | + return mAstarteInterfaces.keySet(); |
| 45 | + } |
| 46 | + |
| 47 | + public Collection<AstarteInterface> getAllInterfaces() { |
| 48 | + return mAstarteInterfaces.values(); |
| 49 | + } |
| 50 | + |
| 51 | + public AstarteInterface getInterface(String interfaceName) { |
| 52 | + return mAstarteInterfaces.get(interfaceName); |
| 53 | + } |
| 54 | + |
| 55 | + public void addGlobalEventListener(AstarteGlobalEventListener eventListener) { |
| 56 | + for (Map.Entry<String, AstarteInterface> interfaceEntry : mAstarteInterfaces.entrySet()) { |
| 57 | + AstarteInterface astarteInterface = interfaceEntry.getValue(); |
| 58 | + if (astarteInterface instanceof AstarteServerPropertyInterface) { |
| 59 | + ((AstarteServerPropertyInterface) astarteInterface).addListener(eventListener); |
| 60 | + } else if (astarteInterface instanceof AstarteServerDatastreamInterface) { |
| 61 | + ((AstarteServerDatastreamInterface) astarteInterface).addListener(eventListener); |
| 62 | + } else if (astarteInterface instanceof AstarteServerAggregateDatastreamInterface) { |
| 63 | + ((AstarteServerAggregateDatastreamInterface) astarteInterface).addListener(eventListener); |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + public void removeGlobalListener(AstarteGlobalEventListener eventListener) { |
| 69 | + for (Map.Entry<String, AstarteInterface> interfaceEntry : mAstarteInterfaces.entrySet()) { |
| 70 | + AstarteInterface astarteInterface = interfaceEntry.getValue(); |
| 71 | + if (astarteInterface instanceof AstarteServerPropertyInterface) { |
| 72 | + ((AstarteServerPropertyInterface) astarteInterface).removeListener(eventListener); |
| 73 | + } else if (astarteInterface instanceof AstarteServerDatastreamInterface) { |
| 74 | + ((AstarteServerDatastreamInterface) astarteInterface).removeListener(eventListener); |
| 75 | + } else if (astarteInterface instanceof AstarteServerAggregateDatastreamInterface) { |
| 76 | + ((AstarteServerAggregateDatastreamInterface) astarteInterface) |
| 77 | + .removeListener(eventListener); |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + public abstract String getDeviceId(); |
| 83 | + |
| 84 | + public abstract String getAstarteRealm(); |
| 85 | + |
| 86 | + public abstract AstarteMessageListener getAstarteMessageListener(); |
| 87 | + |
| 88 | + public abstract void setAstarteMessageListener(AstarteMessageListener astarteMessageListener); |
| 89 | + |
| 90 | + public abstract void connect() |
| 91 | + throws AstarteTransportException, AstarteCryptoException, AstartePairingException; |
| 92 | + |
| 93 | + public abstract void disconnect() throws AstarteTransportException; |
| 94 | + |
| 95 | + public abstract boolean isConnected(); |
| 96 | + |
| 97 | + public boolean alwaysReconnects() { |
| 98 | + return mAlwaysReconnect; |
| 99 | + } |
| 100 | + |
| 101 | + public void setAlwaysReconnect(boolean alwaysReconnect) { |
| 102 | + this.mAlwaysReconnect = alwaysReconnect; |
| 103 | + } |
| 104 | +} |
0 commit comments