@@ -187,3 +187,56 @@ async def telemetry_generator(
187
187
state ["step_energy_discharged__Wh" ] = 0
188
188
else :
189
189
continue
190
+
191
+
192
+ if __name__ == "__main__" :
193
+ import os
194
+
195
+ from pyspark .sql import SparkSession
196
+
197
+ from pulse_telemetry .sparklib import iceberg , telemetry
198
+ from pulse_telemetry .utils import channel
199
+
200
+ catalog = os .environ ["PULSE_TELEMETRY_CATALOG" ]
201
+ database = os .environ ["PULSE_TELEMETRY_DATABASE" ]
202
+ num_channels = int (os .environ ["PULSE_TELEMETRY_NUM_CHANNELS" ])
203
+ timeout_seconds = float (os .environ ["PULSE_TELEMETRY_TIMEOUT_SECONDS" ])
204
+ acquisition_frequency = int (os .environ ["PULSE_TELEMETRY_ACQUISITION_FREQUENCY" ])
205
+ points_per_step = int (os .environ ["PULSE_TELEMETRY_POINTS_PER_STEP" ])
206
+
207
+ spark = SparkSession .builder .appName ("TelemetryGenerator" ).getOrCreate ()
208
+
209
+ # Create telemetry table if not exists
210
+ iceberg .create_table_if_not_exists (
211
+ spark = spark ,
212
+ catalog_name = catalog ,
213
+ database_name = database ,
214
+ table_name = "telemetry" ,
215
+ table_comment = telemetry .telemetry_comment ,
216
+ table_schema = telemetry .telemetry_schema ,
217
+ partition_columns = telemetry .telemetry_partitions ,
218
+ write_order_columns = telemetry .telemetry_write_order ,
219
+ )
220
+
221
+ # Runs generator and loads data into iceberg table
222
+ local_buffer = channel .LocalBuffer ()
223
+ channel .run_with_timeout (
224
+ source = telemetry_generator ,
225
+ sink = local_buffer ,
226
+ topic = "telemetry" ,
227
+ num_channels = num_channels ,
228
+ timeout_seconds = timeout_seconds ,
229
+ acquisition_frequency = acquisition_frequency ,
230
+ points_per_step = points_per_step ,
231
+ lower_voltage_limit = 3 , # V
232
+ upper_voltage_limit = 4 , # V
233
+ current = 1.0 , # A
234
+ )
235
+ iceberg .merge_into_table (
236
+ spark = spark ,
237
+ source_df = local_buffer .dataframe (spark , telemetry .telemetry_schema ),
238
+ catalog_name = catalog ,
239
+ database_name = database ,
240
+ table_name = "telemetry" ,
241
+ match_columns = telemetry .telemetry_composite_key ,
242
+ )
0 commit comments