Skip to content
This repository has been archived by the owner on Apr 13, 2018. It is now read-only.

Commit

Permalink
Reminder (#43)
Browse files Browse the repository at this point in the history
* 修复local模式下,Mic类某些变量未定义的问题

* 新增日程提醒功能

* 修复flake8检查的错误
  • Loading branch information
yunxiyinzhe authored and wzpan committed Sep 14, 2017
1 parent 59bf4cb commit 77a71e4
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 4 deletions.
76 changes: 76 additions & 0 deletions client/app_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
6 changes: 3 additions & 3 deletions client/local_mic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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
14 changes: 14 additions & 0 deletions client/notifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from plugins import Email
from apscheduler.schedulers.background import BackgroundScheduler
import logging
import app_utils
import time


class Notifier(object):
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand Down
13 changes: 12 additions & 1 deletion client/robot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 77a71e4

Please # to comment.