ratatouille/src/ratatouille.py

166 lines
5.2 KiB
Python

import datetime
import logging
import os
import time
import config
import src.tools.date as date
from src.tools.str import int_to_str
from src.tools.parse_entities import parse_entities
from src.const.temperature_keyword import TEMPERATURE_KEYWORD
class Ratatouille():
def __init__(self,yoda, walle, mopidy, schedule):
self.yoda = yoda
self.walle = walle
self.mopidy = mopidy
self.schedule = schedule
# schedule.every().day.at(config.hibernate_time).do(self.hibernate)
# schedule.every().day.at(config.wakeup_time).do(self.clear_hibernate)
# yoda.say('Ratatouille a bien démmaré')
logging.info('loaded')
def parse_rhasspy_command(self,payload):
command = payload['intent']['intentName']
print(command)
response = ''
if command == 'GetTime':
response = self.send_hour()
elif command == 'GetDate':
response = self.send_date()
elif command == 'Temperature':
response = self.send_temperature()
elif command == 'PlayMusicGenre':
response = self.play_genre(payload['slots'][0]['value']['value'])
elif command == 'PauseMusic':
response = self.pause_music()
elif command == 'StartMusic':
response = self.start_music()
self.yoda.say(response)
return
def parseAlexa(self,payload):
print(payload)
intent = payload['intents'][0]['label']
entities = payload['entities']
if intent == 'datetime_query':
return self.send_hour()
if intent == 'iot_hue_lightoff':
return self.light_off(entities)
if intent == 'iot_hue_lighton':
return self.light_on(entities)
if intent == 'weather_query':
return self.weather_query(parse_entities(entities))
return '42'
def weather_query(self,entities):
if any(a in entities['B-weather_descriptor'] for a in TEMPERATURE_KEYWORD):
res = self.send_temperature()
return res
def play_genre(self,genre):
try:
self.mopidy.play_genre(genre)
except Exception as e:
logging.error(e)
return 'Le serveur de musique a un problème'
return
def pause_music(self):
try:
self.mopidy.pause()
except Exception as e:
logging.error(e)
return 'Le serveur de musique a un problème'
return
def start_music(self):
try:
self.mopidy.start()
except Exception as e:
logging.error(e)
return 'Le serveur de musique a un problème'
return
def send_hour(self):
now = datetime.datetime.now()
return date.get_time(now)
def send_date(self):
now = datetime.datetime.now()
return date.get_date(now)
def send_temperature(self):
logging.info('Send temperature')
data = self.walle.get('weather.toulouse')
temperature = str(data['attributes']['temperature']).replace('.',' virgule ')
return 'il fait '+temperature+' degrés'
def light_off(self,entities):
number_error = 0
for lamp in entities:
try:
self.walle.light_off(lamp['word'].strip(""))
except Exception as e:
logging.warning("Error light off:")
logging.warning(e)
number_error = number_error + 1
if number_error == 0:
return 'Et voila !'
else:
return 'J\'ai pas pu eteindre ' + int_to_str(number_error,'f') + ' lampes'
def light_on(self,entities):
number_error = 0
for lamp in entities:
try:
self.walle.light_on(lamp['word'].strip(""))
except Exception as e:
logging.warning("Error light on:")
logging.warning(e)
number_error = number_error + 1
if number_error == 0:
return 'Et voila !'
else:
return 'J\'ai pas pu allumer ' + int_to_str(number_error,'f') + ' lampes'
# -- -- hibernate -- --
def can_hibernate(self):
lamp_desk = self.walle.get('switch.prise_bureau_switch')
if lamp_desk:
desk_is_on = lamp_desk['attributes'].get('state','OFF') == 'ON'
lamp_bathroom = self.walle.get('light.chihiro_chihiro')
if lamp_bathroom:
bathroom_is_on = lamp_bathroom.get('state','OFF') == 'on'
#bathroom_is_on = self.walle.get('switch.prise_bureau_switch')['attributes']['state'] == 'ON'
return lamp_desk and lamp_bathroom and not desk_is_on and not bathroom_is_on
def hibernate(self):
if self.can_hibernate():
logging.info('will hibernate')
self.schedule.clear('hourly-hibernate')
os.system("sudo /bin/systemctl suspend")
time.sleep(5)
else:
self.schedule.clear('hourly-hibernate')
self.schedule.every(3).minutes.do(self.hibernate).tag('hourly-hibernate')
logging.info('retry to hibernate in 3 minutes')
return
def clear_hibernate(self):
self.schedule.clear('hourly-hibernate')
def test(self):
print('lol')