|
| 1 | +""" |
| 2 | +This script reads all the csv files in the given folder and |
| 3 | +publishes the data as an application output. The published data gets |
| 4 | +stored in the timeseries datastore as well. |
| 5 | +
|
| 6 | +""" |
| 7 | + |
| 8 | +import argparse |
| 9 | +import csv |
| 10 | +import logging |
| 11 | +import os |
| 12 | + |
| 13 | +from datetime import datetime |
| 14 | +from gridappsd import GridAPPSD |
| 15 | +from gridappsd import topics |
| 16 | + |
| 17 | +_log = logging.getLogger(__name__) |
| 18 | + |
| 19 | +def _main(): |
| 20 | + _log.debug("Starting application") |
| 21 | + parser = argparse.ArgumentParser() |
| 22 | + parser.add_argument("app_name", |
| 23 | + help="Application name") |
| 24 | + parser.add_argument("folder_path", |
| 25 | + help="Folder path where files are located") |
| 26 | + parser.add_argument("--simulation_id", |
| 27 | + help="Simulation id to use for responses on the message bus.", |
| 28 | + default=None) |
| 29 | + |
| 30 | + opts = parser.parse_args() |
| 31 | + |
| 32 | + gapps = GridAPPSD() |
| 33 | + topic = topics.application_output_topic(opts.app_name, opts.simulation_id) |
| 34 | + |
| 35 | + for file in os.listdir(opts.folder_path): |
| 36 | + if file.endswith(".csv"): |
| 37 | + data = {} |
| 38 | + data['timestamp'] = int(datetime.now().timestamp()) |
| 39 | + data['datatype'] = file.split('.')[0] |
| 40 | + if opts.simulation_id is not None: |
| 41 | + data['simulation_id'] = str(opts.simulation_id) |
| 42 | + data['tags'] = ["simulation_id"] |
| 43 | + data['message'] = [] |
| 44 | + |
| 45 | + file_path = opts.folder_path + '/' + file |
| 46 | + with open(file_path, newline='', encoding="utf-8-sig") as csvfile: |
| 47 | + reader = csv.DictReader(csvfile, skipinitialspace=True) |
| 48 | + for row in reader: |
| 49 | + data['message'].append(row) |
| 50 | + |
| 51 | + print(data) |
| 52 | + print('\n\n') |
| 53 | + gapps.send(topic,data) |
| 54 | + |
| 55 | + |
| 56 | +if __name__ == "__main__": |
| 57 | + _main() |
0 commit comments