-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_acquisition.py
More file actions
202 lines (147 loc) · 7.18 KB
/
data_acquisition.py
File metadata and controls
202 lines (147 loc) · 7.18 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
from pymodbus.client import ModbusTcpClient
from datetime import datetime, date
import threading
import time
import os, sys
import zipfile
username = os.environ.get("USERNAME")
CONST_POLE = "spo"
CONST_FILETYPE = ".csv"
data_frequency = 60 # The number here defines the frequency of data collection
def make_filename(date: date) -> str:
username = os.getlogin()
folder_path = f"C:/Users/{username}/Documents/Icecube"
if not os.path.exists(folder_path):
os.mkdir(folder_path)
folder_name = f"{str(date.year)[-2:]}_{date.month}"
folder_path = f"C:/Users/{username}/Documents/Icecube/{folder_name}/"
#Check if the folder exist, if not, create it
if not os.path.exists(folder_path):
os.mkdir(folder_path)
filename = ""
filename += CONST_POLE
filename += str(date.today().year)[-2:]
filename += "_"
filename += str(date.timetuple().tm_yday).rjust(3, '0')
filename += CONST_FILETYPE
return os.path.join(folder_path, filename)
def record_data(filename: str):
while not flag.is_set():
try:
file_exists = os.path.exists(filename)
with open(filename, 'a') as f:
print("Opening file: ", filename)
print("Recording beginning...")
if not file_exists:
f.write("Date,Time,Device ID,Voltage,Current,Temp,Response\n")
prev_time = datetime.now()
prev_time = prev_time.replace(second=(prev_time.second - (prev_time.second % int((60/size)))))
while not flag.is_set():
for i in range(size):
print("Recording device %s... (press CTRL+C to save results and quit)" % str(i + 1))
while True:
if flag.is_set():
break
current_time = datetime.now()
if (current_time - prev_time).total_seconds() >= int(60/size) and current_time != prev_time:
prev_time = current_time
prev_time = prev_time.replace(second=(prev_time.second - (prev_time.second % int((60/size)))))
break
response = ser.read_input_registers(address=0x00, count=2, slave=int(unit_arr[i]))
try:
volt = response.registers[0] / 100
curr = response.registers[1] / 100
temp = ser.read_input_registers(address=0x00, count=1, slave=1).registers[0] / 10
f.write("{},{},{},"
"{},{},{},OK\n".format(datetime.today().date(),
prev_time.strftime(
'%H:%M:%S'),unit_arr[i],
volt, curr, temp))
except AttributeError as e:
print("fail")
print(e)
f.write("{},{},{},"
"{},{},{},NO RESPONSE\n".format(datetime.today().date(),
prev_time.strftime(
'%H:%M:%S'),unit_arr[i],
"NA", "NA", "NA"))
f.flush()
time.sleep(1)
f.close()
print("File closed")
except FileNotFoundError as e:
print("File not found: ", e)
break
except PermissionError as e:
print("Permission denied. Please ensure the file is closed before running program.")
quit()
break
ser.close()
print("Recording ending...")
ser = ModbusTcpClient('192.168.28.104', #Most likely needs to be manually entered for automatic running of program
port = 8899)
def detect_devices(client, start_id=2, end_id=8, address=0x00, count=1):
detected_units = []
for unit_id in range(start_id, end_id + 1):
try:
response = client.read_input_registers(address=address, count=count, slave=unit_id)
if response and hasattr(response, 'registers'):
# Device responded, add to list
detected_units.append(str(unit_id))
print(f"Device {unit_id} detected.")
else:
print(f"No response from device {unit_id}.")
except Exception as e:
print(f"Error communicating with device {unit_id}: {e}")
return detected_units
unit_arr = detect_devices(ser, 2, 8)
size = len(unit_arr)
if size == 0:
print("No devices detected. Exiting.")
sys.exit()
def zip_month_folder(year: int, month: int):
username = os.getlogin()
folder_name = f"{str(year)[-2:]}_{month}"
folder_path = f"C:/Users/{username}/Documents/Icecube/{folder_name}/"
zip_filename = os.path.join(folder_path, f"{folder_name}.zip")
if not os.path.exists(folder_path):
print(f"Folder {folder_path} does not exist, skipping zipping.")
return
print(f"Zipping folder {folder_path} into {zip_filename}...")
with zipfile.ZipFile(zip_filename, 'w', zipfile.ZIP_DEFLATED) as zipf:
for root, _, files in os.walk(folder_path):
for file in files:
if file.endswith('.csv'):
file_path = os.path.join(root, file)
arcname = os.path.relpath(file_path, folder_path)
zipf.write(file_path, arcname)
print(f"Zipping complete.")
if connect_str := ser.connect():
print("Connection found" if ser.is_socket_open() else "No connection found")
current_time = time.time()
date = datetime.now().date()
filename = make_filename(date)
flag = threading.Event()
recording_thread = threading.Thread(target=record_data, args=(filename,))
recording_thread.start()
try:
while True:
new_date = datetime.now().date()
if date != new_date:
flag.set()
time.sleep(2)
# Zip the previous month's folder before resetting (optional)
zip_month_folder(date.year, date.month)
# Reset
date = new_date
filename = make_filename(date)
flag.clear()
recording_thread = threading.Thread(target=record_data, args=(filename,))
recording_thread.start()
time.sleep(1) # Add a small sleep to avoid busy looping
except KeyboardInterrupt:
flag.set()
except KeyboardInterrupt as e:
flag.set()
recording_thread.join()
print("Program exiting...")