Пример #1
0
    def __init__(self, instance_queue, global_queue):
        #
        # "sched|port|command or action"
        #
        self.logger = logging.getLogger('Hasip.sched')
        self.sched = Scheduler()
        self.items = ConfigItemReader()
        self.jobs_config = ConfigJobReader()
        self.mod_list = self.items.get_items_dict()  # getting module list from item file
        self.queue_identifier = 'sched'  # this is the 'module address'
        self.instance_queue = instance_queue  # worker queue to receive jobs
        self.global_queue = global_queue  # queue to communicate back to main thread
        self.jobstore = {}
        self.sched.start()

        # read jobs configuration
        self.jobstore = self.jobs_config.get_jobs_dict()
        for job in self.jobstore.keys():
            self.logger.debug(self.jobstore[job][2])
            self.sched.add_cron_job(self.send_msg,
                                    name=job,
                                    year=self.jobstore[job][2],
                                    month=self.jobstore[job][3],
                                    day=self.jobstore[job][4],
                                    week=self.jobstore[job][5],
                                    day_of_week=self.jobstore[job][6],
                                    hour=self.jobstore[job][7],
                                    minute=self.jobstore[job][8],
                                    second=self.jobstore[job][9],
                                    args=(self.jobstore[job][0], self.jobstore[job][1]))
Пример #2
0
  def __init__(self, instance_queue, global_queue):

    self.logger = logging.getLogger('Hasip.rest')
    self.queue_identifier = 'rest'        # this is the 'module address'  
    self.instance_queue = instance_queue  # worker queue to receive jobs 
    self.global_queue = global_queue      # queue to communicate back to main thread
    self.items  = ConfigItemReader()      # config item reader
    self.config = ConfigBaseReader().get_values() # base config reader
    self.reply_cache={}                   # cache dictionary for catching replies to status requests of modules
 
    # ################################################################################
    # This method is for controlling all modules that are based on the switch template.
    # Only actions that are supported by the target module can be taken.
    # Takes the variables 'modname' and 'action' from the URL call and creates
    # a queue message out of it and sends it to the respective module. 
    # In case the "get_status" action has been called the method waits for an answer of
    # the asked module and returns the payload from the "opt_args" part of the answer.
    #
    # Note: To get a match for the answer the replying module need fill out the "module_from"
    #       parameter with its own name including port!!!!  e.g. gpio1
    #
    # @return:     command confirmation or payload of "opt_args" from response in cas of "get_status".
    # ################################################################################

   
    @route('/module/<module>/<action>')
    def rest_switch_modules(module, action):     # ########################################
      mod_list = self.items.get_items_dict()     # getting module list from item file
      if module in mod_list.keys():              # checking existence of requested module
        rcpt = mod_list[module][0]               # setting receiving module from item file
        mid = mod_list[module][1]                # setting module id from item file
        msg = {                                  # creating queue message
          'module_from_port':  0,                # ########################################
          'module_from':    'rest',
          'module_rcpt':    rcpt,
          'module_addr':    mid,
          'cmd':            action,
          'opt_args':       ''
        }                                                   # #################################################################
        self.global_queue.put(msg)                          # sending message to global queue
        if action == 'get_status':                          # checking if requested action was "get_status"
          esc = 0
          self.logger.debug("Waiting for module answer")
          while not (rcpt + str(mid) in self.reply_cache.keys()): # waiting until target module answer appears in worker queue
            time.sleep(0.05)                               # small break
            esc = esc+1
            if esc == 20:
              self.logger.error('No answer from module ' + str(rcpt) + ' received!')
              return 'Error: No status from module ' + str(module) + ' received!'
          self.logger.debug("Module answer received")       #
          val = self.reply_cache[rcpt + str(mid)]           # storing answer from target
          del self.reply_cache[rcpt + str(mid)]             # removing answer from reply_cache
          return module + " is " + val                      # returning module status
        elif action == 'set_on' or action == 'set_off':     # checking if if action was "set_on" or "set_off"
          return "Done"                                     # Placeholder for frontend. Command confirmation.
        else:                                               #
          return "Action not supported"                     # No supported command has been used
      return "No module named " + module                    # Module was not in the item file
Пример #3
0
    def __init__(self, instance_queue, global_queue):

        self.logger = logging.getLogger('Hasip.rest')
        self.queue_identifier = 'rest'  # this is the 'module address'
        self.instance_queue = instance_queue  # worker queue to receive jobs
        self.global_queue = global_queue  # queue to communicate back to main thread
        self.items = ConfigItemReader()  # config item reader
        self.config = ConfigBaseReader().get_values()  # base config reader
        self.reply_cache = {}  # cache dictionary for catching replies to status requests of modules
Пример #4
0
  def __init__(self, instance_queue, global_queue):
    #
    # "sched|port|command or action"
    #

    self.logger = logging.getLogger('Hasip.sched')
    self.sched = Scheduler()
    self.items  = ConfigItemReader()
    self.mod_list = self.items.get_items_dict()     # getting module list from item file
    self.queue_identifier = 'sched'       # this is the 'module address'  
    self.instance_queue = instance_queue  # worker queue to receive jobs 
    self.global_queue = global_queue      # queue to communicate back to main thread

    self.sched.start()

    # read jobs configuration
    self.config = ConfigBaseReader('config/jobs/')
    if self.config.has_config_files(): # true
      self.logger.info('Loading config files...')
      self.jobs = self.config.get_values()
    else:
      self.logger.info('No config files present.')
      self.jobs = []

    sched_params={}
    for section in self.jobs:
      for item in self.jobs[section]:
        if self.jobs[section][item] != '':
          sched_params.update({item : self.jobs[section][item]})
        else:
          sched_params.update({item : None})
 
      
      self.sched.add_cron_job(self.send_msg,
        year   = sched_params['year'], 
        month  = sched_params['month'],
        day    = sched_params['day'],
        week   = sched_params['week'],
        day_of_week = sched_params['day_of_week'],
        hour   = sched_params['hour'],
        minute = sched_params['minute'],
        second = sched_params['second'],
        args=(sched_params['module'],sched_params['action']))

    
    self.logger.debug(self.sched.print_jobs())
