-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_latest.py
executable file
·60 lines (55 loc) · 1.89 KB
/
get_latest.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
#!/usr/bin/env python
from feshiedb import FeshieDb
import logging
import protocol_buffers.readings_pb2 as readings
from feshie_reading import printReading
from optparse import OptionParser, OptionGroup
#from google.protobuf.message import DecodeError
from binascii import unhexlify as unhex
DEFAULT_LOG_LEVEL = logging.ERROR
DEFAULT_CONFIG = "db.ini"
def unpacklatest(DB_CONFIG, LOG_LEVEL):
logger = logging.getLogger("Latest unpacker")
logger.setLevel(LOG_LEVEL)
DB = FeshieDb(DB_CONFIG)
RAW = DB.get_latest_unprocessed()
SAMPLE = readings.Sample()
print "node: %s" % RAW.node
print "Recieved time: %s" % RAW.recieved_time
print "Processed: %s" % RAW.processed
print "Raw data:%s" % RAW.data
#try:
SAMPLE.ParseFromString(unhex(RAW.data[2:]))
#except DecodeError:
# print("Unable to decode protocol buffer")
# return
printReading(SAMPLE)
if __name__ == "__main__":
LOG_LEVEL = DEFAULT_LOG_LEVEL
PARSER = OptionParser()
GROUP = OptionGroup(
PARSER, "Verbosity Options",
"Options to change the level of output")
GROUP.add_option(
"-q", "--quiet", action="store_true",
dest="quiet", default=False,
help="Supress all but critical errors")
GROUP.add_option(
"-v", "--verbose", action="store_true",
dest="verbose", default=False,
help="Print all information available")
PARSER.add_option_group(GROUP)
PARSER.add_option(
"-c", "--config", action="store",
type="string", dest="config_file",
help="Config file containing database credentials")
(OPTIONS, ARGS) = PARSER.parse_args()
if OPTIONS.quiet:
LOG_LEVEL = logging.CRITICAL
elif OPTIONS.verbose:
LOG_LEVEL = logging.DEBUG
if OPTIONS.config_file is None:
CONFIG = DEFAULT_CONFIG
else:
CONFIG = OPTIONS.config_file
unpacklatest(CONFIG, LOG_LEVEL)