network_states_logger.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 import influxdb
4 import rospy
5 
6 from influxdb_store.utils import timestamp_to_influxdb_time
7 
8 from std_msgs.msg import Float32
9 
10 
11 class NetworkStatesLogger(object):
12  def __init__(self):
13  host = rospy.get_param('~host', 'localhost')
14  port = rospy.get_param('~port', 8086)
15  database = rospy.get_param('~database', 'test')
16  self.client = influxdb.InfluxDBClient(
17  host=host, port=port, database=database)
18  self.client.create_database(database)
19  self.sub_transmit = rospy.Subscriber(
20  '~input/transmit', Float32, self._transmit_cb, queue_size=10)
21  self.sub_receive = rospy.Subscriber(
22  '~input/receive', Float32, self._receive_cb, queue_size=10)
23 
24  def _transmit_cb(self, msg):
25  transmit_time = timestamp_to_influxdb_time(rospy.Time.now())
26  transmit_bps = msg.data
27  query = [{
28  "measurement": "network_states",
29  "tags": {
30  "type": "transmit"
31  },
32  "time": transmit_time,
33  "fields": {
34  "bps": transmit_bps,
35  }
36  }]
37  try:
38  self.client.write_points(query, time_precision='ms')
39  except influxdb.exceptions.InfluxDBServerError as e:
40  rospy.logerr("InfluxDB error: {}".format(e))
41 
42  def _receive_cb(self, msg):
43  receive_time = timestamp_to_influxdb_time(rospy.Time.now())
44  receive_bps = msg.data
45  query = [{
46  "measurement": "network_states",
47  "tags": {
48  "type": "receive"
49  },
50  "time": receive_time,
51  "fields": {
52  "bps": receive_bps,
53  }
54  }]
55  try:
56  self.client.write_points(query, time_precision='ms')
57  except influxdb.exceptions.InfluxDBServerError as e:
58  rospy.logerr("InfluxDB error: {}".format(e))
59 
60 
61 if __name__ == '__main__':
62  rospy.init_node('network_states_logger')
64  rospy.spin()
def timestamp_to_influxdb_time(timestamp)
Definition: utils.py:5


influxdb_store
Author(s): Shingo Kitagawa
autogenerated on Sat Jun 24 2023 02:40:23