Пример #5
0
  def __init__(self):

    self.config_items = ConfigItemReader()
    self.config_hasip = ConfigBaseReader()

    self.global_queue = Queue.Queue()

    self.log = Log()
    self.logger = logging.getLogger('Hasip.main')

    # dynamically creation of communication queues and instances of all used
    # modules. Afterwards a worker of each module is started in background and
    # is waiting for jobs on the incomming queue.
    #
    # (1) create a temp dictionary
    # (2) add a module specific communication queue
    # (3) capitalize & create instance for used module
    # (4) as params we give: "global_queue" and the previously created queue
    # (5) start "worker" of each module in background
    #

    # list of 'modules' to start
    modules_to_start =  []
    modules_to_start += self.config_items.modules_items()
    modules_to_start += self.config_hasip.modules_services()

    self.modules = {}
    for module_name in modules_to_start:
      self.modules[module_name] = {}                                      # (1)
      self.modules[module_name]["instance_queue"] = Queue.Queue()         # (2)
      self.modules[module_name]["instance_object"] = eval(
        "lib.modules." + module_name.capitalize()                         # (3)
      ) (self.modules[module_name]["instance_queue"], self.global_queue)  # (4)

      t = threading.Thread(                                               # (5)
        target = self.modules[module_name]["instance_object"].worker
      )

      self.logger.debug('Loaded Module %s', str(module_name))
      t.daemon = True
      t.start()
Пример #6
0
  def __init__(self, instance_queue, global_queue):

    self.logger = logging.getLogger('Hasip.cul')
    self.queue_identifier = 'cul'     # this is the 'module address'  
    self.instance_queue = instance_queue  # worker queue to receive jobs 
    self.global_queue = global_queue      # queue to communicate back to main thread
    self.items  = ConfigItemReader()      # config item reader

  # ##########################################################################
  # Uncomment this block below on a prepared pi --> wheezy - fhem installation
  # and uncomment the 3 lines in the functions below.
  # refer to cul.md for detailed description
  # ##########################################################################  

  #  self.serial = serial.Serial('/dev/ttyAMA0', 38400) # connects to the serial adapter created by the FHEM setup for CUL
  #  self.sio = io.TextIOWrapper(io.BufferedRWPair(self.serial, self.serial)) # creates read write buffer for data exchange with the serial connection
  #  data='X21'                           # Sends the "X21" command to the cul to receive radio messages
  #  self.sio.write(unicode(data+'\n'))   # write the command + address + escape sequence to buffer
  #  self.sio.flush()                     # get the data out *now*

    self.ports = [                        # internal port names
      { 
        'id'      : 0,
        'type'    : 'FS20',
        'address' : 'F1B1B00',
        'status'  : 'off'

      }] # defining internal ports here (...)

    self.tx_addr = [
      {
        'addr'    : 'F1B1BBA00',
        'module'  : 'Pumpe1',
        'action'  : 'set_off'
      }, {
        'addr'    : 'F1B1BBA11',
        'module'  : 'Pumpe1',
        'action'  : 'set_on'
      }]
Пример #7
0
class Sched(Basemodule):

  # ################################################################################
  # initialization of module and optional load of config files
  # ################################################################################
  def __init__(self, instance_queue, global_queue):
    #
    # "sched|port|command or action"
    #

    self.logger = logging.getLogger('Hasip.sched')
    self.sched = Scheduler()
    self.items  = ConfigItemReader()
    self.mod_list = self.items.get_items_dict()     # getting module list from item file
    self.queue_identifier = 'sched'       # this is the 'module address'  
    self.instance_queue = instance_queue  # worker queue to receive jobs 
    self.global_queue = global_queue      # queue to communicate back to main thread

    self.sched.start()

    # read jobs configuration
    self.config = ConfigBaseReader('config/jobs/')
    if self.config.has_config_files(): # true
      self.logger.info('Loading config files...')
      self.jobs = self.config.get_values()
    else:
      self.logger.info('No config files present.')
      self.jobs = []

    sched_params={}
    for section in self.jobs:
      for item in self.jobs[section]:
        if self.jobs[section][item] != '':
          sched_params.update({item : self.jobs[section][item]})
        else:
          sched_params.update({item : None})
 
      
      self.sched.add_cron_job(self.send_msg,
        year   = sched_params['year'], 
        month  = sched_params['month'],
        day    = sched_params['day'],
        week   = sched_params['week'],
        day_of_week = sched_params['day_of_week'],
        hour   = sched_params['hour'],
        minute = sched_params['minute'],
        second = sched_params['second'],
        args=(sched_params['module'],sched_params['action']))

    
    self.logger.debug(self.sched.print_jobs())

  # @TODO loading jobs from persistent store and create them in the scheduler


  # ################################################################################
  # main thread of this module file which runs in background and constanly
  # checks working queue for new tasks. 
  # ################################################################################

  def worker(self):
    while True:
      instance_queue_element = self.instance_queue.get(True)

      _senderport = instance_queue_element.get("module_from_port")
      _sender	  = instance_queue_element.get("module_from")
      _port       = instance_queue_element.get("module_addr")
      _action     = instance_queue_element.get("cmd")
      _optargs    = instance_queue_element.get("opt_args")
      
      options = {
        "create"   : self.create,
        "delete"  : self.delete
      }
      options[_action](_sender, _senderport, _port, _optargs)

  # ################################################################################
  #
  # "private" methods from here on...
  #
  # ################################################################################

  def create(self, sender, senderport, port, optargs):
    # @TODO
    print "Function to put jobs in the running scheduler job queue and store them persistent"
    pass

  def delete(self, sender, senderport, port, optargs):
    # @TODO
    print "Function to delete running and persistent jobs"
    pass
  
  def send_msg(self, module, action):               # ########################################
    if module in self.mod_list.keys():              # checking existence of requested module
      rcpt = self.mod_list[module][0]               # setting receiving module from item file
      mid = self.mod_list[module][1]                # setting module id from item file
      msg = {                                       # creating queue message
        'module_from_port': 0,                      # ########################################
        'module_from':    'sched',
        'module_rcpt':    rcpt,
        'module_addr':    mid,
        'cmd':            action,
        'opt_args':       ''
      }                                                 
      self.global_queue.put(msg)
