Skip to content

Commit 3f55073

Browse files
committed
send process deletes old empty files & logs error
1 parent 933488e commit 3f55073

File tree

3 files changed

+104
-48
lines changed

3 files changed

+104
-48
lines changed

lib/functions_send.py

+75-44
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ def send_server():
4646
led_pins = [pm.red] # Define GPIO pins for each LED
4747
folder_path = pm.predictions_folder_path
4848
status_every = pm.status_every
49+
errors_path = pm.errors_path
4950

5051
# Configure LEDs
5152
GPIO.setmode(GPIO.BCM) # Set up GPIO mode
@@ -56,7 +57,7 @@ def send_server():
5657
GPIO.setup(pin, GPIO.OUT)
5758

5859
#### WATCHDOG code ###
59-
watchdog_pin=pm.watchdog
60+
watchdog_pin = pm.watchdog
6061
GPIO.setup(watchdog_pin, GPIO.OUT)
6162
####################
6263

@@ -72,65 +73,95 @@ def send_server():
7273
if len(files) != 0:
7374
# There are files!
7475
files.sort()
75-
for single_file in files:
76-
print("file ", single_file)
77-
# Check if file is not empty
78-
if os.path.getsize(single_file) > 0:
79-
# File has content
80-
# Read and send content
81-
with open(single_file, "r") as file:
82-
content = json.load(file)
83-
84-
# Sensor status
85-
print(counter_status)
86-
if counter_status == 0:
87-
sensor_info = gather_raspberry_pi_info()
88-
content["sensor_info"] = sensor_info
89-
90-
response = client.post_sensor_data(
91-
data=content,
92-
sensor_timestamp=content["datetime"],
93-
save_to_disk=False,
94-
)
76+
most_recent = files[-1]
77+
# Check if most recent file is not empty
78+
if os.path.getsize(most_recent) > 0:
79+
for single_file in files:
80+
print("file ", single_file)
81+
# Check if file is not empty
82+
if os.path.getsize(single_file) > 0:
83+
# File has content
84+
# Read and send content
85+
with open(single_file, "r") as file:
86+
content = json.load(file)
87+
88+
# Sensor status
89+
print(counter_status)
90+
if counter_status == 0:
91+
sensor_info = gather_raspberry_pi_info()
92+
content["sensor_info"] = sensor_info
93+
94+
response = client.post_sensor_data(
95+
data=content,
96+
sensor_timestamp=content["datetime"],
97+
save_to_disk=False,
98+
)
9599

96-
if response != False: # Connection is good
97-
if response.ok == True: # File sent
98-
print(f"Prediction sent - {single_file}")
99-
# Proceed to delete sent file
100-
os.remove(single_file)
101-
print(f"Deleted.")
102-
turn_leds_on(GPIO, led_pins) # Turn on LEDs
103-
#### WATCHDOG code ###
104-
GPIO.output(watchdog_pin, GPIO.HIGH) # Send pulse
105-
####################
106-
# Update counter status
107-
counter_status = counter_status + 1
108-
if counter_status >= status_every:
109-
counter_status = 0
100+
if response != False: # Connection is good
101+
if response.ok == True: # File sent
102+
print(f"Prediction sent - {single_file}")
103+
# Proceed to delete sent file
104+
os.remove(single_file)
105+
print(f"Deleted.")
106+
# turn_leds_on(GPIO, led_pins) # Turn on LEDs
107+
#### WATCHDOG code ###
108+
# GPIO.output(watchdog_pin, GPIO.HIGH) # Send pulse
109+
####################
110+
# Update counter status
111+
counter_status = counter_status + 1
112+
if counter_status >= status_every:
113+
counter_status = 0
114+
else:
115+
print(
116+
f"File {single_file} could not be sent. Server response: {response}"
117+
)
118+
# turn_leds_off(GPIO, led_pins)
119+
#### WATCHDOG code ###
120+
# GPIO.output(watchdog_pin, GPIO.LOW) # Stop pulse
121+
####################
110122
else:
111-
print(
112-
f"File {single_file} could not be sent. Server response: {response}"
113-
)
114-
turn_leds_off(GPIO, led_pins)
115-
#### WATCHDOG code ###
116-
GPIO.output(watchdog_pin, GPIO.LOW) # Stop pulse
117-
####################
123+
print("No connection.")
118124
else:
119-
print("No connection.")
125+
# File is old and empty! Delete to not accumulate!
126+
os.remove(single_file)
127+
log_text = (
128+
f"Send process: Deleted because empty --> {single_file}"
129+
)
130+
update_logs_file(errors_path, log_text)
120131

121132
time.sleep(0.2)
122133

123134
# If nothing to send, turn off
124135
# print("waiting...")
125136
turn_leds_off(GPIO, led_pins)
126137
#### WATCHDOG code ###
127-
GPIO.output(watchdog_pin, GPIO.LOW) # Stop pulse
138+
GPIO.output(watchdog_pin, GPIO.LOW) # Stop pulse
128139
####################
129140

