From 0c08dcd124235c5f9a6f3f6e87b79cb799a55289 Mon Sep 17 00:00:00 2001 From: poorva1209 Date: Tue, 5 Dec 2023 13:34:56 -0800 Subject: [PATCH] publishing csv data as app output --- timeseries/stream_csv_as_app_output.py | 57 ++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 timeseries/stream_csv_as_app_output.py diff --git a/timeseries/stream_csv_as_app_output.py b/timeseries/stream_csv_as_app_output.py new file mode 100644 index 0000000..fcdcebb --- /dev/null +++ b/timeseries/stream_csv_as_app_output.py @@ -0,0 +1,57 @@ +""" +This script reads all the csv files in the given folder and +publishes the data as an application output. The published data gets +stored in the timeseries datastore as well. + +""" + +import argparse +import csv +import logging +import os + +from datetime import datetime +from gridappsd import GridAPPSD +from gridappsd import topics + +_log = logging.getLogger(__name__) + +def _main(): + _log.debug("Starting application") + parser = argparse.ArgumentParser() + parser.add_argument("app_name", + help="Application name") + parser.add_argument("folder_path", + help="Folder path where files are located") + parser.add_argument("--simulation_id", + help="Simulation id to use for responses on the message bus.", + default=None) + + opts = parser.parse_args() + + gapps = GridAPPSD() + topic = topics.application_output_topic(opts.app_name, opts.simulation_id) + + for file in os.listdir(opts.folder_path): + if file.endswith(".csv"): + data = {} + data['timestamp'] = int(datetime.now().timestamp()) + data['datatype'] = file.split('.')[0] + if opts.simulation_id is not None: + data['simulation_id'] = str(opts.simulation_id) + data['tags'] = ["simulation_id"] + data['message'] = [] + + file_path = opts.folder_path + '/' + file + with open(file_path, newline='', encoding="utf-8-sig") as csvfile: + reader = csv.DictReader(csvfile, skipinitialspace=True) + for row in reader: + data['message'].append(row) + + print(data) + print('\n\n') + gapps.send(topic,data) + + +if __name__ == "__main__": + _main()