Skip to content

Commit

Permalink
Merge pull request #3 from MacherLabs/behaviourcloudconfig
Browse files Browse the repository at this point in the history
behaviour_base: added auto config fetching from cloud if BEHAVIOUR_ID…
  • Loading branch information
ML-Guy authored Sep 26, 2019
2 parents 2a9ce5b + d6b251e commit e3678a3
Showing 1 changed file with 41 additions and 6 deletions.
47 changes: 41 additions & 6 deletions src/eazyserver/core/behaviour_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,46 @@
logger = logging.getLogger(__name__)
logger.debug("Loaded " + __name__)

# Import app to get api_config
def get_beh_config(behaviour_type, behaviour_id):
import requests
from requests.auth import HTTPBasicAuth
from flask import current_app as app

#TODO: Generalise AUTH config: change VedaUser to User and so on.
api_config = app.config['VEDA_AUTH']
Veda_auth = HTTPBasicAuth(api_config['VedaUser'], api_config['VedaPassword'])

final_url = "{}/{}/{}/{}".format(api_config['ServerUrl'], api_config['API_VERSION'], behaviour_type, behaviour_id)
logging.info("Fetching Behaviour config: {}".format(final_url))

resp = {}
try:
resp = requests.get(final_url, auth=Veda_auth)
resp.raise_for_status()
resp = resp.json()
except Exception as e:
logging.error("getConfig Failed:{}".format(e))
resp = None
if resp is None:
raise RuntimeError("Failed to fetch cloud config for behaviour {}".format(behaviour_id))

if(behaviour_type is "cameras"):
resp['enabled'] = resp.get("isEnabled",True)
else:
resp['enabled'] = resp.get("params",{}).get("enable",True)

return resp

class Behaviour(object):
def __init__(self, Config):
self.id = Config["_id"]
self.enabled = True
self.Config = Config

def run(self, data, params):
def __init__(self, config, behaviour_id=None, behaviour_type="behaviours"):

if behaviour_id:
config = get_beh_config(behaviour_type=behaviour_type, behaviour_id=behaviour_id)

self.id = config["_id"]
self.config = config
self.enabled = config.get("enabled",True)

def run(self, data):
return(data)

0 comments on commit e3678a3

Please sign in to comment.