def db_write(pub, state): """tms_db_writerにトピックを送る(結果、DBにデータを書き込む) args: pub: node.publisher型。tms_db_writerへのトピック送信用 state: int型。1なら測定中、0なら測定停止中 """ global node, roll, pitch, temp, rate, wave now_time = datetime.now().isoformat() send_data = {"temp": temp, "rate": rate, "wave": wave} db_msg = TmsdbStamped() db_msg.header.frame_id = "/world" # TODO: db_msgのヘッダーのstampがわからなかったので書いていない # clock = Clock() # db_msg.header.stamp = clock.now() tmp_data = Tmsdb() tmp_data.time = now_time tmp_data.name = "whs1_mybeat" tmp_data.id = 3021 tmp_data.place = 5001 tmp_data.sensor = 3021 tmp_data.state = state tmp_data.rr = roll tmp_data.rp = pitch tmp_data.ry = 0.0 tmp_data.note = json.dumps(send_data) db_msg.tmsdb = [tmp_data] pub.publish(db_msg)
def main(): print "Hello World" ###init mind wave mobile cmd_release = "sudo rfcomm release " + RFCOMM_NUM cmd_bind = "sudo rfcomm bind " + RFCOMM_NUM + " " + BT_ADDR cmd_chmod = "sudo chmod a+rw /dev/rfcomm" + RFCOMM_NUM print cmd_release + "\n", subprocess.check_output(cmd_release.split(" ")) print cmd_bind + "\n", subprocess.check_output(cmd_bind.split(" ")) print cmd_chmod + "\n", subprocess.check_output(cmd_chmod.split(" ")) dev = MindWaveMobile(DEV_PORT) ###init ROS rospy.init_node('tms_ss_vs_mindwave') db_pub = rospy.Publisher('tms_db_data', TmsdbStamped, queue_size=10) r = rospy.Rate(1) while not rospy.is_shutdown(): dev.update( ) # must update() many times in a loop because most of buffer data is stuff data if dev.is_updated: r.sleep() dev.is_updated = False ###make json text note_d = { "meditation": str(dev.meditation), "attention": str(dev.attention), "poor_signal": str(dev.poor_signal) } note_j = json.dumps(note_d) ###regist to DB msg = TmsdbStamped() db = Tmsdb() db.time = datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S.%f") db.name = "mindwavemobile" db.id = 3017 db.sensor = 3017 db.place = 1001 db.note = note_j msg.tmsdb.append(db) msg.header.stamp = rospy.get_rostime() + rospy.Duration( 9 * 60 * 60) db_pub.publish(msg) ###show messege print "Med:", print dev.meditation, print " Att:", print dev.attention, print " signal:", print dev.poor_signal
def main(): print "Hello World" ###init mind wave mobile cmd_release = "sudo rfcomm release "+RFCOMM_NUM cmd_bind = "sudo rfcomm bind "+RFCOMM_NUM+" "+BT_ADDR cmd_chmod = "sudo chmod a+rw /dev/rfcomm"+RFCOMM_NUM print cmd_release+"\n", subprocess.check_output(cmd_release.split(" ")) print cmd_bind+"\n", subprocess.check_output(cmd_bind.split(" ")) print cmd_chmod+"\n", subprocess.check_output(cmd_chmod.split(" ")) dev = MindWaveMobile(DEV_PORT) ###init ROS rospy.init_node('tms_ss_vs_mindwave') db_pub = rospy.Publisher('tms_db_data', TmsdbStamped, queue_size=10) r = rospy.Rate(1) while not rospy.is_shutdown(): dev.update() # must update() many times in a loop because most of buffer data is stuff data if dev.is_updated: r.sleep() dev.is_updated = False ###make json text note_d = {"meditation": str(dev.meditation), "attention": str(dev.attention), "poor_signal": str(dev.poor_signal)} note_j = json.dumps(note_d) ###regist to DB msg = TmsdbStamped() db = Tmsdb() db.time = datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S.%f") db.name = "mindwavemobile" db.id = 3017 db.sensor = 3017 db.place = 1001 db.note = note_j msg.tmsdb.append(db) msg.header.stamp = rospy.get_rostime()+rospy.Duration(9*60*60) db_pub.publish(msg) ###show messege print "Med:", print dev.meditation, print " Att:", print dev.attention, print " signal:", print dev.poor_signal
def call_dbwriter(self, msg): print 'SleepState -> %s' % (msg) note_d = {"sleepstate": str(msg)} note_j = json.dumps(note_d) msg = TmsdbStamped() db = Tmsdb() db.time = datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S.%f") db.name = "nursingcare_bed" db.type = "sensor" db.id = 3020 db.sensor = 3020 db.place = 5001 db.note = note_j msg.tmsdb.append(db) msg.header.stamp = rp.get_rostime() + rp.Duration(9 * 60 * 60) self.db_pub.publish(msg) return True
def dbReaderSrvCallback(self, req, ret): # self.get_logger().info("Received the service call!") # self.get_logger().info(req) temp_dbdata = Tmsdb() # db = TmsdbGetData() result = False mode = 0 temp_id = 0 temp_type = "" temp_name = "" temp_place = 0 sid = 100000 MODE_ALL = 0 MODE_NAME_IDTABLE = 1 MODE_NAME = 2 MODE_NAME_SENSOR = 3 MODE_ID_IDTABLE = 4 MODE_ID = 5 MODE_ID_SENSOR = 6 MODE_TYPE_IDTABLE = 7 MODE_TYPE = 8 MODE_TYPE_SENSOR = 9 MODE_PLACE_IDTABLE = 10 MODE_PLACE = 11 MODE_PLACE_TYPE = 12 MODE_HIERARCHY = 13 MODE_TAG_IDTABLE = 14 MODE_ID_STATE = 15 MODE_ID_SENSOR_STATE = 16 MODE_ERROR = 999 if req.tmsdb.tag != '': mode = MODE_TAG_IDTABLE elif (req.tmsdb.id == 0) and (req.tmsdb.type == '') and ( req.tmsdb.sensor == 0) and (req.tmsdb.place == 0) and (req.tmsdb.name == ''): mode = MODE_ALL elif (req.tmsdb.id == sid) and (req.tmsdb.name != ''): mode = MODE_NAME_IDTABLE elif (req.tmsdb.id == 0) and (req.tmsdb.name != '') and (req.tmsdb.id != sid) and ( req.tmsdb.type == '') and (req.tmsdb.sensor == 0) and (req.tmsdb.place == 0): mode = MODE_NAME elif (req.tmsdb.id == 0) and (req.tmsdb.name != '') and ( req.tmsdb.id != sid) and (req.tmsdb.type == '') and ( req.tmsdb.sensor != 0) and (req.tmsdb.place == 0): mode = MODE_NAME_SENSOR elif (req.tmsdb.id > sid) and (req.tmsdb.type == ''): req.tmsdb.id -= sid mode = MODE_ID_IDTABLE elif (req.tmsdb.id != 0) and (req.tmsdb.id != sid) and ( req.tmsdb.type == '') and (req.tmsdb.sensor == 0) and (req.tmsdb.place == 0): if (req.tmsdb.state == 1): mode = MODE_ID_STATE else: mode = MODE_ID elif (req.tmsdb.id != 0) and (req.tmsdb.id != sid) and ( req.tmsdb.type == '') and (req.tmsdb.sensor != 0) and (req.tmsdb.place == 0): if (req.tmsdb.state == 1): mode = MODE_ID_SENSOR_STATE else: mode = MODE_ID_SENSOR elif (req.tmsdb.id == sid) and (req.tmsdb.type != ''): mode = MODE_TYPE_IDTABLE elif (req.tmsdb.id == 0) and (req.tmsdb.type != '') and ( req.tmsdb.sensor == 0) and (req.tmsdb.place == 0): mode = MODE_TYPE elif (req.tmsdb.id == 0) and (req.tmsdb.type != '') and ( req.tmsdb.sensor != 0) and (req.tmsdb.place == 0): mode = MODE_TYPE_SENSOR elif (req.tmsdb.id == sid) and (req.tmsdb.type == '') and (req.tmsdb.place != 0): mode = MODE_PLACE_IDTABLE elif (req.tmsdb.id == 0) and (req.tmsdb.type == '') and (req.tmsdb.place != 0): mode = MODE_PLACE elif (req.tmsdb.id == 0) and (req.tmsdb.type != '') and (req.tmsdb.place != 0): mode = MODE_PLACE_TYPE elif (req.tmsdb.id != 0) and (req.tmsdb.type == '') and (req.tmsdb.place == sid): mode = MODE_HIERARCHY elif (req.tmsdb.id > 0) and ((req.tmsdb.id < 1000) or (req.tmsdb.id > 20002)): mode = MODE_ERROR else: mode = MODE_ERROR print("[mode] : " + str(mode)) if mode == MODE_ERROR: temp_dbdata.note = "Wrong request! Try to check the command!" # ret = TmsdbGetDataResponse() ret.tmsdb.append(temp_dbdata) return ret # Search the ID, type, etc infomation in ID table if (mode == MODE_NAME) or (mode == MODE_NAME_SENSOR): cursor = db.default.find({'name': req.tmsdb.name}) temp_type = cursor[0]['type'] temp_id = cursor[0]['id'] temp_place = cursor[0]['place'] # Search the type, name, etc infomation in ID table if (mode == MODE_ID) or (mode == MODE_ID_SENSOR) or ( mode == MODE_ID_STATE) or (mode == MODE_ID_SENSOR_STATE) or ( mode == MODE_HIERARCHY): cursor = db.default.find({'id': req.tmsdb.id}) temp_type = cursor[0]['type'] temp_name = cursor[0]['name'] temp_place = cursor[0]['place'] # Search only using the place tag if mode == MODE_PLACE: # ret = TmsdbGetDataResponse() cursor = db.now.find({'place': req.tmsdb.place}) if cursor.count() == 0: temp_dbdata.note = "Wrong request! Try to check the place tag!" ret.tmsdb.append(temp_dbdata) return ret else: for doc in cursor: del doc['_id'] temp_dbdata = db_util.document_to_msg(doc, Tmsdb) ret.tmsdb.append(temp_dbdata) return ret if mode == MODE_HIERARCHY: loop_end_tag = False temp_place = req.tmsdb.id while temp_place != 0: cursor = db.now.find({'id': temp_place}).sort({'time': -1}) if cursor.count() == 0: temp_dbdata.note = "Wrong request! Try to check the target ID, place info!" ret.tmsdb.append(temp_dbdata) return ret else: for doc in cursor: del doc['_id'] temp_dbdata = db_util.document_to_msg(doc, Tmsdb) ret.tmsdb.append(temp_dbdata) if loop_end_tag: break temp_id = temp_dbdata.place cursor = db.default.find({'id': temp_id}) temp_type = cursor[0]['type'] temp_name = cursor[0]['name'] temp_place = cursor[0]['place'] if temp_id == temp_place: loop_end_tag = True else: loop_end_tag = False temp_place = temp_id return ret if mode == MODE_ALL: cursor = db.default.find() elif mode == MODE_TAG_IDTABLE: cursor = db.default.find({'tag': {'$regex': req.tmsdb.tag}}) elif mode == MODE_NAME_IDTABLE: cursor = db.default.find({'name': req.tmsdb.name}) elif mode == MODE_NAME: cursor = db.now.find({'name': req.tmsdb.name}) elif mode == MODE_NAME_SENSOR: cursor = db.now.find({ 'name': req.tmsdb.name, 'sensor': req.tmsdb.sensor }) elif mode == MODE_ID_IDTABLE: cursor = db.default.find({'id': req.tmsdb.id}) elif mode == MODE_ID: cursor = db.now.find({'id': req.tmsdb.id}) elif mode == MODE_ID_STATE: cursor = db.now.find({'id': req.tmsdb.id, 'state': 1}) elif mode == MODE_ID_SENSOR: cursor = db.now.find({ 'id': req.tmsdb.id, 'sensor': req.tmsdb.sensor }) elif mode == MODE_ID_SENSOR_STATE: cursor = db.now.find({ 'id': req.tmsdb.id, 'sensor': req.tmsdb.sensor, 'state': 1 }) elif mode == MODE_TYPE_IDTABLE: cursor = db.default.find({'type': req.tmsdb.type}) elif mode == MODE_TYPE: cursor = db.now.find({'type': req.tmsdb.type}) elif mode == MODE_TYPE_SENSOR: cursor = db.now.find({'sensor': req.tmsdb.sensor}) elif mode == MODE_PLACE_IDTABLE: cursor = db.default.find({'place': req.tmsdb.place}) elif mode == MODE_PLACE_TYPE: cursor = db.now.find({'place': req.tmsdb.place}) # ret = TmsdbGetDataResponse() if cursor.count() == 0: temp_dbdata.note = "Wrong request! Try to check the command" ret.tmsdb.append(temp_dbdata) return ret else: for doc in cursor: del doc['_id'] temp_dbdata = db_util.document_to_msg(doc, Tmsdb) ret.tmsdb.append(temp_dbdata) return ret
r = rospy.Rate(1) while not rospy.is_shutdown(): r.sleep() # ##get heartrate value dev.write("G5\r") # get heartrate buffer[0~4] time.sleep(0.5) ret = dev.read(dev.inWaiting()) rate = ret.split(" ")[2] print "heartrate:" + rate + " raw:" + ret if int(rate) == 0: print " failed to get heartrate value" continue # ##make json text note_d = {"heartrate": rate} # print json.dumps(note_d, indent=4) note_j = json.dumps(note_d) # ##regist to DB msg = TmsdbStamped() db = Tmsdb() db.time = datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S.%f") db.name = "heartrate_sensor" db.id = 3018 db.sensor = 3018 db.place = 1001 db.note = note_j msg.tmsdb.append(db) msg.header.stamp = rospy.get_rostime() + rospy.Duration(9 * 60 * 60) db_pub.publish(msg)
def dbReaderSrvCallback(self, req): rospy.loginfo("Received the service call!") rospy.loginfo(req) temp_dbdata = Tmsdb() TmsdbGetData() result = False mode = 0; temp_id = 0; temp_type = ""; temp_name = ""; temp_place = 0; sid = 100000 MODE_ALL = 0 MODE_NAME_IDTABLE = 1 MODE_NAME = 2 MODE_NAME_SENSOR = 3 MODE_ID_IDTABLE = 4 MODE_ID = 5 MODE_ID_SENSOR = 6 MODE_TYPE_IDTABLE = 7 MODE_TYPE = 8 MODE_TYPE_SENSOR = 9 MODE_PLACE_IDTABLE = 10 MODE_PLACE = 11 MODE_PLACE_TYPE = 12 MODE_HIERARCHY = 13 MODE_TAG_IDTABLE = 14 MODE_ERROR = 999 if req.tmsdb.tag != '': mode = MODE_TAG_IDTABLE elif (req.tmsdb.id == 0) and (req.tmsdb.type == '') and (req.tmsdb.sensor == 0) and (req.tmsdb.place == 0) and (req.tmsdb.name == ''): mode = MODE_ALL elif (req.tmsdb.id == sid) and (req.tmsdb.name != ''): mode = MODE_NAME_IDTABLE elif (req.tmsdb.id == 0) and (req.tmsdb.name != '') and (req.tmsdb.id != sid) and (req.tmsdb.type == '') and (req.tmsdb.sensor == 0) and (req.tmsdb.place == 0): mode = MODE_NAME elif (req.tmsdb.id == 0) and (req.tmsdb.name != '') and (req.tmsdb.id != sid) and (req.tmsdb.type == '') and (req.tmsdb.sensor != 0) and (req.tmsdb.place == 0): mode = MODE_NAME_SENSOR elif (req.tmsdb.id > sid) and (req.tmsdb.type == ''): req.tmsdb.id -= sid mode = MODE_ID_IDTABLE elif (req.tmsdb.id != 0) and (req.tmsdb.id != sid) and (req.tmsdb.type == '') and (req.tmsdb.sensor == 0) and (req.tmsdb.place == 0): mode = MODE_ID elif (req.tmsdb.id != 0) and (req.tmsdb.id != sid) and (req.tmsdb.type == '') and (req.tmsdb.sensor != 0) and (req.tmsdb.place == 0): mode = MODE_ID_SENSOR elif (req.tmsdb.id == sid) and (req.tmsdb.type != ''): mode = MODE_TYPE_IDTABLE elif (req.tmsdb.id == 0) and (req.tmsdb.type != '') and (req.tmsdb.sensor == 0) and (req.tmsdb.place ==0): mode = MODE_TYPE elif (req.tmsdb.id == 0) and (req.tmsdb.type != '') and (req.tmsdb.sensor != 0) and (req.tmsdb.place ==0): mode = MODE_TYPE_SENSOR elif (req.tmsdb.id == sid) and (req.tmsdb.type == '') and (req.tmsdb.place !=0): mode = MODE_PLACE_IDTABLE elif (req.tmsdb.id == 0) and (req.tmsdb.type == '') and (req.tmsdb.place !=0): mode = MODE_PLACE elif (req.tmsdb.id == 0) and (req.tmsdb.type != '') and (req.tmsdb.place !=0): mode = MODE_PLACE_TYPE elif (req.tmsdb.id != 0) and (req.tmsdb.type == '') and (req.tmsdb.place == sid): mode = MODE_HIERARCHY elif (req.tmsdb.id > 0) and ((req.tmsdb.id < 1000) or (req.tmsdb.id > 20002)): mode = MODE_ERROR else: mode = MODE_ERROR if mode == MODE_ERROR: temp_dbdata.note = "Wrong request! Try to check the command!"; ret = TmsdbGetDataResponse() ret.tmsdb.append(temp_dbdata) return ret # Search the ID, type, etc infomation in ID table if (mode == MODE_NAME) or (mode == MODE_NAME_SENSOR): cursor = db.default.find({'name':req.tmsdb.name}) temp_type = cursor[0]['type']; temp_id = cursor[0]['id']; temp_place = cursor[0]['place']; # Search the type, name, etc infomation in ID table if(mode == MODE_ID) or (mode == MODE_ID_SENSOR) or (mode == MODE_HIERARCHY): cursor = db.default.find({'id':req.tmsdb.id}) temp_type = cursor[0]['type']; temp_name = cursor[0]['name']; temp_place = cursor[0]['place']; # Search only using the place tag if mode == MODE_PLACE : ret = TmsdbGetDataResponse() cursor = db.now.find({'place':req.tmsdb.place}) if cursor.count() == 0: temp_dbdata.note = "Wrong request! Try to check the place tag!"; ret.tmsdb.append(temp_dbdata) return ret else: for doc in cursor: del doc['_id'] temp_dbdata = db_util.document_to_msg(doc, Tmsdb) ret.tmsdb.append(temp_dbdata) return ret if mode == MODE_HIERARCHY: loop_end_tag = False; temp_place = req.tmsdb.id; while temp_place != 0: cursor = db.now.find({'id':temp_place}).sort({'time':-1}) if cursor.count() == 0: temp_dbdata.note = "Wrong request! Try to check the target ID, place info!"; ret.tmsdb.append(temp_dbdata) return ret else: for doc in cursor: del doc['_id'] temp_dbdata = db_util.document_to_msg(doc, Tmsdb) ret.tmsdb.append(temp_dbdata) if loop_end_tag: break temp_id = temp_dbdata.place; cursor = db.default.find({'id':temp_id}) temp_type = cursor[0]['type']; temp_name = cursor[0]['name']; temp_place = cursor[0]['place']; if temp_id == temp_place: loop_end_tag = True; else: loop_end_tag = False; temp_place = temp_id; return ret if mode == MODE_ALL: cursor = db.default.find() elif mode == MODE_TAG_IDTABLE: cursor = db.default.find({'tag':{'$regex': req.tmsdb.tag}}) elif mode == MODE_NAME_IDTABLE: cursor = db.default.find({'name':req.tmsdb.name}) elif mode == MODE_NAME: cursor = db.now.find({'name':req.tmsdb.name}) elif mode == MODE_NAME_SENSOR: cursor = db.now.find({'name':req.tmsdb.name, 'sensor':req.tmsdb.sensor}) elif mode == MODE_ID_IDTABLE: cursor = db.default.find({'id':req.tmsdb.id}) elif mode == MODE_ID: cursor = db.now.find({'id':req.tmsdb.id}) elif mode == MODE_ID_SENSOR: cursor = db.now.find({'id':req.tmsdb.id, 'sensor':req.tmsdb.sensor}) elif mode == MODE_TYPE_IDTABLE: cursor = db.default.find({'type':req.tmsdb.type}) elif mode == MODE_TYPE: cursor = db.now.find({'type':req.tmsdb.type}) elif mode == MODE_TYPE_SENSOR: cursor = db.now.find({'sensor':req.tmsdb.sensor}) elif mode == MODE_PLACE_IDTABLE: cursor = db.default.find({'place':req.tmsdb.place}) elif mode == MODE_PLACE_TYPE: cursor = db.now.find({'place':req.tmsdb.place}) ret = TmsdbGetDataResponse() if cursor.count() == 0: temp_dbdata.note = "Wrong request! Try to check the command"; ret.tmsdb.append(temp_dbdata) return ret else: for doc in cursor: del doc['_id'] temp_dbdata = db_util.document_to_msg(doc, Tmsdb) ret.tmsdb.append(temp_dbdata) return ret
r = rospy.Rate(1) while not rospy.is_shutdown(): r.sleep() # ##get heartrate value dev.write("G5\r") # get heartrate buffer[0~4] time.sleep(0.5) ret = dev.read(dev.inWaiting()) rate = ret.split(" ")[2] print "heartrate:"+rate+" raw:"+ret if int(rate) == 0: print " failed to get heartrate value" continue # ##make json text note_d = {"heartrate": rate} # print json.dumps(note_d, indent=4) note_j = json.dumps(note_d) # ##regist to DB msg = TmsdbStamped() db = Tmsdb() db.time = datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S.%f") db.name = "heartrate_sensor" db.id = 3018 db.sensor = 3018 db.place = 1001 db.note = note_j msg.tmsdb.append(db) msg.header.stamp = rospy.get_rostime()+rospy.Duration(9*60*60) db_pub.publish(msg)