Beispiel #1
0
class Skill_RobotCommander:
    def __init__(self):

        self.queue = Queue.Queue()
        self.thread_handler = ThreadHandler()
        self.thread_handler.run(target=self.start_blocking)
        self.thread_handler.start_run_loop()
        print(ser)

    def start_blocking(self, run_event):
        while run_event.is_set():
            try:
                self.queue.get(False)
            except Queue.Empty:
                with Hermes(MQTT_ADDR) as h:
                    h.subscribe_intents(self.callback).start()

    # section -> extraction of slot value


    def extract_angle(self, intent_message, default_angle):
        angle = default_angle
        if intent_message.slots.angle:
            angle = intent_message.slots.angle.first().value
        if angle < 0:
            angle = 0
        if angle > 360:
            angle = 360
        return angle
        
    def extract_distance(self, intent_message, default_distance):
        distance = default_distance
        if intent_message.slots.distance:
            distance = intent_message.slots.distance.first().value
        if distance < 0:
            distance = 0
        return distance
        
    def extract_speed(self, intent_message, default_speed):
        speed = default_speed
        if intent_message.slots.speed:
            speed = intent_message.slots.speed.first().value
            if speed == 'fast':
                speed = 'F'
            elif speed == 'slow':
                speed = 'S'
            else:
                speed = 'M'        
        print (speed)
        return speed
   

    # section -> handlers of intents

    def callback(self, hermes, intent_message):
        print("[COMMAND] Received")
        intent_name = intent_message.intent.intent_name
        print(intent_name)
        if ':' in intent_name:
            intent_name = intent_name.split(":")[1]
        if intent_name == 'forward':
            self.queue.put(self.forward(hermes, intent_message))
        if intent_name == 'backward':
            self.queue.put(self.backward(hermes, intent_message))
        if intent_name == 'left':
            self.queue.put(self.left(hermes, intent_message))
        if intent_name == 'right':
            self.queue.put(self.right(hermes, intent_message))
						
    def forward(self, hermes, intent_message):
        distance = self.extract_distance(intent_message, 10)
        speed = self.extract_speed(intent_message, 'M')
        command = b"".join(['FRWD:', speed, ':',str(int(distance)),'\n'])
        print(command)
        ser.write(command)
        self.terminate_feedback(hermes, intent_message)
		
    def backward(self, hermes, intent_message):
        distance = self.extract_distance(intent_message, 10)
        speed = self.extract_speed(intent_message, 'M')
        command = b"".join(['BKWD:', speed, ':',str(int(distance)),'\n'])
        print(command)
        ser.write(command)
        self.terminate_feedback(hermes, intent_message)
				
    def left(self, hermes, intent_message):
        angle = self.extract_angle(intent_message, 45)
        command = b"".join(['LEFT:S:',str(int(angle)),'\n'])
        ser.write(command)
        self.terminate_feedback(hermes, intent_message)

    def right(self, hermes, intent_message):
        angle = self.extract_angle(intent_message, 45)
        command = b"".join(['RGHT:S:',str(int(angle)),'\n'])
        ser.write(command)
        self.terminate_feedback(hermes, intent_message)

    
    def terminate_feedback(self, hermes, intent_message, mode='default'):
        if mode == 'default':
            hermes.publish_end_session(intent_message.session_id, "")
        else:
            # more design
            hermes.publish_end_session(intent_message.session_id, "")
