|
| 1 | +# Very simple datapoint generator for bus example config |
| 2 | + |
| 3 | +import random |
| 4 | +from argparse import ArgumentParser |
| 5 | +from datetime import datetime, timedelta |
| 6 | +from sys import stderr |
| 7 | +from time import sleep |
| 8 | + |
| 9 | +import requests |
| 10 | + |
| 11 | + |
| 12 | +def random_initial_location(): |
| 13 | + latitude = random.uniform(39.0, 41.0) |
| 14 | + longitute = random.uniform(18.0, 22.0) |
| 15 | + |
| 16 | + return [round(latitude, 3), round(longitute, 3)] |
| 17 | + |
| 18 | + |
| 19 | +def do_random_location_increment(current_location): |
| 20 | + current_location[0] = round(current_location[0] + random.uniform(-1.0, 1.0), 3) |
| 21 | + current_location[1] = round(current_location[1] + random.uniform(-1.0, 1.0), 3) |
| 22 | + |
| 23 | + |
| 24 | +def t1(): |
| 25 | + return datetime.utcnow() |
| 26 | + |
| 27 | + |
| 28 | +def random_t2(): |
| 29 | + return datetime.utcnow() + timedelta(minutes=random.randint(5, 15)) |
| 30 | + |
| 31 | + |
| 32 | +def random_passenger_counts_3(): |
| 33 | + return [random.randint(0, 40), random.randint(0, 40), random.randint(0, 40)] |
| 34 | + |
| 35 | + |
| 36 | +bus_lines = { |
| 37 | + "10": {"label": "Special bus 10", "location": random_initial_location()}, |
| 38 | + "11": {"label": "Special bus 11", "location": random_initial_location()}, |
| 39 | + "12": {"label": "Bus 12", "location": random_initial_location()}, |
| 40 | + "19": {"label": "Bus 19 ", "location": random_initial_location()}, |
| 41 | + "48": {"label": "Bus 48", "location": random_initial_location()}, |
| 42 | + "93": {"label": "Bus 93", "location": random_initial_location()}, |
| 43 | + "E73": {"label": "Express bus 73", "location": random_initial_location()}, |
| 44 | + "N72": {"label": "Night bus 72", "location": random_initial_location()}, |
| 45 | + "S20": {"label": "School bus 20", "location": random_initial_location()}, |
| 46 | +} |
| 47 | + |
| 48 | + |
| 49 | +if __name__ == "__main__": |
| 50 | + # Parse arguments |
| 51 | + parser = ArgumentParser("Simple datapoint generator for DP3 testing.") |
| 52 | + parser.add_argument( |
| 53 | + "--endpoint-url", |
| 54 | + help="Base DP3 API endpoint. Default='http://127.0.0.1:5000'.", |
| 55 | + default="http://127.0.0.1:5000", |
| 56 | + type=str, |
| 57 | + ) |
| 58 | + parser.add_argument( |
| 59 | + "--interval", |
| 60 | + help="Interval of sending datapoints in minutes. Default: 1 minute.", |
| 61 | + default=1, |
| 62 | + type=int, |
| 63 | + ) |
| 64 | + args = parser.parse_args() |
| 65 | + |
| 66 | + # API endpoint |
| 67 | + datapoints_url = f"{args.endpoint_url}/datapoints" |
| 68 | + |
| 69 | + # Datapoints buffer |
| 70 | + dps = [] |
| 71 | + |
| 72 | + # One-time datapoints |
| 73 | + for n in bus_lines: |
| 74 | + bus_line = bus_lines[n] |
| 75 | + |
| 76 | + dps.append( |
| 77 | + { |
| 78 | + "type": "bus", |
| 79 | + "id": n, |
| 80 | + "attr": "label", |
| 81 | + "v": bus_line["label"], |
| 82 | + "t1": t1().isoformat(), |
| 83 | + "t2": random_t2().isoformat(), |
| 84 | + "src": "Manual", |
| 85 | + } |
| 86 | + ) |
| 87 | + |
| 88 | + # Realtime datapoints |
| 89 | + while True: |
| 90 | + for n in bus_lines: |
| 91 | + bus_line = bus_lines[n] |
| 92 | + |
| 93 | + dps.append( |
| 94 | + { |
| 95 | + "type": "bus", |
| 96 | + "id": n, |
| 97 | + "attr": "location", |
| 98 | + "v": bus_line["location"], |
| 99 | + "t1": t1().isoformat(), |
| 100 | + "t2": random_t2().isoformat(), |
| 101 | + "src": "Tracking SW", |
| 102 | + } |
| 103 | + ) |
| 104 | + do_random_location_increment(bus_line["location"]) |
| 105 | + |
| 106 | + dps.append( |
| 107 | + { |
| 108 | + "type": "bus", |
| 109 | + "id": n, |
| 110 | + "attr": "speed", |
| 111 | + "v": round(random.uniform(0.0, 100.0), 1), |
| 112 | + "t1": t1().isoformat(), |
| 113 | + "t2": random_t2().isoformat(), |
| 114 | + "src": "Tracking SW", |
| 115 | + } |
| 116 | + ) |
| 117 | + |
| 118 | + t1_local = t1() |
| 119 | + dps.append( |
| 120 | + { |
| 121 | + "type": "bus", |
| 122 | + "id": n, |
| 123 | + "attr": "passengers_in_out", |
| 124 | + "v": { |
| 125 | + "front_in": random_passenger_counts_3(), |
| 126 | + "front_out": random_passenger_counts_3(), |
| 127 | + "middle_in": random_passenger_counts_3(), |
| 128 | + "middle_out": random_passenger_counts_3(), |
| 129 | + "back_in": random_passenger_counts_3(), |
| 130 | + "back_out": random_passenger_counts_3(), |
| 131 | + }, |
| 132 | + "t1": t1_local.isoformat(), |
| 133 | + "t2": (t1_local + timedelta(minutes=30)).isoformat(), |
| 134 | + "src": "Bus counter", |
| 135 | + } |
| 136 | + ) |
| 137 | + |
| 138 | + # Send datapoints |
| 139 | + try: |
| 140 | + response = requests.post(url=datapoints_url, json=dps) |
| 141 | + |
| 142 | + if response.status_code == requests.codes.ok: |
| 143 | + print(f"Sent {len(dps)} datapoints of attribute(s)") |
| 144 | + else: |
| 145 | + response_content = response.content.decode("utf-8") |
| 146 | + print( |
| 147 | + f"Sending datapoints failed: {response.reason} {response_content}", |
| 148 | + file=stderr, |
| 149 | + ) |
| 150 | + print(f"Payload: {dps}", file=stderr) |
| 151 | + |
| 152 | + except requests.exceptions.ConnectionError: |
| 153 | + print("Sending datapoints failed: connection error", file=stderr) |
| 154 | + |
| 155 | + except KeyboardInterrupt: |
| 156 | + print("Received keyboard interrupt, exiting.", file=stderr) |
| 157 | + break |
| 158 | + |
| 159 | + # Clear buffer |
| 160 | + dps = [] |
| 161 | + |
| 162 | + # Wait for next cycle |
| 163 | + sleep(60 * args.interval) |
0 commit comments