import time
import json
import sys
import argparse
import boto3

def emit_json_to_iot(topic, json_file):
    '''
    takes json file, emits it one line at a time to host with a 0.1 sec pause
    example usage from cli

    $python emit_json_data.py anom/detect SF36.json
    '''
    client = boto3.client("iot-data")
    with open(json_file) as f:
        data = json.load(f)
    for observation in data:
        #add key for ddb table              
        response = client.publish(topic=topic, qos=1, payload=json.dumps(observation))
        time.sleep(0.1)
        print('emitted line: '+ str(observation))

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument("topic", help="topic to publish to")
    parser.add_argument("json_file", help="file to send to AWS")
    args = parser.parse_args()
    emit_json_to_iot(args.topic, args.json_file)
