00001
00002
00003
00004
00005 import rospy
00006 from docomo_perception.srv import (Chat, ChatResponse)
00007
00008 import json
00009 import requests
00010 import yaml
00011 import threading
00012
00013 global APIHOST, APIKEY, NICKNAME, NICKNAME_Y, SEX, BLOODTYPE, BIRTHDATEY, BIRTHDATEM, BIRTHDATED, AGE, CONSTELLATIONS, PLACE
00014 settings_path = "/var/lib/robot/docomo_chat_settings.yaml"
00015
00016
00017 class DocomoChatNode(object):
00018 def __init__(self):
00019 self.srv = rospy.Service("chat", Chat, self.serviceCallback)
00020 self.lock = threading.Lock()
00021
00022 self.context = None
00023
00024 def serviceCallback(self, req):
00025 dic = {
00026 "utt": req.text,
00027 "nickname": NICKNAME,
00028 "nickname_y": NICKNAME_Y,
00029 "sex": SEX,
00030 "bloodtype": BLOODTYPE,
00031 "birthdateY": BIRTHDATEY,
00032 "birthdateM": BIRTHDATEM,
00033 "birthdateD": BIRTHDATED,
00034 "age": AGE,
00035 "constellations": CONSTELLATIONS,
00036 "place": PLACE,
00037 "mode": "dialog"
00038 }
00039 if self.context:
00040 dic['context'] = self.context
00041 rospy.loginfo("context: %s", self.context)
00042 sendData = json.dumps(dic)
00043 url = "%s?APIKEY=%s" % (APIHOST, APIKEY)
00044 self.lock.acquire()
00045 urlres = requests.post(url, data=sendData)
00046 self.lock.release()
00047 rospy.loginfo("sent request %s -> %d", req.text, urlres.status_code)
00048 rospy.loginfo("content: %s", urlres.content)
00049 if urlres.status_code is not requests.codes.ok:
00050 rospy.logerr("bad status code: %d", urlres.status_code)
00051 return ChatResponse()
00052 resdic = json.loads(urlres.content)
00053 res = ChatResponse()
00054 res.text = resdic['utt']
00055 self.context = resdic['context']
00056 rospy.loginfo("received %s %s", res.text, self.context)
00057 return res
00058
00059 def load_chat_settings():
00060 global APIHOST, APIKEY, NICKNAME, NICKNAME_Y, SEX, BLOODTYPE, BIRTHDATEY, BIRTHDATEM, BIRTHDATED, AGE, CONSTELLATIONS, PLACE
00061
00062 try:
00063 with open(settings_path) as f:
00064 key = yaml.load(f)
00065 APIHOST = key['APIHOST']
00066 APIKEY = key['APIKEY']
00067 NICKNAME = key['NICKNAME']
00068 NICKNAME_Y = key['NICKNAME_Y']
00069 SEX = key['SEX']
00070 BLOODTYPE = key['BLOODTYPE']
00071 BIRTHDATEY = key['BIRTHDATEY']
00072 BIRTHDATEM = key['BIRTHDATEM']
00073 BIRTHDATED = key['BIRTHDATED']
00074 AGE = key['AGE']
00075 CONSTELLATIONS = key['CONSTELLATIONS']
00076 PLACE = key['PLACE']
00077 rospy.loginfo("loaded settings")
00078 except IOError as e:
00079 rospy.logerr('"%s" not found : %s' % (settings_path, e))
00080 exit(-1)
00081 except Exception as e:
00082 rospy.logerr("failed to load settings: %s", e)
00083 rospy.logerr("check if exists valid setting file in %s", settings_path)
00084 exit(-1)
00085
00086
00087 if __name__ == '__main__':
00088 rospy.init_node("docomo_chat_node")
00089 n = DocomoChatNode()
00090 load_chat_settings()
00091 rospy.spin()