#!/usr/bin/python3
"""
my.backend.status

STATUS



FYI, nearly every call to server requires a token.
The token is sent to the client as part of the reponse to a client's attempt to log into the dollhouse.
So, let's say the client sends message '_login'

CPU temperature, workload, etc.

Major functions:-

    get_procstat_dct()        read /proc/stat and create a ditionary of its results
    get_cpu_temperature()
    get_the_dollhouse_disk_usage()
    get_the_root_disk_usage()
    infinite_loop_to_monitor_services_and_echo_their_status()
    __get_dollhouse_status()


    dct=get_procstat_dct()
    print(dct.user,
          dct.nice,
          dct.system,
          dct.idle,
          dct.iowait,
          dct.irq,
          dct.softirq,
          steal)

These figures are integers.
"""

import json

from my.classes.hardware.cpu.procstats.back import get_dictionary_of_procstats


def backendtask__get_procstats(
):  # TODO: This is ugly. Replace it with calls to a preexisting Python library.
    dct = get_dictionary_of_procstats()
    dct['outcome'] = 'success'
    return json.dumps(dct)


def backendtask__get_dollhouse_status():
    """Obtain and return the status of the dollhouse.

    Args:
        None

    Returns:
        Dictionary containing status of various attributes of dollhouse:
            'outcome': 'success' or the name of the exception if there was one
            'open': is it open or not (True if mounted and unlocked; else, False)
            'mounted': True if mounted, else False
            'unlocked': True if unlocked, else False
            'disk_usage': Dollhouse's disk usage as a percentage
            'message': verbose description of exception, if an exception was raised

    Raises:
        None

    """
    from my.backend import Backend
    try:
        dct = {
            'outcome': 'success',
            'open': Backend.dollhouse.is_open,
            'mounted': Backend.dollhouse.is_mounted,
            'unlocked': Backend.dollhouse.is_unlocked,
            'disk_usage': Backend.dollhouse.disk_usage,
        }
        return json.dumps(dct)
    except Exception as e:
        return json.dumps({
            'outcome':
            'BackendCaughtFailureError',
            'message':
            '_get_dollhouse_status() returned %s => %s' % (type(e), str(e))
        })


# ------------------------------------------------------------------------------------------------------------------------------------------------------------

if __name__ == "__main__":
    raise SystemExit('Do not run hardware.py')