class Skill_Hue:
    def __init__(self):
        try:
            config = SnipsConfigParser.read_configuration_file(CONFIG_INI)
        except:
            config = None
        hostname = None
        code = None
        if config and config.get('secret', None) is not None:
            if config.get('secret').get('hostname', None) is not None:
                hostname = config.get('secret').get('hostname')
                if hostname == "":
                    hostname = None
            if config.get('secret').get(API_KEY, None) is not None:
                code = config.get('secret').get(API_KEY)
                if code == "":
                    code = None
            elif os.path.isfile(CACHE_INI):
                try:
                    cached_config = SnipsConfigParser.read_configuration_file(
                        CACHE_INI)
                except:
                    cached_config = None
                if cached_config and cached_config.get('secret',
                                                       None) is not None:
                    if cached_config.get('secret').get(API_KEY,
                                                       None) is not None:
                        code = cached_config.get('secret').get(API_KEY)
                        if code == "":
                            code = None
        if hostname is None or code is None:
            print('No configuration')
        self.snipshue = SnipsHue(hostname, code)
        hostname = self.snipshue.hostname
        code = self.snipshue.username
        self.update_config(CACHE_INI, config, hostname, code)
        self.queue = Queue.Queue()
        self.thread_handler = ThreadHandler()
        self.thread_handler.run(target=self.start_blocking)
        self.thread_handler.start_run_loop()

    def update_config(self, filename, data, hostname, code):
        if not os.path.exists(CACHE_INI_DIR):
            os.makedirs(CACHE_INI_DIR)
        if 'secret' not in data or data['secret'] is None:
            data['secret'] = {}
        data['secret']['hostname'] = hostname
        data['secret'][API_KEY] = code
        SnipsConfigParser.write_configuration_file(filename, data)

    def start_blocking(self, run_event):
        while run_event.is_set():
            try:
                self.queue.get(False)
            except Queue.Empty:
                with Hermes(MQTT_ADDR) as h:
                    h.subscribe_intents(self.callback).start()

    # section -> extraction of slot value
    def extract_house_rooms(self, intent_message):
        house_rooms = []
        if intent_message.slots.house_room:
            for room in intent_message.slots.house_room.all():
                print type(room.value)
                house_rooms.append(room.value)
        return house_rooms

    def extract_percentage(self, intent_message, default_percentage):
        percentage = default_percentage
        if intent_message.slots.percent:
            percentage = intent_message.slots.percent.first().value
        if percentage < 0:
            percentage = 0
        if percentage > 100:
            percentage = 100
        return percentage

    def extract_color(self, intent_message):
        color_code = None
        if intent_message.slots.color:
            color_code = intent_message.slots.color.first().value
        return color_code

    def extract_scene(self, intent_message):
        scene_code = None
        if intent_message.slots.scene:
            scene_code = intent_message.slots.scene.first().value
        return scene_code

    # section -> handlers of intents

    def callback(self, hermes, intent_message):
        print("[HUE] Received")
        # all the intents have a house_room slot, extract here
        rooms = self.extract_house_rooms(intent_message)
        intent_name = intent_message.intent.intent_name
        if ':' in intent_name:
            intent_name = intent_name.split(":")[1]
        if intent_name == 'turnOn':
            self.queue.put(self.turn_on(hermes, intent_message, rooms))
        if intent_name == 'turnOff':
            self.queue.put(self.turn_off(hermes, intent_message, rooms))
        if intent_name == 'setBrightness':
            self.queue.put(self.set_brightness(hermes, intent_message, rooms))
        if intent_name == 'setColor':
            self.queue.put(self.set_color(hermes, intent_message, rooms))
        if intent_name == 'setScene':
            self.queue.put(self.set_scene(hermes, intent_message, rooms))
        if intent_name == 'shiftUp':
            self.queue.put(self.shift_up(hermes, intent_message, rooms))
        if intent_name == 'shiftDown':
            self.queue.put(self.shift_down(hermes, intent_message, rooms))

    def turn_on(self, hermes, intent_message, rooms):
        if len(rooms) > 0:
            for room in rooms:
                self.snipshue.light_on(room.lower())
        else:
            self.snipshue.light_on_all()
        self.terminate_feedback(hermes, intent_message)

    def turn_off(self, hermes, intent_message, rooms):
        if len(rooms) > 0:
            for room in rooms:
                self.snipshue.light_off(room.lower())
        else:
            self.snipshue.light_off_all()
        self.terminate_feedback(hermes, intent_message)

    def set_brightness(self, hermes, intent_message, rooms):
        percent = self.extract_percentage(intent_message, None)
        if percent is None:
            self.terminate_feedback(hermes, intent_message)
            return
        if len(rooms) > 0:
            for room in rooms:
                self.snipshue.light_brightness(percent, room.lower())
        else:
            self.snipshue.light_brightness_all(percent)
        self.terminate_feedback(hermes, intent_message)

    def set_color(self, hermes, intent_message, rooms):
        color = self.extract_color(intent_message)
        if color is None:
            self.terminate_feedback(hermes, intent_message)
            return
        if len(rooms) > 0:
            for room in rooms:
                self.snipshue.light_color(color, room.lower())
        else:

            self.snipshue.light_color_all(color)
        self.terminate_feedback(hermes, intent_message)

    def set_scene(self, hermes, intent_message, rooms):
        scene = self.extract_scene(intent_message)
        if scene is None:
            self.terminate_feedback(hermes, intent_message)
            return
        if len(rooms) > 0:
            for room in rooms:
                self.snipshue.light_scene(scene, room.lower())
        else:
            self.snipshue.light_scene_all(scene)
        self.terminate_feedback(hermes, intent_message)

    def shift_up(self, hermes, intent_message, rooms):
        percent = self.extract_percentage(intent_message, 20)
        if len(rooms) > 0:
            for room in rooms:
                self.snipshue.light_up(percent, room.lower())
        else:
            self.snipshue.light_up_all(percent)
        self.terminate_feedback(hermes, intent_message)

    def shift_down(self, hermes, intent_message, rooms):
        percent = self.extract_percentage(intent_message, 20)
        if len(rooms) > 0:
            for room in rooms:
                self.snipshue.light_down(percent, room.lower())
        else:
            self.snipshue.light_down_all(percent)
        self.terminate_feedback(hermes, intent_message)

    # section -> feedback reply // future function
    def terminate_feedback(self, hermes, intent_message, mode='default'):
        if mode == 'default':
            hermes.publish_end_session(intent_message.session_id, "")
        else:
            # more design
            hermes.publish_end_session(intent_message.session_id, "")
