@@ -46,6 +46,7 @@ def send_server():
46
46
led_pins = [pm .red ] # Define GPIO pins for each LED
47
47
folder_path = pm .predictions_folder_path
48
48
status_every = pm .status_every
49
+ errors_path = pm .errors_path
49
50
50
51
# Configure LEDs
51
52
GPIO .setmode (GPIO .BCM ) # Set up GPIO mode
@@ -56,7 +57,7 @@ def send_server():
56
57
GPIO .setup (pin , GPIO .OUT )
57
58
58
59
#### WATCHDOG code ###
59
- watchdog_pin = pm .watchdog
60
+ watchdog_pin = pm .watchdog
60
61
GPIO .setup (watchdog_pin , GPIO .OUT )
61
62
####################
62
63
@@ -72,65 +73,95 @@ def send_server():
72
73
if len (files ) != 0 :
73
74
# There are files!
74
75
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
+ )
95
99
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
+ ####################
110
122
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." )
118
124
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 )
120
131
121
132
time .sleep (0.2 )
122
133
123
134
# If nothing to send, turn off
124
135
# print("waiting...")
125
136
turn_leds_off (GPIO , led_pins )
126
137
#### WATCHDOG code ###
127
- GPIO .output (watchdog_pin , GPIO .LOW ) # Stop pulse
138
+ GPIO .output (watchdog_pin , GPIO .LOW ) # Stop pulse
128
139
####################
129
140
130
141
finally :
131
142
print ("Adios" )
132
143
133
144
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
+
134
165
def send_library ():
135
166
136
167
# Get parameters needed
0 commit comments