#!/usr/bin/python
# -*- coding: utf8 -*-
# -*- Mode: Python; py-indent-offset: 4 -*-
"""
    Utils that let us send message over socket.
"""
import argparse
import serial
from time import sleep
import logging
import logging.config

DEFAULT_LOG_FILE = "/opt/timevortex/timevortex-currentcost.conf"
logging.config.fileConfig(DEFAULT_LOG_FILE)
LOGGER = logging.getLogger("currentcost")


def argument_parser():
    """Method that parse arguments from command line, return an error in
    case of missing parameter or return arguments with their value.

    :returns:  dict -- Dict containing arguments.

    """
    # Get command line arguments
    parser = argparse.ArgumentParser()
    # Define expected arguments
    parser.add_argument("message", help="Message to send")
    parser.add_argument("tty", help="Tty port to connect and send message")
    # Return list of argument passed in command line
    return parser.parse_args()


def main():
    """
        Main function of this module.
    """
    args = argument_parser()
    message = args.message
    tty = args.tty
    ser = serial.Serial(tty)
    ser.write("%s\n" % message)
    LOGGER.error(message)
    ser.close()

if __name__ == "__main__":
    # Launch main method
    main()
