def init_regulators(db_session):
    # query components needed for initialisation
    inside_temp = db_session.query(Parameter).filter_by(name='Inside Air Temperature').one()
    inside_humi = db_session.query(Parameter).filter_by(name='Inside Air Humidity').one()
    exhaust_fan = db_session.query(Device).filter_by(name='Exhaust Fan').one()
    root_mist_pump = db_session.query(Device).filter_by(name='Rootchamber Mist pump').one()

    # create regulators
    air_temp_diff = Regulator('Air Temperature Difference', 'Difference', '')
    real_reg = regulator_factory(air_temp_diff.algorithm_name)
    real_reg.initialize_db(air_temp_diff)
    air_temp_diff.inputs['a'].connected_output = inside_temp.outputs['value']
    air_temp_diff.inputs['b'].connected_output = inside_temp.outputs['setpoint']
    db_session.add(air_temp_diff)

    air_temp_reg = Regulator('Air Temperature Regulator', 'PI', '')
    real_reg = regulator_factory(air_temp_reg.algorithm_name)
    real_reg.initialize_db(air_temp_reg)
    air_temp_reg.inputs['diff'].connected_output = air_temp_diff.outputs['result']
    db_session.add(air_temp_reg)

    root_humidifier = Regulator('Root Humidifier', 'RootHumidifier', '')
    real_reg = regulator_factory(root_humidifier.algorithm_name)
    real_reg.initialize_db(root_humidifier)
    root_humidifier.inputs['T_i'].connected_output = inside_temp.outputs['value']
    root_humidifier.inputs['H_i'].connected_output = inside_humi.outputs['value']
    root_humidifier.inputs['T_i_sp'].connected_output = inside_temp.outputs['setpoint']
    root_humidifier.inputs['H_i_sp'].connected_output = inside_humi.outputs['setpoint']
    db_session.add(root_humidifier)

    # connect output device
    exhaust_fan.inputs['value'].connected_output = air_temp_reg.outputs['result']
    root_mist_pump.inputs['value'].connected_output = root_humidifier.outputs['pump']
Beispiel #2
0
 def handle_component_input_changes(self, msg):
     print('handle component input changes')
     logging.info('handling component input changes')
     ci_id = int(msg)
     ci = self.db_session.query(ComponentInput).filter_by(_id=ci_id).one()
     if ci.component.component_type == 'device':
         self.reload_devices()
     if ci.component.component_type == 'regulator':
         if ci.component_id in self.regulators:
             reg = self.regulators[ci.component_id]
             old_order = reg.order
             self.db_session.refresh(reg)
             if old_order != reg.order:
                 self.max_regulation_order = 0
                 for reg_id in self.regulators:
                     order = self.regulators[reg_id].order
                     if order > self.max_regulation_order:
                         self.max_regulation_order = order
         else:
             reg = self.db_session.query(Regulator).filter_by(
                 _id=ci.component_id).one()
             real_reg = regulator_factory(reg.algorithm_name)
             real_reg.initialize(reg)
             inputs = {}
             for inp in reg.inputs:
                 inputs[inp] = get_redis_number(self.redis_conn,
                                                reg.inputs[inp].redis_key)
             if real_reg.is_executable(inputs):
                 self.regulators[reg.id] = reg
                 self.real_regulators[reg.id] = real_reg
Beispiel #3
0
 def regulator_update(self):
     ret_dict = {}
     controls = self.request.POST.items()
     reg_id = self.request.matchdict['reg_id']
     reg = DBSession.query(Regulator).filter_by(_id=reg_id).one()
     form = Form(EditRegulatorSchema().bind(regulator=reg),
                 formid='edit_regulator_form_' + reg_id,
                 action=self.request.route_url('regulator_update', reg_id=reg_id),
                 use_ajax=True,
                 ajax_options='{"success": function (rText, sText, xhr, form) {edit_regulator(rText);}}',
                 buttons=('Save',))
     try:
         vals = form.validate(controls)
         ret_dict['error'] = False
     except ValidationFailure as e:
         ret_dict['error'] = True
         ret_dict['regulator'] = reg.serialize
         ret_dict['form'] = e.render()
         return ret_dict
     reg.algorithm_name = vals['algorithm']
     real_reg = regulator_factory(reg.algorithm_name)
     real_reg.initialize_db(reg)
     DBSession.flush()
     ret_dict['form'] = form.render(regulator=reg)
     ret_dict['regulator'] = reg.serialize
     ret_dict['regulator_panel'] = self.request.layout_manager.render_panel('regulator_panel', context=reg)
     self.request.redis.publish('regulator_changes', 'changed '+str(reg.id))
     return ret_dict
