diff --git a/client/app_utils.py b/client/app_utils.py index c35c6db..5ea11b4 100644 --- a/client/app_utils.py +++ b/client/app_utils.py @@ -5,6 +5,8 @@ import logging import os from pytz import timezone +import time +import subprocess def sendEmail(SUBJECT, BODY, ATTACH_LIST, TO, FROM, SENDER, @@ -129,3 +131,77 @@ def getTimezone(profile): return timezone(profile['timezone']) except: return None + + +def create_reminder(remind_event, remind_time): + _logger = logging.getLogger(__name__) + if len(remind_time) == 14: + cmd = 'task add ' + remind_event + ' due:' +\ + remind_time[:4] + '-' + remind_time[4:6] + '-' + \ + remind_time[6:8] + 'T' + remind_time[8:10] + ':' + \ + remind_time[10:12] + ':' + remind_time[12:] + try: + p = subprocess.Popen( + cmd, + stdout=subprocess.PIPE, shell=True) + p.wait() + line = p.stdout.readline() + + if 'Created task' in line: + return True + except Exception, e: + _logger.error(e) + return False + else: + return False + + +def get_due_reminders(): + task_ids = [] + due_tasks = [] + _logger = logging.getLogger(__name__) + try: + p = subprocess.Popen( + 'task status:pending count', + stdout=subprocess.PIPE, shell=True) + p.wait() + + pending_task_num = int(p.stdout.readline()) + + p = subprocess.Popen( + 'task list', + stdout=subprocess.PIPE, shell=True) + p.wait() + lines = p.stdout.readlines()[3:(3 + pending_task_num)] + for line in lines: + task_ids.append(line.split()[0]) + + now = int(time.strftime('%Y%m%d%H%M%S')) + + for id in task_ids: + p = subprocess.Popen( + 'task _get ' + id + '.due', + stdout=subprocess.PIPE, shell=True) + p.wait() + due_time = p.stdout.readline() + due_time_format = int( + due_time[:4] + due_time[5:7] + due_time[8:10] + + due_time[11:13] + due_time[14:16] + due_time[17:19]) + if due_time_format <= now: + p = subprocess.Popen( + 'task _get ' + id + '.description', + stdout=subprocess.PIPE, shell=True) + p.wait() + event = p.stdout.readline() + due_tasks.append(event.strip('\n') + u',时间到了') + cmd = 'task delete ' + id + p = subprocess.Popen( + cmd.split(), + stdout=subprocess.PIPE, + stdin=subprocess.PIPE) + p.stdin.write('yes\n') + + except Exception, e: + _logger.error(e) + + return due_tasks diff --git a/client/local_mic.py b/client/local_mic.py index 5fb1b3d..3566410 100644 --- a/client/local_mic.py +++ b/client/local_mic.py @@ -10,6 +10,9 @@ class Mic: prev = None def __init__(self, config, speaker, passive_stt_engine, active_stt_engine): + self.stop_passive = False + self.skip_passive = False + self.chatting_mode = False return def passiveListen(self, PERSONA): @@ -30,6 +33,3 @@ def activeListen(self, THRESHOLD=None, LISTEN=True, MUSIC=False): def say(self, phrase, OPTIONS=None): print("DINGDANG: %s" % phrase) - - def stop_passive(self): - pass diff --git a/client/notifier.py b/client/notifier.py index c77f076..64d768f 100644 --- a/client/notifier.py +++ b/client/notifier.py @@ -4,6 +4,8 @@ from plugins import Email from apscheduler.schedulers.background import BackgroundScheduler import logging +import app_utils +import time class Notifier(object): @@ -32,6 +34,10 @@ def __init__(self, profile, brain): self._logger.debug('email account not set ' + 'in profile, email notifier will not be used') + if 'robot' in profile and profile['robot'] == 'emotibot': + self.notifiers.append(self.NotificationClient( + self.handleRemenderNotifications, None)) + sched = BackgroundScheduler(daemon=True) sched.start() sched.add_job(self.gather, 'interval', seconds=30) @@ -66,6 +72,14 @@ def styleEmail(e): return lastDate + def handleRemenderNotifications(self, lastDate): + lastDate = time.strftime('%d %b %Y %H:%M:%S') + due_reminders = app_utils.get_due_reminders() + for reminder in due_reminders: + self.q.put(reminder) + + return lastDate + def getNotification(self): """Returns a notification. Note that this function is consuming.""" try: diff --git a/client/robot.py b/client/robot.py index 6fca39f..44bb7dc 100644 --- a/client/robot.py +++ b/client/robot.py @@ -3,10 +3,12 @@ import json import logging from uuid import getnode as get_mac -from app_utils import sendToUser +from app_utils import sendToUser, create_reminder from abc import ABCMeta, abstractmethod import sys + + reload(sys) sys.setdefaultencoding('utf-8') @@ -166,6 +168,15 @@ def chat(self, texts): else: responds.append(jsondata.get('data')[0].get('value')) result = '\n'.join(responds) + + if jsondata.get('data')[0]['cmd'] == 'reminder': + data = jsondata.get('data')[0] + remind_info = data.get('data').get('remind_info') + remind_event = remind_info[0].get('remind_event') + remind_time = remind_info[0].get('remind_time') + + if not create_reminder(remind_event, remind_time): + result = '\n'.join('创建提醒失败了') else: result = u"抱歉, 我的大脑短路了,请稍后再试试." max_length = 200