Skip to content

Commit

Permalink
v1.16.2
Browse files Browse the repository at this point in the history
version 1.16.2
  • Loading branch information
thusser authored Aug 20, 2024
2 parents 6528f07 + 8ce5a65 commit eadec3b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 10 deletions.
50 changes: 41 additions & 9 deletions pyobs/comm/xmpp/xmppcomm.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def __init__(
resource: Resource part of the JID.
password: Password for given JID.
server: Server to connect to. If not given, domain from JID is used.
use_tls: Whether or not to use TLS.
use_tls: Whether to use TLS.
"""
Comm.__init__(self, *args, **kwargs)

Expand Down Expand Up @@ -169,12 +169,23 @@ async def open(self) -> None:
Whether opening was successful.
"""

# connect
await self._connect()

# subscribe to events
await self.register_event(LogEvent)

# open Comm
await Comm.open(self)

async def _connect(self) -> None:
# create client
self._xmpp = XmppClient(self._jid, self._password)
# self._xmpp = slixmpp.ClientXMPP(self._jid, password)
self._xmpp.add_event_handler("pubsub_publish", self._handle_event)
self._xmpp.add_event_handler("got_online", self._got_online)
self._xmpp.add_event_handler("got_offline", self._got_offline)
self._xmpp.add_event_handler("disconnected", self._disconnected)

# server given?
server = () if self._server is None else tuple(self._server.split(":"))
Expand All @@ -198,12 +209,6 @@ async def open(self) -> None:
self.module.quit()
return

# subscribe to events
await self.register_event(LogEvent)

# open Comm
await Comm.open(self)

# wait a little and finished
await asyncio.sleep(1)
self._connected = True
Expand All @@ -217,6 +222,25 @@ async def close(self) -> None:
# disconnect from sleekxmpp server
await self._xmpp.disconnect()

async def _reconnect(self):
"""Sleep a little and reconnect"""
await asyncio.sleep(2)
await self._connect()

def _disconnected(self, event: Any):
"""Reset connection after disconnect."""
log.info("Disconnected from server, waiting for reconnect...")

# disconnect all clients
for jid in self._online_clients:
self._jid_got_offline(jid)

# reconnect
asyncio.ensure_future(
self._reconnect(),
loop=self._loop,
)

@property
def name(self) -> Optional[str]:
"""Name of this client."""
Expand Down Expand Up @@ -391,9 +415,16 @@ def _got_offline(self, msg: Any) -> None:
Args:
msg: XMPP message.
"""
self._jid_got_offline(msg["from"].full)

def _jid_got_offline(self, jid: str) -> None:
"""If a new client disconnects, remove it from list.
Args:
jid: JID that got offline.
"""

# remove from list
jid = msg["from"].full
if jid in self._online_clients:
self._online_clients.remove(jid)

Expand All @@ -402,7 +433,8 @@ def _got_offline(self, msg: Any) -> None:
del self._interface_cache[jid]

# send event
self._send_event_to_module(ModuleClosedEvent(), msg["from"].username)
username = jid[: jid.find("@")]
self._send_event_to_module(ModuleClosedEvent(), username)

@property
def clients(self) -> List[str]:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[tool.poetry]
name = "pyobs-core"
packages = [{ include = "pyobs" }]
version = "1.16.1"
version = "1.16.2"
description = "robotic telescope software"
authors = ["Tim-Oliver Husser <[email protected]>"]
license = "MIT"
Expand Down

0 comments on commit eadec3b

Please sign in to comment.