Skip to content

Commit

Permalink
Removed aiortc from dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
glennerichall committed Mar 6, 2024
1 parent 1371d16 commit c74ca81
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 151 deletions.
4 changes: 2 additions & 2 deletions octoprint_zupfe/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from .startup import start_push_poll_loops, initialize_plugin
from octoprint_zupfe.loops.temperature_manager import TemperatureManager
from octoprint_zupfe.wrappers.webcam_wrapper import WebcamWrapper
from octoprint_zupfe.transport.webrtc import AIORTC_AVAILABLE, accept_webrtc_offer, get_webrtc_reply
# from octoprint_zupfe.transport.webrtc import AIORTC_AVAILABLE, accept_webrtc_offer, get_webrtc_reply
from .worker import AsyncTaskWorker
from octoprint_zupfe.messaging.message_factory import MessageFactory
from .zupfe_api import ZupfeApiPlugin
Expand Down Expand Up @@ -62,7 +62,7 @@ def __init__(self):
@property
def version(self):
# TODO get the version from setup.py
return "o.0.1.0"
return "o.0.1.1"

@property
def host(self):
Expand Down
46 changes: 23 additions & 23 deletions octoprint_zupfe/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,33 @@
get_command_name, RPC_REQUEST_RECEIVE_PROGRESS, RPC_REQUEST_STOP_PROGRESS, RPC_REQUEST_STOP_TEMPERATURES, \
RPC_REQUEST_READ_TEMPERATURES
from octoprint_zupfe.transport.request import request_get
from octoprint_zupfe.transport.webrtc import AIORTC_AVAILABLE, accept_webrtc_offer, get_webrtc_reply
# from octoprint_zupfe.transport.webrtc import AIORTC_AVAILABLE, accept_webrtc_offer, get_webrtc_reply

logger = logging.getLogger("octoprint.plugins.zupfe")


def handle_message(plugin, message, reply, reject, transport):
async def on_request_p2p():
logger.debug("Receiving webrtc offer")
offer = message.json()
if AIORTC_AVAILABLE:
try:
logger.debug("Setting-up ICE connection")
# if the offer is accepted, webrtc data channel will call back the lambda
# which in return will call back the current "handle_message" handler, letting
# webbsocket and webrtc use the same route for message handling.
p2p = await accept_webrtc_offer(plugin,
lambda _message, _reply, _reject, _transport: handle_message(plugin, _message, _reply, _reject, _transport),
offer)
answer = get_webrtc_reply(p2p)
logger.debug("Replying webrtc answer")
reply(answer)
except Exception as e:
logger.debug("Unable top reply answer " + str(e))
reply(None)
else:
logger.debug("Aiortc is unavailable, denying offer")
reply(None)
# async def on_request_p2p():
# logger.debug("Receiving webrtc offer")
# offer = message.json()
# if AIORTC_AVAILABLE:
# try:
# logger.debug("Setting-up ICE connection")
# # if the offer is accepted, webrtc data channel will call back the lambda
# # which in return will call back the current "handle_message" handler, letting
# # webbsocket and webrtc use the same route for message handling.
# p2p = await accept_webrtc_offer(plugin,
# lambda _message, _reply, _reject, _transport: handle_message(plugin, _message, _reply, _reject, _transport),
# offer)
# answer = get_webrtc_reply(p2p)
# logger.debug("Replying webrtc answer")
# reply(answer)
# except Exception as e:
# logger.debug("Unable top reply answer " + str(e))
# reply(None)
# else:
# logger.debug("Aiortc is unavailable, denying offer")
# reply(None)

