-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
executable file
·136 lines (113 loc) · 4.23 KB
/
client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/bin/env python
# Copyright (c) 2024 FZI Forschungszentrum Informatik
# SPDX-License-Identifier: Apache-2.0
import argparse
import requests
import sys
import time
class MongeuClient:
"""Mongeu API client"""
def __init__(self, base_url: str):
self.base_url = base_url
def ping(self) -> bool:
"""Ping the mongeu API"""
return requests.get(self.base_url + "/v1/ping").ok
def health(self) -> dict:
r = requests.get(self.base_url + "/v1/health")
if not r.ok:
print(r, file=sys.stderr)
return None
return r.json()
def oneshot(self, duration_ms: int) -> dict:
"""Perform a oneshot measurement"""
if duration_ms is None:
params = None
else:
params = {'duration': duration_ms}
r = requests.get(self.base_url + "/v1/energy", params=params)
if not r.ok:
print(r, file=sys.stderr)
return None
return Measurement.from_json(r.json())
def new_campaign(self):
"""Create a new campaign"""
r = requests.post(self.base_url + "/v1/energy", allow_redirects=False)
if not r.is_redirect:
print(r, file=sys.stderr)
return None
location = r.headers['location']
if location.startswith('http'):
return Campaign(location)
return Campaign(self.base_url + location)
def new_campaign2(self):
"""Create a new campaign"""
r = requests.post(self.base_url + "/v1/energy", allow_redirects=True)
if not r.ok:
print(r, file=sys.stderr)
return None
return Campaign(r.url)
class Campaign:
"""A measurement campaign"""
def __init__(self, url):
self.url = url
def get(self):
"""Get a current measurement for this campaign"""
r = requests.get(self.url)
if not r.ok:
print(r, file=sys.stderr)
return None
return Measurement.from_json(r.json())
def __del__(self):
requests.delete(self.url)
class Measurement:
"""A single measurement"""
def __init__(self, duration: int, devices: list):
self.duration = duration
self.devices = devices
def from_json(data):
"""Create a measurement from a JSON representation"""
devices = list(map(lambda d: DeviceMeasurement(d['id'], d['energy']), data['devices']))
return Measurement(data['duration'], devices)
class DeviceMeasurement:
"""Measurement data for a specific device"""
def __init__(self, id: int, energy: int):
self.id = id
self.energy = energy
if __name__ == '__main__':
def print_measurement(measurement: Measurement):
line = f"{measurement.duration:>7}ms | "
line = line + ", ".join(map(lambda d: f"{d.id:>2}: {d.energy:>10}mJ", measurement.devices))
print(line)
parser = argparse.ArgumentParser(description='Mongeu API client demo')
parser.add_argument('url')
parser.add_argument('action', choices=['ping', 'health', 'oneshot', 'campaign'])
parser.add_argument('--campaign_method', choices=['1','2'], default='1')
parser.add_argument('-i', '--interval', type=int, default=500)
parser.add_argument('-c', '--count', type=int, default=4)
args = parser.parse_args()
client = MongeuClient(args.url)
if args.action == 'ping':
if not client.ping():
sys.exit("Could not ping API")
else:
print("Ping successfull")
elif args.action == 'health':
print(client.health())
elif args.action == 'oneshot':
measurement = client.oneshot(args.interval)
if measurement is None:
sys.exit("Could not issue oneshot measurement")
print_measurement(measurement)
elif args.action == 'campaign':
if args.campaign_method == 1:
campaign = client.new_campaign()
else:
campaign = client.new_campaign2()
if campaign is None:
sys.exit(f"Failed to create a new campaign using method {args.campaign_method}")
for _ in range(0,args.count):
time.sleep(args.interval/1000.0)
print_measurement(campaign.get())
del campaign
else:
sys.exit("Unknown action: " + args.action)