Beispiel #4
0
 def regulator_save(self):
     ret_dict = {}
     controls = self.request.POST.items()
     form = Form(NewRegulatorSchema().bind(),
                 formid='add_regulator_form',
                 action=self.request.route_url('regulator_save'),
                 use_ajax=True,
                 ajax_options='{"success": function (rText, sText, xhr, form) {add_regulator(rText);}}',
                 buttons=('Save',))
     try:
         vals = form.validate(controls)
         ret_dict['error'] = False
     except ValidationFailure as e:
         ret_dict['form'] = e.render()
         ret_dict['error'] = True
         return ret_dict
     new_reg = Regulator(vals['name'], vals['algorithm'], vals['description'])
     real_reg = regulator_factory(new_reg.algorithm_name)
     real_reg.initialize_db(new_reg)
     DBSession.add(new_reg)
     DBSession.flush()
     ret_dict['form'] = form.render()
     ret_dict['regulator_panel'] = self.request.layout_manager.render_panel('regulator_panel', context=new_reg)
     self.request.redis.publish('regulator_changes', 'added '+str(new_reg.id))
     return ret_dict
 def handle_component_input_changes(self, msg):
     print('handle component input changes')
     logging.info('handling component input changes')
     ci_id = int(msg)
     ci = self.db_session.query(ComponentInput).filter_by(_id=ci_id).one()
     if ci.component.component_type == 'device':
         self.reload_devices()
     if ci.component.component_type == 'regulator':
         if ci.component_id in self.regulators:
             reg = self.regulators[ci.component_id]
             old_order = reg.order
             self.db_session.refresh(reg)
             if old_order != reg.order:
                 self.max_regulation_order = 0
                 for reg_id in self.regulators:
                     order = self.regulators[reg_id].order
                     if order > self.max_regulation_order:
                         self.max_regulation_order = order
         else:
             reg = self.db_session.query(Regulator).filter_by(_id=ci.component_id).one()
             real_reg = regulator_factory(reg.algorithm_name)
             real_reg.initialize(reg)
             inputs = {}
             for inp in reg.inputs:
                 inputs[inp] = get_redis_number(self.redis_conn, reg.inputs[inp].redis_key)
             if real_reg.is_executable(inputs):
                 self.regulators[reg.id] = reg
                 self.real_regulators[reg.id] = real_reg
Beispiel #6
0
def init_regulators(db_session):
    # query components needed for initialisation
    inside_temp = db_session.query(Parameter).filter_by(
        name='Inside Air Temperature').one()
    inside_humi = db_session.query(Parameter).filter_by(
        name='Inside Air Humidity').one()
    exhaust_fan = db_session.query(Device).filter_by(name='Exhaust Fan').one()
    root_mist_pump = db_session.query(Device).filter_by(
        name='Rootchamber Mist pump').one()

    # create regulators
    air_temp_diff = Regulator('Air Temperature Difference', 'Difference', '')
    real_reg = regulator_factory(air_temp_diff.algorithm_name)
    real_reg.initialize_db(air_temp_diff)
    air_temp_diff.inputs['a'].connected_output = inside_temp.outputs['value']
    air_temp_diff.inputs['b'].connected_output = inside_temp.outputs[
        'setpoint']
    db_session.add(air_temp_diff)

    air_temp_reg = Regulator('Air Temperature Regulator', 'PI', '')
    real_reg = regulator_factory(air_temp_reg.algorithm_name)
    real_reg.initialize_db(air_temp_reg)
    air_temp_reg.inputs['diff'].connected_output = air_temp_diff.outputs[
        'result']
    db_session.add(air_temp_reg)

    root_humidifier = Regulator('Root Humidifier', 'RootHumidifier', '')
    real_reg = regulator_factory(root_humidifier.algorithm_name)
    real_reg.initialize_db(root_humidifier)
    root_humidifier.inputs['T_i'].connected_output = inside_temp.outputs[
        'value']
    root_humidifier.inputs['H_i'].connected_output = inside_humi.outputs[
        'value']
    root_humidifier.inputs['T_i_sp'].connected_output = inside_temp.outputs[
        'setpoint']
    root_humidifier.inputs['H_i_sp'].connected_output = inside_humi.outputs[
        'setpoint']
    db_session.add(root_humidifier)

    # connect output device
    exhaust_fan.inputs['value'].connected_output = air_temp_reg.outputs[
        'result']
    root_mist_pump.inputs['value'].connected_output = root_humidifier.outputs[
        'pump']
