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 09:46:09 +1200
commitae9df80edaf0db2481d8e3272426c9bcc59f573d (patch)
tree6ac090022f4d716115c5cbdd411eba96336aea2d
parent520c7e61cfc855e8576fd386faaf533bac28b38b (diff)
downloadxmpp-ae9df80edaf0db2481d8e3272426c9bcc59f573d.tar.gz
xmpp-ae9df80edaf0db2481d8e3272426c9bcc59f573d.tar.bz2
Send a Snapshot via HTTP Upload
-rw-r--r--__init__.py65
1 files changed, 50 insertions, 15 deletions
diff --git a/__init__.py b/__init__.py
index 4157a0b..2286005 100644
--- a/__init__.py
+++ b/__init__.py
@@ -1,26 +1,63 @@
# coding=utf-8
from __future__ import absolute_import
+import io
+import requests
from sleekxmpp import ClientXMPP
-import octoprint.plugin
+from octoprint.plugin import EventHandlerPlugin, SettingsPlugin
-class OctoXMPP(octoprint.plugin.EventHandlerPlugin,
- octoprint.plugin.SettingsPlugin):
+
+class OctoXMPP(EventHandlerPlugin, 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"])
+
+ full_jid = sleekxmpp.JID(jid, resource=resource)
+ self._xmpp = ClientXMPP(full_jid, passwd)
- self._xmpp = ClientXMPP(jid, passwd)
self._xmpp.add_event_handler("session_start", self._xmpp.send_presence)
+ 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(block=False)
def get_settings_defaults(self):
- return dict(jid="rupert@derflob.de", password="KosselMini", receivers=["derflob@derflob.de", "jabber@dermeb.de"])
+ return {
+ 'xmpp': {
+ jid: "rupert@derflob.de",
+ resource: "Rupert",
+ password: "KosselMini",
+ receivers: [("groupchat", "m3k@conference.m3k.cc")]
+ }
+ }
+
+ def _msg_receivers(self, msg, send_image=True):
+ for (t, r) in self._settings.get(["xmpp", "receivers"]):
+
+ if (t == "groupchat"):
+ nick = self._settings.get(["xmpp", "resource"])
+ self._xmpp["xep_0045"].joinMUC(r, nick, wait=True)
+
+ self._xmpp.send_message(mto=r, mbody=msg, mtype=t)
+
+ if (send_image):
+ snapshot = self._settings.get(["webcam", "snapshot"])
+ if (snapshot):
+ r = requests.get(snapshot)
+ if (r.ok):
+ i = self._xmpp['xep_0363'].start_upload(
+ 'rupert.jpg', io.BytesIO(r.content), len(r.content), r.headers.get('content-type', None))
+
+ def __send_get_url(get_url):
+ m = self._xmpp.make_message(mto=r, mbody=get_url, mtype=t)
+ m['oob']['url'] = get_url
+ m.send()
- def _msg_receivers(self, msg):
- for r in self._settings.get(["receivers"]):
- self._xmpp.send_message(mto=r, mbody=msg)
+ self._xmpp.add_event_handler("http_upload::{}:finished".format(i), __send_get_url)
def on_event(self, event, data):
if (event == "PrintDone"):
@@ -28,15 +65,13 @@ class OctoXMPP(octoprint.plugin.EventHandlerPlugin,
elif (event == "PrintFailed"):
self._msg_receivers("Da ging was schief!")
elif (event == "PrintPaused"):
- self._msg_receivers("Druck pausiert.")
+ self._msg_receivers("Druck pausiert.", False)
elif (event == "PrintResumed"):
- self._msg_receivers("Druck resümiert.")
+ self._msg_receivers("Druck resümiert.", False)
elif (event == "PrintCancelled"):
self._msg_receivers("Druck abgebrochen!")
- else:
- self._xmpp.send_message(mto="derflob@derflob.de", mbody=event)
__plugin_name__ = "OctoXMPP"
-__plugin_version__ = "0.1.0"
+__plugin_version__ = "0.2.0"
__plugin_description__ = "Notify about some Events over XMPP."
__plugin_implementation__ = OctoXMPP()