]> git.basschouten.com Git - openhab-addons.git/commitdiff
[mikrotik] Basic PPP/LTE interface support (#11395)
authorOleg Vivtash <oleg@vivtash.net>
Sun, 17 Oct 2021 12:04:47 +0000 (15:04 +0300)
committerGitHub <noreply@github.com>
Sun, 17 Oct 2021 12:04:47 +0000 (14:04 +0200)
Signed-off-by: Oleg Vivtash <oleg@vivtash.net>
bundles/org.openhab.binding.mikrotik/README.md
bundles/org.openhab.binding.mikrotik/src/main/java/org/openhab/binding/mikrotik/internal/handler/MikrotikInterfaceThingHandler.java
bundles/org.openhab.binding.mikrotik/src/main/java/org/openhab/binding/mikrotik/internal/model/RouterosDevice.java
bundles/org.openhab.binding.mikrotik/src/main/java/org/openhab/binding/mikrotik/internal/model/RouterosInterfaceType.java
bundles/org.openhab.binding.mikrotik/src/main/java/org/openhab/binding/mikrotik/internal/model/RouterosLTEInterface.java [new file with mode: 0644]
bundles/org.openhab.binding.mikrotik/src/main/java/org/openhab/binding/mikrotik/internal/model/RouterosPPPCliInterface.java [new file with mode: 0644]

index c45dbb678bc352461e7290ba7312965d26dbdc8f..d0fc821db9dbc373bb016af927af65841f18dcf0 100644 (file)
@@ -106,6 +106,8 @@ At the moment the binding supports the following RouterOS interface types:
 * `wlan`
 * `cap`
 * `pppoe-out`
+* `ppp-out`
+* `lte`
 * `l2tp-in`
 * `l2tp-out`
 
index ab947d446624132ee6e933091620d6639d023208..c9ce0f9a88747d83ff54696edf40a7b75dd9b800 100644 (file)
@@ -28,6 +28,8 @@ import org.openhab.binding.mikrotik.internal.model.RouterosEthernetInterface;
 import org.openhab.binding.mikrotik.internal.model.RouterosInterfaceBase;
 import org.openhab.binding.mikrotik.internal.model.RouterosL2TPCliInterface;
 import org.openhab.binding.mikrotik.internal.model.RouterosL2TPSrvInterface;
+import org.openhab.binding.mikrotik.internal.model.RouterosLTEInterface;
+import org.openhab.binding.mikrotik.internal.model.RouterosPPPCliInterface;
 import org.openhab.binding.mikrotik.internal.model.RouterosPPPoECliInterface;
 import org.openhab.binding.mikrotik.internal.model.RouterosWlanInterface;
 import org.openhab.binding.mikrotik.internal.util.RateCalculator;
@@ -92,7 +94,7 @@ public class MikrotikInterfaceThingHandler extends MikrotikBaseThingHandler<Inte
             RouterosInterfaceBase rosInterface = routeros.findInterface(cfg.name);
             this.iface = rosInterface;
             if (rosInterface == null) {
-                String statusMsg = String.format("Interface %s is not found in RouterOS for thing %s", cfg.name,
+                String statusMsg = String.format("RouterOS interface %s is not found for thing %s", cfg.name,
                         getThing().getUID());
                 updateStatus(OFFLINE, GONE, statusMsg);
             } else {
@@ -184,12 +186,16 @@ public class MikrotikInterfaceThingHandler extends MikrotikBaseThingHandler<Inte
                         newState = getCapIterfaceChannelState(channelID);
                     } else if (iface instanceof RouterosWlanInterface) {
                         newState = getWlanIterfaceChannelState(channelID);
+                    } else if (iface instanceof RouterosPPPCliInterface) {
+                        newState = getPPPCliChannelState(channelID);
                     } else if (iface instanceof RouterosPPPoECliInterface) {
                         newState = getPPPoECliChannelState(channelID);
                     } else if (iface instanceof RouterosL2TPSrvInterface) {
                         newState = getL2TPSrvChannelState(channelID);
                     } else if (iface instanceof RouterosL2TPCliInterface) {
                         newState = getL2TPCliChannelState(channelID);
+                    } else if (iface instanceof RouterosLTEInterface) {
+                        newState = getLTEChannelState(channelID);
                     }
             }
         }
