-
Notifications
You must be signed in to change notification settings - Fork 36
/
BS440test.py
121 lines (106 loc) · 4.26 KB
/
BS440test.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
#-----------------------------------------------------------------------------------------
# BS440 test utility BS440test.py
# About:
# This utility can be used to debug plugins without the need to take of your shoes and
# socks and step on the scale just to find that typo in the plugin.
#
# Data is taken from BS440test.ini and the array of results will be filled.
# Like the scale 30 values will be returned, from now and back, one day each. Values are slightly
# randomized with +/- 5% variation aroud the values supplied in BS440test.ini
# After this the data is presented to all the plugins found.
#
# Author: Keptenkurk
# Date: 22/2/17
#
#------------------------------------------------------------------------------------------
from __future__ import print_function
import logging
from ConfigParser import SafeConfigParser
import time
import subprocess
from struct import *
from binascii import hexlify
import os
import sys
import random
import math
def randomize_a_bit(value):
# deviate from value by -5% .. +5% rouded to 1 decimal
deviation = 9.5 + random.random()
return math.ceil(float(value) * deviation) / 10.0
'''
Main program loop
'''
config = SafeConfigParser()
config.read('BS440test.ini')
path = "plugins/"
plugins = {}
# set up logging
numeric_level = getattr(logging,
config.get('Program', 'loglevel').upper(),
None)
if not isinstance(numeric_level, int):
raise ValueError('Invalid log level: %s' % loglevel)
logging.basicConfig(level=numeric_level,
format='%(asctime)s %(levelname)-8s %(funcName)s %(message)s',
datefmt='%a, %d %b %Y %H:%M:%S',
filename=config.get('Program', 'logfile'),
filemode='w')
log = logging.getLogger(__name__)
# Load configured plugins
if config.has_option('Program', 'plugins'):
config_plugins = config.get('Program', 'plugins').split(',')
config_plugins = [plugin.strip(' ') for plugin in config_plugins]
log.info('Configured plugins: %s' % ', '.join(config_plugins))
sys.path.insert(0, path)
for plugin in config_plugins:
log.info('Loading plugin: %s' % plugin)
mod = __import__(plugin)
plugins[plugin] = mod.Plugin()
log.info('All plugins loaded.')
else:
log.info('No plugins configured.')
sys.path.pop(0)
log.info('BS440 test Started')
persondata = []
weightdata = []
bodydata = []
current_timestamp = int(time.time())
persondict = {}
persondict["valid"] = True
persondict["gender"] = config.get('Scaledata', 'gender')
persondict["person"] = config.get('Scaledata', 'person')
persondict["age"] = config.get('Scaledata', 'age')
persondict["size"] = float(config.get('Scaledata', 'size'))
persondict["activity"] = config.get('Scaledata', 'activity')
persondata.append(persondict)
for i in range (0,29):
weightdict = {}
weightdict["valid"] = True
# generate weighings per day
weightdict["timestamp"] = current_timestamp - i * 86400
weightdict["person"] = config.get('Scaledata', 'person')
weightdict["weight"] = randomize_a_bit(config.get('Scaledata', 'weight'))
weightdata.append(weightdict)
bodydict = {}
bodydict["valid"] = True
# generate bodydata per day
bodydict["timestamp"] = current_timestamp - i * 86400
bodydict["person"] = config.get('Scaledata', 'person')
bodydict["kcal"] = int(randomize_a_bit(config.get('Scaledata', 'kcal')))
bodydict["fat"] = randomize_a_bit(config.get('Scaledata', 'fat'))
bodydict["tbw"] = randomize_a_bit(config.get('Scaledata', 'tbw'))
bodydict["muscle"] = randomize_a_bit(config.get('Scaledata', 'muscle'))
bodydict["bone"] = randomize_a_bit(config.get('Scaledata', 'bone'))
bodydata.append(bodydict)
log.info('Done generating testdata')
# process data if all received well
if persondata and weightdata and bodydata:
# Sort scale output by timestamp to retrieve most recent three results
weightdatasorted = sorted(weightdata, key=lambda k: k['timestamp'], reverse=True)
bodydatasorted = sorted(bodydata, key=lambda k: k['timestamp'], reverse=True)
# Run all plugins found
for plugin in plugins.values():
plugin.execute(config, persondata, weightdatasorted, bodydatasorted)
else:
log.error('No valid testdata found. Unable to process')