def devices_status():
    conn = connect(db)
    c = conn.cursor()
    c.execute('SELECT user, present FROM users')
    for user in c.fetchall():
        if user[1]:
            notif(lang.get("check_devices","msg_inhouse") % user[0])
        else:
            notif(lang.get("check_devices","msg_outhouse") % user[0])
    conn.close()
Example #2
0
def devices_status():
    conn = connect(db)
    c = conn.cursor()
    c.execute('SELECT user, present FROM users')
    for user in c.fetchall():
        if user[1]:
            notif(lang.get("check_devices", "msg_inhouse") % user[0])
        else:
            notif(lang.get("check_devices", "msg_outhouse") % user[0])
    conn.close()
def check_connection():
    ts = time()
    #https://github.com/sivel/speedtest-cli
    a = popen("python "+resource_filename("homenetcontrol", "check_connection/speedtest-cli.py")+" --simple --server 6474").read()
    lines = a.split('\n')
    if "Cannot" in a:
        d = '0'
        u = '0'
    else:
        d = lines[1][10:14]
        u = lines[2][8:12]
    date = datetime.fromtimestamp(ts).strftime('%Y.%m.%d %H:%M')
    out_file = open("/var/local/homenetcontrol/data.csv", 'a')
    writer = csvwriter(out_file)
    writer.writerow((ts,d,u))
    out_file.close()
    logger.info(lang.get("check_connection","msg_log") % (date, d, u))
    if "Cannot" in a:
        notif(lang.get("check_connection","msg_nocon") % date)
    elif (eval(d)<5) or (eval(u)<0.5):
        notif(lang.get("check_connection","msg_notif") % (date, d, u))
def check_connection():
    ts = time()
    #https://github.com/sivel/speedtest-cli
    a = popen("python " + resource_filename(
        "homenetcontrol", "check_connection/speedtest-cli.py") +
              " --simple --server 6474").read()
    lines = a.split('\n')
    if "Cannot" in a:
        d = '0'
        u = '0'
    else:
        d = lines[1][10:14]
        u = lines[2][8:12]
    date = datetime.fromtimestamp(ts).strftime('%Y.%m.%d %H:%M')
    out_file = open("/var/local/homenetcontrol/data.csv", 'a')
    writer = csvwriter(out_file)
    writer.writerow((ts, d, u))
    out_file.close()
    logger.info(lang.get("check_connection", "msg_log") % (date, d, u))
    if "Cannot" in a:
        notif(lang.get("check_connection", "msg_nocon") % date)
    elif (eval(d) < 5) or (eval(u) < 0.5):
        notif(lang.get("check_connection", "msg_notif") % (date, d, u))
def check_devices():
    allowed_devices = {}
    options = config.options("allowed_devices")
    for option in options:
        allowed_devices[option] = config.get("allowed_devices", option)
    users = {}
    options = config.options("watch_users")
    for option in options:
        users[allowed_devices[config.get("watch_users", option)]] = option
    allDevices = []
    device = {}
    conn = connect(db)
    c = conn.cursor()
    c.execute('CREATE TABLE IF NOT EXISTS not_allowed (mac TEXT NOT NULL, advised INTEGER NOT NULL, number INTEGER NOT NULL)')
    c.execute('CREATE TABLE IF NOT EXISTS users (user TEXT NOT NULL, present INTEGER NOT NULL)')
    if not(users=={}):
        for user in users.values():
            t = (user,)
            c.execute('SELECT * FROM users WHERE user=?', t)
            if c.fetchone() == None:
                c.execute("INSERT INTO users VALUES (?,0)", t)
                conn.commit()
    conn.close()
    try:
        # Get devices connected to net
        allDevices = arping(config.get("defaults","NET"),10,1)
        # Open db connection
        conn = connect(db)
        c = conn.cursor()
        # Check if any watched user is connected
        if not(users=={}):
            for user_mac in users.keys():
                found = 0
                for device in allDevices:
                    if device["MAC"] == user_mac:
                        found = 1
                        break
                t = (users[user_mac],)
                if found:
                    c.execute('SELECT present FROM users WHERE user=?', t)
                    if not(c.fetchone()[0]):
                        logger.info(lang.get("check_devices","msg_arrivehouse") % users[user_mac])
                        c.execute("UPDATE users SET present=1 WHERE user=?", t)
                        conn.commit()
                        notif(lang.get("check_devices","msg_arrivehouse") % users[user_mac])
                else:
                    c.execute('SELECT present FROM users WHERE user=?', t)
                    if c.fetchone()[0]:
                        logger.info(lang.get("check_devices","msg_lefthouse") % users[user_mac])
                        c.execute("UPDATE users SET present=0 WHERE user=?", t)
                        conn.commit()
                        notif(lang.get("check_devices","msg_lefthouse") % users[user_mac])
        # Check if unallowed device is connected
        if not(allowed_devices=={}):
            for device in allDevices:
                if not(device.get('MAC') in allowed_devices.values()) and (device.get('MAC') != '<incomplete>'):
                    t = (device.get('MAC'),)
                    c.execute('SELECT * FROM not_allowed WHERE mac=?', t)
                    dbdevice = c.fetchone()
                    if dbdevice == None:
                        logger.info(lang.get("check_devices","msg_notallowed") % (device.get('IP'), device.get('MAC')))
                        c.execute("INSERT INTO not_allowed VALUES (?,0,1)", t)
                        conn.commit()
                        notif(lang.get("check_devices","msg_notallowed") % (device.get('IP'), device.get('MAC')))
        conn.close()
    except arpingerror as msg:
        logger.error("Error msg: %s"%str(msg))
