#!/usr/bin/env python3

import json
import logging
import pyodbc

logging.basicConfig(format='[%(asctime)s,%(filename)s,%(levelname)s] %(message)s',
                    datefmt='%Y-%m-%d %H:%M')

def main():
    try:
        with open('/etc/usm/config.json') as config:
            config = json.load(config)
    except FileNotFoundError:
        # use local config for development
        with open(os.path.join('..', '..', 'etc', 'config.json')):
            config = json.load(config)
            
    database = config['database']

    UPDATE_ONLINE = 'UPDATE tblRoom SET ONLINE=ONLINE-1 WHERE ONLINE>0'
    
    conn = pyodbc.connect(**database)
    cursor = conn.cursor()
    cursor.execute(UPDATE_ONLINE)
    cursor.commit()
    cursor.close()

if __name__ == '__main__':
    try:
        main()
    except Exception as e:
        logging.warning(e)
