import argparse
import time

import serial
from io import BufferedRWPair, TextIOWrapper

temp_usb = '/dev/ttyAMA0'
BAUD_RATE = 9600
parser = argparse.ArgumentParser()
parser.add_argument('oxygen', help='The USB port of the oxygen sensor.')
parser.add_argument('salinity', help='The USB port of the salinity sensor.')
parser.add_argument('server_ip', help='The IP address of the lighthouse node.')
parser.add_argument('port', help='The port of the lighthouse node.')


def init_db():
    # TODO: initialize the sqlite database.
    pass


def create_connection(usb_port):
    print('Creating connection on {}'.format(usb_port))
    ser = serial.Serial(usb_port, BAUD_RATE)
    # disable_continuous_mode(ser)
    return TextIOWrapper(BufferedRWPair(ser, ser), newline='\r', encoding='ascii', line_buffering=True)


def disable_continuous_mode(conn: serial.Serial):
    # TODO: research if we need to send this command every time we connect to the sensors, or if it only
    # needs to be sent once to disable continuous mode. If only once we should move this code into a
    # separate python file.
    print('Disabling continuous mode...')
    conn.write(bytes('E\r', 'ascii'))

    if conn.inWaiting() > 0:
        # clear the buffer if there is anything waiting.
        print('Clearing buffer...')
        conn.read(conn.inWaiting())


def save_data(temperature, salinity, oxygen):
    # TODO save data to database (sqlite)
    pass


def push_data(temperature, salinity, oxygen, server_ip, server_port):
    payload = {'temperature': temperature, 'salinity': salinity, 'oxygen': oxygen}


# TODO push data to lighthouse node.


def initialize_serial_connections(oxy_usb, sal_usb):
    temp_conn = create_connection(temp_usb)
    sal_conn = create_connection(sal_usb)
    oxy_conn = create_connection(oxy_usb)

    return temp_conn, sal_conn, oxy_conn


def run_loop(oxy_usb, sal_usb, server_ip, server_port):
    init_db()
    temp_conn, sal_conn, oxy_conn = initialize_serial_connections(oxy_usb, sal_usb)

    # TODO: Catch serial.serialutil.SerialException on read?
    while True:
        temp_conn.write('R\r')
        temp = temp_conn.readline()

        sal_conn.write('R\r')
        sal = sal_conn.readline()

        # TODO: send temp and sal to oxy sensor first, then retrieve oxy value.
        # oxy.write(<salinity command here>)
        # oxy.write(<temp command here>)

        oxy_conn.write('R\r')
        oxy = oxy_conn.readline()

        print('Temperature: {}, Dissolved Oxygen: {}, Salinity: {}'.format(temp, oxy, sal))

        save_data(temp, oxy, sal)
        push_data(temp, oxy, sal, server_ip, server_port)

        # TODO: Determine how often we should be grabbing data from sensors and pushing to other pi node.
        time.sleep(5)


if __name__ == '__main__':
    # TODO: Create supervisord script to keep run.py running.
    # TODO: Parse command line args for database connection info.
    args = parser.parse_args()
    run_loop(args.oxygen, args.salinity, args.server_ip, args.port)

@@ -82,16 +82,50 @@
 rapper%0A%0A
+from cso_parser import CsoParser%0A%0A
 temp_usb
@@ -1408,16 +1408,37 @@
 , oxygen
+, cso_now, cso_recent
 ):%0A    #
@@ -1529,16 +1529,37 @@
  oxygen,
+ cso_now, cso_recent,
  server_
@@ -1657,16 +1657,77 @@
 : oxygen
+, 'cso_now': cso_now,%0A               'cso_recent': cso_recent
 %7D%0A%0A%0A# TO
@@ -2056,16 +2056,45 @@
 it_db()%0A
+    cso_parser = CsoParser()%0A
     temp
@@ -2741,53 +2741,147 @@
  sal
-)%0A        push_data(temp, oxy, sal, server_ip
+, cso_parser.now_count, cso_parser.recent_count)%0A        push_data(temp, oxy, sal, server_ip, cso_parser.now_count, cso_parser.recent_count
 , se

Use CsoParser in run.py