Example #6
0
def check_devices():
    allowed_devices = {}
    options = config.options("allowed_devices")
    for option in options:
        allowed_devices[option] = config.get("allowed_devices", option)
    users = {}
    options = config.options("watch_users")
    for option in options:
        users[allowed_devices[config.get("watch_users", option)]] = option
    allDevices = []
    device = {}
    conn = connect(db)
    c = conn.cursor()
    c.execute(
        'CREATE TABLE IF NOT EXISTS not_allowed (mac TEXT NOT NULL, advised INTEGER NOT NULL, number INTEGER NOT NULL)'
    )
    c.execute(
        'CREATE TABLE IF NOT EXISTS users (user TEXT NOT NULL, present INTEGER NOT NULL)'
    )
    if not (users == {}):
        for user in users.values():
            t = (user, )
            c.execute('SELECT * FROM users WHERE user=?', t)
            if c.fetchone() == None:
                c.execute("INSERT INTO users VALUES (?,0)", t)
                conn.commit()
    conn.close()
    try:
        # Get devices connected to net
        allDevices = arping(config.get("defaults", "NET"), 10, 1)
        # Open db connection
        conn = connect(db)
        c = conn.cursor()
        # Check if any watched user is connected
        if not (users == {}):
            for user_mac in users.keys():
                found = 0
                for device in allDevices:
                    if device["MAC"] == user_mac:
                        found = 1
                        break
                t = (users[user_mac], )
                if found:
                    c.execute('SELECT present FROM users WHERE user=?', t)
                    if not (c.fetchone()[0]):
                        logger.info(
                            lang.get("check_devices", "msg_arrivehouse") %
                            users[user_mac])
                        c.execute("UPDATE users SET present=1 WHERE user=?", t)
                        conn.commit()
                        notif(
                            lang.get("check_devices", "msg_arrivehouse") %
                            users[user_mac])
                else:
                    c.execute('SELECT present FROM users WHERE user=?', t)
                    if c.fetchone()[0]:
                        logger.info(
                            lang.get("check_devices", "msg_lefthouse") %
                            users[user_mac])
                        c.execute("UPDATE users SET present=0 WHERE user=?", t)
                        conn.commit()
                        notif(
                            lang.get("check_devices", "msg_lefthouse") %
                            users[user_mac])
        # Check if unallowed device is connected
        if not (allowed_devices == {}):
            for device in allDevices:
                if not (device.get('MAC') in allowed_devices.values()) and (
                        device.get('MAC') != '<incomplete>'):
                    t = (device.get('MAC'), )
                    c.execute('SELECT * FROM not_allowed WHERE mac=?', t)
                    dbdevice = c.fetchone()
                    if dbdevice == None:
                        logger.info(
                            lang.get("check_devices", "msg_notallowed") %
                            (device.get('IP'), device.get('MAC')))
                        c.execute("INSERT INTO not_allowed VALUES (?,0,1)", t)
                        conn.commit()
                        notif(
                            lang.get("check_devices", "msg_notallowed") %
                            (device.get('IP'), device.get('MAC')))
        conn.close()
    except arpingerror as msg:
        logger.error("Error msg: %s" % str(msg))