def reflash(self, wavList): """Reflash the sound flash memory. Only available for CLIENT_LEVEL_RESTRICTED and CLIENT_LEVEL_ROOT levels. @param wavList: wave file path list. @return: (SOUND_REFLASH_NO_ERROR|SOUND_REFLASH_ERROR_PARAMETERS| SOUND_REFLASH_ERROR_RF_OFFLINE|SOUND_REFLASH_ERROR_WAV| SOUND_REFLASH_ERROR_USB) """ if self.__parent.server.getClientLevel() not in [ CLIENT_LEVEL_RESTRICTED, CLIENT_LEVEL_ROOT ]: raise TuxAPIExceptionCL, self.__parent.server.getClientLevel() if not checkValue(wavList, "list"): return SOUND_REFLASH_ERROR_PARAMETERS if len(wavList) <= 0: return SOUND_REFLASH_ERROR_PARAMETERS tracks = "" for wav in wavList: if not checkValue(wav, "str"): return SOUND_REFLASH_ERROR_PARAMETERS tracks = "%s%s|" % (tracks, wav) tracks = tracks[:-1] cmd = "sound_flash/reflash?tracks=%s" % tracks if not self.__cmdSimpleResult(cmd): return SOUND_REFLASH_ERROR_PARAMETERS else: if self.__eventHandlers.waitCondition(ST_NAME_SOUND_REFLASH_END, (SSV_NDEF, None), 5.0): self.__eventHandlers.waitCondition(ST_NAME_SOUND_REFLASH_END, (None, None), 150.0) value, delay = self.__parent.status.requestOne( ST_NAME_SOUND_REFLASH_END) return value
def playAsync(self, track, volume=100.0): """Play a sound from the internal memory. (Asynchronous) @param track: index of the sound. @param volume: volume (0.0 .. 100.0) @return: the success of the command. """ if not checkValue(track, "int"): return False if not checkValue(volume, "float", 0.0, 100.0): return False cmd = "sound_flash/play?track=%d&volume=%f" % (track, volume) return self.__cmdSimpleResult(cmd)
def connect(self, level, name, passwd): """Attempt to connect to the server. @param level: requested level of the client. @param name: name of the client. @param passwd: password of the client. @return: the success of the connection. """ # If already connected - Success if self.getConnected(): return True # If client level is invalid - Failed if level not in CLIENT_LEVELS: return False self.__clientLevel = level # If client level is ANONYME - Success if level == CLIENT_LEVEL_ANONYME: cmdUrl = "0/" self.__setCmdUrl(cmdUrl) self.__setConnected(True) return True # Check name and passw type if not checkValue(name, 'str'): return False if not checkValue(passwd, 'str'): return False # Make command cmd = "client/create?level=%d&name=%s&passwd=%s" % (level, name, passwd) varStruct = { 'client_id': 'data0.client_id', } varResult = {} # Request if not self.request(cmd, varStruct, varResult, forceExec=True): return False # Check client_id if varResult['client_id'] in ['-1', None]: return False # Ok !!! self.__clientName = name self.__clientPasswd = passwd cmdUrl = "%s/" % varResult['client_id'] self.__setCmdUrl(cmdUrl) self.__setConnected(True) print "TuxAPI is connected." return True
def send(self, b0, b1, b2, b3, b4): """ """ if not checkValue(b0, "int"): return False if not checkValue(b1, "int"): return False if not checkValue(b2, "int"): return False if not checkValue(b3, "int"): return False if not checkValue(b4, "int"): return False cmd = "macro/play?macro=0.0:RAW_CMD:0x%.2x:0x%.2x:0x%.2x:0x%.2x:0x%.2x" % ( b0, b1, b2, b3, b4) return self.__cmdSimpleResult(cmd)
def setPitch(self, value): """Set the pitch of the locutor. @param value: pitch (50 .. 200) """ if not checkValue(value, 'int', 50, 200): return self.__pitch = value
def setLocutor(self, value): """Set the locutor. @param value: name of the locutor. """ if not checkValue(value, 'str'): return self.__locutor = value
def onAsync(self, count, finalState=SSV_NDEF): """Move this body part. (asynchronous) @param count: number of movements. @param finalState: requested state after the movement. (SSV_NDEF|SSV_OPEN|SSV_CLOSE) @return: the success of the command. """ if not checkValue(count, "int"): return False if not checkValue(finalState, "str"): return False if finalState not in SSV_MOUTHEYES_POSITIONS: return False cmd = "%s/on?count=%d&final_state=%s" % (self.__partName, count, finalState) return self.__cmdSimpleResult(cmd)
def onDuringAsync(self, duration, finalState=SSV_NDEF): """Move this body part during a number of seconds. (asynchronous) @param duration: duration time in seconds. @param finalState: requested state after the movement. (SSV_NDEF|SSV_OPEN|SSV_CLOSE) @return: the success of the command. """ if not checkValue(duration, "float"): return False if not checkValue(finalState, "str"): return False if finalState not in SSV_MOUTHEYES_POSITIONS: return False cmd = "%s/on_during?duration=%f&final_state=%s" % ( self.__partName, duration, finalState) return self.__cmdSimpleResult(cmd)
def play(self, begin = 0.0): """Play the loaded attitune. @param begin: starting second. @return: the success of the command. """ if not checkValue(begin, "float"): return False cmd = "attitune/play?begin=%f" % begin return self.__cmdSimpleResult(cmd)
def speakAsync(self, text, locutor=None, pitch=None): """Read a text with the text to speak engine. (Asynchronous) @param text: text to speak. @param locutor: name of the locutor. @param pitch: pitch (50 .. 200) @return: the success of the command. """ # Check the text var type if not checkValue(text, 'str'): return False # Set the locutor if locutor != None: self.setLocutor(locutor) cmd = "tts/locutor?name=%s" % self.__locutor ret = self.__cmdSimpleResult(cmd) if not ret: return False # Set the pitch if pitch != None: self.setPitch(pitch) cmd = "tts/pitch?value=%d" % self.__pitch ret = self.__cmdSimpleResult(cmd) if not ret: return False # Try to encode the string try: text = text.decode(self.__encoding) text = text.encode("utf-8", 'replace') except: pass # Remove ending lines text = text.replace("\n", ".") # Perform the speech mText = urllib.urlencode({ 'text': text, }) cmd = "tts/speak?%s" % mText ret = self.__cmdSimpleResult(cmd) if not ret: return False if self.__parent.server.getClientLevel() == CLIENT_LEVEL_ANONYME: return ret # Wait the speak status ret = self.__eventHandlers.waitCondition(ST_NAME_SPEAK_STATUS, (None, None), 5.0) if not ret: return False # Get the speak status value, delay = self.__parent.status.requestOne(ST_NAME_SPEAK_STATUS) if value == "NoError": return True else: return False
def load(self, path): """Load an attitune file. @param path: path of the attitune file. @return: the success of the command. """ if not checkValue(path, "str"): return False path = urllib.urlencode({'path' : path, }) cmd = "attitune/load?%s" % path return self.__cmdSimpleResult(cmd)
def wait(self, statusName, condition = None, timeout = 999999999.0): """Wait a specific state of a status. Not available for CLIENT_LEVEL_ANONYME level. @param statusName: name of the status. @param condition: list of the rules of the condition. @param timeout: maximal delay to wait. @return: the success of the waiting. """ if self.__parent.server.getClientLevel() == CLIENT_LEVEL_ANONYME: raise TuxAPIExceptionCL, self.__parent.server.getClientLevel() if not checkValue(statusName, "str"): return False if not checkValue(timeout, "float"): return False if condition != None: if not checkValue(condition, "tuple"): return False return self.__parent.event.handler.waitCondition(statusName, condition, timeout)
def send(self, statusName, statusValues, encoding = "latin-1"): """Send a status. @param statusName: name of the status. @param statusValues: values of the status as list. @param encoding: encoding format of the source. ( By example, the encoding must be set with sys.stdin.encoding if the status is sent from Tuxshell. The encoding must be set to "utf-8" if the status is sent from a python script coded in utf-8) @return: the success of the command. """ if not checkValue(statusName, "str"): return False if not checkValue(statusValues, "list"): return False if len(statusValues) == 0: return False for statusValue in statusValues: if not checkValue(statusValue, "str"): return False valuesStr = "" for statusValue in statusValues: valuesStr += "%s|" % statusValue valuesStr = valuesStr[:-1] try: u = unicode(valuesStr, encoding) valuesStr = u.encode("latin-1", 'replace') except: pass try: u = statusName.decode(encoding) statusName = u.encode("latin-1", 'replace') except: pass pcmd = urllib.urlencode({'name' : statusName, 'value' : valuesStr,}) cmd = "status/send?%s" % pcmd if self.__parent != None: # Request if self.__parent.server.request(cmd, {}, {}): return True return False
def on(self, count, finalState=SSV_NDEF): """Move this body part. @param count: number of movements. @param finalState: requested state after the movement. (SSV_NDEF|SSV_OPEN|SSV_CLOSE) @return: the success of the command. """ if not checkValue(count, "int"): return False if not checkValue(finalState, "str"): return False if finalState not in SSV_MOUTHEYES_POSITIONS: return False timeout = count * 1.0 ret = self.onAsync(count, finalState) if self.__parent.server.getClientLevel() == CLIENT_LEVEL_ANONYME: return ret if ret: ret = self.waitMovingOff(timeout) return ret
def onDuring(self, duration, finalState=SSV_NDEF): """Move this body part during a number of seconds. @param duration: duration time in seconds. @param finalState: requested state after the movement. (SSV_NDEF|SSV_OPEN|SSV_CLOSE) @return: the success of the command. """ if not checkValue(duration, "float"): return False if not checkValue(finalState, "str"): return False if finalState not in SSV_MOUTHEYES_POSITIONS: return False timeout = 2.0 * duration ret = self.onDuringAsync(duration, finalState) if self.__parent.server.getClientLevel() == CLIENT_LEVEL_ANONYME: return ret if ret: ret = self.waitMovingOff(timeout) return ret
def setSpeed(self, speed): """Set the speed of rotation. @param speed: speed of the rotation. (SPV_VERYSLOW|SPV_SLOW|SPV_NORMAL|SPV_FAST|SPV_VERYFAST) @return: the success of the command. """ if not checkValue(speed, "int"): return False speed = self.__checkSpeed(speed) cmd = "spinning/speed?value=%d" % speed return self.__cmdSimpleResult(cmd)
def onDuringAsync(self, duration, finalState = SSV_UP, speed = SPV_VERYFAST): """Move the flippers during a number of seconds. (asynchronous) @param duration: duration time in seconds. @param finalState: requested state after the movement. (SSV_NDEF|SSV_UP|SSV_DOWN) @param speed: speed of the rotation. (SPV_VERYSLOW|SPV_SLOW|SPV_NORMAL|SPV_FAST|SPV_VERYFAST) @return: the success of the command. """ if not checkValue(duration, "float"): return False if not checkValue(finalState, "str"): return False if finalState not in SSV_FLIPPERS_POSITIONS: return False cmd = "flippers/on_during?duration=%f&final_state=%s" % (duration, finalState) ret = self.__cmdSimpleResult(cmd) if ret: ret = self.setSpeed(speed) return ret
def onAsync(self, count, finalState = SSV_UP, speed = SPV_VERYFAST): """Move the flippers. (asynchronous) @param count: number of movements. @param finalState: requested state after the movement. (SSV_NDEF|SSV_UP|SSV_DOWN) @param speed: speed of the rotation. (SPV_VERYSLOW|SPV_SLOW|SPV_NORMAL|SPV_FAST|SPV_VERYFAST) @return: the success of the command. """ if not checkValue(count, "int"): return False if not checkValue(finalState, "str"): return False if finalState not in SSV_FLIPPERS_POSITIONS: return False cmd = "flippers/on?count=%d&final_state=%s" % (count, finalState) ret = self.__cmdSimpleResult(cmd) if ret: ret = self.setSpeed(speed) return ret
def playAsync(self, waveFile, begin=0.0, end=0.0): """Play a wave file. (Asynchronous) @param waveFile: wave file to play. @param begin: start seconds. @param end: stop seconds. @return: the success of the command. """ if not checkValue(waveFile, 'str'): return False if not checkValue(begin, 'float'): return False if not checkValue(end, 'float'): return False params = urllib.urlencode({ 'path': waveFile, 'begin': begin, 'end': end }) cmd = "wav/play?%s" % params return self.__cmdSimpleResult(cmd)
def onDuring(self, duration, finalState = SSV_UP, speed = SPV_VERYFAST): """Move the flippers during a number of seconds. @param duration: duration time in seconds. @param finalState: requested state after the movement. (SSV_NDEF|SSV_UP|SSV_DOWN) @param speed: speed of the movement. (SPV_VERYSLOW|SPV_SLOW|SPV_NORMAL|SPV_FAST|SPV_VERYFAST) @return: the success of the command. """ if not checkValue(duration, "float"): return False if not checkValue(finalState, "str"): return False if finalState not in SSV_FLIPPERS_POSITIONS: return False timeout = 2.0 * duration ret = self.onDuringAsync(duration, finalState, speed) if self.__parent.server.getClientLevel() == CLIENT_LEVEL_ANONYME: return ret if ret: ret = self.waitMovingOff(timeout) return ret
def on(self, count, finalState = SSV_UP, speed = SPV_VERYFAST): """Move the flippers. @param count: number of movements. @param finalState: requested state after the movement. (SSV_NDEF|SSV_UP|SSV_DOWN) @param speed: speed of the movement. (SPV_VERYSLOW|SPV_SLOW|SPV_NORMAL|SPV_FAST|SPV_VERYFAST) @return: the success of the command. """ if not checkValue(count, "int"): return False if not checkValue(finalState, "str"): return False if finalState not in SSV_FLIPPERS_POSITIONS: return False timeout = count * 1.0 ret = self.onAsync(count, finalState, speed) if self.__parent.server.getClientLevel() == CLIENT_LEVEL_ANONYME: return ret if ret: ret = self.waitMovingOff(timeout) return ret
def setPause(self, value=True): """Set the pause state of the wave player. @param value: True or False. @return: the success of the command. """ # Check the value var type if not checkValue(value, 'bool'): return False if value: pause = "True" else: pause = "False" cmd = "wav/pause?value=%s" % pause return self.__cmdSimpleResult(cmd)
def requestOne(self, statusName): """Get the value and delay of a status. """ if not checkValue(statusName, "str"): return None, None cmd = "status/request_one?status_name=%s" % statusName varStruct = { "value" : "data0.value", "delay" : "data0.delay", } varResult = {} if self.__parent != None: # Request if self.__parent.server.request(cmd, varStruct, varResult): return varResult["value"], varResult["delay"] return None, None
def rightOnDuringAsync(self, duration, speed=SPV_VERYFAST): """Move the robot to the right during a number of seconds. (asynchronous) @param duration: duration of the rotation. @param speed: speed of the rotation. (SPV_VERYSLOW|SPV_SLOW|SPV_NORMAL|SPV_FAST|SPV_VERYFAST) @return: the success of the command. """ if not checkValue(duration, "float"): return False cmd = "spinning/right_on_during?duration=%f" % duration ret = self.__cmdSimpleResult(cmd) if ret: ret = self.setSpeed(speed) return ret
def waitDisconnected(self, timeout): """Wait until the radio/dongle was disconnected. Not available for CLIENT_LEVEL_ANONYME level. @param timeout: maximal delay to wait. @return: the state of the wait result. """ if self.__parent.server.getClientLevel() == CLIENT_LEVEL_ANONYME: raise TuxAPIExceptionCL, self.__parent.server.getClientLevel() if not checkValue(timeout, "float"): return False if not self.getConnected(): return True if self.__parent != None: return self.__eventHandler.waitCondition(("False", None), timeout) return False
def waitPressed(self, timeout): """Wait until the switch was pressed. Not available for CLIENT_LEVEL_ANONYME level. @param timeout: maximal delay to wait. @return: the state of the wait result. """ if self.__parent.server.getClientLevel() == CLIENT_LEVEL_ANONYME: raise TuxAPIExceptionCL, self.__parent.server.getClientLevel() if not checkValue(timeout, "float"): return False if self.getState(): return True if self.__parent != None: return self.__eventHandlers.waitCondition(self.__switchStName, ("True", None), timeout) return False
def waitReleased(self, timeout): """Wait until the remote was released. Not available for CLIENT_LEVEL_ANONYME level. @param timeout: maximal delay to wait. @return: the state of the wait result. """ if self.__parent.server.getClientLevel() == CLIENT_LEVEL_ANONYME: raise TuxAPIExceptionCL, self.__parent.server.getClientLevel() if not checkValue(timeout, "float"): return False if self.getState() == K_RELEASED: return True if self.__parent != None: return self.__eventHandlers.waitCondition(ST_NAME_REMOTE_BUTTON, (K_RELEASED, None), timeout) return False
def waitConnected(self, timeout=99999999999.): """Wait until the client was connected to the server. Not available for CLIENT_LEVEL_ANONYME level. @param timeout: maximal delay to wait. @return: the state of the wait result. """ if self.getClientLevel() == CLIENT_LEVEL_ANONYME: raise TuxAPIExceptionCL, self.getClientLevel() if not checkValue(timeout, "float"): return False if self.getConnected(): return True if self.__connectedEventHandler != None: return self.__connectedEventHandler.waitCondition(("True", None), timeout) else: return False
def rightOnAsync(self, turns, speed=SPV_VERYFAST): """Move the robot to the right. (asynchronous) @param turns: number of turns. @param speed: speed of the rotation. (SPV_VERYSLOW|SPV_SLOW|SPV_NORMAL|SPV_FAST|SPV_VERYFAST) @return: the success of the command. """ if not checkValue(turns, "float"): return False count = int(turns * 4) if (count == 0): count = 1 if (count > 255): count = 255 cmd = "spinning/right_on?count=%d" % count ret = self.__cmdSimpleResult(cmd) if ret: ret = self.setSpeed(speed) return ret
def speakPush(self, text): """Push a text in the TTS stack. @param text: text to speak. @return: the success of the command. """ # Check the text var type if not checkValue(text, 'str'): return False # Try to encode the string try: text = text.decode(self.__encoding) text = text.encode("utf-8", 'replace') except: pass # Remove ending lines text = text.replace("\n", ".") # Perform the speech mText = urllib.urlencode({ 'text': text, }) cmd = "tts/stack_speak?%s" % mText return self.__cmdSimpleResult(cmd)