summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFlorian Baumann <derflob@derflob.de>2016-07-14 09:46:09 +1200
committerFlorian Baumann <derflob@derflob.de>2016-07-14 21:06:07 +1200
commitcf851705b54aa472ab0c2c35f7a2e01f3320a64f (patch)
tree35d3aa52ffbc3a3e69d220783480c06ff77bd95f
parent944ac24be819097090e6da40c89ca8596c8d0ecf (diff)
downloadxmpp-cf851705b54aa472ab0c2c35f7a2e01f3320a64f.tar.gz
xmpp-cf851705b54aa472ab0c2c35f7a2e01f3320a64f.tar.bz2
Send a Snapshot via HTTP Upload
-rw-r--r--__init__.py89
1 files changed, 66 insertions, 23 deletions
diff --git a/__init__.py b/__init__.py
index 5e33bce..f06e48c 100644
--- a/__init__.py
+++ b/__init__.py
@@ -1,49 +1,92 @@
# coding=utf-8
from __future__ import absolute_import
-from sleekxmpp import ClientXMPP
-import octoprint.plugin
+import io
+import requests
+import sleekxmpp
+
+from octoprint.plugin import EventHandlerPlugin, SettingsPlugin
+
+
+class OctoXMPP(EventHandlerPlugin, SettingsPlugin):
-class OctoXMPP(octoprint.plugin.EventHandlerPlugin,
- octoprint.plugin.SettingsPlugin):
def initialize(self):
- jid = self._settings.get(["username"])
- passwd = self._settings.get(["password"])
+ jid = self._settings.get(['xmpp', 'jid'])
+ resource = self._settings.get(['xmpp', 'resource'])
+ passwd = self._settings.get(['xmpp', 'password'])
- self._xmpp = ClientXMPP(jid, passwd)
+ full_jid = sleekxmpp.JID(jid, resource=resource)
+ self._xmpp = sleekxmpp.ClientXMPP(full_jid, passwd)
- def session_start(s):
- self._xmpp.send_presence(ptype="available")
+ def __session_start(event):
+ self._xmpp.send_presence()
self._xmpp.get_roster()
+ self._join_groupchats()
- self._xmpp.add_event_handler("session_start", session_start)
+ self._xmpp.add_event_handler('session_start', __session_start)
+
+ self._xmpp.register_plugin('xep_0030') # disco
+ self._xmpp.register_plugin('xep_0045') # muc
+ self._xmpp.register_plugin('xep_0066') # oob
+ self._xmpp.register_plugin('xep_0363') # http upload
self._xmpp.connect()
self._xmpp.process(threaded=True)
def get_settings_defaults(self):
- return dict(username="rupert@derflob.de", password="KosselMini", receivers=["derflob@derflob.de", "jabber@dermeb.de"])
+ return {
+ 'xmpp': {
+ 'jid': "rupert@m3k.cc",
+ 'resource': "Rupert",
+ 'password': "KosselMini",
+ 'recipients': [("groupchat", "3k@conference.m3k.cc"), ("chat", "derflob@derflob.de")]
+ }
+ }
+
+ def _msg_recipients(self, msg, send_image=True):
+ for (chat_type, recipient) in self._settings.get(['xmpp', 'recipients']):
+ self._xmpp.send_message(mto=recipient, mbody=msg, mtype=chat_type)
+
+ if (send_image):
+ snapshot = self._settings.global_get(['webcam', 'snapshot'])
+ if (snapshot):
+ req = requests.get(snapshot)
+
+ if (req.ok):
+ i = self._xmpp['xep_0363'].start_upload(
+ 'rupert.jpg', io.BytesIO(req.content), len(req.content), req.headers.get('content-type', None))
+
+ def __send_get_url(chat_type, recipient):
+ for (chat_type, recipient) in self._settings.get(['xmpp', 'recipients']):
+ m = self._xmpp.make_message(mto=recipient, mbody=get_url, mtype=chat_type)
+ m['oob']['url'] = get_url
+ m.send()
+
+ self._xmpp.add_event_handler("http_upload::{}::finished".format(i),
+ __send_get_url, disposable=True)
- def _msg_receivers(self, msg):
- for r in self._settings.get(["receivers"]):
- self._xmpp.send_message(mto=r, mbody=msg, mtype="chat")
+ def _join_groupchats(self):
+ nick = self._settings.get(['xmpp', 'resource'])
+ for (chat_type, recipient) in self._settings.get(['xmpp', 'recipients']):
+ if (chat_type = "groupchat"):
+ self._xmpp['xep_0045'].joinMUC(recipient, nick)
def on_event(self, event, data):
if (event == "PrintDone"):
- self._msg_receivers("Zack, Feditsch!")
+ self._msg_recipients("Zack, Feditsch!")
elif (event == "PrintFailed"):
- self._msg_receivers("Da ging was schief!")
+ self._msg_recipients("Da ging was schief!")
elif (event == "PrintPaused"):
- self._msg_receivers("Druck pausiert.")
+ self._msg_recipients("Druck pausiert.", False)
elif (event == "PrintResumed"):
- self._msg_receivers("Druck resümiert.")
+ self._msg_recipients("Druck resümiert.", False)
elif (event == "PrintCancelled"):
- self._msg_receivers("Druck abgebrochen!")
+ self._msg_recipients("Druck abgebrochen!")
elif (event == "PrintStarted"):
- self._msg_receivers("Druck begonnen.")
- #else:
- # self._xmpp.send_message(mto="derflob@derflob.de", mbody=event, mtype="chat")
+ self._msg_recipients("Druck begonnen.")
+ elif (event == "Connected"):
+ self._msg_recipients("Connected to Rupert.")
__plugin_name__ = "OctoXMPP"
-__plugin_version__ = "0.1.1"
+__plugin_version__ = "0.2.0"
__plugin_description__ = "Notify about some Events over XMPP."
__plugin_implementation__ = OctoXMPP()