Пример #8
0
class Hasip(object):

  def __init__(self):

    self.config_items = ConfigItemReader()
    self.config_hasip = ConfigBaseReader()

    self.global_queue = Queue.Queue()

    self.log = Log()
    self.logger = logging.getLogger('Hasip.main')

    # dynamically creation of communication queues and instances of all used
    # modules. Afterwards a worker of each module is started in background and
    # is waiting for jobs on the incomming queue.
    #
    # (1) create a temp dictionary
    # (2) add a module specific communication queue
    # (3) capitalize & create instance for used module
    # (4) as params we give: "global_queue" and the previously created queue
    # (5) start "worker" of each module in background
    #

    # list of 'modules' to start
    modules_to_start =  []
    modules_to_start += self.config_items.modules_items()
    modules_to_start += self.config_hasip.modules_services()

    self.modules = {}
    for module_name in modules_to_start:
      self.modules[module_name] = {}                                      # (1)
      self.modules[module_name]["instance_queue"] = Queue.Queue()         # (2)
      self.modules[module_name]["instance_object"] = eval(
        "lib.modules." + module_name.capitalize()                         # (3)
      ) (self.modules[module_name]["instance_queue"], self.global_queue)  # (4)

      t = threading.Thread(                                               # (5)
        target = self.modules[module_name]["instance_object"].worker
      )

      self.logger.debug('Loaded Module %s', str(module_name))
      t.daemon = True
      t.start()

  # If there are new jobs in the global_queue a new message is generated and
  # sent to the instance queue
  def run(self):
    while True:

        # (1) get last job from global_queue
        # (2) create new message for addressed module
        # (3) get addresed queue object from modules dictonary
        # (4) put instance_queue_element which was generated before (2) to the
        #     instance_queue of module

        global_queue_element = self.global_queue.get(True) # (1)

        # (2)
        instance_queue_element = {
          'module_from_port':  global_queue_element.get('module_from_port'),
          'module_from':  global_queue_element.get('module_from'),
          'module_rcpt':  global_queue_element.get('module_rcpt'),
          'module_addr':  global_queue_element.get('module_addr'),
          'cmd':          global_queue_element.get('cmd'),
          'opt_args':     global_queue_element.get('opt_args')
        }

        module_rcpt = instance_queue_element.get('module_rcpt') # (3)
        self.logger.debug("Message from " + str(global_queue_element.get('module_from')) + " to " + str(global_queue_element.get('module_rcpt')) + " transmitted")
        self.modules[ module_rcpt ]["instance_queue"].put( instance_queue_element ) # (4)
