def get_dailyreport(num_days): reports = [] rlSQL = RobertLogMSSQL(host=config.db_server, user=config.db_user, pwd=config.db_pwd, db="robertlog") cur = datetime.datetime.utcnow() + datetime.timedelta(days=2) actions = rlSQL.GetActionReports(20 * num_days) milk = 0 breast = 0 breastNum = 0 poop = 0 sleep = 0 daysShown = 0 for a in actions: if a.Status == Action.Deleted: continue if a.TimeStamp.day != cur.day and (milk != 0 or breast != 0): reports.append(DailyReport(milk_ml = milk, milk_min = breast,\ milk_num = breastNum, poop = poop, sleep = sleep, date = a.TimeStamp)) milk = 0 breast = 0 poop = 0 breastNum = 0 sleep = 0 daysShown += 1 cur = a.TimeStamp if a.Type == ActionType.Feed: nums = re.findall(r"\d+", a.Detail) if len(nums) > 0: d = int(nums[0]) if a.Detail.find("母乳") >= 0: breast += d breastNum += 1 elif a.Detail.find("奶瓶") >= 0: milk += d elif a.Type == ActionType.Poop: poop += 1 elif a.Type == ActionType.WakeUp: ect = extract_cn_time() sleep += ect.extract_time_delta(a.Detail) elif a.Type == ActionType.Notes: #response += "{0}日{1}\n".format(cur.strftime("%m-%d"),a.GenBrief()) pass return reports
def DetectAction(self, msg): msg.RawContent = msg.RawContent.replace(":", ":") action = Action(msg) num2d = num_cn2digital() ect = extract_cn_time() content = num2d.replace_cn_digital(msg.RawContent) t = ect.extract_time(content) if t is not None and len(t) > 0: action.TimeStamp = t[0] content = ect.remove_time(content) if self.check_strList(msg.RawContent, self.NotesKeywords): action.Type = ActionType.Notes action.Detail = msg.RawContent for k in self.NotesKeywords: action.Detail = action.Detail.lstrip(k) elif self.check_strList(msg.RawContent, self.ListSleepTimeKeywords): action.Type = ActionType.SleepTime self.get_latest_sleep(action, num2d, ect) elif self.check_strList(msg.RawContent, self.ADKeywords): action.Type = ActionType.AD elif self.check_strList(msg.RawContent, self.EatCaKeywords): action.Type = ActionType.EatCa elif self.check_strList(msg.RawContent, self.ComFoodListKeywords): action.Type = ActionType.ComFoodList elif self.check_strList(msg.RawContent, self.ComFoodKeywords): action.Type = ActionType.ComFood start = msg.RawContent.find(self.ComFoodKeywords[0]) if start < 0: start = msg.RawContent.find(self.ComFoodKeywords[1]) detail = msg.RawContent[start+2:].strip() action.Detail = detail elif self.check_strList(msg.RawContent, self.MMFoodLogKeywords): action.Type = ActionType.MMFoodLog start = msg.RawContent.find(self.MMFoodLogKeywords[0]) if start < 0: start = 0 detail = msg.RawContent[start+4:].strip() action.Detail = detail elif self.check_strList(msg.RawContent, self.SnacksKeywords): action.Type = ActionType.Snacks start = msg.RawContent.find(self.SnacksKeywords[0]) detail = msg.RawContent[start+2:].strip() action.Detail = detail elif self.check_strList(content, self.FeedKeywords): #feed action.Type = ActionType.Feed action.Status = Action.Active nums = re.findall(r"\d+",content) if len(nums) > 0: if self.check_strList(content, self.MinKeywords): action.Detail = "母乳:" + nums[0] + u"分钟" else: action.Detail = "奶瓶:" + nums[0] + "mL" if self.check_strList(content, self.PowderKeywords): action.Detail += " (奶粉)" elif self.check_strList(msg.RawContent, self.WeeklyReportsKeywords): action.Type = ActionType.WeeklyReports elif self.check_strList(msg.RawContent, self.RemoveKeywords): action.Type = ActionType.Remove elif self.check_strList(msg.RawContent, self.ReportsKeywords): #reports action.Type = ActionType.Reports action.Status = Action.Active elif self.check_strList(msg.RawContent, self.PoopKeywords): action.Type = ActionType.Poop elif self.check_strList(msg.RawContent, self.BathKeywords): action.Type = ActionType.Bath elif self.check_strList(msg.RawContent, self.FallSleepKeywords): lastAct = self.rlSQL.GetSleepStatus() if lastAct.Type == ActionType.WakeUp: action.Type = ActionType.FallSleep else: action.Type = ActionType.ErrStatus action.Detail = "重复的睡觉,上一次是:" action.Detail += lastAct.TimeStamp.strftime( "%H:%M") elif self.check_strList(msg.RawContent, self.WakeUpKeywords): lastAct = self.rlSQL.GetSleepStatus() if lastAct.Type == ActionType.FallSleep: action.Type = ActionType.WakeUp self.get_latest_sleep(action, num2d, ect) else: action.Type = ActionType.ErrStatus action.Detail = "重复的睡醒,上一次是:" action.Detail += lastAct.TimeStamp.strftime( "%H:%M") elif self.check_strList(msg.RawContent, self.ViewNotesKeywords): action.Type = ActionType.ViewNotes elif self.check_strList(msg.RawContent, self.DebugMsgKeywords): action.Type = ActionType.DebugMsg elif self.check_strList(msg.RawContent, self.FixInputKeywords): action.LoadFromString(msg.RawContent) elif self.check_strList(msg.RawContent, self.PillsKeywords): action.Type = ActionType.Pills start = content.index("药") detail = content[start+1:].strip() action.Detail = detail elif self.check_strList(msg.RawContent, self.ListImageKeywords): action.Type = ActionType.ListImage files = cn_utility.listimgfiles(config.ImageRoot, 7) action.ImageList = [] for f in files: action.ImageList.append((f[5:16], \ "http://stansunlog.eastasia.cloudapp.azure.com/robert_image?name="+f)) else: action.Type = ActionType.UnKnown try: int(msg.RawContent) msgs = self.rlSQL.GetMsgFromUser(msg.FromUser, 2) if self.check_strList(msgs[1].RawContent, self.RemoveKeywords): action.Type = ActionType.RemoveSpecific except ValueError: pass if action.FromUser not in self.users_can_write and action.Type not in \ {ActionType.Reports, ActionType.WeeklyReports, ActionType.ListImage}: action.Type = ActionType.NoPermission return action
def GenResponse(self, action): response = "抱歉没听懂." if action.Type == ActionType.Feed: response = "收到,萝卜在{1}吃了{0}".format(action.Detail, action.TimeStamp.strftime( "%H:%M")) elif action.Type == ActionType.Reports: response = "统计结果:" cur = datetime.datetime.utcnow() + datetime.timedelta(days=2) actions = self.rlSQL.GetActionReports(30) actions.sort(key=lambda a:a.TimeStamp) lastmilk = sleepstatus = Action() for a in actions: if a.Status == Action.Deleted: continue if a.Type == ActionType.FallSleep: sleepstatus = a continue elif a.Type == ActionType.WakeUp: sleepstatus = a elif a.Type == ActionType.Feed: lastmilk = a if a.Type not in self.actiontype_skip_log : if a.TimeStamp.day != cur.day: cur = a.TimeStamp response += "\n{0}日(第{1}天)记录:\n".format(cur.strftime("%m-%d"), \ config.get_days_to_birth(cur)) if len(response) > 250: response = response[-250:] response += (a.GenBrief() + "\n") tnow = cn_utility.GetNowForUTC8() if sleepstatus.Type == ActionType.FallSleep: #is sleeping response += (sleepstatus.GenBrief() + "\n") elif sleepstatus.Type == ActionType.WakeUp: delta_minutes = int((tnow - sleepstatus.TimeStamp).total_seconds()/60) if delta_minutes > 240: response += "\n醒了{0}小时{1}分钟了,该睡了".format(int(delta_minutes/60), delta_minutes%60) else: #don't find last status skip pass #disable milk alert for now #delta_minutes = int((tnow - lastmilk.TimeStamp).total_seconds()/60) #if delta_minutes > 240: #response += "\n上次喂奶是{0}小时{1}分钟前:{2}".format(int(delta_minutes/60), delta_minutes%60, lastmilk.GenBrief()) elif action.Type == ActionType.WeeklyReports: response = "统计结果: \n" cur = datetime.datetime.utcnow() + datetime.timedelta(days=2) actions = self.rlSQL.GetActionReports(300) milk = 0 breast = 0 breastNum = 0 poop = 0 sleep = 0 daysShown = 0 pillstaken = 0 comFoodCount = 0 snackCount = 0 notesPerDay = "" for a in actions: if a.Status == Action.Deleted: continue if a.TimeStamp.day != cur.day and (milk !=0 or breast !=0): #response += "{0}日:奶瓶{1}mL,母乳{2}次共{3}分钟,睡觉{5}小时{6}分钟,大便{4}次\n".format(\ #cur.strftime("%m-%d"), milk, breastNum, breast, poop, int(sleep/60), sleep%60) #no breast milk version response += "{0}日:奶瓶{1}mL,辅食{5}次,零食{6}次,睡觉{3}小时{4}分钟,大便{2}次\n".format(\ cur.strftime("%m-%d"), milk, poop, int(sleep/60), sleep%60, comFoodCount, snackCount) if len(notesPerDay) > 0: response += "今日备注: {0}\n".format(notesPerDay) if pillstaken > 0: response += "吃药{0}次\n".format(pillstaken) milk = 0 breast = 0 poop = 0 breastNum = 0 sleep = 0 pillstaken = 0 comFoodCount = 0 snackCount = 0 notesPerDay = "" daysShown += 1 if daysShown >= 8 : break cur = a.TimeStamp if a.Type == ActionType.Feed: nums = re.findall(r"\d+",a.Detail) if len(nums) > 0: d = int(nums[0]) if a.Detail.find("母乳") >= 0: breast += d breastNum += 1 elif a.Detail.find("奶瓶") >= 0: milk += d elif a.Type == ActionType.Poop: poop += 1 elif a.Type == ActionType.WakeUp: ect = extract_cn_time() sleep += ect.extract_time_delta(a.Detail) #print(cur, sleep, a.Detail) elif a.Type == ActionType.ComFood: comFoodCount += 1 elif a.Type == ActionType.Snacks: snackCount += 1 elif a.Type == ActionType.Pills: pillstaken += 1 elif a.Type == ActionType.Notes: #notesPerDay += "{0}日{1}\t".format(cur.strftime("%m-%d"),a.GenBrief()) notesPerDay += a.GenBrief() pass if (milk !=0 or breast !=0) and daysShown < 7: #response += "{0}日:奶瓶{1}mL,母乳{2}次共{3}分钟,睡觉{5}小时{6}分钟,大便{4}次\n".format(\ # cur.strftime("%m-%d"), milk, breastNum, breast, poop, int(sleep/60), sleep%60) #no breast version response += "{0}日:奶瓶{1}mL,辅食{5}次,零食{6}次,睡觉{3}小时{4}分钟,大便{2}次\n".format(\ cur.strftime("%m-%d"), milk, poop, int(sleep/60), sleep%60, comFoodCount, snackCount) if len(notesPerDay) > 0: response += "今日备注: {0}\n".format(notesPerDay) if pillstaken > 0: response += "吃药{0}次\n".format(pillstaken) elif action.Type == ActionType.Remove: response = "请输入要删除的项目序号\n" actions = self.rlSQL.GetActionReports(6) actions.sort(key=lambda a:a.TimeStamp) for a in actions: if a.Status == Action.Deleted: continue response += "序号:{0} 内容:{1},{2}\n".format(\ a.ActionID, self.user_mapping.get(a.FromUser, a.FromUser), a.GenBrief()) elif action.Type == ActionType.RemoveSpecific: self.rlSQL.DeleteAction(int(action.message.RawContent)) response ="已删除一条记录.\n" elif action.Type == ActionType.ListImage: return action.ImageList elif action.Type == ActionType.DebugMsg: msg_list = self.rlSQL.GetLastNumMsg(30) response = "List:\n" for m in msg_list: response +="[{0}] {1}:{2} \n".format(m.TimeStamp.strftime( "%H:%M"), self.user_mapping.get(m.FromUser, m.FromUser), m.RawContent) elif action.Type == ActionType.NoPermission: response = "抱歉您没有权限,可以尝试 '总结' 或 '一周总结' 查看萝卜成长状态。" elif action.Type == ActionType.ComFoodList: response = "吃饭记录:\n" foodList = self.rlSQL.GetActionList( ActionType.ComFood, 80) foodList.sort(key=lambda a:a.TimeStamp) cur = datetime.datetime.utcnow() + datetime.timedelta(days=2) for f in foodList: if f.TimeStamp.day != cur.day: #a new day response += "\n[{0}] {1} ".format(f.TimeStamp.strftime("%m-%d"), f.Detail) cur = f.TimeStamp else: response += f.Detail #f.GenBrief() elif action.Type == ActionType.ViewNotes: response = "备注记录:\n" noteList = self.rlSQL.GetActionList( ActionType.Notes, 5) noteList.sort(key=lambda a:a.TimeStamp) cur = datetime.datetime.utcnow() + datetime.timedelta(days=2) for f in noteList: if f.TimeStamp.day != cur.day: #a new day response += "\n[{0}] {1} ".format(f.TimeStamp.strftime("%m-%d"), f.Detail) cur = f.TimeStamp else: response += f.Detail #f.GenBrief() else: response = action.GenBrief() response += warning.GetWarnings(self.rlSQL) return response