@@ -274,6 +280,22 @@ public class MikrotikInterfaceThingHandler extends MikrotikBaseThingHandler<Inte
         }
     }
 
+    protected State getPPPCliChannelState(String channelID) {
+        RouterosPPPCliInterface pppCli = (RouterosPPPCliInterface) this.iface;
+        if (pppCli == null) {
+            return UnDefType.UNDEF;
+        }
+
+        switch (channelID) {
+            case MikrotikBindingConstants.CHANNEL_STATE:
+                return StateUtil.stringOrNull(pppCli.getStatus());
+            case MikrotikBindingConstants.CHANNEL_UP_SINCE:
+                return StateUtil.timeOrNull(pppCli.getUptimeStart());
+            default:
+                return UnDefType.UNDEF;
+        }
+    }
+
     protected State getL2TPSrvChannelState(String channelID) {
         RouterosL2TPSrvInterface vpnSrv = (RouterosL2TPSrvInterface) this.iface;
         if (vpnSrv == null) {
@@ -306,6 +328,22 @@ public class MikrotikInterfaceThingHandler extends MikrotikBaseThingHandler<Inte
         }
     }
 
+    protected State getLTEChannelState(String channelID) {
+        RouterosLTEInterface lte = (RouterosLTEInterface) this.iface;
+        if (lte == null) {
+            return UnDefType.UNDEF;
+        }
+
+        switch (channelID) {
+            case MikrotikBindingConstants.CHANNEL_STATE:
+                return StateUtil.stringOrNull(lte.getStatus());
+            case MikrotikBindingConstants.CHANNEL_UP_SINCE:
+                return StateUtil.timeOrNull(lte.getUptimeStart());
+            default:
+                return UnDefType.UNDEF;
+        }
+    }
+
     @Override
     protected void executeCommand(ChannelUID channelUID, Command command) {
         if (iface == null) {
index 473613281671f8b9320191eb3917ba6c5cf5b477..0c5fe96ec77d64a92c667d7365f404eabd62cb6e 100644 (file)
@@ -87,10 +87,14 @@ public class RouterosDevice {
                 return Optional.of(new RouterosWlanInterface(interfaceProps));
             case PPPOE_CLIENT:
                 return Optional.of(new RouterosPPPoECliInterface(interfaceProps));
+            case PPP_CLIENT:
+                return Optional.of(new RouterosPPPCliInterface(interfaceProps));
             case L2TP_SERVER:
                 return Optional.of(new RouterosL2TPSrvInterface(interfaceProps));
             case L2TP_CLIENT:
                 return Optional.of(new RouterosL2TPCliInterface(interfaceProps));
+            case LTE:
+                return Optional.of(new RouterosLTEInterface(interfaceProps));
             default:
                 return Optional.empty();
         }
index 26d89becbdb4110a805a7e8a4c8c0ab9bcc1996e..be372161183fca5b1c2463a78a6e00ca5f60dcd3 100644 (file)
@@ -27,9 +27,11 @@ public enum RouterosInterfaceType {
     BRIDGE("bridge"),
     WLAN("wlan"),
     CAP("cap"),
+    PPP_CLIENT("ppp-out"),
     PPPOE_CLIENT("pppoe-out"),
     L2TP_SERVER("l2tp-in"),
-    L2TP_CLIENT("l2tp-out");
+    L2TP_CLIENT("l2tp-out"),
+    LTE("lte");
 
     private final String typeName;
 
diff --git a/bundles/org.openhab.binding.mikrotik/src/main/java/org/openhab/binding/mikrotik/internal/model/RouterosLTEInterface.java b/bundles/org.openhab.binding.mikrotik/src/main/java/org/openhab/binding/mikrotik/internal/model/RouterosLTEInterface.java
new file mode 100644 (file)
index 0000000..cf2815f
--- /dev/null
@@ -0,0 +1,69 @@
+/**
+ * Copyright (c) 2010-2021 Contributors to the openHAB project
+ *
+ * See the NOTICE file(s) distributed with this work for additional
+ * information.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License 2.0 which is available at
+ * http://www.eclipse.org/legal/epl-2.0
+ *
+ * SPDX-License-Identifier: EPL-2.0
+ */
+package org.openhab.binding.mikrotik.internal.model;
+
+import java.time.LocalDateTime;
+import java.util.Map;
+
+import org.eclipse.jdt.annotation.NonNullByDefault;
+import org.eclipse.jdt.annotation.Nullable;
+import org.openhab.binding.mikrotik.internal.util.Converter;
+
+/**
+ * The {@link RouterosLTEInterface} is a model class for `lte` interface models having casting accessors for
+ * data that is specific to this network interface kind. Is a subclass of {@link RouterosInterfaceBase}.
+ *
+ * @author Oleg Vivtash - Initial contribution
+ */
+@NonNullByDefault
+public class RouterosLTEInterface extends RouterosInterfaceBase {
+    public RouterosLTEInterface(Map<String, String> props) {
+        super(props);
+    }
+
+    @Override
+    public RouterosInterfaceType getDesignedType() {
+        return RouterosInterfaceType.LTE;
+    }
+
+    @Override
+    public String getApiType() {
+        return "lte";
+    }
+
+    @Override
+    public boolean hasDetailedReport() {
+        return false;
+    }
+
+    @Override
+    public boolean hasMonitor() {
+        return false;
+    }
+
+    public @Nullable String getStatus() {
+        // I only have an RNDIS/HiLink 4G modem which doesn't report status at all. This should be tested/fixed
+        // by someone who has PCIe/serial 4G modem.
+        return getProp("status");
+    }
+
+    public @Nullable String getUptime() {
+        // Same as above. Also a custom info command need to be implemented for this to work.
+        // https://forum.mikrotik.com/viewtopic.php?t=164035#p808281
+        return getProp("session-uptime");
+    }
+
+    public @Nullable LocalDateTime getUptimeStart() {
+        return Converter.routerosPeriodBack(getUptime());
+    }
+}
diff --git a/bundles/org.openhab.binding.mikrotik/src/main/java/org/openhab/binding/mikrotik/internal/model/RouterosPPPCliInterface.java b/bundles/org.openhab.binding.mikrotik/src/main/java/org/openhab/binding/mikrotik/internal/model/RouterosPPPCliInterface.java
new file mode 100644 (file)
index 0000000..e0bd6bb
--- /dev/null
@@ -0,0 +1,77 @@
+/**
+ * Copyright (c) 2010-2021 Contributors to the openHAB project
+ *
+ * See the NOTICE file(s) distributed with this work for additional
+ * information.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License 2.0 which is available at
+ * http://www.eclipse.org/legal/epl-2.0
+ *
+ * SPDX-License-Identifier: EPL-2.0
+ */
+package org.openhab.binding.mikrotik.internal.model;
+
+import java.time.LocalDateTime;
+import java.util.Map;
+
+import org.eclipse.jdt.annotation.NonNullByDefault;
+import org.eclipse.jdt.annotation.Nullable;
+import org.openhab.binding.mikrotik.internal.util.Converter;
+
+/**
+ * The {@link RouterosPPPCliInterface} is a model class for `pppoe-out` interface models having casting accessors for
+ * data that is specific to this network interface kind. Is a subclass of {@link RouterosInterfaceBase}.
+ *
+ * @author Oleg Vivtash - Initial contribution
+ */
+@NonNullByDefault
+public class RouterosPPPCliInterface extends RouterosInterfaceBase {
+    public RouterosPPPCliInterface(Map<String, String> props) {
+        super(props);
+    }
+
+    @Override
+    public RouterosInterfaceType getDesignedType() {
+        return RouterosInterfaceType.PPP_CLIENT;
+    }
+
+    @Override
+    public String getApiType() {
+        return "ppp-client";
+    }
+
+    @Override
+    public boolean hasDetailedReport() {
+        return false;
+    }
+
+    @Override
+    public boolean hasMonitor() {
+        return true;
+    }
+
+    public @Nullable String getMacAddress() {
+        return null;
+    }
+
+    public @Nullable String getLocalAddress() {
+        return getProp("local-address");
+    }
+
+    public @Nullable String getRemoteAddress() {
+        return getProp("remote-address");
+    }
+
+    public @Nullable String getStatus() {
+        return getProp("status");
+    }
+
+    public @Nullable String getUptime() {
+        return getProp("uptime");
+    }
+
+    public @Nullable LocalDateTime getUptimeStart() {
+        return Converter.routerosPeriodBack(getUptime());
+    }
+}