Пример #9
0
class Rest(Basemodule):
    # ################################################################################
    # initialization of module and optional load of config files
    # ################################################################################
    def __init__(self, instance_queue, global_queue):

        self.logger = logging.getLogger('Hasip.rest')
        self.queue_identifier = 'rest'  # this is the 'module address'
        self.instance_queue = instance_queue  # worker queue to receive jobs
        self.global_queue = global_queue  # queue to communicate back to main thread
        self.items = ConfigItemReader()  # config item reader
        self.config = ConfigBaseReader().get_values()  # base config reader
        self.reply_cache = {}  # cache dictionary for catching replies to status requests of modules

        # ################################################################################
        # This method is for controlling all modules that are based on the switch template.
        # Only actions that are supported by the target module can be taken.
        # Takes the variables 'modname' and 'action' from the URL call and creates
        # a queue message out of it and sends it to the respective module.
        # In case the "get_status" action has been called the method waits for an answer of
        # the asked module and returns the payload from the "opt_args" part of the answer.
        #
        # Note: To get a match for the answer the replying module need fill out the "module_from"
        #       parameter with its own name including port!!!!  e.g. gpio1
        #
        # @return:     command confirmation or payload of "opt_args" from response in cas of "get_status".
        # ################################################################################


    # ################################################################################
    # main thread of this module file which runs in background and constantly
    # checks working queue for new tasks.
    # ################################################################################
    def worker(self):

        app = Flask(__name__)

        @app.route('/modules', methods=['GET'])
        def get_modules():
            mod_list = self.items.get_items_dict()
            dict1 = {}
            string = []
            for module in mod_list.keys():
                dict1['name'] = module
                try:
                    dict1['module_name'] = mod_list[module][0]
                    dict1['module_addr'] = mod_list[module][1]
                    dict1['type'] = mod_list[module][2]
                    dict1['cat'] = 'switches'#mod_list[module][3]
                except Exception as e:
                    self.logger.debug(e)
                string.append(dict1.copy())
                dict1.clear()
            return jsonify({'modules': string})


        @app.route('/modules/<string:module>', methods=['GET'])
        def get_module(module):
            mod_list = self.items.get_items_dict()  # getting module list from item file
            if module in mod_list.keys():
                rcpt = mod_list[module][0]  # setting receiving module from item file
                mid = mod_list[module][1]  # setting module id from item file
                msg = {  # creating queue message
                    'module_from_port': 0,
                    'module_from': 'rest',
                    'module_rcpt': rcpt,
                    'module_addr': mid,
                    'cmd': 'get_status',
                    'opt_args': ''
                }
                self.global_queue.put(msg)
                esc = 0
                self.logger.debug("Waiting for module answer")
                while not (rcpt + str(mid) in self.reply_cache.keys()):  # waiting until target module answer appears in worker queue
                    time.sleep(0.05)  # small break
                    esc = esc + 1
                    if esc == 20:
                        self.logger.error('No answer from module ' + str(rcpt) + ' received!')
                        abort(404)
                self.logger.debug("Module answer received")  #
                val = self.reply_cache[rcpt + str(mid)]  # storing answer from target
                del self.reply_cache[rcpt + str(mid)]  # removing answer from reply_cache
                return jsonify({'status': val})
            abort(404)


        @app.route('/modules/<string:module>/<string:action>', methods = ['PUT'])
        def update_module(module, action):
            mod_list = self.items.get_items_dict()  # getting module list from item file
            if module in mod_list.keys():
                rcpt = mod_list[module][0]  # setting receiving module from item file
                mid = mod_list[module][1]  # setting module id from item file
                msg = {  # creating queue message
                    'module_from_port': 0,
                    'module_from': 'rest',
                    'module_rcpt': rcpt,
                    'module_addr': mid,
                    'cmd': action,
                    'opt_args': ''
                }
                self.global_queue.put(msg)
                return jsonify( { rcpt: 'something happened ...... maybe?' } )
            abort(404)


        @app.route('/jobs', methods=['GET'])
        def get_jobs():
            rcpt = 'sched'  # setting receiving module from item file
            mid = 0  # setting module id from item file
            msg = {  # creating queue message
                'module_from_port': 0,
                'module_from': 'rest',
                'module_rcpt': rcpt,
                'module_addr': mid,
                'cmd': 'list_jobs',
                'opt_args': ''
            }
            self.global_queue.put(msg)
            esc = 0
            self.logger.debug(msg)
            self.logger.debug("Waiting for module answer")
            while not (rcpt + str(mid) in self.reply_cache.keys()):  # waiting until target module answer appears in worker queue
                time.sleep(0.05)  # small break
                esc = esc + 1
                if esc == 20:
                    self.logger.error('No answer from module ' + str(rcpt) + ' received!')
                    abort(404)
            self.logger.debug("Module answer received")  #
            val = self.reply_cache[rcpt + str(mid)]  # storing answer from target
            del self.reply_cache[rcpt + str(mid)]  # removing answer from reply_cache
            return jsonify({'status': val})


        @app.route('/jobs/create/<string:name>', methods = ['POST'])
        @app.route('/jobs/update/<string:name>', methods = ['POST'])
        def make_job(name):
            seconds = 10
            module = 'Pumpe1'
            action = 'set_on'
            string = {
                'module': module,
                'action': action,
                'name': name,
                'year': None,
                'month': None,
                'week': None,
                'day': None,
                'day_of_week': None,
                'hour': None,
                'minute': None,
                'second': seconds}

            mod_list = self.items.get_items_dict()  # getting module list from item file
            if module in mod_list.keys():
                rcpt = 'sched'   # setting receiving module from item file
                mid = mod_list[module][1]  # setting module id from item file
                msg = {  # creating queue message
                    'module_from_port': 0,
                    'module_from': 'rest',
                    'module_rcpt': rcpt,
                    'module_addr': mid,
                    'cmd': 'create',
                    'opt_args': string
                }
                self.global_queue.put(msg)
                self.logger.debug(msg)
            return "test"


        @app.route('/jobs/delete/<string:name>', methods = ['DELETE'])
        def delete_job(name):
            rcpt = 'sched'   # setting receiving module from item file
            mid = 0  # setting module id from item file
            msg = {  # creating queue message
                'module_from_port': 0,
                'module_from': 'rest',
                'module_rcpt': rcpt,
                'module_addr': mid,
                'cmd': 'delete',
                'opt_args': name
            }
            self.global_queue.put(msg)
            self.logger.debug(msg)
            return "test"


        @app.errorhandler(404)
        def not_found(error):
            return make_response(jsonify( { 'error': 'Not found' } ), 404)


        # ##############################################################################
        # Starting rest webserver in own thread otherwise it will block the
        # whole application
        # ##############################################################################

        t = threading.Thread(target=lambda: app.run(host='0.0.0.0', port=80, debug=None))
        t.daemon = True
        t.start()

        while True:
            instance_queue_element = self.instance_queue.get(True)
            _senderport = instance_queue_element.get("module_from_port")
            _sender = instance_queue_element.get("module_from")
            _port = instance_queue_element.get("module_addr")
            _action = instance_queue_element.get("cmd")
            _optargs = instance_queue_element.get("opt_args")

            self.reply_cache[_sender + str(_senderport)] = _optargs
