-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinitialize.py
95 lines (67 loc) · 3.5 KB
/
initialize.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
from subprocess import call
import json
import datetime
import time
initial_glucose = 65
rate = 0
duration = 30
min_ago_temp_delivered = 10
temp_has_initial_value = 1
glucose = []
#current_timestamp = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%dT%H:%M:%S-07:00') ## Original
current_timestamp = datetime.datetime.fromtimestamp(time.time()+4*60*60).strftime('%Y-%m-%dT%H:%M:%S-07:00') ## After time change
#current_timestamp = datetime.datetime.fromtimestamp(time.time()-(min_ago_temp_delivered*60)) # this is the time of 10 minutes (600 sec) ago
loaded_pump_history_to_dump = []
if temp_has_initial_value:
with open("monitor/pumphistory.json") as read_pump_history:
loaded_pump_history = json.load(read_pump_history) # read whole pump_history.json
pump_history_0 = loaded_pump_history[0].copy() #load first element
pump_history_1 = loaded_pump_history[1].copy() #load second element, fist and second are both for one temp basal
pump_history_0['duration (min)'] = duration
pump_history_1['rate'] = rate
pump_history_0['timestamp'] = current_timestamp
pump_history_1['timestamp'] = current_timestamp
loaded_pump_history_to_dump.insert(0,pump_history_1)
loaded_pump_history_to_dump.insert(0,pump_history_0)
with open("monitor/pumphistory.json", "w") as write_pump_history:
json.dump(loaded_pump_history_to_dump, write_pump_history, indent=4)
with open("monitor/temp_basal.json") as read_temp_basal:
loaded_temp_basal = json.load(read_temp_basal)
loaded_temp_basal['rate'] = rate
loaded_temp_basal['duration'] = (duration - min_ago_temp_delivered)
with open("monitor/temp_basal.json", "w") as write_temp_basal:
json.dump(loaded_temp_basal, write_temp_basal, indent=4)
else:
with open("monitor/temp_basal.json") as read_temp_basal:
loaded_temp_basal = json.load(read_temp_basal)
loaded_temp_basal['rate'] = 0
loaded_temp_basal['duration'] = 0
with open("monitor/temp_basal.json", "w") as write_temp_basal:
json.dump(loaded_temp_basal, write_temp_basal, indent=4)
with open("monitor/pumphistory.json") as read_pump_history:
loaded_pump_history = json.load(read_pump_history) # read whole pump_history.json
pump_history_0 = loaded_pump_history[0].copy() #load first element
pump_history_1 = loaded_pump_history[1].copy() #load second element, fist and second are both for one temp basal
pump_history_0['duration (min)'] = 0
pump_history_1['rate'] = 0
pump_history_0['timestamp'] = current_timestamp
pump_history_1['timestamp'] = current_timestamp
loaded_pump_history_to_dump.insert(0,pump_history_1)
loaded_pump_history_to_dump.insert(0,pump_history_0)
with open("monitor/pumphistory.json", "w") as write_pump_history:
json.dump(loaded_pump_history_to_dump, write_pump_history, indent=4)
with open("monitor/glucose.json") as read_glucose:
loaded_glucose = json.load(read_glucose)
#print("loaded_glucose: ",loaded_glucose)
loaded_glucose_first_element = loaded_glucose[0].copy()
loaded_glucose_first_element["glucose"] = initial_glucose
#print("loaded_glucose_first_element: ", loaded_glucose_first_element)
glucose.insert(0,loaded_glucose_first_element)
#print(glucose)
with open("monitor/glucose.json", "w") as write_glucose:
json.dump(glucose, write_glucose, indent=4)
#call(["cp", "initial_file/glucose.json", "monitor/"])
#call(["cp", "initial_file/pumphistory.json", "monitor/"])
to_glucosym = open('../glucosym/closed_loop_algorithm_samples/glucose_output_algo_bw.txt', 'w')
to_glucosym.write(str(initial_glucose))
#call(["python", "simplex_latest.py"])