Skip to content

Commit

Permalink
Merge pull request #125 from Bart1909/FilamentManger-Integration
Browse files Browse the repository at this point in the history
Filament manager integration
  • Loading branch information
rlogiacco authored Jul 16, 2019
2 parents 6d43580 + 2450f8c commit 2aa530f
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ If you already have a bot, you only need your bot token to proceed. GOTO `4.` (o
status - Sends the current status including a current photo.
settings - Displays the current notification settings and allows you to change them.
files - Lists all the files available for printing.
filament - Shows you your filament spools or lets you change it.
print - Lets you start a print. A confirmation is required.
togglepause - Pause/Resume current Print.
con - Connect/disconnect printer.
Expand Down Expand Up @@ -216,6 +217,8 @@ In this section you can configure the content of the notification messages.

**`/files`** - Lists all the files available for printing in upload folder and allows you download and delete them. You also can view detailed informations of the file like print history. If OctoPrint Version is >= 1.3.0, subdirectories are listed and you are able to move/copy files.

**`/filament`** - Shows your filament spools and the percentage of usage. In addition you can change the filament spool with this command. Requires the [Filament Manager Plugin](https://plugins.octoprint.org/plugins/filamentmanager/)

**`/print`** - Will open a file dialog showing the files stored in octoprint. You can select a file to print it.

**`/togglepause`** - Pause/resume the current print.
Expand Down
96 changes: 96 additions & 0 deletions octoprint_telegram/telegramCommands.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import absolute_import
import logging, sarge, hashlib, datetime,time,operator
import octoprint.filemanager
import requests
from flask.ext.babel import gettext
from .telegramNotifications import telegramMsgDict

Expand Down Expand Up @@ -34,6 +35,7 @@ def __init__(self, main):
'/print': {'cmd': self.cmdPrint, 'param': True},
'/files': {'cmd': self.cmdFiles, 'param': True},
'/upload': {'cmd': self.cmdUpload},
'/filament': {'cmd': self.cmdFilament, 'param': True},
'/sys': {'cmd': self.cmdSys, 'param': True},
'/ctrl': {'cmd': self.cmdCtrl, 'param': True},
'/con': {'cmd': self.cmdConnection, 'param': True},
Expand Down Expand Up @@ -537,6 +539,99 @@ def cmdTune(self,chat_id,from_id,cmd,parameter):
keys.append([[self.main.emojis['cross mark']+gettext(" Close"),"No"]])
self.main.send_msg(msg, responses=keys,chatID=chat_id,msg_id=msg_id,markup="Markdown")
############################################################################################
def cmdFilament(self,chat_id,from_id,cmd,parameter):
if parameter and parameter != "back":
self._logger.info("Parameter received for filament: %s" % parameter)
params = parameter.split('_')
apikey = self.main._settings.global_get(['api','key'])
errorText = ""
if params[0] == "spools":
try:
resp = requests.get("http://localhost/plugin/filamentmanager/spools?apikey="+apikey)
resp2 = requests.get("http://localhost/plugin/filamentmanager/selections?apikey="+apikey)
if (resp.status_code != 200):
errorText = resp.text
resp = resp.json()
resp2 = resp2.json()
self._logger.info("Spools: %s" % resp["spools"])
message = self.gEmo('info') + " The following Spools are known:\n"
for spool in resp["spools"]:
weight = spool["weight"]
used = spool["used"]
usedPercent = int(used / weight * 100)
message += str(spool["name"]) + "- Used: " + str(usedPercent) + "%\n"
for selection in resp2["selections"]:
if selection["tool"] == 0:
message += "\n\nCurrently selected is spool: " + str(selection["spool"]["name"])
msg_id=self.main.getUpdateMsgId(chat_id)
self.main.send_msg(message,chatID=chat_id,msg_id = msg_id,inline=False)
except ValueError:
message = self.gEmo('mistake')+" Error getting spools. Are you sure, you have installed the Filament Manager Plugin?"
if (errorText != ""):
message += "\nError text: " + str(errorText)
msg_id=self.main.getUpdateMsgId(chat_id)
self.main.send_msg(message,chatID=chat_id,msg_id = msg_id,inline=False)
if params[0] == "changeSpool":
self._logger.info("Command to change spool: %s" % params)
if len(params) > 1:
self._logger.info("Changing to spool: %s" % params[1])
try:
payload = {"selection": {"spool": {"id": params[1]},"tool": 0}}
self._logger.info("Payload: %s" % payload)
resp = requests.patch("http://localhost/plugin/filamentmanager/selections/0?apikey="+apikey, json=payload, headers={'Content-Type': 'application/json'})
if (resp.status_code != 200):
errorText = resp.text
self._logger.info("Response: %s" % resp)
resp = resp.json()
message = self.gEmo('check')+"Selected Spool is now: " + str(resp["selection"]["spool"]["name"])
msg_id=self.main.getUpdateMsgId(chat_id)
self.main.send_msg(message,chatID=chat_id,msg_id = msg_id,inline=False)
except ValueError:
message = self.gEmo('mistake')+" Error changing spool"
if (errorText != ""):
message += "\nError text: " + str(errorText)
msg_id=self.main.getUpdateMsgId(chat_id)
self.main.send_msg(message,chatID=chat_id,msg_id = msg_id,inline=False)
else:
self._logger.info("Asking for spool")
try:
resp = requests.get("http://localhost/plugin/filamentmanager/spools?apikey="+apikey)
if (resp.status_code != 200):
errorText = resp.text
resp = resp.json()
message = self.gEmo('question') + " which filament spool do you want to select?"
keys = []
tmpKeys = []
i = 1
for spool in resp["spools"]:
self._logger.info("Appending spool: %s" % spool)
tmpKeys.append([str(spool['name']),"/filament_changeSpool_"+str(spool['id'])])
if i%2 == 0:
keys.append(tmpKeys)
tmpKeys = []
i += 1
if len(tmpKeys) > 0:
keys.append(tmpKeys)
keys.append([[self.main.emojis['cross mark']+gettext(" Close"),"No"]])
msg_id=self.main.getUpdateMsgId(chat_id)
self._logger.info("Sending message")
self.main.send_msg(message,chatID=chat_id,responses=keys,msg_id=msg_id)
except ValueError:
message = self.gEmo('mistake')+" Error changing spool"
if (errorText != ""):
message += "\nError text: " + str(errorText)
msg_id=self.main.getUpdateMsgId(chat_id)
self.main.send_msg(message,chatID=chat_id,msg_id = msg_id,inline=False)
else:
message = self.gEmo('info') + " The following Filament Commands are known."
keys = []
keys.append([["Show Spools","/filament_spools"]])
keys.append([["Change Spool","/filament_changeSpool"]])
keys.append([[self.main.emojis['cross mark']+gettext(" Close"),"No"]])
msg_id=self.main.getUpdateMsgId(chat_id) if parameter == "back" else ""
self.main.send_msg(message,chatID=chat_id,responses=keys,msg_id=msg_id)

############################################################################################
def cmdHelp(self,chat_id,from_id,cmd,parameter):
self.main.send_msg(self.gEmo('info') + gettext(" *The following commands are known:*\n\n"
"/abort - Aborts the currently running print. A confirmation is required.\n"
Expand All @@ -545,6 +640,7 @@ def cmdHelp(self,chat_id,from_id,cmd,parameter):
"/status - Sends the current status including a current photo.\n"
"/settings - Displays the current notification settings and allows you to change them.\n"
"/files - Lists all the files available for printing.\n"
"/filament - Shows you your filament spools or lets you change it. Requires the Filament Manager plugin.\n"
"/print - Lets you start a print. A confirmation is required.\n"
"/togglepause - Pause/Resume current Print.\n"
"/con - Connect/disconnect printer.\n"
Expand Down

0 comments on commit 2aa530f

Please sign in to comment.