Skip to content

Commit 525e282

Browse files
author
gruenwaldi
authored
Merge pull request #14 from ubirch/UNG-2711
UNG 2711
2 parents f1d12dc + 725ec4b commit 525e282

16 files changed

+2642
-163
lines changed

examples/EXAMPLES.md

Lines changed: 455 additions & 0 deletions
Large diffs are not rendered by default.

examples/data-sender.py

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
import sys
2+
import logging
3+
import argparse
4+
import msgpack
5+
import requests
6+
import binascii
7+
import uuid
8+
9+
import ubirch
10+
11+
12+
DEFAULT_ENV = "dev"
13+
14+
15+
logging.basicConfig(format='%(asctime)s %(name)20.20s %(funcName)20.20s() %(levelname)-8.8s %(message)s', level=logging.INFO)
16+
logger = logging.getLogger()
17+
18+
19+
class Main:
20+
def __init__(self):
21+
self.argparser : argparse.ArgumentParser = None
22+
self.args : argparse.Namespace = None
23+
24+
self.uuid_str : str = None
25+
self.uuid : uuid.UUID = None
26+
self.auth : str = None
27+
self.env : str = None
28+
self.input : str = None
29+
30+
self.api : ubirch.API = None
31+
32+
# initialize the argument parser
33+
self.setup_argparse()
34+
35+
return
36+
37+
def setup_argparse(self):
38+
self.argparser = argparse.ArgumentParser(
39+
description="Send some data to the uBirch Simple Data Service",
40+
epilog="Note that the input data should follow this pattern: "
41+
"{\"timestamp\": TIMESTAMP, \"uuid\": \"UUID\", \"msg_type\": 0, \"data\": DATA, \"hash\": \"UPP_HASH\"}. "
42+
"For more information take a look at the EXAMPLES.md file."
43+
)
44+
45+
self.argparser.add_argument("uuid", metavar="UUID", type=str,
46+
help="UUID to work with; e.g.: 56bd9b85-6c6e-4a24-bf71-f2ac2de10183"
47+
)
48+
self.argparser.add_argument("auth", metavar="AUTH", type=str,
49+
help="uBirch device authentication token, e.g.: 12345678-1234-1234-1234-123456789abc (this is NOT the UUID)"
50+
)
51+
self.argparser.add_argument("--env", "-e", metavar="ENV", type=str, default=DEFAULT_ENV,
52+
help="environment to operate in; dev, demo or prod (default: %s)" % DEFAULT_ENV
53+
)
54+
self.argparser.add_argument("input", metavar="INPUT", type=str,
55+
help="data to be sent to the simple data service"
56+
)
57+
58+
return
59+
60+
def process_args(self) -> bool:
61+
# parse cli arguments (exists on err)
62+
self.args = self.argparser.parse_args()
63+
64+
# get all needed args
65+
self.uuid_str = self.args.uuid
66+
self.auth = self.args.auth
67+
self.env = self.args.env
68+
self.input = self.args.input
69+
70+
# process the uuid
71+
try:
72+
self.uuid = uuid.UUID(hex=self.uuid_str)
73+
except Exception as e:
74+
logger.error("Invalid UUID: \"%s\"" % self.uuid_str)
75+
logger.exception(e)
76+
77+
return False
78+
79+
# validate env
80+
if self.env.lower() not in ["dev", "demo", "prod"]:
81+
logger.error("Invalid value for --env: \"%s\"!" % self.env)
82+
83+
return False
84+
85+
return True
86+
87+
def init_api(self) -> bool:
88+
try:
89+
# initialize the uBirch api
90+
self.api = ubirch.API(env=self.env, debug=True)
91+
self.api.set_authentication(self.uuid, self.auth)
92+
except Exception as e:
93+
logger.exception(e)
94+
95+
return False
96+
97+
return True
98+
99+
def send_data(self) -> bool:
100+
try:
101+
r = self.api.send_data(self.uuid, self.input.encode())
102+
103+
# check the response
104+
if r.status_code == 200:
105+
logger.info("Successfully sent all data to the Simple Data Service! (%d)" % r.status_code)
106+
else:
107+
logger.error("Failed to send data to the Simple Data Service! (%d)" % r.status_code)
108+
logger.error(r.content)
109+
except Exception as e:
110+
logger.exception(e)
111+
112+
return False
113+
114+
return True
115+
116+
def run(self):
117+
# process all args
118+
if self.process_args() != True:
119+
logger.error("Errors occured during argument processing - exiting!\n")
120+
121+
self.argparser.print_usage()
122+
123+
return 1
124+
125+
# initialize the api
126+
if self.init_api() != True:
127+
logger.error("Errors occured while initializing the uBirch API - exiting!\n")
128+
129+
self.argparser.print_usage()
130+
131+
return 1
132+
133+
# send data
134+
if self.send_data() != True:
135+
logger.error("Errors occured while sending data to the simple data service - exiting!\n")
136+
137+
self.argparser.print_usage()
138+
139+
return 1
140+
141+
return 0
142+
143+
144+
if __name__ == "__main__":
145+
sys.exit(Main().run())

0 commit comments

Comments
 (0)