|
| 1 | +#!/home/pi/.virtualenvs/python34/bin/python |
| 2 | + |
| 3 | +import boto3 |
| 4 | +import boto3.session |
| 5 | +import json |
| 6 | +from subprocess import call |
| 7 | +from time import sleep |
| 8 | +import os.path |
| 9 | +from botocore.exceptions import ClientError |
| 10 | +from botocore.exceptions import BotoCoreError |
| 11 | +import logging |
| 12 | +LOG_FILENAME = '/home/pi/log/echoremote.log' |
| 13 | +logging.basicConfig(filename=LOG_FILENAME, level=logging.INFO) |
| 14 | + |
| 15 | + |
| 16 | +def log(msg): |
| 17 | + logging.info(msg) |
| 18 | + |
| 19 | +sequences = {'all_off': ['tv_off.txt', 'receiver_off.txt', |
| 20 | + 'xbox_off.txt', 'apple_tv_home.txt'], |
| 21 | + 'apple_tv': ['tv_on.txt', |
| 22 | + 'receiver_apple_tv.txt', |
| 23 | + 'apple_tv_home.txt'], |
| 24 | + 'xbox': ['tv_on.txt', 'receiver_xbox.txt', 'xbox_on.txt'], |
| 25 | + 'raspberry_pie': ['tv_on.txt', 'receiver_raspberry_pie.txt']} |
| 26 | +keys = ('ActionA', 'ActionB', 'ActionC', 'ActionD', 'ActionE', 'ActionF') |
| 27 | +signals_path = '/home/pi/scribbles/python/echoir/signals/' |
| 28 | + |
| 29 | + |
| 30 | +def handle_seq(sequence): |
| 31 | + for signal in sequence: |
| 32 | + send_signal(signal) |
| 33 | + sleep(0.05) |
| 34 | + |
| 35 | + |
| 36 | +def send_signal(signal): |
| 37 | + signal_full = os.path.join(signals_path, signal) |
| 38 | + if os.path.isfile(signal_full): |
| 39 | + call(['/usr/bin/igclient', '--send', signal_full]) |
| 40 | + log(" Sent: {}".format(signal)) |
| 41 | + else: |
| 42 | + log(" NOT SENT: {}".format(signal)) |
| 43 | + |
| 44 | + |
| 45 | +def process_msg(msg): |
| 46 | + log(msg.body) |
| 47 | + intent = json.loads(msg.body) |
| 48 | + action = intent['name'] |
| 49 | + if action == 'Power': |
| 50 | + handle_power(intent['slots']) |
| 51 | + elif action == 'Volume': |
| 52 | + handle_volume(intent['slots']) |
| 53 | + elif action == 'Launch': |
| 54 | + handle_launch(intent['slots']) |
| 55 | + elif action == 'Action': |
| 56 | + handle_action(intent['slots']) |
| 57 | + msg.delete() |
| 58 | + |
| 59 | + |
| 60 | +def handle_power(slots): |
| 61 | + device = slots['Device']['value'].replace('the ', '').replace(' ', '_') |
| 62 | + onoff = slots['OnOff']['value'] |
| 63 | + if device == 'everything': |
| 64 | + handle_seq(sequences['all_off']) |
| 65 | + else: |
| 66 | + signal = '{}_{}.txt'.format(device, onoff) |
| 67 | + send_signal(signal) |
| 68 | + |
| 69 | + |
| 70 | +def handle_volume(slots): |
| 71 | + signal = 'receiver_volume_{}.txt'.format(slots['UpDown']['value']) |
| 72 | + reps = int(slots['Repeat']['value']) |
| 73 | + sequence = [signal for i in range(reps)] |
| 74 | + handle_seq(sequence) |
| 75 | + |
| 76 | + |
| 77 | +def handle_launch(slots): |
| 78 | + action = slots['Activity']['value'].replace(' ', '_') |
| 79 | + if action in sequences: |
| 80 | + handle_seq(sequences[action]) |
| 81 | + |
| 82 | + |
| 83 | +def handle_action(slots): |
| 84 | + device = slots['Device']['value'].replace('the ', '').replace(' ', '_') |
| 85 | + device = device.lower() |
| 86 | + sequence = [] |
| 87 | + for key in keys: |
| 88 | + if 'value' in slots[key] and slots[key]['value'] is not None: |
| 89 | + sequence.append(slots[key]['value'].replace(' ', '_').lower()) |
| 90 | + signals = ['{}_{}.txt'.format(device, a) for a in sequence] |
| 91 | + handle_seq(signals) |
| 92 | + |
| 93 | + |
| 94 | +url = 'https://queue.amazonaws.com/720549148055/echoRemote' |
| 95 | +s = boto3.session.Session(region_name='us-east-1') |
| 96 | +queue = s.resource('sqs').Queue(url) |
| 97 | + |
| 98 | + |
| 99 | +while True: |
| 100 | + try: |
| 101 | + log("Polling for messages...") |
| 102 | + messages = queue.receive_messages(WaitTimeSeconds=20) |
| 103 | + for message in messages: |
| 104 | + log(" Processing a message.") |
| 105 | + process_msg(message) |
| 106 | + except (ClientError, BotoCoreError) as e: |
| 107 | + log("Request failed. Sleeping for a minute.") |
| 108 | + log(str(e)) |
| 109 | + sleep(60) |
0 commit comments