class Skill:
    def __init__(self):
        try:
            config = SnipsConfigParser.read_configuration_file(CONFIG_INI)
        except:
            config = None
        hostname = None
        code = None
        if config and config.get('secret') is not None:
            if config.get('secret').get('hostname') is not None:
                hostname = config.get('secret').get('hostname')
                if hostname == "":
                    hostname = None
            if config.get('secret').get(API_KEY) is not None:
                code = config.get('secret').get(API_KEY)
                if code == "":
                    code = None
        if hostname is None or code is None:
            print('No configuration')
        self.snipshue = SnipsHue(hostname, code)
        hostname = self.snipshue.hostname
        code = self.snipshue.username
        self.update_config(CONFIG_INI, config, hostname, code)
        self.queue = Queue.Queue()
        self.thread_handler = ThreadHandler()
        self.thread_handler.run(target=self.start_blocking)
        self.thread_handler.start_run_loop()

    def update_config(self, filename, data, hostname, code):
        if not os.path.exists(CONFIG_INI_DIR):
            os.makedirs(CONFIG_INI_DIR)
        if 'secret' not in data or data['secret'] is None:
            data['secret'] = {}
        data['secret']['hostname'] = hostname
        data['secret'][API_KEY] = code
        SnipsConfigParser.write_configuration_file(filename, data)

    def start_blocking(self, run_event):
        while run_event.is_set():
            try:
                self.queue.get(False)
            except Queue.Empty:
                with Hermes(MQTT_ADDR) as h:
                    h.subscribe_intents(self.callback).start()

    def extract_house_rooms(self, intent_message):
        house_rooms = []
        if intent_message.slots.house_room is not None:
            for room in intent_message.slots.house_room:
                house_rooms.append(room.slot_value.value.value)
        return house_rooms

    def extract_number(self, intent_message, default_number):
        number = default_number
        if intent_message.slots.intensity_number:
            number = intent_message.slots.intensity_number.first().value
        if intent_message.slots.intensity_percent:
            number = intent_message.slots.intensity_percent.first().value
        return number

    def extract_up_down(self, intent_message):
        res = "down"
        if intent_message.slots.up_down:
            res = intent_message.slots.up_down.first().value
        return res

    def extract_color(self, intent_message):
        res = None
        if intent_message.slots.color:
            res = intent_message.slots.color.first().value
        return res
        pass

    def callback(self, hermes, intent_message):
        rooms = self.extract_house_rooms(intent_message)
        if intent_message.intent.intent_name == 'lightsTurnOff':
            self.queue.put(self.lights_turn_off(hermes, intent_message, rooms))
        if intent_message.intent.intent_name == 'lightsTurnOnSet':
            self.queue.put(
                self.lights_turn_on_set(hermes, intent_message, rooms))
        if intent_message.intent.intent_name == 'lightsShift':
            self.queue.put(self.lights_shift(hermes, intent_message, rooms))
        if intent_message.intent.intent_name == 'lightsSet':
            self.queue.put(
                self.lights_turn_on_set(hermes, intent_message, rooms))
        if intent_message.intent.intent_name == 'snips-labs:lightsColor_FR_':
            self.queue.put(
                self.lights_turn_on_set(hermes, intent_message, rooms))
        if intent_message.intent.intent_name == 'snips-labs:lightsColor_EN':
            self.queue.put(
                self.lights_turn_on_set(hermes, intent_message, rooms))

    def lights_turn_off(self, hermes, intent_message, rooms):
        hermes.publish_end_session(intent_message.session_id, None)
        if len(rooms) > 0:
            for room in rooms:
                self.snipshue.light_off(room)
        else:
            self.snipshue.light_off(None)

    def lights_turn_on_set(self, hermes, intent_message, rooms):
        hermes.publish_end_session(intent_message.session_id, None)
        number = self.extract_number(intent_message, 100)
        color = self.extract_color(intent_message)
        if len(rooms) > 0:
            for room in rooms:
                self.snipshue.light_on_set(color, number, room)
        else:
            self.snipshue.light_on_set(color, number, None)

    def lights_shift(self, hermes, intent_message, rooms):
        hermes.publish_end_session(intent_message.session_id, None)
        number = self.extract_number(intent_message, 20)
        if "down" == self.extract_up_down(intent_message):
            self.lights_turn_down(number, rooms)
        else:
            self.lights_turn_up(number, rooms)

    def lights_turn_down(self, number, rooms):
        if len(rooms) > 0:
            for room in rooms:
                self.snipshue.light_down(number, room)
        else:
            self.snipshue.light_down(number, None)

    def lights_turn_up(self, number, rooms):
        if len(rooms) > 0:
            for room in rooms:
                self.snipshue.light_up(number, room)
        else:
            self.snipshue.light_up(number, None)
