8 from sesame_ros.srv
import Command
9 from sesame_ros.srv
import CommandResponse
10 from sesame_ros.srv
import Status
11 from sesame_ros.srv
import StatusResponse
17 auth_token = rospy.get_param(
'~auth_token')
18 if osp.isfile(osp.expanduser(auth_token)):
19 with open(osp.expanduser(auth_token),
'r') as f: 23 self.
device_id = rospy.get_param(
'~device_id',
None)
24 self.
nickname = rospy.get_param(
'~nickname',
None)
35 'https://api.candyhouse.co/public/sesames',
37 if ret.status_code == 200:
42 for sesame_
in sesames:
43 if self.
device_id == sesame_[
'device_id']:
47 if not hasattr(self,
'sesame'):
48 for sesame_
in sesames:
49 if self.
nickname == sesame_[
'nickname']
and \
50 not hasattr(self,
'sesame'):
52 elif (self.
nickname == sesame_[
'nickname']
and 53 hasattr(self,
'sesame')):
55 'Multiple Sesames found for nickname [{}]. ' 56 'The first found Sesame will be used.'.
59 if not hasattr(self,
'sesame'):
61 'Neither [~device_id] nor [~nickname] matched. ' 62 'The first found Sesame will be used.')
66 'Neither [~device_id] nor [~nickname] is specified. ' 67 'The first found Sesame will be used.')
70 rospy.logerr(
'[{}] {}'.format(ret.status_code, ret.text))
71 rospy.logerr(
'No Sesames found.')
72 self.
sesame = {
'nickname':
None,
'serial':
None,
'device_id':
None}
78 if not hasattr(self,
'sesame'):
80 if self.
sesame[
'device_id']
is not None:
82 'https://api.candyhouse.co/public/sesame/{}'.
83 format(str(self.
sesame[
'device_id'])),
85 if ret.status_code == 200:
86 battery = int(ret.json()[
'battery'])
87 locked = bool(ret.json()[
'locked'])
88 responsive = bool(ret.json()[
'responsive'])
90 rospy.logerr(
'[{}] {}'.format(ret.status_code, ret.text))
92 self.
sesame[
'device_id'], battery, locked, responsive
98 if not hasattr(self,
'sesame'):
101 'https://api.candyhouse.co/public/sesame/{}'.
102 format(str(self.
sesame[
'device_id'])),
104 json={
'command': command})
105 if ret.status_code == 200:
106 task_id = ret.json()[
'task_id']
109 rospy.logerr(
'[{}] {}'.format(ret.status_code, ret.text))
117 'https://api.candyhouse.co/public/action-result?task_id={}'.
120 if ret.status_code == 200:
121 status = ret.json()[
'status']
122 if status ==
'terminated':
123 successful = bool(ret.json()[
'successful'])
124 error = str(ret.json().get(
'error'))
126 rospy.logerr(
'[{}] {}'.format(ret.status_code, ret.text))
127 return status, successful, error
130 time_limit = time.time() + timeout
134 while status !=
'terminated':
135 if time.time() > time_limit:
136 rospy.logerr(
'Operation Timeout. command: {}'.format(command))
142 rospy.logerr(
'Task status is None.')
147 return status, successful, error
150 return CommandResponse(
154 return CommandResponse(
158 return CommandResponse(
162 if __name__ ==
'__main__':
163 rospy.init_node(
'sesame_server')
def _get_sesame_status(self)
def force_sync(self, req)
def _get_task_status(self, task_id)
def get_sesame_status(self, req)
def _post_command(self, command)
def _post_command_and_wait(self, command, timeout)