Пример #10
0
class Rest(Basemodule):
    # ################################################################################
    # initialization of module and optional load of config files
    # ################################################################################
    def __init__(self, instance_queue, global_queue):

        self.logger = logging.getLogger("Hasip.rest")
        self.queue_identifier = "rest"  # this is the 'module address'
        self.instance_queue = instance_queue  # worker queue to receive jobs
        self.global_queue = global_queue  # queue to communicate back to main thread
        self.items = ConfigItemReader()  # config item reader
        self.config = ConfigBaseReader().get_values()  # base config reader
        self.reply_cache = {}  # cache dictionary for catching replies to status requests of modules

        # ################################################################################
        # This method is for controlling all modules that are based on the switch template.
        # Only actions that are supported by the target module can be taken.
        # Takes the variables 'modname' and 'action' from the URL call and creates
        # a queue message out of it and sends it to the respective module.
        # In case the "get_status" action has been called the method waits for an answer of
        # the asked module and returns the payload from the "opt_args" part of the answer.
        #
        # Note: To get a match for the answer the replying module need fill out the "module_from"
        #       parameter with its own name including port!!!!  e.g. gpio1
        #
        # @return:     command confirmation or payload of "opt_args" from response in cas of "get_status".
        # ################################################################################

    # ################################################################################
    # main thread of this module file which runs in background and constantly
    # checks working queue for new tasks.
    # ################################################################################
    def worker(self):

        app = Flask(__name__)

        def nocache(view):
            @wraps(view)
            def no_cache(*args, **kwargs):
                response = make_response(view(*args, **kwargs))
                response.headers["Last-Modified"] = datetime.now()
                response.headers[
                    "Cache-Control"
                ] = "no-store, no-cache, must-revalidate, post-check=0, pre-check=0, max-age=0"
                response.headers["Pragma"] = "no-cache"
                response.headers["Expires"] = "-1"
                return response

            return update_wrapper(no_cache, view)

        @app.route("/", methods=["GET"])
        def display_webpage():
            return render_template("index.html")

        @app.route("/time", methods=["PUT"])
        def get_time():
            time = datetime.now()
            dicttime = {}
            dicttime["hour"] = time.hour
            dicttime["minute"] = time.minute
            dicttime["second"] = time.second
            return jsonify({"time": dicttime})

        @app.route("/modules", methods=["PUT"])
        def get_modules():
            mod_list = self.items.get_items_dict()
            dict1 = {}
            string = []
            for module in mod_list.keys():
                dict1["name"] = module
                try:
                    dict1["module_name"] = mod_list[module][0]
                    dict1["module_addr"] = mod_list[module][1]
                    dict1["type"] = mod_list[module][2]
                    dict1["cat"] = mod_list[module][3]
                except Exception as e:
                    self.logger.debug(e)
                string.append(dict1.copy())
                dict1.clear()
            return jsonify({"modules": string})

        @app.route("/modules/<string:module>", methods=["PUT"])
        def get_module(module):
            mod_list = self.items.get_items_dict()  # getting module list from item file
            if module in mod_list.keys():
                rcpt = mod_list[module][0]  # setting receiving module from item file
                mid = mod_list[module][1]  # setting module id from item file
                msg = {  # creating queue message
                    "module_from_port": 0,
                    "module_from": "rest",
                    "module_rcpt": rcpt,
                    "module_addr": mid,
                    "cmd": "get_status",
                    "opt_args": "",
                }
                self.global_queue.put(msg)
                esc = 0
                self.logger.debug("Waiting for module answer")
                while not (
                    rcpt + str(mid) in self.reply_cache.keys()
                ):  # waiting until target module answer appears in worker queue
                    time.sleep(0.05)  # small break
                    esc = esc + 1
                    if esc == 40:
                        self.logger.error("No answer from module " + str(rcpt) + " received!")
                        abort(404)
                self.logger.debug("Module answer received")  #
                val = self.reply_cache[rcpt + str(mid)]  # storing answer from target
                del self.reply_cache[rcpt + str(mid)]  # removing answer from reply_cache
                return jsonify({"status": val})
            abort(404)

        @app.route("/modules/<string:module>/<string:action>", methods=["PUT"])
        def update_module(module, action):
            mod_list = self.items.get_items_dict()  # getting module list from item file
            if module in mod_list.keys():
                rcpt = mod_list[module][0]  # setting receiving module from item file
                mid = mod_list[module][1]  # setting module id from item file
                msg = {  # creating queue message
                    "module_from_port": 0,
                    "module_from": "rest",
                    "module_rcpt": rcpt,
                    "module_addr": mid,
                    "cmd": action,
                    "opt_args": "",
                }
                self.global_queue.put(msg)
                if action == "sensor_data":
                    esc = 0
                    self.logger.debug("Waiting for module answer")
                    while not (
                        rcpt + str(mid) in self.reply_cache.keys()
                    ):  # waiting until target module answer appears in worker queue
                        time.sleep(0.05)  # small break
                        esc = esc + 1
                        if esc == 20:
                            self.logger.error("No answer from module " + str(rcpt) + " received!")
                            abort(404)
                    self.logger.debug("Module answer received")  #
                    val = self.reply_cache[rcpt + str(mid)]  # storing answer from target
                    del self.reply_cache[rcpt + str(mid)]  # removing answer from reply_cache
                    return jsonify({"sensor": val})
                return jsonify({"result": True}), 200
            abort(404)

        @app.route("/jobs", methods=["PUT"])
        def get_jobs():
            rcpt = "sched"  # setting receiving module from item file
            mid = 0  # setting module id from item file
            msg = {  # creating queue message
                "module_from_port": 0,
                "module_from": "rest",
                "module_rcpt": rcpt,
                "module_addr": mid,
                "cmd": "list_jobs",
                "opt_args": "",
            }
            self.global_queue.put(msg)
            esc = 0
            self.logger.debug(msg)
            self.logger.debug("Waiting for module answer")
            while not (
                rcpt + str(mid) in self.reply_cache.keys()
            ):  # waiting until target module answer appears in worker queue
                time.sleep(0.05)  # small break
                esc = esc + 1
                if esc == 40:
                    self.logger.error("No answer from module " + str(rcpt) + " received!")
                    abort(404)
            self.logger.debug("Module answer received")  #
            val = self.reply_cache[rcpt + str(mid)]  # storing answer from target
            del self.reply_cache[rcpt + str(mid)]  # removing answer from reply_cache
            return jsonify({"status": val})

        @app.route(
            "/jobs/create/<string:name>/<string:module>/<string:action>/<string:year>/<string:month>/<string:week>/<string:day>/<string:day_of_week>/<string:hour>/<string:minute>/<string:second>",
            methods=["PUT"],
        )
        @app.route(
            "/jobs/update/<string:name>/<string:module>/<string:action>/<string:year>/<string:month>/<string:week>/<string:day>/<string:day_of_week>/<string:hour>/<string:minute>/<string:second>",
            methods=["PUT"],
        )
        def make_job(name, module, action, year, month, week, day, day_of_week, hour, minute, second):
            # dict1 = request.get_json(force=False, silent=False, cache=True)
            # self.logger.debug(dict1)
            # dict2 = request.json()
            # self.logger.debug('bla')
            # self.logger.debug(dict2)
            # module = dict1['device']
            # self.logger.debug('get')
            # self.logger.debug(dict1['week'])
            # string = {
            #    'module': module,
            #    'action': 'set_' + dict1.get('action'),
            #    'name': name,
            #    'year': dict1.get('year'),
            #    'month': dict1.get('month'),
            #    'week': dict1.get('week'),
            #    'day': dict1.get('day'),
            #    'day_of_week': dict1.get('day_of_week'),
            #    'hour': dict1.get('hour'),
            #    'minute': dict1.get('min'),
            #    'second': dict1.get('sec')
            # }
            self.logger.debug("start")
            year = checkNone(year)
            month = checkNone(month)
            week = checkNone(week)
            day = checkNone(day)
            day_of_week = checkNone(day_of_week)
            hour = checkNone(hour)
            minute = checkNone(minute)
            second = checkNone(second)
            self.logger.debug(minute)
            string = {
                "module": module,
                "action": "set_" + action,
                "name": name,
                "year": year,
                "month": month,
                "week": week,
                "day": day,
                "day_of_week": day_of_week,
                "hour": hour,
                "minute": minute,
                "second": second,
            }
            self.logger.debug(string)
            mod_list = self.items.get_items_dict()  # getting module list from item file
            if module in mod_list.keys():
                rcpt = "sched"  # setting receiving module from item file
                mid = mod_list[module][1]  # setting module id from item file
                msg = {  # creating queue message
                    "module_from_port": 0,
                    "module_from": "rest",
                    "module_rcpt": rcpt,
                    "module_addr": mid,
                    "cmd": "create",
                    "opt_args": string,
                }
                self.global_queue.put(msg)
                self.logger.debug(msg)
            return jsonify({"result": True}), 201

        @app.route("/jobs/delete/<string:name>", methods=["PUT"])
        def delete_job(name):
            rcpt = "sched"  # setting receiving module from item file
            mid = 0  # setting module id from item file
            msg = {  # creating queue message
                "module_from_port": 0,
                "module_from": "rest",
                "module_rcpt": rcpt,
                "module_addr": mid,
                "cmd": "delete",
                "opt_args": name,
            }
            self.global_queue.put(msg)
            self.logger.debug(msg)
            return jsonify({"result": True}), 200

        @app.errorhandler(404)
        def not_found(error):
            return make_response(jsonify({"error": "Not found"}), 404)

        def checkNone(var):
            if var == "None":
                return None
            return var

        # ##############################################################################
        # Starting rest webserver in own thread otherwise it will block the
        # whole application
        # ##############################################################################

        t = threading.Thread(target=lambda: app.run(host="0.0.0.0", port=80, debug=None))
        t.daemon = True
        t.start()

        while True:
            instance_queue_element = self.instance_queue.get(True)
            _senderport = instance_queue_element.get("module_from_port")
            _sender = instance_queue_element.get("module_from")
            _port = instance_queue_element.get("module_addr")
            _action = instance_queue_element.get("cmd")
            _optargs = instance_queue_element.get("opt_args")

            self.reply_cache[_sender + str(_senderport)] = _optargs
