# -*- coding: utf-8 -*-
"""
    demeter web
    name:sub.py
    author:rabin
"""
import time
from demeter.core import *
from demeter.mqtt import *
timeSleep = 1000
import random
class Subs(object):

    def run(self):
        self.handle()
        time.sleep(timeSleep)

    def handle(self):
        topic = Demeter.config['mqtt']['sub'].split('|')
        if topic:
            mqtt = Demeter.path + 'mqtt.py'
            for v in topic:
                command = 'python3 ' + mqtt + ' -m subs -t ' + v

                check = "ps aux|grep '"+command+"'|grep -v entrypoint|grep -v grep|grep -v process|awk '{print $1}'"
                check = Shell.popen(check)
                if not check:
                    Shell.popen(command, sub=True, bg=True)

    def load(self, topic):
        Sub(topic)