Moving waiting logic to CLI.

This commit is contained in:
William Pietri
2017-12-19 16:01:16 -08:00
parent 7748bcd705
commit d300df99eb
5 changed files with 78 additions and 119 deletions
+16 -49
View File
@@ -5,7 +5,6 @@ from base64 import b64decode, b64encode
from collections import OrderedDict
from threading import Event
import click
import requests
import stringcase
from sleekxmpp import ClientXMPP, Callback, MatchXPath
@@ -149,7 +148,6 @@ class VacBot():
if hasattr(self, method):
getattr(self, method)(ctl)
def _handle_clean_report(self, event):
self.clean_status = event['type']
logging.debug("*** clean_status = " + self.clean_status)
@@ -186,7 +184,6 @@ class VacBot():
def run(self, action):
self.send_command(action.to_xml())
action.wait_for_completion(self)
def disconnect(self, wait=False):
self.xmpp.disconnect(wait=wait)
@@ -221,7 +218,6 @@ class EcoVacsXMPP(ClientXMPP):
def subscribe_to_ctls(self, function):
self.ctl_subscribers.append(function)
def _handle_ctl(self, message):
the_good_part = message.get_payload()[0][0]
as_dict = self._ctl_to_dict(the_good_part)
@@ -272,8 +268,7 @@ class EcoVacsXMPP(ClientXMPP):
class VacBotCommand:
CLEAN_MODE ={
CLEAN_MODE = {
'auto': 'auto',
'edge': 'border',
'spot': 'spot',
@@ -303,16 +298,11 @@ class VacBotCommand:
'stop': 'stop'
}
def __init__(self, name, args={}, wait=None, terminal=False):
def __init__(self, name, args=None):
if args is None:
args = {}
self.name = name
self.args = args
self.wait = wait
self.terminal = terminal
def wait_for_completion(self, bot):
if self.wait:
click.echo("waiting in " + self.command_name() + " for " + str(self.wait) + "s")
time.sleep(self.wait)
def to_xml(self):
ctl = ET.Element('ctl', {'td': self.name})
@@ -332,51 +322,28 @@ class VacBotCommand:
class Clean(VacBotCommand):
def __init__(self, mode='auto', speed='normal', wait=None, terminal=False):
super().__init__('Clean', {'clean': {'type': self.CLEAN_MODE[mode], 'speed': self.FAN_SPEED[speed]}}, wait=wait, terminal=terminal )
def __init__(self, mode='auto', speed='normal', terminal=False):
super().__init__('Clean', {'clean': {'type': self.CLEAN_MODE[mode], 'speed': self.FAN_SPEED[speed]}})
class Edge(Clean):
def __init__(self, wait=None, terminal=False):
super().__init__('edge', 'high', wait=wait, terminal=terminal)
def __init__(self):
super().__init__('edge', 'high')
class Spot(Clean):
def __init__(self, wait=None, terminal=False):
super().__init__('spot', 'high', wait=wait, terminal=terminal)
def __init__(self):
super().__init__('spot', 'high')
class Stop(Clean):
def __init__(self, wait=None, terminal=False):
super().__init__('stop', 'normal', wait=wait, terminal=terminal)
class StopAndWaitForCompletion(Stop):
def __init__(self, terminal=False):
super().__init__(terminal=False)
def wait_for_completion(self, bot):
logging.debug("waiting in " + self.name)
while bot.clean_status not in ['stop']:
time.sleep(0.5)
logging.debug("done waiting in " + self.name)
def __init__(self):
super().__init__('stop', 'normal')
class Charge(VacBotCommand):
def __init__(self, terminal=False):
super().__init__('Charge', {'charge': {'type': self.CHARGE_MODE['return']}}, terminal=terminal)
class ChargeAndWaitForCompletion(Charge):
def __init__(self, terminal=False):
super().__init__(terminal=terminal)
def wait_for_completion(self, bot):
logging.debug("waiting in " + self.name)
while bot.charge_status not in ['charging']:
time.sleep(0.5)
logging.debug("done waiting in " + self.name)
click.echo("docked")
def __init__(self):
super().__init__('Charge', {'charge': {'type': self.CHARGE_MODE['return']}})
class Move(VacBotCommand):
@@ -401,9 +368,9 @@ class GetBatteryState(VacBotCommand):
class GetLifeSpan(VacBotCommand):
def __init__(self, component):
super().__init__('GetLifeSpan', {'type':self.COMPONENT[component]})
super().__init__('GetLifeSpan', {'type': self.COMPONENT[component]})
class SetTime(VacBotCommand):
def __init__(self, timestamp, timezone):
super().__init__('SetTime', {'time':{'t':timestamp, 'tz':timezone}})
super().__init__('SetTime', {'time': {'t': timestamp, 'tz': timezone}})
+53 -7
View File
@@ -5,6 +5,7 @@ import platform
import random
import re
import click
from pycountry_convert import country_alpha2_to_continent_code
from sucks import *
@@ -39,6 +40,46 @@ class FrequencyParamType(click.ParamType):
FREQUENCY = FrequencyParamType()
class BotWait():
pass
def wait(self, bot):
raise NotImplementedError()
class TimeWait(BotWait):
def __init__(self, seconds):
super().__init__()
self.seconds = seconds
def wait(self, bot):
click.echo("waiting for " + str(self.seconds) + "s")
time.sleep(self.seconds)
class StatusWait(BotWait):
def __init__(self, wait_on, wait_for):
super().__init__()
self.wait_on = wait_on
self.wait_for = wait_for
def wait(self, bot):
if not hasattr(bot, self.wait_on):
raise ValueError("object " + bot + " does not have method " + self.wait_on)
logging.debug("waiting on " + self.wait_on + " for value " + self.wait_for)
while getattr(bot, self.wait_on) != self.wait_for:
time.sleep(0.5)
logging.debug("wait complete; " + self.wait_on + " is now " + self.wait_for)
class CliAction:
def __init__(self, vac_command, terminal=False, wait=None):
self.vac_command = vac_command
self.terminal = terminal
self.wait = wait
def config_file():
if platform.system() == 'Windows':
return os.path.join(os.getenv('APPDATA'), 'sucks.conf')
@@ -125,7 +166,7 @@ def login(email, password, country_code, continent_code):
@click.argument('minutes', type=click.FLOAT)
def clean(frequency, minutes):
if should_run(frequency):
return Clean(wait=minutes * 60)
return CliAction(Clean(), wait=TimeWait(minutes * 60))
@cli.command(help='cleans room edges for the specified number of minutes')
@@ -133,24 +174,28 @@ def clean(frequency, minutes):
@click.argument('minutes', type=click.FLOAT)
def edge(frequency, minutes):
if should_run(frequency):
return Edge(wait=minutes * 60)
return CliAction(Edge(), wait=TimeWait(minutes * 60))
@cli.command(help='returns to charger')
def charge():
return ChargeAndWaitForCompletion(terminal=True)
return charge_action()
def charge_action():
return CliAction(Charge(), terminal=True, wait=StatusWait('charge_status', 'charging'))
@cli.command(help='stops the robot in its current position')
def stop():
return StopAndWaitForCompletion(terminal=True)
return CliAction(Stop(), terminal=True, wait=StatusWait('clean_status', 'stop'))
@cli.resultcallback()
def run(actions, debug):
actions = list(filter(None.__ne__, actions))
if actions and charge and not actions[-1].terminal:
actions.append(Charge())
actions.append(charge_action())
if not config_file_exists():
click.echo("Not logged in. Do 'click login' first.")
@@ -168,8 +213,9 @@ def run(actions, debug):
vacbot.connect_and_wait_until_ready()
for action in actions:
click.echo("performing " + str(action))
vacbot.run(action)
click.echo("performing " + str(action.vac_command))
vacbot.run(action.vac_command)
action.wait.wait(vacbot)
vacbot.disconnect(wait=True)