Пример #11
0
class Sched(Basemodule):
    # ################################################################################
    # initialization of module and optional load of config files
    # ################################################################################
    def __init__(self, instance_queue, global_queue):
        #
        # "sched|port|command or action"
        #
        self.logger = logging.getLogger('Hasip.sched')
        self.sched = Scheduler()
        self.items = ConfigItemReader()
        self.jobs_config = ConfigJobReader()
        self.mod_list = self.items.get_items_dict()  # getting module list from item file
        self.queue_identifier = 'sched'  # this is the 'module address'
        self.instance_queue = instance_queue  # worker queue to receive jobs
        self.global_queue = global_queue  # queue to communicate back to main thread
        self.jobstore = {}
        self.sched.start()

        # read jobs configuration
        self.jobstore = self.jobs_config.get_jobs_dict()
        for job in self.jobstore.keys():
            self.logger.debug(self.jobstore[job][2])
            self.sched.add_cron_job(self.send_msg,
                                    name=job,
                                    year=self.jobstore[job][2],
                                    month=self.jobstore[job][3],
                                    day=self.jobstore[job][4],
                                    week=self.jobstore[job][5],
                                    day_of_week=self.jobstore[job][6],
                                    hour=self.jobstore[job][7],
                                    minute=self.jobstore[job][8],
                                    second=self.jobstore[job][9],
                                    args=(self.jobstore[job][0], self.jobstore[job][1]))

    # ################################################################################
    # main thread of this module file which runs in background and constantly checks working queue for new tasks.
    # ################################################################################
    def worker(self):
        while True:
            instance_queue_element = self.instance_queue.get(True)
            _senderport = instance_queue_element.get("module_from_port")
            _sender = instance_queue_element.get("module_from")
            _port = instance_queue_element.get("module_addr")
            _action = instance_queue_element.get("cmd")
            _optargs = instance_queue_element.get("opt_args")
            options = {
                "create": self.create,
                "delete": self.delete,
                "list_jobs": self.list_jobs
            }
            options[_action](_sender, _senderport, _port, _optargs)

    # ################################################################################
    #
    # "private" methods from here on...
    #
    # ################################################################################
    #Function to put jobs in the running scheduler job queue and store them persistent

    def create(self, sender, senderport, port, optargs):
        if optargs.get('name') in self.jobstore:
            self.delete(sender, senderport, port, optargs.get('name'))
        self.sched.add_cron_job(self.send_msg,
                                name=optargs.get('name'),
                                year=optargs.get('year'),
                                month=optargs.get('month'),
                                day=optargs.get('day'),
                                week=optargs.get('week'),
                                day_of_week=optargs.get('day_of_week'),
                                hour=optargs.get('hour'),
                                minute=optargs.get('minute'),
                                second=optargs.get('second'),
                                args=(optargs.get('module'), optargs.get('action')))
        self.jobstore.update(
            {
                optargs.get('name'): [
                    optargs.get('module'),
                    optargs.get('action'),
                    optargs.get('year'),
                    optargs.get('month'),
                    optargs.get('day'),
                    optargs.get('week'),
                    optargs.get('day_of_week'),
                    optargs.get('hour'),
                    optargs.get('minute'),
                    optargs.get('second')
                ]
            }
        )
        self.write_to_config()

    def delete(self, sender, senderport, port, optargs):
        for job in self.sched.get_jobs():
            if job.name == optargs:
                self.sched.unschedule_job(job)
                del self.jobstore[job.name]
                self.write_to_config()

    def list_jobs(self, sender, senderport, port, optargs):
        dict1 = {}
        string = []
        for job in self.jobstore.keys():
            dict1['jobname'] = job
            dict1['device'] = self.jobstore[job][0]
            dict1['action'] = self.jobstore[job][1]
            dict1['year'] = self.jobstore[job][2]
            dict1['month'] = self.jobstore[job][3]
            dict1['day'] = self.jobstore[job][4]
            dict1['week'] = self.jobstore[job][5]
            dict1['day_of_week'] = self.jobstore[job][6]
            dict1['hour'] = self.jobstore[job][7]
            dict1['min'] = self.jobstore[job][8]
            dict1['sec'] = self.jobstore[job][9]
            string.append(dict1.copy())
            dict1.clear()
        self.logger.debug("help")
        queue_msg = {
            'module_from_port': str(port),
            'module_from': self.queue_identifier,
            'module_rcpt': sender,
            'module_addr': senderport,
            'cmd': 'reply',
            'opt_args': string
        }
        self.global_queue.put(queue_msg)

    def send_msg(self, module, action):  # ########################################
        if module in self.mod_list.keys():  # checking existence of requested module
            rcpt = self.mod_list[module][0]  # setting receiving module from item file
            mid = self.mod_list[module][1]  # setting module id from item file
            msg = {  # creating queue message
                     'module_from_port': 0,  # ########################################
                     'module_from': 'sched',
                     'module_rcpt': rcpt,
                     'module_addr': mid,
                     'cmd': action,
                     'opt_args': ''
            }
            self.global_queue.put(msg)
    
    def write_to_config(self):
        with open('/home/hasip/hasip/config/jobs/example.jobs', 'w') as f:
            conf = ConfigParser()
            for job in self.jobstore.keys():
                try:
                    conf.add_section(job)
                except Exception:
                    pass
                conf.set(job.upper(), 'module', self.jobstore[job][0])
                conf.set(job.upper(), 'action', self.jobstore[job][1])
                conf.set(job.upper(), 'year', self.jobstore[job][2])
                conf.set(job.upper(), 'month', self.jobstore[job][3])
                conf.set(job.upper(), 'week', self.jobstore[job][4])
                conf.set(job.upper(), 'day', self.jobstore[job][5])
                conf.set(job.upper(), 'day_of_week', self.jobstore[job][6])
                conf.set(job.upper(), 'hour', self.jobstore[job][7])
                conf.set(job.upper(), 'minute', self.jobstore[job][8])
                conf.set(job.upper(), 'second', self.jobstore[job][9])
            conf.write(f)
