Exemplo n.º 1
0
class Watcher:
    """
    PiMotics core watcher
    """

    def __init__(self):
        self.module = __name__
        self.system = System()

    def check_services(self):
        """
        Checks the status of required services

        :returns: status of the services
        :rtype: bool
        """

        logging = LogHandler(System().get_watcher_log_dir())

        # Checking the API
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        result = sock.connect_ex((self.system.get_grid_ip(), self.system.get_api_port()))

        if result == 0:
            api_status = True
            logging.logger("Status of API: ACTIVE", 4, module)
        else:
            api_status = False
            logging.logger("Status of API: INACTIVE", 4, module)

        # Checking the LOCAL_NET
        if ServiceManager().get_service_status("pimotics-local-net"):
            local_net_status = True
            logging.logger("Status of LOCAL_NET: ACTIVE", 4, module)
        else:
            local_net_status = False
            logging.logger("Status of LOCAL_NET: INACTIVE", 4, module)

        # Output result
        if api_status and local_net_status:
            return True
        else:
            return False
Exemplo n.º 2
0
    def __init__(self, logfile):
        """
        constructor

        :param logfile: log file absolute location
        :type logfile: str
        """

        self.module = __name__
        self.sys = System()
        self.LOG = logfile
        self.debug = self.sys.get_debug_mode()

        # create if log_dir does not exists
        if not os.path.dirname(self.LOG):
            os.makedirs(self.LOG)
Exemplo n.º 3
0
    ledState = not bool(GPIO.input(LED))
    GPIO.output(LED, ledState)
    return str({'success': True, 'status': ledState})


@app.route("{0}/toggle/7".format(API_BASEDIR))
def led2():
    LED = 10
    GPIO.setup(LED, GPIO.OUT)
    ledState = not bool(GPIO.input(LED))
    GPIO.output(LED, ledState)
    return str({'success': True, 'status': ledState})


@app.route("{0}/temperature/2".format(API_BASEDIR))
def sensor():
    TEMP = 5
    SENSOR = 2302

    result = SensorController.get_temperature_and_humidity(SENSOR, TEMP)

    return str(result)

if __name__ == "__main__":

    system = System()

    app.run(host=system.get_grid_ip(),
            port=system.get_api_port(),
            debug=system.get_debug_mode())
Exemplo n.º 4
0
 def __init__(self):
     self.module = __name__
     self.system = System()
Exemplo n.º 5
0
class LogHandler:
    """
    Creates log files for API
    """

    def __init__(self, logfile):
        """
        constructor

        :param logfile: log file absolute location
        :type logfile: str
        """

        self.module = __name__
        self.sys = System()
        self.LOG = logfile
        self.debug = self.sys.get_debug_mode()

        # create if log_dir does not exists
        if not os.path.dirname(self.LOG):
            os.makedirs(self.LOG)

    def logger(self, message, level, module):
        """
        Logs all messages to the desired log files

        :param message: log message
        :param level: log level
            * failed = 0
            * succeeded = 1
            * warning = 2
            * info = 3
            * debug = 4
            * exception = 5
        :param module: module name

        :type message: str
        :type level: int
        :type module: str
        """

        target = open(self.LOG, 'a')
        now = datetime.datetime.now()

        if level == 0:
            target.write("{0} - [FAILURE] - [{1}] - {2}\n".format(now, module, message))

        elif level == 1:
            target.write("{0} - [SUCCEEDED] - [{1}] - {2}\n".format(now, module, message))

        elif level == 2:
            target.write("{0} - [WARNING] - [{1}] - {2}\n".format(now, module, message))

        elif level == 3:
            target.write("{0} - [INFO] - [{1}] - {2}\n".format(now, module, message))

        elif level == 4:
            if self.debug:
                target.write("{0} - [DEBUG] - [{1}] - {2}\n".format(now, module, message))

        elif level == 5:
            target.write("{0} - [EXCEPTION] - [{1}] - {2}\n".format(now, module, message))

        target.close()