-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
97 lines (79 loc) · 3.46 KB
/
util.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
import logging
import json
import datetime
import csv
import RPi.GPIO as GPIO
import time
from os.path import exists
from random import shuffle
logger = logging.getLogger('main')
simulation_file_path = '/home/pi/shws/resources/simulations/'
#file_name = 'resources/simulations/TestSimulation.json'
def load_simulation(simulation_file_name):
simulation_json = open(simulation_file_name)
simulation = json.load(simulation_json)
return simulation
def setup_logger():
logFormatter = logging.Formatter("%(asctime)s [%(threadName)-12.12s] [%(levelname)-5.5s] %(message)s")
rootLogger = logging.getLogger('main')
rootLogger.setLevel(logging.INFO)
#fileHandler = logging.FileHandler("{0}/{1}.log".format('resources', 'log'))
fileHandler = logging.FileHandler("{0}/{1}.log".format('/home/pi/shws/resources', 'log'))
fileHandler.setFormatter(logFormatter)
rootLogger.addHandler(fileHandler)
consoleHandler = logging.StreamHandler()
consoleHandler.setFormatter(logFormatter)
rootLogger.addHandler(consoleHandler)
return rootLogger
def match_times_with_events(times, events):
shuffle(times)
merged_list = zip(times, events)
return tuple(merged_list)
def init_feedback_file(simulation_file_name):
logger = logging.getLogger('main')
#print("Feedback : Init feedback file")
simulation = load_simulation(simulation_file_name)
user_id = simulation['user_data']['id']
headers = ['user_id', 'event_id', 'answer', 'time_triggerd', 'time_acknowledge', 'missed', 'ack_medium']
file_name = '/home/pi/shws/resources/feedback_{}.csv'.format(user_id)
if exists(file_name):
for i in range(100):
file_name = '/home/pi/shws/resources/feedback_{}_{}.csv'.format(user_id, i+1)
if not exists(file_name):
break
logger.info("Init feedback file: " + file_name)
file = open('/home/pi/shws/resources/feedback_{}.csv'.format(user_id), 'w')
writer = csv.writer(file)
writer.writerow(headers)
file.close()
return file_name
def save_feedback(simulation_file_name, feedback_file_name, event, answer, missed, media):
logger = logging.getLogger('main')
logger.info("Store feedback for event {}".format(event['id']))
simulation = load_simulation(simulation_file_name)
user_id = simulation['user_data']['id']
# TODO Calculate and store time user needed for perception
feedback = [user_id, event['id'], answer, event['time_triggered'], event['time_acknowledge'], missed, media]
#print("Feedback : Store feedback for question {}".format(question_id))
file = open(feedback_file_name, 'a+', newline='')
writer = csv.writer(file)
writer.writerow(feedback)
file.close()
def is_time_between(begin_time, end_time, check_time=None):
# If check time is not given, default to current UTC time
begin_time = datetime.time(int(begin_time.split(':')[0]), int(begin_time.split(':')[1]))
end_time = datetime.time(int(end_time.split(':')[0]), int(end_time.split(':')[1]))
check_time = check_time or datetime.datetime.now().time()
if begin_time < end_time:
return check_time >= begin_time and check_time <= end_time
else: # crosses midnight
return check_time >= begin_time or check_time <= end_time
def connect_to_gsm_hat():
GPIO.setmode(GPIO.BOARD)
GPIO.setup(7, GPIO.OUT)
while True:
GPIO.output(7, GPIO.LOW)
time.sleep(4)
GPIO.output(7, GPIO.HIGH)
break
GPIO.cleanup()