Skip to content
This repository has been archived by the owner on May 2, 2023. It is now read-only.

Commit

Permalink
Merge pull request #5 from PeSme/discover_on_all_networks
Browse files Browse the repository at this point in the history
enable discovery on all network interfaces
  • Loading branch information
macdylan authored May 31, 2022
2 parents 7b6a33d + 3f63080 commit 73191d3
Showing 1 changed file with 25 additions and 13 deletions.
38 changes: 25 additions & 13 deletions SM2OutputDeviceManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@
QHttpPart,
QNetworkRequest,
QNetworkAccessManager,
QNetworkReply)
QNetworkReply,
QNetworkInterface,
QAbstractSocket
)
QNetworkAccessManagerOperations = QNetworkAccessManager.Operation
QNetworkRequestAttributes = QNetworkRequest.Attribute
QNetworkReplyNetworkErrors = QNetworkReply.NetworkError
QHostAddressAny = QHostAddress.SpecialAddress.AnyIPv4
QHostAddressBroadcast = QHostAddress.SpecialAddress.Broadcast
QIPv4Protocol = QAbstractSocket.NetworkLayerProtocol.IPv4Protocol
except ImportError:
from PyQt5.QtCore import QTimer
from PyQt5.QtNetwork import (
Expand All @@ -24,13 +27,15 @@
QHttpPart,
QNetworkRequest,
QNetworkAccessManager,
QNetworkReply
QNetworkReply,
QNetworkInterface,
QAbstractSocket
)
QNetworkAccessManagerOperations = QNetworkAccessManager
QNetworkRequestAttributes = QNetworkRequest
QNetworkReplyNetworkErrors = QNetworkReply
QHostAddressAny = QHostAddress.SpecialAddress.AnyIPv4
QHostAddressBroadcast = QHostAddress.SpecialAddress.Broadcast
QIPv4Protocol = QAbstractSocket.NetworkLayerProtocol.IPv4Protocol

from cura.CuraApplication import CuraApplication
from cura.PrinterOutput.NetworkedPrinterOutputDevice import NetworkedPrinterOutputDevice, AuthState
Expand Down Expand Up @@ -60,9 +65,15 @@ class SM2OutputDeviceManager(OutputDevicePlugin):

def __init__(self):
super().__init__()
self._discover_socket = QUdpSocket()
self._discover_socket.bind(QHostAddressAny)
self._discover_socket.readyRead.connect(self._udpProcessor)

self._discover_sockets = []
for ipAddress in QNetworkInterface.allAddresses():
if not ipAddress.isLoopback() and ipAddress.protocol() == QIPv4Protocol:
Logger.log("i", "Discovering printers on network interface: {}".format(ipAddress.toString()))
socket = QUdpSocket()
socket.bind(ipAddress)
socket.readyRead.connect(lambda: self._udpProcessor(socket))
self._discover_sockets.append(socket)

self._discover_timer = QTimer()
self._discover_timer.setInterval(6000)
Expand Down Expand Up @@ -94,8 +105,8 @@ def start(self):
Logger.log("i", "Snapmaker2Plugin started.")

def stop(self):
if self._discover_socket:
self._discover_socket.abort()
for socket in self._discover_sockets:
socket.abort()
if self._discover_timer and self._discover_timer.isActive():
self._discover_timer.stop()
self._saveTokens()
Expand All @@ -121,10 +132,10 @@ def startDiscovery(self):
Logger.log("i", "Discovering ...")
self._onDiscovering()

def _udpProcessor(self):
def _udpProcessor(self, socket):
devices = set()
while self._discover_socket.hasPendingDatagrams():
data = self._discover_socket.receiveDatagram()
while socket.hasPendingDatagrams():
data = socket.receiveDatagram()
if data.isValid() and not data.senderAddress().isNull():
ip = data.senderAddress().toString()
try:
Expand All @@ -141,7 +152,8 @@ def _udpProcessor(self):
self.discoveredDevicesChanged.emit()

def _onDiscovering(self, *args, **kwargs):
self._discover_socket.writeDatagram(b"discover", QHostAddressBroadcast, 20054)
for socket in self._discover_sockets:
socket.writeDatagram(b"discover", QHostAddressBroadcast, 20054)
self._saveTokens() # TODO

def addOutputDevice(self):
Expand Down

0 comments on commit 73191d3

Please sign in to comment.