def showsubscriptions(): subscriptions = semanticEnrichment.get_subscriptions() return render_template( 'subscriptions.html', subscriptions=subscriptions.values(), id=str(uuid.uuid4()), endpoint=Config.getEnvironmentVariable('SE_CALLBACK'))
def _initialise_subscriptions(subscriptions): # first get active subscriptions _get_active_subscriptions(subscriptions) # iterate list and check for IotStream, StreamObservation, Sensor subscription, if not found subscribe for t in (ngsi_parser.NGSI_Type.IoTStream, ngsi_parser.NGSI_Type.Sensor, ngsi_parser.NGSI_Type.StreamObservation): subscribe = True for key, value in subscriptions.items(): sub = value.subscription try: if ngsi_parser.get_type(sub['entities'][0]) is t: # it is of type IotStream, check if it is our endpoint if sub['notification']['endpoint'][ 'uri'] == Config.getEnvironmentVariable( 'SE_CALLBACK'): logger.debug("Subscription for " + str(t) + " already existing!") subscribe = False break except KeyError: pass if subscribe: logger.debug("Initialise system with subscription for " + str(t)) _subscribe_forTypeId(t, None, subscriptions)
def _subscribe_forTypeId(ngsi_type, entityId, sublist): logger.debug("Subscribe for " + str(ngsi_type) + " " + str(entityId)) # check if subscription already in sublist # solution is not optimal... but no other option at the moment if entityId: for key, value in sublist.items(): sub = value.subscription try: tmposid = sub['entities'][0]['id'] if tmposid == entityId: logger.debug("Subscription for " + tmposid + " already existing!") return except KeyError: pass # create subscription filename = "" if ngsi_type is ngsi_parser.NGSI_Type.Sensor: filename = 'static/json/subscription_sensor.json' elif ngsi_type is ngsi_parser.NGSI_Type.IoTStream: filename = 'static/json/subscription_iotstream.json' elif ngsi_type is ngsi_parser.NGSI_Type.StreamObservation: filename = 'static/json/subscription_streamobservation.json' with open(filename) as jFile: subscription = json.load(jFile) subscription['id'] = subscription['id'] + str(uuid.uuid4()) # replace callback subscription['notification']['endpoint'][ 'uri'] = Config.getEnvironmentVariable('SE_CALLBACK') # set entity to subscribe to if entityId: subscription['entities'][0]['id'] = entityId _add_subscription(subscription, sublist)
def _add_subscription(subscription, subscriptions): # subscribe to ngsi-ld endpoint sub = Subscription(subscription['id'], Config.getEnvironmentVariable('NGSI_ADDRESS'), subscription) if ngsi_add_subscription(subscription) is not None: subscriptions[sub.id] = sub
def ngsi_add_subscription(subscription): server_url = Config.getEnvironmentVariable( 'NGSI_ADDRESS') + "/ngsi-ld/v1/subscriptions/" r = requests.post(server_url, json=subscription, headers=headers) logger.info("Adding subscription: " + str(r.status_code) + " " + r.text) if r.status_code != 201: logger.debug("error creating subscription: " + r.text) return None return r.text
def _create_ngsi_entity(ngsi_msg): try: logger.debug("Save entity to ngsi broker: " + str(ngsi_msg)) url = Config.getEnvironmentVariable( 'NGSI_ADDRESS') + "/ngsi-ld/v1/entities/" r = requests.post(url, json=ngsi_msg, headers=headers) if r.status_code == 409: logger.debug("Entity exists, try to update/add attributes") _add_ngsi_attribute(ngsi_msg, ngsi_msg['id']) except requests.exceptions.ConnectionError as e: logger.error("Error while creating ngsi entity" + str(e))
def handlejsonsubscription(data, address, subscriptions): try: if data['notification']['endpoint'][ 'uri'] == Config.getEnvironmentVariable('SE_CALLBACK'): # if data['id'].startswith('SE_', data['id'].rfind(':') + 1): sub = Subscription(data['id'], address, data) subscriptions[sub.id] = sub logger.info("Found active subscription: " + str(data)) else: logger.info("not our subscription") except KeyError: return None
def _get_active_subscriptions(subscriptions): # get old subscriptions for semantic enrichment (starting with 'SE_') server_url = Config.getEnvironmentVariable( 'NGSI_ADDRESS') + "/ngsi-ld/v1/subscriptions/" try: r = requests.get(server_url, headers=headers) if r.status_code == 200: if isinstance(r.json(), list): for data in r.json(): handlejsonsubscription( data, Config.getEnvironmentVariable('NGSI_ADDRESS'), subscriptions) if isinstance(r.json(), dict): handlejsonsubscription( r.json(), Config.getEnvironmentVariable('NGSI_ADDRESS'), subscriptions) else: logger.error("Error getting active subscriptions: " + r.text + str(r.status_code)) except Exception as e: logger.error("Error getting active subscriptions: " + str(e))
def get_entity(entitiyid): try: url = Config.getEnvironmentVariable( 'NGSI_ADDRESS') + "/ngsi-ld/v1/entities/" + entitiyid r = requests.get(url, headers=headers) if r.status_code != 200: logger.error("Error requesting entity " + entitiyid + ": " + r.text) return None return r.json() except requests.exceptions.ConnectionError as e: logger.error("Error while getting entity " + entitiyid + ": " + str(e))
def _add_ngsi_attribute(ngsi_msg, eid): try: logger.debug("Add ngsi attribute to entity " + eid + ":" + str(ngsi_msg)) url = Config.getEnvironmentVariable( 'NGSI_ADDRESS') + "/ngsi-ld/v1/entities/" + eid + "/attrs" r = requests.post(url, json=ngsi_msg, headers=headers) if r.status_code not in (204, 207): logger.debug("Attribute exists, patch it") requests.patch(url, json=ngsi_msg, headers=headers) except requests.exceptions.ConnectionError as e: logger.error("Error while adding attribute to ngsi entity" + str(e))
def get_entities(entitytype, limit, offset): try: url = Config.getEnvironmentVariable( 'NGSI_ADDRESS') + "/ngsi-ld/v1/entities/" params = {'type': entitytype, 'limit': limit, 'offset': offset} r = requests.get(url, headers=headers, params=params) if r.status_code != 200: logger.error("Error requesting entities of type " + entitytype + ": " + r.text) return None return r.json() except requests.exceptions.ConnectionError as e: logger.error("Error while getting entities of type " + entitytype + ": " + str(e))
data = [data] for entity in data: # notify about new iotstream, sensor, streamobservation, initialise qoi system if new stream semanticEnrichment.notify_datasource(entity) return Response('OK', status=200) @bp2.route('/', methods=['GET']) def status(): return "running" # return redirect(url_for('semanticenrichment.index')) @bp.route('/status', methods=['GET']) def status(): return "running" app = Flask(__name__) app.secret_key = 'e3645c25b6d5bf67ae6da68c824e43b530e0cb43b0b9432b' app.register_blueprint(bp, url_prefix='/semanticenrichment') app.register_blueprint(bp2, url_prefix='/') app.jinja_env.filters['datetime'] = format_datetime if __name__ == "__main__": app.run(host=Config.getEnvironmentVariable('SE_HOST'), port=int(Config.getEnvironmentVariable('SE_PORT')), debug=False)