class SkillHarmonyControl:
    snipsConfigParser = SnipsConfigParser()

    def __init__(self):
        print("[HARMONY] Starting INIT")

        try:
            config = SnipsConfigParser.read_configuration_file(CONFIG_INI)
        except:
            config = None
            print("[HARMONY] Config error")

        # Config variables to fill
        harmony_ip = None
        watch_film_activity_id = None

        # Getting conf
        if config and config.get('secret', None) is not None:
            # Get Harmony IP from Config
            if config.get('secret').get(HARMONY_IP_CONFIG_KEY,
                                        None) is not None:
                harmony_ip = config.get('secret').get(HARMONY_IP_CONFIG_KEY)
                if harmony_ip == "":
                    harmony_ip = None

            # Get Activities ID from Config
            if config.get('secret').get(WATCH_FILM_ACTIVITY_CONFIG_KEY,
                                        None) is not None:
                watch_film_activity_id = config.get('secret').get(
                    WATCH_FILM_ACTIVITY_CONFIG_KEY)
                if watch_film_activity_id == "":
                    self.WATCH_FILM_ACTIVITY_ID = None
                else:
                    self.WATCH_FILM_ACTIVITY_ID = watch_film_activity_id

        if harmony_ip is None or watch_film_activity_id is None:
            print('No configuration')

        print("[HARMONY] Creating Harmony Controller")
        self.harmony_controller = HarmonyController(harmony_ip=harmony_ip)
        print("[HARMONY] Harmony Controller Ready")

        self.queue = queue.Queue()
        self.thread_handler = ThreadHandler()
        self.thread_handler.run(target=self.start_blocking)
        self.thread_handler.start_run_loop()

        print("[HARMONY] Ending INIT")

    def action_wrapper(self, hermes, intent_message):
        print("[HARMONY] Received")

        # all the intents have a house_room slot, extract here
        intent_name = intent_message.intent.intent_name
        if ':' in intent_name:
            intent_name = intent_name.split(":")[1]
        if intent_name == 'watchFilm':
            self.queue.put(self.start_watch_film(hermes, intent_message))

        if intent_name == 'powerOff':
            self.queue.put(self.power_off(hermes, intent_message))

    def start_watch_film(self, hermes, intent_message):
        self.start_activity(hermes, intent_message,
                            self.WATCH_FILM_ACTIVITY_ID)

    def start_activity(self, hermes, intent_message, activity_id):
        self.harmony_controller.start_activity(activity_id)
        self.terminate_feedback(hermes, intent_message)

    def power_off(self, hermes, intent_message):
        self.harmony_controller.power_off()
        self.terminate_feedback(hermes, intent_message)

    def terminate_feedback(self, hermes, intent_message, mode='default'):
        if mode == 'default':
            hermes.publish_end_session(intent_message.session_id, "")
        else:
            # more design
            hermes.publish_end_session(intent_message.session_id, "")

    def subscribe_intent_callback(self, hermes, intent_message):
        self.action_wrapper(hermes, intent_message)

    def start_blocking(self, run_event):
        while run_event.is_set():
            try:
                self.queue.get(False)
            except queue.Empty:
                with Hermes(MQTT_ADDR) as h:
                    h.subscribe_intents(self.subscribe_intent_callback).start()