async def on_linked():
plugin.settings.save_if_updated('linked', True)
Expand Down Expand Up @@ -257,7 +257,7 @@ async def on_request_stop_temperatures():
rpc_handlers = {
RPC_REQUEST_GET_FILE_LIST: on_request_file_list,
RPC_REQUEST_STREAM: on_request_file_stream,
RPC_REQUEST_WEBRTC: on_request_p2p,
# RPC_REQUEST_WEBRTC: on_request_p2p,
RPC_REQUEST_GET_STATE: on_request_state,
RPC_REQUEST_PRINT_ACTIVE_FILE: on_request_print_active_file,
RPC_REQUEST_SET_ACTIVE_FILE: on_request_set_active_file,
Expand Down
248 changes: 124 additions & 124 deletions octoprint_zupfe/transport/webrtc.py
Original file line number Diff line number Diff line change
@@ -1,124 +1,124 @@
import asyncio
import logging
import uuid
from asyncio import Future

from octoprint_zupfe.messaging.message_builder import MessageBuilder

logger = logging.getLogger("octoprint.plugins.zupfe")

try:
from aiortc import RTCPeerConnection, RTCSessionDescription

logger.debug("Loaded aiortc successfully")
AIORTC_AVAILABLE = True

except ImportError as e:
AIORTC_AVAILABLE = False
logger.debug("Loading aiortc failed with error: " + str(e))


class RTCPeerConnection:
def __init__(self, *args, **kwargs):
raise NotImplementedError("RTCPeerConnection is not available due to import error")


class RTCSessionDescription:
def __init__(self, *args, **kwargs):
raise NotImplementedError("RTCSessionDescription is not available due to import error")

from octoprint_zupfe.constants import RPC_REQUEST_STREAM
from octoprint_zupfe.transport.request import create_reply, create_stream, create_rejection


# logger = logging.getLogger('aiortc')
# logger.setLevel(logging.DEBUG)

def get_webrtc_reply(peer_connection):
return {
"type": peer_connection.localDescription.type,
"sdp": peer_connection.localDescription.sdp
}


class WebrtcClient:
def __init__(self, channel, uuid, worker):
self.channel = channel
self._close_callbacks = []
self._uuid = uuid
self._worker = worker


@property
def type(self):
return "webrtc"

def send_binary(self, data):
self._worker.submit_coroutines(self.send_async(data))

async def send_async(self, data):
self.channel.send(data)

def on_close(self, callback):
self._close_callbacks.append(callback)
return lambda: self._close_callbacks.remove(callback)

def close(self):
logger.debug(f"WebRTC data channel closed for {self.uuid}")
for callback in self._close_callbacks:
callback(self)

@property
def uuid(self):
return self._uuid


async def accept_webrtc_offer(plugin, on_message, offer):
peer_connection = RTCPeerConnection()
remote_description = RTCSessionDescription(sdp=offer['sdp'], type=offer['type'])
await peer_connection.setRemoteDescription(remote_description)

webrtc_uuid = str(uuid.uuid4())

@peer_connection.on("datachannel")
def on_datachannel(channel):
transport = WebrtcClient(channel, webrtc_uuid, plugin.worker)

@channel.on("close")
def on_close():
logger.debug('WebRTC Channel closed')

@peer_connection.on("iceconnectionstatechange")
async def on_iceconnectionstatechange():
logger.debug(f'WebRTC Ice connection state changed, {peer_connection.iceConnectionState}')
if (peer_connection.iceConnectionState == 'disconnected' or
peer_connection.iceConnectionState == 'failed') :
transport.close()

@channel.on("message")
def on_channel_message(message):
message = MessageBuilder().unpack(message)

if message.command == RPC_REQUEST_STREAM:
reply = create_stream(transport, message)
else:
reply = create_reply(transport, message)

reject = create_rejection(transport, message)
on_message(message, reply, reject, transport)

local_description = await peer_connection.createAnswer()
await peer_connection.setLocalDescription(local_description)

future = Future()

if peer_connection.iceGatheringState == 'complete':
future.set_result(peer_connection)
else:
@peer_connection.on("icegatheringstatechange")
def on_icegatheringstatechange():
if peer_connection.iceGatheringState == 'complete':
future.set_result(peer_connection)
peer_connection.remove_listener('icegatheringstatechange', on_icegatheringstatechange)

return await future
# import asyncio
# import logging
# import uuid
# from asyncio import Future
#
# from octoprint_zupfe.messaging.message_builder import MessageBuilder
#
# logger = logging.getLogger("octoprint.plugins.zupfe")
#
# try:
# from aiortc import RTCPeerConnection, RTCSessionDescription
#
# logger.debug("Loaded aiortc successfully")
# AIORTC_AVAILABLE = True
#
# except ImportError as e:
# AIORTC_AVAILABLE = False
# logger.debug("Loading aiortc failed with error: " + str(e))
#
#
# class RTCPeerConnection:
# def __init__(self, *args, **kwargs):
# raise NotImplementedError("RTCPeerConnection is not available due to import error")
#
#
# class RTCSessionDescription:
# def __init__(self, *args, **kwargs):
# raise NotImplementedError("RTCSessionDescription is not available due to import error")
#
# from octoprint_zupfe.constants import RPC_REQUEST_STREAM
# from octoprint_zupfe.transport.request import create_reply, create_stream, create_rejection
#
#
# # logger = logging.getLogger('aiortc')
# # logger.setLevel(logging.DEBUG)
#
# def get_webrtc_reply(peer_connection):
# return {
# "type": peer_connection.localDescription.type,
# "sdp": peer_connection.localDescription.sdp
# }
#
#
# class WebrtcClient:
# def __init__(self, channel, uuid, worker):
# self.channel = channel
# self._close_callbacks = []
# self._uuid = uuid
# self._worker = worker
#
#
# @property
# def type(self):
# return "webrtc"
#
# def send_binary(self, data):
# self._worker.submit_coroutines(self.send_async(data))
#
# async def send_async(self, data):
# self.channel.send(data)
#
# def on_close(self, callback):
# self._close_callbacks.append(callback)
# return lambda: self._close_callbacks.remove(callback)
#
# def close(self):
# logger.debug(f"WebRTC data channel closed for {self.uuid}")
# for callback in self._close_callbacks:
# callback(self)
#
# @property
# def uuid(self):
# return self._uuid
#
#
# async def accept_webrtc_offer(plugin, on_message, offer):
# peer_connection = RTCPeerConnection()
# remote_description = RTCSessionDescription(sdp=offer['sdp'], type=offer['type'])
# await peer_connection.setRemoteDescription(remote_description)
#
# webrtc_uuid = str(uuid.uuid4())
#
# @peer_connection.on("datachannel")
# def on_datachannel(channel):
# transport = WebrtcClient(channel, webrtc_uuid, plugin.worker)
#
# @channel.on("close")
# def on_close():
# logger.debug('WebRTC Channel closed')
#
# @peer_connection.on("iceconnectionstatechange")
# async def on_iceconnectionstatechange():
# logger.debug(f'WebRTC Ice connection state changed, {peer_connection.iceConnectionState}')
# if (peer_connection.iceConnectionState == 'disconnected' or
# peer_connection.iceConnectionState == 'failed') :
# transport.close()
#
# @channel.on("message")
# def on_channel_message(message):
# message = MessageBuilder().unpack(message)
#
# if message.command == RPC_REQUEST_STREAM:
# reply = create_stream(transport, message)
# else:
# reply = create_reply(transport, message)
#
# reject = create_rejection(transport, message)
# on_message(message, reply, reject, transport)
#
# local_description = await peer_connection.createAnswer()
# await peer_connection.setLocalDescription(local_description)
#
# future = Future()
#
# if peer_connection.iceGatheringState == 'complete':
# future.set_result(peer_connection)
# else:
# @peer_connection.on("icegatheringstatechange")
# def on_icegatheringstatechange():
# if peer_connection.iceGatheringState == 'complete':
# future.set_result(peer_connection)
# peer_connection.remove_listener('icegatheringstatechange', on_icegatheringstatechange)
#
# return await future
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
plugin_name = "OctoPrint-Zupfe"

# The plugin's version. Can be overwritten within OctoPrint's internal data via __plugin_version__ in the plugin module
plugin_version = "0.1.0"
plugin_version = "0.1.1"

# The plugin's description. Can be overwritten within OctoPrint's internal data via __plugin_description__ in the plugin
# module
Expand Down Expand Up @@ -61,7 +61,7 @@
# you would like to support Python 2 as well as 3 (not recommended).
additional_setup_parameters = {"python_requires": ">=3.7,<4"}

plugin_requires = ["websocket_client>=1.5.1", "aiohttp>=3.8.4", "aiortc>=1.5.0"]
plugin_requires = ["websocket_client>=1.5.1", "aiohttp>=3.8.4"]

########################################################################################################################

Expand Down

0 comments on commit c74ca81

Please sign in to comment.