# Path: myDevices/utils/logger.py
# def debug(message):
#     outerFrame = getouterframes(currentframe())[1][0]
#     (args, _, _, values) = getargvalues(outerFrame)
#     argsValue = ''
#     
#     for i in args:
#         if i is 'self':
#             continue
#         argsValue += "(%s=%s)" % (i, str(values[i]))
#     
#     stack = extract_stack()
#     (filename, line, procname, text) = stack[-2]
#     LOGGER.debug(str(filename) + ' ' + str(procname) + str(argsValue) + ':' + str(line) + '> '  + str(message))
# 
# def error(message, *args, **kwargs):
#     if checkFlood(message):
#         return
#     LOGGER.error(message, *args, **kwargs)
# 
# def exception(message):
#     if checkFlood(message):
#         return
#     LOGGER.exception(message)
# 
# def info(message):
#     # if checkFlood(message):
#     #     return
#     LOGGER.info(message)
# 
# def logJson(message, category = ''):
#     if debugEnabled() == False:
#         return
#     if checkFlood(message):
#         return
#     try:
#         if category not in jsonData:
#             jsonData[category] = []
#         if len(jsonData[category]) > MAX_JSON_ITEMS_PER_CATEGORY:
#             del jsonData[category][0]
#         #2016-03-23 03:53:16 - myDevices - INFO -
#         message = str(datetime.today()) + ' - myDevices - INFO - ' + message
#         jsonData[category].append(message)
#         rotatorJson()
#     except Exception as ex:
#         exception("logJson failed")
# 
# def setDebug():
#     LOGGER.setLevel(DEBUG)
#     return
# 
# def warn(message):
#     if checkFlood(message):
#         return
#     LOGGER.warn(message)


from datetime import datetime
from json import dumps, loads
from sqlite3 import connect
from threading import RLock, Thread
from time import sleep
from myDevices.requests_futures.sessions import FuturesSession
from myDevices.utils.logger import debug, error, exception, info, logJson, setDebug, warn
import myDevices.schedule as schedule
        debug('Update scheduled event')
        result = False
        try:
            schedule_item = {'event': event, 'job': None}
            with self.mutex:
                try:
                    old_item = self.schedule_items[event['id']]
                    schedule.cancel_job(old_item['job'])
                    result = self.create_job(schedule_item)
                    debug('Update scheduled event result: {}'.format(result))
                    if result == True:
                        self.update_database_record(event['id'], event)
                        self.schedule_items[event['id']] = schedule_item
                except KeyError:
                    debug('Old schedule with id = {} not found'.format(event['id']))
        except:
            exception('Failed to update scheduled event')
        return result

    def remove_scheduled_event(self, event):
        """Remove an event that has been scheduled
        
        event: the event to remove"""
        debug('Remove scheduled event')
        return self.remove_scheduled_item_by_id(event['id'])

    def update_scheduled_events(self, events):
        """Update all scheduled events
        
        events: list of all the new events to schedule"""

# Path: myDevices/utils/logger.py
# def debug(message):
#     outerFrame = getouterframes(currentframe())[1][0]
#     (args, _, _, values) = getargvalues(outerFrame)
#     argsValue = ''
#     
#     for i in args:
#         if i is 'self':
#             continue
#         argsValue += "(%s=%s)" % (i, str(values[i]))
#     
#     stack = extract_stack()
#     (filename, line, procname, text) = stack[-2]
#     LOGGER.debug(str(filename) + ' ' + str(procname) + str(argsValue) + ':' + str(line) + '> '  + str(message))
# 
# def error(message, *args, **kwargs):
#     if checkFlood(message):
#         return
#     LOGGER.error(message, *args, **kwargs)
# 
# def exception(message):
#     if checkFlood(message):
#         return
#     LOGGER.exception(message)
# 
# def info(message):
#     # if checkFlood(message):
#     #     return
#     LOGGER.info(message)
# 
# def logJson(message, category = ''):
#     if debugEnabled() == False:
#         return
#     if checkFlood(message):
#         return
#     try:
#         if category not in jsonData:
#             jsonData[category] = []
#         if len(jsonData[category]) > MAX_JSON_ITEMS_PER_CATEGORY:
#             del jsonData[category][0]
#         #2016-03-23 03:53:16 - myDevices - INFO -
#         message = str(datetime.today()) + ' - myDevices - INFO - ' + message
#         jsonData[category].append(message)
#         rotatorJson()
#     except Exception as ex:
#         exception("logJson failed")
# 
# def setDebug():
#     LOGGER.setLevel(DEBUG)
#     return
# 
# def warn(message):
#     if checkFlood(message):
#         return
#     LOGGER.warn(message)


# Path: myDevices/cloud/scheduler.py
from datetime import datetime
from json import dumps, loads
from sqlite3 import connect
from threading import RLock, Thread
from time import sleep
from myDevices.requests_futures.sessions import FuturesSession
from myDevices.utils.logger import debug, error, exception, info, logJson, setDebug, warn
import myDevices.schedule as schedule

        debug('Update scheduled event')
        result = False
        try:
            schedule_item = {'event': event, 'job': None}
            with self.mutex:
                try:
                    old_item = self.schedule_items[event['id']]
                    schedule.cancel_job(old_item['job'])
                    result = self.create_job(schedule_item)
                    debug('Update scheduled event result: {}'.format(result))
                    if result == True:
                        self.update_database_record(event['id'], event)
                        self.schedule_items[event['id']] = schedule_item
                except KeyError:
                    debug('Old schedule with id = {} not found'.format(event['id']))
        except:
            exception('Failed to update scheduled event')
        return result

    def remove_scheduled_event(self, event):
        """Remove an event that has been scheduled
        
        event: the event to remove"""
        debug('Remove scheduled event')
        return self.remove_scheduled_item_by_id(event['id'])

    def update_scheduled_events(self, events):
        """Update all scheduled events
        
        events: list of all the new events to schedule"""

        logJson('Update schedules:  {}'.format(events))