# Path: myDevices/utils/threadpool.py
# class ThreadPool(Singleton):
#     """Singleton thread pool class"""
# 
#     @staticmethod
#     def Submit(func):
#         """Submit a function for the thread pool to run"""
#         executor.submit(func)
# 
#     @staticmethod
#     def Shutdown():
#         """Shutdown the thread pool"""
#         executor.shutdown()


from logging import Formatter, getLogger, StreamHandler, WARN, INFO, DEBUG
from logging.handlers import TimedRotatingFileHandler,MemoryHandler, RotatingFileHandler
from inspect import getouterframes, currentframe, getargvalues
from traceback import extract_stack
from os import path, getpid, remove
from datetime import datetime
from hashlib import sha256
from myDevices.utils.threadpool import ThreadPool
from glob import iglob
import tarfile
import time
        # Remove old myDevices.log backups if they are older than a week. This code can be removed
        # in later versions if myDevices.log files have been replaced with cayenne.log.
        for old_file in iglob('/var/log/myDevices/myDevices.log*'):
            if path.getmtime(old_file) + 604800 < time.time(): 
                remove(old_file)
    except Exception as ex:
        print('Log rotator failed with: ' +str(ex))

def rotatorJsonMp():
    try:
        debug('rotatorJsonMp called')
        global jsonData
        output = open(JSON_FILE_LOGGER, 'w')
        for key in jsonData.keys():
            for message in jsonData[key]:
                output.write(message)
        output.close()
    except:
        exception('rotatorJsonMp exception')
        return 1
    return 0

lastRotate=datetime(1970, 1, 1)
def rotatorJson():
    try:
        global lastRotate
        if (datetime.now() - lastRotate).total_seconds() < JSON_FILE_DUMP_TIME:
            return
        debug('rotatorJson called')
        lastRotate = datetime.now()

# Path: myDevices/utils/threadpool.py
# class ThreadPool(Singleton):
#     """Singleton thread pool class"""
# 
#     @staticmethod
#     def Submit(func):
#         """Submit a function for the thread pool to run"""
#         executor.submit(func)
# 
#     @staticmethod
#     def Shutdown():
#         """Shutdown the thread pool"""
#         executor.shutdown()


# Path: myDevices/utils/logger.py
from logging import Formatter, getLogger, StreamHandler, WARN, INFO, DEBUG
from logging.handlers import TimedRotatingFileHandler,MemoryHandler, RotatingFileHandler
from inspect import getouterframes, currentframe, getargvalues
from traceback import extract_stack
from os import path, getpid, remove
from datetime import datetime
from hashlib import sha256
from myDevices.utils.threadpool import ThreadPool
from glob import iglob
import tarfile
import time

        # Remove old myDevices.log backups if they are older than a week. This code can be removed
        # in later versions if myDevices.log files have been replaced with cayenne.log.
        for old_file in iglob('/var/log/myDevices/myDevices.log*'):
            if path.getmtime(old_file) + 604800 < time.time(): 
                remove(old_file)
    except Exception as ex:
        print('Log rotator failed with: ' +str(ex))

def rotatorJsonMp():
    try:
        debug('rotatorJsonMp called')
        global jsonData
        output = open(JSON_FILE_LOGGER, 'w')
        for key in jsonData.keys():
            for message in jsonData[key]:
                output.write(message)
        output.close()
    except:
        exception('rotatorJsonMp exception')
        return 1
    return 0

lastRotate=datetime(1970, 1, 1)
def rotatorJson():
    try:
        global lastRotate
        if (datetime.now() - lastRotate).total_seconds() < JSON_FILE_DUMP_TIME:
            return
        debug('rotatorJson called')
        lastRotate = datetime.now()

        ThreadPool.Submit(rotatorJsonMp)