From 2d457be6fc39f50d4b3d6f73cbdcf95058ddf0d4 Mon Sep 17 00:00:00 2001 From: derpicknicker1 <10minutemail@web.de> Date: Mon, 6 Jun 2016 17:34:14 +0200 Subject: [PATCH 1/6] Introduced states for user command "chains" --- octoprint_telegram/__init__.py | 2 +- octoprint_telegram/telegramCommands.py | 70 +++++++++++++++++++------- 2 files changed, 52 insertions(+), 20 deletions(-) diff --git a/octoprint_telegram/__init__.py b/octoprint_telegram/__init__.py index 3f01c6c..b546b06 100644 --- a/octoprint_telegram/__init__.py +++ b/octoprint_telegram/__init__.py @@ -203,7 +203,7 @@ def handleTextMessage(self, message, chat_id, from_id): if command.startswith("/"): self.main.track_action("command/" + command[1:]) # execute command - self.main.tcmd.commandDict[command]['cmd'](chat_id=chat_id,parameter=parameter) + self.main.tcmd.commandDict[command]['cmd'](chat_id=chat_id,parameter=parameter,cmd=command) # we dont need the messageResponseID anymore self.main.messageResponseID = None else: diff --git a/octoprint_telegram/telegramCommands.py b/octoprint_telegram/telegramCommands.py index 2e4f7d8..826b3e9 100644 --- a/octoprint_telegram/telegramCommands.py +++ b/octoprint_telegram/telegramCommands.py @@ -15,22 +15,22 @@ class TCMD(): def __init__(self, main): self.main = main self.gEmo = self.main.gEmo - self.tmpSysCmd = {} + self.stateList = {} self._logger = main._logger.getChild("TCMD") self.commandDict = { gettext("Yes"): {'cmd': self.cmdYes, 'bind_none': True}, gettext("Cancel"): {'cmd': self.cmdNo, 'bind_none': True}, gettext("No"): {'cmd': self.cmdNo,'bind_none': True}, - gettext("Change height"): {'cmd': self.cmdChgHeight, 'bind_cmd': '/settings'}, - (self.gEmo('enter') + gettext(" Enter height")): {'cmd': self.cmdSetHeight, 'bind_cmd': '/settings'}, - gettext(" Enter height"): {'cmd': self.cmdSetHeight, 'bind_cmd': '/settings'}, - gettext("Change time"): {'cmd': self.cmdChgTime, 'bind_cmd': '/settings'}, - (self.gEmo('enter') + gettext(" Enter time")): {'cmd': self.cmdSetTime, 'bind_cmd': '/settings'}, - gettext(" Enter time"): {'cmd': self.cmdSetTime, 'bind_cmd': '/settings'}, - gettext("Start print"): {'cmd': self.cmdStartPrint, 'bind_cmd': '/print'}, - gettext("Stop print"): {'cmd': self.cmdHalt, 'bind_cmd': '/print'}, - gettext("Don't print"): {'cmd': self.cmdDontPrint, 'bind_cmd': '/print'}, - gettext("Do System Command"): {'cmd': self.cmdSysRun, 'bind_cmd': '/sys'}, + gettext("Change height"): {'cmd': self.cmdChgHeight, 'bind_cmd': '/settings', 'lastState': '/settings'}, + (self.gEmo('enter') + gettext(" Enter height")): {'cmd': self.cmdSetHeight, 'bind_cmd': '/settings', 'lastState': gettext("Change height")}, + gettext(" Enter height"): {'cmd': self.cmdSetHeight, 'bind_cmd': '/settings', 'lastState': gettext("Change height")}, + gettext("Change time"): {'cmd': self.cmdChgTime, 'bind_cmd': '/settings', 'lastState': '/settings'}, + (self.gEmo('enter') + gettext(" Enter time")): {'cmd': self.cmdSetTime, 'bind_cmd': '/settings', 'lastState': gettext("Change time")}, + gettext(" Enter time"): {'cmd': self.cmdSetTime, 'bind_cmd': '/settings', 'lastState': gettext("Change time")}, + gettext("Start print"): {'cmd': self.cmdStartPrint, 'bind_cmd': '/print', 'lastState': '/print_'}, + gettext("Stop print"): {'cmd': self.cmdHalt, 'bind_cmd': '/abort', 'lastState': '/abort'}, + gettext("Don't print"): {'cmd': self.cmdDontPrint, 'bind_cmd': '/print', 'lastState': '/print_'}, + gettext("Do System Command"): {'cmd': self.cmdSysRun, 'bind_cmd': '/sys', 'lastState': '/sys_'}, '/print_': {'cmd': self.cmdRunPrint, 'bind_cmd': '/print'}, '/test': {'cmd': self.cmdTest}, '/status': {'cmd': self.cmdStatus}, @@ -48,18 +48,32 @@ def __init__(self, main): '/ctrl_': {'cmd': self.cmdCtrlRun, 'bind_cmd': '/ctrl'} } + def checkState(self, chat_id, cmd, parameter = ""): + if not chat_id in self.stateList: + self.stateList[chat_id] = ["",""] + ret = True + if 'lastState' in self.commandDict[cmd]: + if self.commandDict[cmd]['lastState'] != self.stateList[chat_id][0]: + ret = False + self.stateList[chat_id][0] = cmd + if parameter is not None and parameter is not "": + self.stateList[chat_id][1] = parameter + return ret + def cmdYes(self,chat_id,**kwargs): + if not self.checkState(chat_id,kwargs['cmd']): return self.main.send_msg(gettext("Alright."),chatID=chat_id) def cmdNo(self,chat_id,**kwargs): - if chat_id in self.tmpSysCmd: - del self.tmpSysCmd[chat_id] + if not self.checkState(chat_id,kwargs['cmd']): return self.main.send_msg(gettext("Maybe next time."),chatID=chat_id) def cmdTest(self,chat_id,**kwargs): + if not self.checkState(chat_id,kwargs['cmd']): return self.main.send_msg(self.gEmo('question') + gettext(" Is this a test?\n\n") , responses=[gettext("Yes"), gettext("No")],chatID=chat_id) def cmdStatus(self,chat_id,**kwargs): + if not self.checkState(chat_id,kwargs['cmd']): return if not self.main._printer.is_operational(): self.main.send_msg(self.gEmo('warning') + gettext(" Not connected to a printer."),chatID=chat_id) elif self.main._printer.is_printing(): @@ -68,53 +82,65 @@ def cmdStatus(self,chat_id,**kwargs): self.main.on_event("StatusNotPrinting", {},chatID=chat_id) def cmdSettings(self,chat_id,**kwargs): + if not self.checkState(chat_id,kwargs['cmd']): return msg = self.gEmo('settings') + gettext(" Current notification settings are:\n\n\n"+self.gEmo('height')+" height: %(height)fmm\n\n"+self.gEmo('clock')+" time: %(time)dmin\n\n\n"+self.gEmo('question')+"Which value do you want to change?", height=self.main._settings.get_float(["notification_height"]), time=self.main._settings.get_int(["notification_time"])) self.main.send_msg(msg, responses=[gettext("Change height"), gettext("Change time"), gettext("Cancel")],chatID=chat_id) def cmdChgHeight(self,chat_id,**kwargs): + if not self.checkState(chat_id,kwargs['cmd']): return self.main.send_msg(self.gEmo('enter') + " " + gettext("Enter height"), force_reply=True,chatID=chat_id) - def cmdSetHeight(self,chat_id,parameter,**kwargs): + def cmdSetHeight(self,chat_id,parameter,**kwargs): + if not self.checkState(chat_id,kwargs['cmd'],parameter): return self.main._settings.set_float(['notification_height'], parameter, force=True) self.main.send_msg(self.gEmo('height') + gettext(" Notification height is now %(height)fmm.", height=self.main._settings.get_float(['notification_height'])),chatID=chat_id) def cmdChgTime(self,chat_id,**kwargs): + if not self.checkState(chat_id,kwargs['cmd']): return self.main.send_msg(self.gEmo('enter') + " " +gettext("Enter time"), force_reply=True,chatID=chat_id) def cmdSetTime(self,chat_id,parameter,**kwargs): + if not self.checkState(chat_id,kwargs['cmd'],parameter): return self.main._settings.set_int(['notification_time'], parameter, force=True) self.main.send_msg(self.gEmo('clock') + gettext(" Notification time is now %(time)dmins.", time=self.main._settings.get_int(['notification_time'])),chatID=chat_id) def cmdAbort(self,chat_id,**kwargs): + if not self.checkState(chat_id,kwargs['cmd']): return if self.main._printer.is_printing(): self.main.send_msg(self.gEmo('question') + gettext(" Really abort the currently running print?"), responses=[gettext("Stop print"), gettext("Cancel")],chatID=chat_id) else: self.main.send_msg(self.gEmo('warning') + gettext(" Currently I'm not printing, so there is nothing to stop."),chatID=chat_id) def cmdHalt(self,chat_id,**kwargs): + if not self.checkState(chat_id,kwargs['cmd']): return self.main.send_msg(self.gEmo('info') + gettext(" Aborting the print."),chatID=chat_id) self.main._printer.cancel_print() def cmdDontPrint(self, chat_id, **kwargs): + if not self.checkState(chat_id,kwargs['cmd']): return self.main._printer.unselect_file() self.main.send_msg(gettext("Maybe next time."),chatID=chat_id) def cmdShutup(self,chat_id,**kwargs): + if not self.checkState(chat_id,kwargs['cmd']): return if chat_id not in self.main.shut_up: self.main.shut_up[chat_id] = True self.main.send_msg(self.gEmo('noNotify') + gettext(" Okay, shutting up until the next print is finished." + self.gEmo('shutup')+" Use /imsorrydontshutup to let me talk again before that. "),chatID=chat_id) def cmdNShutup(self,chat_id,**kwargs): + if not self.checkState(chat_id,kwargs['cmd']): return if chat_id in self.main.shut_up: del self.main.shut_up[chat_id] self.main.send_msg(self.gEmo('notify') + gettext(" Yay, I can talk again."),chatID=chat_id) def cmdPrint(self,chat_id,**kwargs): + if not self.checkState(chat_id,kwargs['cmd']): return self.main.send_msg(self.gEmo('info') + " Use /list to get a list of files and click the command beginning with /print after the correct file.",chatID=chat_id) def cmdRunPrint(self,chat_id,parameter,**kwargs): + if not self.checkState(chat_id,kwargs['cmd'],parameter): return self._logger.debug("Looking for hash: %s", parameter) destination, file = self.find_file_by_hash(parameter) self._logger.debug("Destination: %s", destination) @@ -135,6 +161,7 @@ def cmdRunPrint(self,chat_id,parameter,**kwargs): self.main.send_msg(self.gEmo('info') + gettext(" Okay. The file %(file)s is loaded.\n\n"+self.gEmo('question')+" Do you want me to start printing it now?", file=data['job']['file']['name']), responses=[gettext("Start print"), gettext("Don't print")],chatID=chat_id) def cmdStartPrint(self,chat_id,**kwargs): + if not self.checkState(chat_id,kwargs['cmd']): return data = self.main._printer.get_current_data() if data['job']['file']['name'] is None: self.main.send_msg(self.gEmo('warning') + gettext(" Uh oh... No file is selected for printing. Did you select one using /list?"),chatID=chat_id) @@ -149,13 +176,16 @@ def cmdStartPrint(self,chat_id,**kwargs): self.main.send_msg(self.gEmo('rocket') + gettext(" Started the print job."),chatID=chat_id) def cmdList(self,chat_id,**kwargs): + if not self.checkState(chat_id,kwargs['cmd']): return files = self.get_flat_file_tree() self.main.send_msg(self.gEmo('save') + " File List:\n\n" + "\n".join(files) + "\n\n"+self.gEmo('info')+" You can click the command beginning with /print after a file to start printing this file.",chatID=chat_id) def cmdUpload(self,chat_id,**kwargs): + if not self.checkState(chat_id,kwargs['cmd']): return self.main.send_msg(self.gEmo('info') + " To upload a gcode file, just send it to me.",chatID=chat_id) def cmdSys(self,chat_id,**kwargs): + if not self.checkState(chat_id,kwargs['cmd']): return message = self.gEmo('info') + " You have to pass a System Command. The following System Commands are known.\n(Click to execute)\n\n" empty = True for action in self.main._settings.global_get(['system','actions']): @@ -169,21 +199,20 @@ def cmdSys(self,chat_id,**kwargs): def cmdSysReq(self,chat_id,parameter,**kwargs): if parameter is None or parameter is "": + kwargs['cmd'] = "/sys" self.cmdSys(chat_id, **kwargs) return + if not self.checkState(chat_id,kwargs['cmd'],parameter): return actions = self.main._settings.global_get(['system','actions']) command = next((d for d in actions if 'action' in d and self.hashMe(d['action'], 6) == parameter) , False) if command : - self.tmpSysCmd.update({chat_id: parameter}) self.main.send_msg(self.gEmo('question') + " Really execute "+command['name']+"?",responses=[gettext("Do System Command"), gettext("Cancel")],chatID=chat_id) return self.main.send_msg(self.gEmo('warning') + " Sorry, i don't know this System Command.",chatID=chat_id) def cmdSysRun(self,chat_id,**kwargs): - if chat_id not in self.tmpSysCmd: - return - parameter = self.tmpSysCmd[chat_id] - del self.tmpSysCmd[chat_id] + parameter = self.stateList[chat_id][1] + if not self.checkState(chat_id,kwargs['cmd']): return actions = self.main._settings.global_get(['system','actions']) action = next((i for i in actions if self.hashMe(i['action'], 6) == parameter), False) ### The following is taken from OctoPrint/src/octoprint/server/api/__init__.py -> performSystemAction() @@ -208,6 +237,7 @@ def cmdSysRun(self,chat_id,**kwargs): self.main.send_msg(self.gEmo('warning') + " Command failed with exception: %s!" % e,chatID = chat_id) def cmdCtrl(self,chat_id,**kwargs): + if not self.checkState(chat_id,kwargs['cmd']): return message = self.gEmo('info') + " You have to pass a Printer Control Command. The following Printer Controls are known.\n(Click to execute)\n\n" empty = True for action in self.get_controls_recursively(): @@ -220,6 +250,7 @@ def cmdCtrlRun(self,chat_id,parameter,**kwargs): if parameter is None or parameter is "": self.cmdCtrl(chat_id, **kwargs) return + if not self.checkState(chat_id,kwargs['cmd'],parameter): return actions = self.get_controls_recursively() command = next((d for d in actions if d['hash'] == parameter), False) if command: @@ -233,6 +264,7 @@ def cmdCtrlRun(self,chat_id,parameter,**kwargs): self.main.send_msg(self.gEmo('warning') + " Control Command ctrl_" + parameter + " not found." ,chatID=chat_id) def cmdHelp(self,chat_id,**kwargs): + if not self.checkState(chat_id,kwargs['cmd']): return self.main.send_msg(self.gEmo('info') + gettext(" You can use following commands:\n\n" "/abort - Aborts the currently running print. A confirmation is required.\n" "/shutup - Disables automatic notifications till the next print ends.\n" From 89e0db6be39d6cc13c90546c9067994058a5c7e2 Mon Sep 17 00:00:00 2001 From: derpicknicker1 <10minutemail@web.de> Date: Tue, 7 Jun 2016 10:17:13 +0200 Subject: [PATCH 2/6] Make user state check central --- octoprint_telegram/__init__.py | 2 +- octoprint_telegram/telegramCommands.py | 25 ------------------------- 2 files changed, 1 insertion(+), 26 deletions(-) diff --git a/octoprint_telegram/__init__.py b/octoprint_telegram/__init__.py index b546b06..b9898c7 100644 --- a/octoprint_telegram/__init__.py +++ b/octoprint_telegram/__init__.py @@ -195,7 +195,7 @@ def handleTextMessage(self, message, chat_id, from_id): self.main.send_msg("I do not understand you! " + self.gEmo('mistake'),chatID=chat_id) raise ExitThisLoopException() # check if user is allowed to execute the command - if self.isCommandAllowed(chat_id,from_id,command): + if self.isCommandAllowed(chat_id,from_id,command) and self.main.tcmd.checkState(from_id, command, parameter): # messageRespondID is needed to send command replys only to the sender # if message is from a group self.main.messageResponseID = message['message']['message_id'] diff --git a/octoprint_telegram/telegramCommands.py b/octoprint_telegram/telegramCommands.py index 826b3e9..cca091c 100644 --- a/octoprint_telegram/telegramCommands.py +++ b/octoprint_telegram/telegramCommands.py @@ -61,19 +61,15 @@ def checkState(self, chat_id, cmd, parameter = ""): return ret def cmdYes(self,chat_id,**kwargs): - if not self.checkState(chat_id,kwargs['cmd']): return self.main.send_msg(gettext("Alright."),chatID=chat_id) def cmdNo(self,chat_id,**kwargs): - if not self.checkState(chat_id,kwargs['cmd']): return self.main.send_msg(gettext("Maybe next time."),chatID=chat_id) def cmdTest(self,chat_id,**kwargs): - if not self.checkState(chat_id,kwargs['cmd']): return self.main.send_msg(self.gEmo('question') + gettext(" Is this a test?\n\n") , responses=[gettext("Yes"), gettext("No")],chatID=chat_id) def cmdStatus(self,chat_id,**kwargs): - if not self.checkState(chat_id,kwargs['cmd']): return if not self.main._printer.is_operational(): self.main.send_msg(self.gEmo('warning') + gettext(" Not connected to a printer."),chatID=chat_id) elif self.main._printer.is_printing(): @@ -82,65 +78,53 @@ def cmdStatus(self,chat_id,**kwargs): self.main.on_event("StatusNotPrinting", {},chatID=chat_id) def cmdSettings(self,chat_id,**kwargs): - if not self.checkState(chat_id,kwargs['cmd']): return msg = self.gEmo('settings') + gettext(" Current notification settings are:\n\n\n"+self.gEmo('height')+" height: %(height)fmm\n\n"+self.gEmo('clock')+" time: %(time)dmin\n\n\n"+self.gEmo('question')+"Which value do you want to change?", height=self.main._settings.get_float(["notification_height"]), time=self.main._settings.get_int(["notification_time"])) self.main.send_msg(msg, responses=[gettext("Change height"), gettext("Change time"), gettext("Cancel")],chatID=chat_id) def cmdChgHeight(self,chat_id,**kwargs): - if not self.checkState(chat_id,kwargs['cmd']): return self.main.send_msg(self.gEmo('enter') + " " + gettext("Enter height"), force_reply=True,chatID=chat_id) def cmdSetHeight(self,chat_id,parameter,**kwargs): - if not self.checkState(chat_id,kwargs['cmd'],parameter): return self.main._settings.set_float(['notification_height'], parameter, force=True) self.main.send_msg(self.gEmo('height') + gettext(" Notification height is now %(height)fmm.", height=self.main._settings.get_float(['notification_height'])),chatID=chat_id) def cmdChgTime(self,chat_id,**kwargs): - if not self.checkState(chat_id,kwargs['cmd']): return self.main.send_msg(self.gEmo('enter') + " " +gettext("Enter time"), force_reply=True,chatID=chat_id) def cmdSetTime(self,chat_id,parameter,**kwargs): - if not self.checkState(chat_id,kwargs['cmd'],parameter): return self.main._settings.set_int(['notification_time'], parameter, force=True) self.main.send_msg(self.gEmo('clock') + gettext(" Notification time is now %(time)dmins.", time=self.main._settings.get_int(['notification_time'])),chatID=chat_id) def cmdAbort(self,chat_id,**kwargs): - if not self.checkState(chat_id,kwargs['cmd']): return if self.main._printer.is_printing(): self.main.send_msg(self.gEmo('question') + gettext(" Really abort the currently running print?"), responses=[gettext("Stop print"), gettext("Cancel")],chatID=chat_id) else: self.main.send_msg(self.gEmo('warning') + gettext(" Currently I'm not printing, so there is nothing to stop."),chatID=chat_id) def cmdHalt(self,chat_id,**kwargs): - if not self.checkState(chat_id,kwargs['cmd']): return self.main.send_msg(self.gEmo('info') + gettext(" Aborting the print."),chatID=chat_id) self.main._printer.cancel_print() def cmdDontPrint(self, chat_id, **kwargs): - if not self.checkState(chat_id,kwargs['cmd']): return self.main._printer.unselect_file() self.main.send_msg(gettext("Maybe next time."),chatID=chat_id) def cmdShutup(self,chat_id,**kwargs): - if not self.checkState(chat_id,kwargs['cmd']): return if chat_id not in self.main.shut_up: self.main.shut_up[chat_id] = True self.main.send_msg(self.gEmo('noNotify') + gettext(" Okay, shutting up until the next print is finished." + self.gEmo('shutup')+" Use /imsorrydontshutup to let me talk again before that. "),chatID=chat_id) def cmdNShutup(self,chat_id,**kwargs): - if not self.checkState(chat_id,kwargs['cmd']): return if chat_id in self.main.shut_up: del self.main.shut_up[chat_id] self.main.send_msg(self.gEmo('notify') + gettext(" Yay, I can talk again."),chatID=chat_id) def cmdPrint(self,chat_id,**kwargs): - if not self.checkState(chat_id,kwargs['cmd']): return self.main.send_msg(self.gEmo('info') + " Use /list to get a list of files and click the command beginning with /print after the correct file.",chatID=chat_id) def cmdRunPrint(self,chat_id,parameter,**kwargs): - if not self.checkState(chat_id,kwargs['cmd'],parameter): return self._logger.debug("Looking for hash: %s", parameter) destination, file = self.find_file_by_hash(parameter) self._logger.debug("Destination: %s", destination) @@ -161,7 +145,6 @@ def cmdRunPrint(self,chat_id,parameter,**kwargs): self.main.send_msg(self.gEmo('info') + gettext(" Okay. The file %(file)s is loaded.\n\n"+self.gEmo('question')+" Do you want me to start printing it now?", file=data['job']['file']['name']), responses=[gettext("Start print"), gettext("Don't print")],chatID=chat_id) def cmdStartPrint(self,chat_id,**kwargs): - if not self.checkState(chat_id,kwargs['cmd']): return data = self.main._printer.get_current_data() if data['job']['file']['name'] is None: self.main.send_msg(self.gEmo('warning') + gettext(" Uh oh... No file is selected for printing. Did you select one using /list?"),chatID=chat_id) @@ -176,16 +159,13 @@ def cmdStartPrint(self,chat_id,**kwargs): self.main.send_msg(self.gEmo('rocket') + gettext(" Started the print job."),chatID=chat_id) def cmdList(self,chat_id,**kwargs): - if not self.checkState(chat_id,kwargs['cmd']): return files = self.get_flat_file_tree() self.main.send_msg(self.gEmo('save') + " File List:\n\n" + "\n".join(files) + "\n\n"+self.gEmo('info')+" You can click the command beginning with /print after a file to start printing this file.",chatID=chat_id) def cmdUpload(self,chat_id,**kwargs): - if not self.checkState(chat_id,kwargs['cmd']): return self.main.send_msg(self.gEmo('info') + " To upload a gcode file, just send it to me.",chatID=chat_id) def cmdSys(self,chat_id,**kwargs): - if not self.checkState(chat_id,kwargs['cmd']): return message = self.gEmo('info') + " You have to pass a System Command. The following System Commands are known.\n(Click to execute)\n\n" empty = True for action in self.main._settings.global_get(['system','actions']): @@ -202,7 +182,6 @@ def cmdSysReq(self,chat_id,parameter,**kwargs): kwargs['cmd'] = "/sys" self.cmdSys(chat_id, **kwargs) return - if not self.checkState(chat_id,kwargs['cmd'],parameter): return actions = self.main._settings.global_get(['system','actions']) command = next((d for d in actions if 'action' in d and self.hashMe(d['action'], 6) == parameter) , False) if command : @@ -212,7 +191,6 @@ def cmdSysReq(self,chat_id,parameter,**kwargs): def cmdSysRun(self,chat_id,**kwargs): parameter = self.stateList[chat_id][1] - if not self.checkState(chat_id,kwargs['cmd']): return actions = self.main._settings.global_get(['system','actions']) action = next((i for i in actions if self.hashMe(i['action'], 6) == parameter), False) ### The following is taken from OctoPrint/src/octoprint/server/api/__init__.py -> performSystemAction() @@ -237,7 +215,6 @@ def cmdSysRun(self,chat_id,**kwargs): self.main.send_msg(self.gEmo('warning') + " Command failed with exception: %s!" % e,chatID = chat_id) def cmdCtrl(self,chat_id,**kwargs): - if not self.checkState(chat_id,kwargs['cmd']): return message = self.gEmo('info') + " You have to pass a Printer Control Command. The following Printer Controls are known.\n(Click to execute)\n\n" empty = True for action in self.get_controls_recursively(): @@ -250,7 +227,6 @@ def cmdCtrlRun(self,chat_id,parameter,**kwargs): if parameter is None or parameter is "": self.cmdCtrl(chat_id, **kwargs) return - if not self.checkState(chat_id,kwargs['cmd'],parameter): return actions = self.get_controls_recursively() command = next((d for d in actions if d['hash'] == parameter), False) if command: @@ -264,7 +240,6 @@ def cmdCtrlRun(self,chat_id,parameter,**kwargs): self.main.send_msg(self.gEmo('warning') + " Control Command ctrl_" + parameter + " not found." ,chatID=chat_id) def cmdHelp(self,chat_id,**kwargs): - if not self.checkState(chat_id,kwargs['cmd']): return self.main.send_msg(self.gEmo('info') + gettext(" You can use following commands:\n\n" "/abort - Aborts the currently running print. A confirmation is required.\n" "/shutup - Disables automatic notifications till the next print ends.\n" From 7a4bbb929454cf3c4eafaa4f76e1e479ed1404de Mon Sep 17 00:00:00 2001 From: derpicknicker1 <10minutemail@web.de> Date: Tue, 7 Jun 2016 10:59:32 +0200 Subject: [PATCH 3/6] Simplyfied TCMD class a bit * all notifications are now started by startEvent() --- octoprint_telegram/__init__.py | 3 +- octoprint_telegram/telegramNotifications.py | 35 +++++++++------------ 2 files changed, 15 insertions(+), 23 deletions(-) diff --git a/octoprint_telegram/__init__.py b/octoprint_telegram/__init__.py index b9898c7..ea53ff6 100644 --- a/octoprint_telegram/__init__.py +++ b/octoprint_telegram/__init__.py @@ -782,7 +782,7 @@ def on_event(self, event, payload, **kwargs): if event in self.tmsg.msgCmdDict: self._logger.debug("Got an event: " + event + " Payload: " + str(payload)) # Start event handler - self.tmsg.msgCmdDict[event](event, payload, **kwargs) + self.tmsg.startEvent(event, payload, **kwargs) else: # return as fast as possible return @@ -1090,7 +1090,6 @@ def track_action(self, action): t.daemon = True t.run() - def route_hook(self, server_routes, *args, **kwargs): from octoprint.server.util.tornado import LargeResponseHandler, UrlProxyHandler, path_validation_factory from octoprint.util import is_hidden_path diff --git a/octoprint_telegram/telegramNotifications.py b/octoprint_telegram/telegramNotifications.py index e8f4f32..01e66d2 100644 --- a/octoprint_telegram/telegramNotifications.py +++ b/octoprint_telegram/telegramNotifications.py @@ -84,19 +84,16 @@ def __init__(self, main): 'StatusPrinting': self.msgStatusPrinting } - def _prepMsg(self, event): + def startEvent(self, event, payload, **kwargs): status = self.main._printer.get_current_data() self.z = status['currentZ'] or 0.0 - if 'bind_msg' in telegramMsgDict[event]: - event = telegramMsgDict[event]['bind_msg'] - return event + kwargs['event'] = event + self.msgCmdDict[event](payload, **kwargs) - def msgPrinterStart_Shutdown(self, event, payload, **kwargs): - kwargs['event'] = self._prepMsg(event) + def msgPrinterStart_Shutdown(self, payload, **kwargs): self._sendNotification(payload, **kwargs) - def msgZChange(self, event, payload, **kwargs): - kwargs['event'] = self._prepMsg(event) + def msgZChange(self, payload, **kwargs): status = self.main._printer.get_current_data() if not status['state']['flags']['printing'] or not self.is_notification_necessary(payload['new'], payload['old']): return @@ -109,40 +106,36 @@ def msgZChange(self, event, payload, **kwargs): self.main._settings.get_int(['notification_time'])) self._sendNotification(payload, **kwargs) - def msgPrintStarted(self, event, payload, **kwargs): - kwargs['event'] = self._prepMsg(event) + def msgPrintStarted(self, payload, **kwargs): self.last_z = 0.0 self.last_notification_time = time.time() self._sendNotification(payload, **kwargs) - def msgPrintDone(self, event, payload, **kwargs): - kwargs['event'] = self._prepMsg(event) + def msgPrintDone(self, payload, **kwargs): self.main.shut_up = {} kwargs["delay"] = self.main._settings.get_int(["message_at_print_done_delay"]) self._sendNotification(payload, **kwargs) - def msgPrintFailed(self, event, payload, **kwargs): - kwargs['event'] = self._prepMsg(event) + def msgPrintFailed(self, payload, **kwargs): self.main.shut_up = {} self._sendNotification(payload, **kwargs) - def msgStatusPrinting(self, event, payload, **kwargs): - kwargs['event'] = self._prepMsg(event) + def msgStatusPrinting(self, payload, **kwargs): self.track = False self._sendNotification(payload, **kwargs) - def msgStatusNotPrinting(self, event, payload, **kwargs): - kwargs['event'] = self._prepMsg(event) + def msgStatusNotPrinting(self, payload, **kwargs): self.track = False self._sendNotification(payload, **kwargs) def _sendNotification(self, payload, **kwargs): status = self.main._printer.get_current_data() + event = kwargs['event'] + kwargs['event'] = telegramMsgDict[event]['bind_msg'] if 'bind_msg' in telegramMsgDict[event] else event kwargs['with_image'] = self.main._settings.get(['messages',str(kwargs['event']),'image']) self._logger.debug("Printer Status" + str(status)) # define locals for string formatting z = self.z - event = kwargs['event'] temps = self.main._printer.get_current_temperatures() self._logger.debug("TEMPS - " + str(temps)) bed_temp = temps['bed']['actual'] if 'bed' in temps else 0.0 @@ -166,7 +159,7 @@ def _sendNotification(self, payload, **kwargs): message = self.main._settings.get(["messages",kwargs['event'],"text"]).format(emo,**locals()) except Exception as ex: self._logger.debug("Exception on formatting message: " + str(ex)) - message = self.main.gEmo('warning') + " ERROR: I was not able to format the Notification for '"+kwargs['event']+"' properly. Please open your OctoPrint settings for " + self.main._plugin_name + " and check message settings for '" + kwargs['event'] + "'." + message = self.main.gEmo('warning') + " ERROR: I was not able to format the Notification for '"+event+"' properly. Please open your OctoPrint settings for " + self.main._plugin_name + " and check message settings for '" + event + "'." self._logger.debug("Sending Notification: " + message) # Do we want to send with Markup? kwargs['markup'] = self.main._settings.get(["messages",kwargs['event'],"markup"]) @@ -174,7 +167,7 @@ def _sendNotification(self, payload, **kwargs): self.main.send_msg(message, **kwargs) if self.track: - self.main.track_action("notification/" + kwargs['event']) + self.main.track_action("notification/" + event) self.track = True # Helper to determine if notification will be send on gcode ZChange event From 4e9e138e8e2e36bf4cdb7fd55630b2fa14c7cc8d Mon Sep 17 00:00:00 2001 From: derpicknicker1 <10minutemail@web.de> Date: Sat, 24 Sep 2016 14:58:50 +0200 Subject: [PATCH 4/6] Bug in user image cleanup --- octoprint_telegram/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/octoprint_telegram/__init__.py b/octoprint_telegram/__init__.py index ea53ff6..460bc44 100644 --- a/octoprint_telegram/__init__.py +++ b/octoprint_telegram/__init__.py @@ -477,7 +477,7 @@ def on_after_startup(self): if fcut not in self.chats: self._logger.debug("Removing pic" +fcut+".jpg") try: - os.remove(self.get_plugin_data_folder()++"/img/user/"+f) + os.remove(self.get_plugin_data_folder()+"/img/user/"+f) except OSError: pass #Update user profile photos From 734f1f8c2bad51cc8073bb48a37e0ae45f77da34 Mon Sep 17 00:00:00 2001 From: derpicknicker1 <10minutemail@web.de> Date: Sat, 24 Sep 2016 17:16:15 +0200 Subject: [PATCH 5/6] Fixed bug in getting user pofile pics --- octoprint_telegram/__init__.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/octoprint_telegram/__init__.py b/octoprint_telegram/__init__.py index 460bc44..77563be 100644 --- a/octoprint_telegram/__init__.py +++ b/octoprint_telegram/__init__.py @@ -250,7 +250,7 @@ def parseUserData(self, message): self.main.chats[chat_id] = data self.main.send_msg(self.gEmo('info') + "Now i know you. Before you can do anything, go to OctoPrint Settings and edit some rights.",chatID=chat_id) kwargs = {'chat_id':int(chat_id)} - t.threading.Thread(target=self.main.get_usrPic, kwargs=kwargs) + t = threading.Thread(target=self.main.get_usrPic, kwargs=kwargs) t.daemon = True t.run() self._logger.debug("Got new User") @@ -483,11 +483,12 @@ def on_after_startup(self): #Update user profile photos for key in self.chats: try: - kwargs = {} - kwargs['chat_id'] = int(key) - t = threading.Thread(target=self.get_usrPic, kwargs=kwargs) - t.daemon = True - t.run() + if key is not 'zBOTTOMOFCHATS': + kwargs = {} + kwargs['chat_id'] = int(key) + t = threading.Thread(target=self.get_usrPic, kwargs=kwargs) + t.daemon = True + t.run() except Exception: pass From c42f1e8475df48db39e060175c858af17d55657e Mon Sep 17 00:00:00 2001 From: derpicknicker1 <10minutemail@web.de> Date: Sat, 24 Sep 2016 18:22:00 +0200 Subject: [PATCH 6/6] New version number for release --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index c7bb208..c81f983 100644 --- a/setup.py +++ b/setup.py @@ -14,7 +14,7 @@ plugin_name = "OctoPrint-Telegram" # The plugin's version. Can be overwritten within OctoPrint's internal data via __plugin_version__ in the plugin module -plugin_version = "1.3.2" +plugin_version = "1.3.3" # The plugin's description. Can be overwritten within OctoPrint's internal data via __plugin_description__ in the plugin # module