Beispiel #7
0
 def handle_regulator_changes(self, msg):
     print('handle regulator changes')
     logging.info('handling regulator changes')
     change_type, r_id_str = msg.split(' ')
     r_id = int(r_id_str)
     if change_type == 'added':
         r = self.db_session.query(Regulator).filter_by(_id=r_id).one()
         rr = regulator_factory(r.algorithm_name)
         rr.initialize(r)
         inputs = {}
         for inp in r.inputs:
             inputs[inp] = get_redis_number(self.redis_conn,
                                            r.inputs[inp].redis_key)
         if rr.is_executable(inputs):
             self.regulators[r.id] = r
             self.real_regulators[r.id] = rr
             logging.info('added ' + str(r.name))
             print('added ' + str(r.name))
     if change_type == 'changed':
         if r_id in self.regulators.keys():
             r = self.regulators[r_id]
             self.db_session.refresh(r)
         else:
             r = self.db_session.query(Regulator).filter_by(_id=r_id).one()
         rr = regulator_factory(r.algorithm_name)
         rr.initialize(r)
         inputs = {}
         for inp in r.inputs:
             inputs[inp] = get_redis_number(self.redis_conn,
                                            r.inputs[inp].redis_key)
         if rr.is_executable(inputs) or r.order > 0:
             logging.info('using: ' + str(r.name))
             print('using: ' + r.name)
             self.regulators[r.id] = r
             self.real_regulators[r.id] = rr
             if r.order > self.max_regulation_order:
                 self.max_regulation_order = r.order
     elif change_type == 'removed':
         if r_id in self.regulators.keys():
             logging.info('removing regulator: ' +
                          self.regulators[r_id].name)
             print('removing regulator: ' + self.regulators[r_id].name)
             self.devices.pop(r_id, None)
 def handle_regulator_changes(self, msg):
     print('handle regulator changes')
     logging.info('handling regulator changes')
     change_type, r_id_str = msg.split(' ')
     r_id = int(r_id_str)
     if change_type == 'added':
         r = self.db_session.query(Regulator).filter_by(_id=r_id).one()
         rr = regulator_factory(r.algorithm_name)
         rr.initialize(r)
         inputs = {}
         for inp in r.inputs:
             inputs[inp] = get_redis_number(self.redis_conn, r.inputs[inp].redis_key)
         if rr.is_executable(inputs):
             self.regulators[r.id] = r
             self.real_regulators[r.id] = rr
             logging.info('added ' + str(r.name))
             print('added ' + str(r.name))
     if change_type == 'changed':
         if r_id in self.regulators.keys():
             r = self.regulators[r_id]
             self.db_session.refresh(r)
         else:
             r = self.db_session.query(Regulator).filter_by(_id=r_id).one()
         rr = regulator_factory(r.algorithm_name)
         rr.initialize(r)
         inputs = {}
         for inp in r.inputs:
             inputs[inp] = get_redis_number(self.redis_conn, r.inputs[inp].redis_key)
         if rr.is_executable(inputs) or r.order > 0:
             logging.info('using: ' + str(r.name))
             print('using: ' + r.name)
             self.regulators[r.id] = r
             self.real_regulators[r.id] = rr
             if r.order > self.max_regulation_order:
                 self.max_regulation_order = r.order
     elif change_type == 'removed':
         if r_id in self.regulators.keys():
             logging.info('removing regulator: ' + self.regulators[r_id].name)
             print('removing regulator: ' + self.regulators[r_id].name)
             self.devices.pop(r_id, None)
Beispiel #9
0
 def reload_regulators(self):
     logging.info('FM: reloading regulators')
     print('FM: reloading regulators')
     self.max_regulation_order = 0
     self.regulators = {}
     self.real_regulators = {}
     regs = self.db_session.query(Regulator).all()
     for reg in regs:
         real_reg = regulator_factory(reg.algorithm_name)
         real_reg.initialize(reg)
         # print('input['+inp+'] = '+str(inputs[inp])+', redis: '+reg.inputs[inp].redis_key)
         # print('maybe addind '+reg.name)
         logging.info('FM: using: ' + str(reg.name))
         print('FM: using: ' + reg.name)
         self.regulators[reg.id] = reg
         self.real_regulators[reg.id] = real_reg
         if reg.order > self.max_regulation_order:
             print('FM: setting max order: ' + str(reg.order))
             self.max_regulation_order = reg.order
 def reload_regulators(self):
     logging.info('FM: reloading regulators')
     print('FM: reloading regulators')
     self.max_regulation_order = 0
     self.regulators = {}
     self.real_regulators = {}
     regs = self.db_session.query(Regulator).all()
     for reg in regs:
         real_reg = regulator_factory(reg.algorithm_name)
         real_reg.initialize(reg)
         # print('input['+inp+'] = '+str(inputs[inp])+', redis: '+reg.inputs[inp].redis_key)
         # print('maybe addind '+reg.name)
         logging.info('FM: using: ' + str(reg.name))
         print('FM: using: ' + reg.name)
         self.regulators[reg.id] = reg
         self.real_regulators[reg.id] = real_reg
         if reg.order > self.max_regulation_order:
             print('FM: setting max order: ' + str(reg.order))
             self.max_regulation_order = reg.order