Skip to content

Commit a3556f5

Browse files
author
Andy Werner
committed
v0.1.1: Added network IP addresses
1 parent 3d0ce1a commit a3556f5

File tree

4 files changed

+54
-10
lines changed

4 files changed

+54
-10
lines changed

octoprint_netconnectd/__init__.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88

99
import logging
10+
import netifaces
1011
from flask import jsonify, make_response
1112

1213
import octoprint.plugin
@@ -45,15 +46,14 @@ def get_settings_defaults(self):
4546
socket="/var/run/netconnectd.sock",
4647
hostname=None,
4748
forwardUrl='http://find.mr-beam.org',
48-
# ANDYTEST TODO: check timeouts
4949
timeout=80
5050
)
5151

5252
##~~ TemplatePlugin API
5353

5454
def get_template_configs(self):
5555
return [
56-
dict(type="settings", name="Network connection")
56+
dict(type="settings", name="Network Connection")
5757
]
5858

5959
##~~ SimpleApiPlugin API
@@ -85,7 +85,11 @@ def on_api_get(self, request):
8585
wifis=wifis,
8686
status=status,
8787
hostname=self.hostname,
88-
forwardUrl=self.forwardUrl
88+
forwardUrl=self.forwardUrl,
89+
ip_addresses=dict(
90+
eth0=self._get_ip_address('eth0'),
91+
wlan0=self._get_ip_address('wlan0')
92+
)
8993
))
9094

9195
def on_api_command(self, command, data, adminRequired=True):
@@ -238,6 +242,17 @@ def _send_message(self, message, data):
238242
finally:
239243
sock.close()
240244

245+
def _get_ip_address(self, interface):
246+
"""
247+
Returns the external IP address of the given interface
248+
:param interface:
249+
:return: String IP
250+
"""
251+
try:
252+
return netifaces.ifaddresses(interface)[netifaces.AF_INET][0]['addr']
253+
except:
254+
pass
255+
241256
__plugin_name__ = "Netconnectd Client"
242257

243258

octoprint_netconnectd/static/js/netconnectd.js

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@ $(function() {
3131
current_ssid: ko.observable(),
3232
current_address: ko.observable(),
3333
present: ko.observable()
34-
}
34+
},
35+
ip_addresses: {
36+
eth0: ko.observable(),
37+
wlan0: ko.observable(),
38+
}
3539
};
3640
self.statusCurrentWifi = ko.observable();
3741

@@ -79,7 +83,27 @@ $(function() {
7983

8084
return text;
8185
});
82-
86+
87+
self.connectionStateTextEthernet = ko.computed(function() {
88+
if (self.status.connections.wired()) {
89+
return "Connected (IP: " + self.status.ip_addresses.eth0() + ")";
90+
} else {
91+
return "Not connected";
92+
}
93+
});
94+
95+
self.connectionStateTextWifi = ko.computed(function() {
96+
if (self.status.connections.ap()) {
97+
return "Access point (IP: " + self.status.ip_addresses.wlan0() + ")";
98+
} else if (self.status.connections.wifi() && self.status.wifi.current_ssid()) {
99+
return _.sprintf(gettext("Connected to \"%(ssid)s\" (IP: %(ip)s)"), {ssid: self.status.wifi.current_ssid(), ip: self.status.ip_addresses.wlan0()});
100+
} else if (self.status.connections.wifi()) {
101+
return _.sprintf(gettext("Connected to unknown wifi (IP: %(ip)s)"), {ssid: self.status.wifi.current_ssid(), ip: self.status.ip_addresses.wlan0()});
102+
} else {
103+
return "Access point down, not connected";
104+
}
105+
});
106+
83107
self.onAllBound = function(allViewModels) {
84108
self.allViewModels = allViewModels;
85109
}
@@ -159,6 +183,8 @@ $(function() {
159183
self.status.wifi.current_ssid(response.status.wifi.current_ssid);
160184
self.status.wifi.current_address(response.status.wifi.current_address);
161185
self.status.wifi.present(response.status.wifi.present);
186+
self.status.ip_addresses.eth0(response.ip_addresses.eth0);
187+
self.status.ip_addresses.wlan0(response.ip_addresses.wlan0);
162188

163189
self.statusCurrentWifi(undefined);
164190
if (response.status.wifi.current_ssid && response.status.wifi.current_address) {

octoprint_netconnectd/templates/netconnectd_settings.jinja2

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
<div id="settings_plugin_netconnectd_dialog" data-bind="allowBindings: true">
1+
<div id="settings_plugin_netconnectd_dialog" data-bind="allowBindings: true" class="scrollable" style="padding-bottom:2em; overflow-y: auto; height: calc(100vh - 100px);">
22
<p>
3-
<strong>{{ _('Connection state') }}:</strong> <span data-bind="text: connectionStateText"></span>
3+
<strong>{{ _('Ethernet') }}:</strong> <span data-bind="text: connectionStateTextEthernet"></span>
4+
</p>
5+
<p>
6+
<strong>{{ _('Wifi') }}:</strong> <span data-bind="text: connectionStateTextWifi"></span>
47
</p>
5-
68
<div class="pull-right" data-bind="visible: enableQualitySorting">
79
<small>
810
{{ _('Sort by') }}: <a href="#" data-bind="click: function() { listHelper.changeSorting('ssid'); }">{{ _('SSID') }} ({{ _('ascending') }})</a> | <a href="#" data-bind="click: function() { listHelper.changeSorting('quality'); }">{{ _('Quality') }} ({{ _('descending') }})</a>

setup.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
setuptools.setup(**octoprint_setuptools.create_plugin_setup_parameters(
66
identifier="netconnectd",
77
name="OctoPrint-Netconnectd",
8-
version="0.1",
8+
version="0.1.1",
99
description="Client for netconnectd that allows configuration of netconnectd through OctoPrint's settings dialog. It's only available for Linux right now.",
1010
author="Gina Häußge",
1111
1212
url="http://github.com/OctoPrint/OctoPrint-Netconnectd",
1313
requires=[
14-
"OctoPrint"
14+
"OctoPrint",
15+
"netifaces"
1516
]
1617
))

0 commit comments

Comments
 (0)