def __init__(self, *args, **kwargs): iot_content1 = entity.IoTContent("source 1", {"type": "temp sensor"}, {"value": 34}) iot_content2 = entity.IoTContent("source 2", {"type": "humidity sensor"}, {"value": 54}) iot_content3 = entity.IoTContent("source 3", {"type": "light sensor"}, {"value": 5}) self.req_id = "123456" self.data = {self.req_id: [iot_content1, iot_content2, iot_content3]}
def _query(self, query, wf_id=""): """ This function search the database to resolve the given query and put search result into a storage space, and return the identifier of that list of search results to the client """ # Extract the relevant query from the input query message query_id = query.query_ID query_content = query.query_content.get(self.query_type, {}) pprint(query_content) # Perform the search to find sensors result_curs = self.mongo_col.find(query_content) mongo_count = self.mongo_col.find(query_content).count() self.redis_client.set("DEBUG_reading_mongo_count:" + query_id, mongo_count) result_set = entity.ResultSet(query_ID=query_id, query_instance=query) self.redis_client.set("DEBUG_reading_init_result_count:" + query_id, len(result_set.results)) ite_num = 0 for result in result_curs: score = {"score": 100} iot_content = entity.IoTContent(iot_content_dict=result) result_set.add_IoTContent_score(iot_content.to_dict(), score) ite_num += 1 self.redis_client.set("DEBUG_reading_ite_count:" + query_id, ite_num) self.redis_client.set("DEBUG_reading_result_count:" + query_id, len(result_set.results)) # Store result set in Redis and return a key for client to retrieve results p_result_set = pickle.dumps(result_set) self.redis_client.set(self.redis_query_id_key + query_id, p_result_set) return query_id
def collect_metadata_from_sensor_url(self, sensor_url): """ Assume that the given source is compliant to OGC SensorThing API """ r = requests.get(sensor_url) sensor_metadata = r.json() try: del sensor_metadata["@iot.id"] del sensor_metadata["@iot.selfLink"] del sensor_metadata["*****@*****.**"] del sensor_metadata["*****@*****.**"] del sensor_metadata["*****@*****.**"] del sensor_metadata["*****@*****.**"] del sensor_metadata["FeatureOfInterest"]["@iot.id"] del sensor_metadata["FeatureOfInterest"]["@iot.selfLink"] del sensor_metadata["FeatureOfInterest"][ "*****@*****.**"] del sensor_metadata["ObservedProperty"]["@iot.id"] del sensor_metadata["ObservedProperty"]["@iot.selfLink"] del sensor_metadata["ObservedProperty"][ "*****@*****.**"] except KeyError: return None sensor_metadata_object = entity.IoTContent(sensor_url, {"type": "sensor_metadata"}, sensor_metadata) return sensor_metadata_object
def get_iot_contents(self, workflow_id = ""): """ This utility helps getting iot contents, either from the received request, or from the callback url available in the request """ callback_url = self.extract_callback_url() iot_contents = [] if callback_url != "not_callback": """ If the input message is a call back, send a request to the callback link to get needed data """ r = requests.get(callback_url, cookies = {"wf_id" : workflow_id}) result = r.json() iot_content_dicts = result["payload"]["iot_contents"] else: """ Otherwise, extract iot_contents from the request """ iot_content_dicts = self.extract_from_payload("iot_contents") for iot_content_dict in iot_content_dicts: iot_contents.append(entity.IoTContent(iot_content_dict = iot_content_dict)) return iot_contents
def get_detect_result(self, req_id = "", wf_id = ""): """ retrieve the list of IoT content sources """ key = "%s%s" % (self.redis_query_id_key, req_id) p_sensor_urls = self.redis_client.get(key) if p_sensor_urls is None: return None else: sensor_urls = pickle.loads(p_sensor_urls) sensor_url_objects = [] for sensor_url in sensor_urls: sensor_url_object = entity.IoTContent(sensor_url, {"type" : "sensor url"}, {"url" : sensor_url}) sensor_url_objects.append(sensor_url_object) return sensor_url_objects
def collect_reading_from_sensor_url(self, sensor_url): """ Assume that the given source is compliant to OGC SensorThing API """ observation_url = "%s/%s" % (sensor_url, "Observations") r = requests.get(observation_url) sensor_observation = r.json() try: del sensor_observation["@iot.id"] del sensor_observation["@iot.selfLink"] del sensor_observation["*****@*****.**"] del sensor_observation["*****@*****.**"] except KeyError: pass sensor_reading_object = entity.IoTContent(sensor_url, {"type" : "sensor_reading"}, sensor_observation) return sensor_reading_object
def process(self, result_sets): if not result_sets: return None else: agg_results_dict = {} """ Use the first set of results as the seed for comparison """ for result in result_sets[0].results: agg_results_dict[result[0]["ID"]] = [(result[0], result[1])] """ Compare each item in the remaining set with the seed. If the ID is matching, then append IoT content info and score to the existing record """ for result_set in result_sets[1:]: for result in result_set.results: if agg_results_dict.get(result[0]["ID"], None) is None: continue else: agg_results_dict[result[0]["ID"]].append((result[0], result[1])) """ Keep only the list with highest length """ agg_result_set = entity.ResultSet(query_ID = result_sets[0].query_ID, query_instance = result_sets[0].query_instance) for item in agg_results_dict.items(): if len(item[1]) == len(result_sets): """ This is an intersection of all result sets """ agg_iot_content = entity.IoTContent(ID=item[0], metadata = {"type" : "aggregated_iot_content"}, content = {"aggregated_content" : [pair[0] for pair in item[1]]}) agg_score = {"aggregated_score" : [pair[1] for pair in item[1]]} agg_result_set.add_IoTContent_score(IoTContent=agg_iot_content, score=agg_score) # Write to file to find out why this service not always return results # with open("test_aggregator.txt", "a") as f: # f.write("===========================\n") # f.write("in process()\n") # f.write("result_sets" + str(result_sets)) # f.write("agg_result_set" + str(agg_result_set)) return agg_result_set
def __init__(self, wsp_host_port=None, *args, **kwargs): super().__init__(*args, **kwargs) iot_source = entity.IoTContent("wsr_1", {"type": "sensor source"}, {"url": wsp_host_port}) self.sources = {"wsp": [iot_source]}