130141
finally:
131142
print("Adios")
132143

133144

145+
def update_logs_file(file_path, new_content):
146+
# Check if the file exists
147+
if not os.path.exists(file_path):
148+
# Create the file and write the initial content
149+
with open(file_path, "w") as file:
150+
file.write(new_content + "\n")
151+
print(f"File created and content written: {new_content}")
152+
else:
153+
# Read the current content
154+
with open(file_path, "r") as file:
155+
current_content = file.read()
156+
print("Current content of the file:")
157+
print(current_content)
158+
159+
# Append new content
160+
with open(file_path, "a") as file:
161+
file.write(new_content + "\n")
162+
print(f"New content appended: {new_content}")
163+
164+
134165
def send_library():
135166

136167
# Get parameters needed

lib/functions_simulation.py

+23-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
import numpy as np
1818
import matplotlib
1919
import sys
20+
import time
21+
import pyaudio
2022
from maad.spl import pressure2leq
2123
from maad.util import mean_dB
2224
from scipy.signal import lfilter
@@ -165,6 +167,15 @@ def sensor_processing(
165167
ch = wf.getnchannels()
166168
sample_width = wf.getsampwidth()
167169

170+
# Initialize PyAudio
171+
p = pyaudio.PyAudio()
172+
stream = p.open(
173+
format=p.get_format_from_width(wf.getsampwidth()),
174+
channels=wf.getnchannels(),
175+
rate=wf.getframerate(),
176+
output=True,
177+
)
178+
168179
# Check sample width
169180
if sample_width == 1:
170181
dtype = np.uint8 # 8-bit unsigned
@@ -216,7 +227,9 @@ def sensor_processing(
216227
audio_folder = audio_file_path.split("/")[-2]
217228

218229
# Declare folder where to save plots and predictions
219-
saving_folder_audio_path = os.path.join(saving_folder_path, audio_name)
230+
saving_folder_audio_path = (
231+
saving_folder_path # os.path.join(saving_folder_path, audio_name)
232+
)
220233

221234
# Read and process the audio stream
222235
try:
@@ -229,6 +242,9 @@ def sensor_processing(
229242
# audio_data = stream.read(segment_samples)
230243
# audio_samples = np.frombuffer(audio_data, dtype=np.int16)
231244

245+
# Play the audio
246+
stream.write(audio_samples)
247+
232248
# Convert audio_samples to a NumPy array
233249
audio_samples = np.frombuffer(audio_samples, dtype=dtype)
234250
audio_samples = audio_samples.reshape(-1, ch) # Shape as [time, channels]
@@ -331,6 +347,7 @@ def sensor_processing(
331347
sensor_timestamp=predictions["datetime"],
332348
save_to_disk=False,
333349
)
350+
print("prediction sent")
334351
""" if response != True:
335352
# Prediction was not sent - SAVE IT
336353
# Create folder to save predictions. Check if folder exists, and if not, create it
@@ -347,6 +364,7 @@ def sensor_processing(
347364
# Prepare timestamp for next iteration
348365
timestamp = timestamp + datetime.timedelta(seconds=seconds_segment)
349366
elapsed_time = elapsed_time + seconds_segment
367+
# time.sleep(seconds_segment)
350368

351369
# Save plots and dataframe
352370
if "save" in action:
@@ -507,6 +525,10 @@ def sensor_processing(
507525
df.to_csv(os.path.join(saving_folder_audio_path, "data.csv"), index=False)
508526
print(f"DataFrame saved.")
509527

528+
# Cleanup
529+
stream.stop_stream()
530+
stream.close()
531+
p.terminate()
510532
except KeyboardInterrupt:
511533
print("Processing interrupted.")
512534

parameters.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
sensor_dB_limit_path = "../dB_limit.txt"
55

66
# CONNECTION DATA
7-
ip = "10.42.0.48"
7+
ip = "192.168.2.2"
88
port = 65432
99

1010
# SOUND SOURCES AND PREDICTION MODELS
@@ -74,13 +74,16 @@
7474
predictions_folder_path = "../predictions"
7575
not_sent_predictions_folder_path = "../predictions_not_sent"
7676

77+
# ERRORS REGISTER FILE PATH
78+
errors_path = "../error_logs.txt"
79+
7780
# MIC CALIBRATION
7881
mic_calib_path = "../noisedata_admin/calib.txt"
7982
mic_calib = 1
8083

8184
# CONFIGURATIONS
82-
segment_length = 3
83-
n_segments_intg = 10
85+
segment_length = 6
86+
n_segments_intg = 5
8487
LAeq_limit = 40
8588

8689
# PINS ASSIGNMENT

0 commit comments

Comments
 (0)