Пример #12
0
class Cul(Basemodule):

  # ################################################################################
  # initialization of module and optional load of config files
  # ################################################################################
  def __init__(self, instance_queue, global_queue):

    self.logger = logging.getLogger('Hasip.cul')
    self.queue_identifier = 'cul'     # this is the 'module address'  
    self.instance_queue = instance_queue  # worker queue to receive jobs 
    self.global_queue = global_queue      # queue to communicate back to main thread
    self.items  = ConfigItemReader()      # config item reader

  # ##########################################################################
  # Uncomment this block below on a prepared pi --> wheezy - fhem installation
  # and uncomment the 3 lines in the functions below.
  # refer to cul.md for detailed description
  # ##########################################################################  

  #  self.serial = serial.Serial('/dev/ttyAMA0', 38400) # connects to the serial adapter created by the FHEM setup for CUL
  #  self.sio = io.TextIOWrapper(io.BufferedRWPair(self.serial, self.serial)) # creates read write buffer for data exchange with the serial connection
  #  data='X21'                           # Sends the "X21" command to the cul to receive radio messages
  #  self.sio.write(unicode(data+'\n'))   # write the command + address + escape sequence to buffer
  #  self.sio.flush()                     # get the data out *now*

    self.ports = [                        # internal port names
      { 
        'id'      : 0,
        'type'    : 'FS20',
        'address' : 'F1B1B00',
        'status'  : 'off'

      }] # defining internal ports here (...)

    self.tx_addr = [
      {
        'addr'    : 'F1B1BBA00',
        'module'  : 'Pumpe1',
        'action'  : 'set_off'
      }, {
        'addr'    : 'F1B1BBA11',
        'module'  : 'Pumpe1',
        'action'  : 'set_on'
      }]


  # ################################################################################
  # main thread of this module file which runs in background and constanly
  # checks working queue for new tasks. 
  # ################################################################################
  def worker(self):
    t = threading.Thread(target=lambda: self.listen())
    t.daemon = True
    t.start()

    while True:
      instance_queue_element = self.instance_queue.get(True)

      _senderport = instance_queue_element.get("module_from_port")
      _sender	     = instance_queue_element.get("module_from")
      _port        = instance_queue_element.get("module_addr")
      _action      = instance_queue_element.get("cmd")
      _optargs    = instance_queue_element.get("opt_args")

      options = {
        "set_on"    : self.set_on,
        "set_off"   : self.set_off,
        "get_status": self.get_status
      }
      options[_action](_sender, _senderport, _port, _optargs)

  # ################################################################################
  #
  # "private" methods from here on...
  #
  # ################################################################################

  def set_on(self, sender, senderport, port, optargs):
    if port != None:
      data=self.ports[port]['address']+'01' # get the FS20 address of module + FS20 command "on"
  #    self.sio.write(unicode(data+'\n'))   # write the command + address + escape sequence to buffer
  #    self.sio.flush()                     # get the data out *now*
  #    self.ports[port]['status']='on'       # set internal port status
      self.logger.debug("CUL Port (" + str(port) + ") set to on")
    else:
      self.logger.error('Port "'+ str(port) + '" doesn\'t exist!')

  def set_off(self, sender, senderport, port, optargs):
    if port != None:
      data=self.ports[port]['address']+'00' # get the FS20 address of module + FS20 command "off"
  #    self.sio.write(unicode(data+'\n'))   # write the command + address + escape sequence to buffer
  #    self.sio.flush()                     # get the data out *now*
  #    self.ports[port]['status']='off'       # set internal port status
      self.logger.debug("CUL Port (" + str(port) + ") set to off")
    else:
      self.logger.error('Port "'+ str(port) + '" doesn\'t exist!')

  def get_status(self, sender, senderport, port, optargs):
    if port != None and sender != None:
      args=str(self.ports[port]['status'])
      queue_msg = {
        'module_from_port':  str(port),
        'module_from':  self.queue_identifier,
        'module_rcpt':  sender,
        'module_addr':  senderport,
        'cmd':          'reply',
        'opt_args':     args
      }
      self.global_queue.put(queue_msg)
    else:
      self.logger.error('Port "'+ str(port) + '" or sender "' + str(sender) + '" doesn\'t exist!')

  def listen(self):
    print 'Dummy line'
   # while True:
   #   buffer = self.serial.readline()
   #   if buffer != '':
   #     self.logger.debug('Received message from cul: ' + str(buffer))
   #     tmp = buffer[:9]
   #     for dict in self.tx_addr:
   #       if dict['addr'] == tmp:
   #         self.send_msg(dict['module'],dict['action'])
      
  def send_msg(self, module, action):               # ########################################
    mod_list = self.items.get_items_dict() 
    if module in mod_list.keys():              # checking existence of requested module
      rcpt = mod_list[module][0]               # setting receiving module from item file
      mid = mod_list[module][1]                # setting module id from item file
      msg = {                                       # creating queue message
        'module_from_port': 0,
        'module_from':    'cul',                  # ########################################
        'module_rcpt':    rcpt,
        'module_addr':    mid,
        'cmd':            action,
        'opt_args':       ''
      }                                                 
      self.global_queue.put(msg)