Beispiel #5
0
class Skill_LGTV:
    def __init__(self):
        config = SnipsConfigParser.read_configuration_file(CONFIG_INI)
        ip = config.get("secret").get("ip", None)
        if not ip:
            print("Could not load [secret][ip] from %s" % CONFIG_INI)
            sys.exit(1)
        mac = config.get("secret").get("mac", None)
        if not mac:
            print("Could not load [secret][mac] from %s" % CONFIG_INI)
            sys.exit(1)
        onkyoip = config.get("secret").get("onkyoip", None)
        if not onkyoip:
            print("Could not load [secret][onkyoip] from %s" % CONFIG_INI)
            sys.exit(1)
        self.snipslgtv = SnipsLGTV(ip, mac, onkyoip)
        self.queue = queue.Queue()
        self.thread_handler = ThreadHandler()
        self.thread_handler.run(target=self.start_blocking)
        self.thread_handler.start_run_loop()

    def start_blocking(self, run_event):
        while run_event.is_set():
            try:
                self.queue.get(False)
            except queue.Empty:
                with Hermes(MQTT_ADDR) as h:
                    h.subscribe_intents(self.callback).start()

    def tvOn(self, hermes, intent_message):
        print("CHANCE: TVON CALLED")
        res = self.snipslgtv.turn_on()
        print("CHANCE: POST_TURNON")
        current_session_id = intent_message.session_id
        self.terminate_feedback(hermes, intent_message)

    def tvOff(self, hermes, intent_message):
        print("CHANCE: TVOFF CALLED")
        res = self.snipslgtv.turn_off()
        current_session_id = intent_message.session_id
        self.terminate_feedback(hermes, intent_message)

    def closeApp(self, hermes, intent_message):
        res = self.snipslgtv.close_app()
        current_session_id = intent_message.session_id
        self.terminate_feedback(hermes, intent_message)

    def openApp(self, hermes, intent_message):
        app_name = intent_message.slots.appName.first().value
        if not app_name:
            print("Could not read App name from intent message")
        res = self.snipslgtv.open_app(app_name)
        current_session_id = intent_message.session_id
        self.terminate_feedback(hermes, intent_message)

    def setVolume(self, hermes, intent_message):
        volume = intent_message.slots.volume.first().value
        if not volume:
            print("Could not read volume from intent message")
        print("CHANCE: VOLUME \\/")
        print(volume)
        res = self.snipslgtv.set_volume(int(volume))
        current_session_id = intent_message.session_id
        self.terminate_feedback(hermes, intent_message)

    def mute(self, hermes, intent):
        res = self.snipslgtv.mute()
        current_session_id = intent_message.session_id
        self.terminate_feedback(hermes, intent_message)

    def unmute(self, hermes, intent):
        res = self.snipslgtv.unmute()
        current_session_id = intent_message.session_id
        self.terminate_feedback(hermes, intent_message)

    def callback(self, hermes, intent_message):
        intent_name = intent_message.intent.intent_name
        print("CHANCE CALLBACK: %s" % intent_name)
        if ':' in intent_name:
            intent_name = intent_name.split(":")[1]
            print("CHANCE AFTER: %s" % intent_name)
        if intent_name == 'tvOn':
            print("CHANCE TV ON")
            self.queue.put(self.tvOn(hermes, intent_message))
        if intent_name == 'tvOff':
            print("CHANCE TV OFF")
            self.queue.put(self.tvOff(hermes, intent_message))
        if intent_name == 'openApp':
            print("CHANCE OPEN APP")
            self.queue.put(self.openApp(hermes, intent_message))
        if intent_name == 'setVolume':
            print("CHANCE SET VOLUME")
            self.queue.put(self.setVolume(hermes, intent_message))
        if intent_name == 'mute':
            print("CHANCE MUTE")
            self.queue.put(self.mute(hermes, intent_message))
        if intent_name == 'unMute':
            print("CHANCE UNMUTE")
            self.queue.put(self.unmute(hermes, intent_message))

    ####    section -> feedback reply // future function
    def terminate_feedback(self, hermes, intent_message, mode='default'):
        if mode == 'default':
            hermes.publish_end_session(intent_message.session_id, "")
        else:
            #### more design
            hermes.publish_end_session(intent_message.session_id, "")