#!/usr/bin/python2
import serial.tools.list_ports
import os

def find_ttyConnected():
    ttyconnected = {}
    i = 0
    # Search for ttyUSB devices
    list = serial.tools.list_ports.comports()
    for tty in list:
        ttyconnected[str(i)] =  {"dev":  str(tty.device), "type": str(tty).split(" ")[2]}
        i += 1
    return ttyconnected


def is_gw(tty_devices):
    gwtty="disconnected"
    for i in sorted(tty_devices.keys()):
        if tty_devices[i]['type'] == 'USB':
            gwtty=i    
    return gwtty



tty_devices = find_ttyConnected()
gw_device = is_gw(tty_devices)

if (gw_device != "disconnected"):
    os.system("gtkterm --port " + str(tty_devices[gw_device]['dev']) + " --speed 115200 &")
    os.system("dunstify -a ' GTKTERM: ' -r 5000 -u normal " + str(tty_devices[gw_device]['dev']))
else:
    os.system("notify-send -a 'GTKTERM' 'Serial Not Found'")

