-
Notifications
You must be signed in to change notification settings - Fork 0
/
trigger_standing_all_modes.py
133 lines (109 loc) · 6.18 KB
/
trigger_standing_all_modes.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
# This file is a lambda that checks for user activity in the past hour on the server and sends a
# STAND command to any desks dependent on their experimental condition
import requests
from datetime import datetime, timedelta
import json
## First set up variables that are used for all modes
time_threshold_second = 3000 # checks for the last 50 minutes at each XX:50
standing_threshold = 900 # above this counts as standing
now = (datetime.now())
time_threshold = (now - timedelta(seconds=time_threshold_second)).strftime("%Y-%m-%d %H:%M:%S")
get_users_url = 'https://desks.medien.ifi.lmu.de/users/condition'
get_heights_url = 'https://desks.medien.ifi.lmu.de/heights/id'
post_commands_url = 'https://desks.medien.ifi.lmu.de/commands/add'
get_commands_url = 'http://desks.medien.ifi.lmu.de/commands/id'
header = {'Content-Type': 'application/json; charset=utf-8'}
# This one only cares about users in condiiton A (apple watch style)
users_json_a = {'condition': 'A'}
# This one only cares about users in condiiton R (regular intervals)
users_json_r = {'condition': 'R'}
def send_post_command(command_json):
print('posting')
response = requests.post(post_commands_url, json=command_json, headers=header)
print(response.json())
def run():
############### Second do the A condition (apple watch style) ################
print('Running A condition')
response = requests.get(get_users_url, json=users_json_a, headers=header) # test version with no filter
users = response.json()
print('Users: ', users)
# If there are any users in this condition, keep going
if(len(users) > 0):
# loop through each user in the database
for user in users:
# Check if they are in the active week (timeline is 1 week manual, 1 week active, 1 week manual)
if len(user['startdate']) == 10:
started_time = datetime.strptime(user['startdate'], '%Y-%m-%d')
elif len(user['startdate']) == 29:
started_time = datetime.strptime(user['startdate'], '%a, %d %b %Y %H:%M:%S %Z')
else:
started_time = datetime.strptime(user['startdate'], '%Y-%m-%d %H:%M:%S')
experiment_day = (now - started_time).days
if (experiment_day >= 7):
# first check if the user already has commands (trying to handle jobs in multiple threads)
get_command_json = {"userid": user['userid']}
response = requests.get(get_commands_url, json=get_command_json, headers=header) # test version with no filter
commands = response.json()
if commands['command'] is None:
# Prep height json and command json for this user
heights_json = {"userid": user['userid'], "time": str(time_threshold)}
command_json = {"command": user['standkey'], "userid": user['userid']}
# get all heights for this user more recent than time_threshold
response = requests.get(get_heights_url, json=heights_json, headers=header)
heights = response.json()
# If there are heights, more checks required
if (len(heights) > 0):
stand_flag = 0
# check if the user has stood this hour
for height in heights:
if (height['height'] > standing_threshold):
stand_flag = 1
# if no flag, the user has not stood this hour
if stand_flag == 0:
send_post_command(command_json)
# if they already stood this hour, do nothing
else:
print('User already stood this hour')
# If there are no heights at all, send stand command
else:
print('no heights')
send_post_command(command_json)
# If they are not within the active week, do nothing
else:
print ("Not in active A condition, currently on day ", experiment_day)
else:
print('No users')
############ First, fetch all users who are in the R condition (regular interval mode) ##################
print('Running R condition!')
response = requests.get(get_users_url, json=users_json_r, headers=header) # test version with no filter
users = response.json()
# If there are any users in this condition, check if they are in the active portion
if(len(users) > 0):
# loop through each user in the database
for user in users:
# Check if they are in the active week (timeline is 1 week manual, 1 week active, 1 week manual)
if len(user['startdate']) == 10:
started_time = datetime.strptime(user['startdate'], '%Y-%m-%d')
elif len(user['startdate']) == 29:
started_time = datetime.strptime(user['startdate'], '%a, %d %b %Y %H:%M:%S %Z')
else:
started_time = datetime.strptime(user['startdate'], '%Y-%m-%d %H:%M:%S')
experiment_day = (now - started_time).days
if (experiment_day >=7):
# first check if the user already has commands (trying to handle jobs in multiple threads)
get_command_json = {"userid": user['userid']}
response = requests.get(get_commands_url, json=get_command_json, headers=header) # test version with no filter
commands = response.json()
if commands['command'] is None:
# Create the command post request
command_json = {"command": user['standkey'], "userid": user['userid']}
# Post standing command to database for each user
send_post_command(command_json)
# print('posting')
else:
print('already has a command')
# If they are not within the active week, do nothing
else:
print ("Not in active R condition, currently on day ", experiment_day)
else:
print('No users')