def simulator_loop(): conf_manager = ConfigManager() frost_config = conf_manager.get_specific_configuration("sensorthings") frost_url = frost_config.pop("url") frost_port = frost_config.pop("port") query_sleep = frost_config.pop("query_sleep") simulator_config = conf_manager.get_specific_configuration("simulator") mu = simulator_config.pop("mu") sigma = simulator_config.pop("sigma") frost_base_url = "http://{}:{}/FROST-Server/v1.0/".format(frost_url, frost_port) frost_observation_url = frost_base_url + "Observations" datastream_id = create_datastream(frost_base_url) with open("schemas/tiltmeter_observation_template.json") as f: template = f.read() template = template.replace("{datastream_id}", datastream_id) while True: axis_one = random.gauss(mu, sigma) axis_two = random.gauss(mu, sigma) dt = datetime.datetime.now() new_data = template.replace("{axis_one}", str(axis_one)) new_data = new_data.replace("{axis_two}", str(axis_two)) new_data = new_data.replace("{datetime}", dt.strftime("%Y-%m-%dT%H:%M:%SZ")) # TODO: find the way to prevent from having to write-read the file every iteration with open("schemas/tiltmeter_sensorthings.json", "w") as f: f.write(new_data) with open("schemas/tiltmeter_sensorthings.json", "rb") as f: response = requests.post(frost_observation_url, headers={"content-type": "application/json"}, data=f) logging.info(response.request) time.sleep(query_sleep)
import pytest from mbconnector import mbcos import subsystem from configmanager import ConfigManager schema_file_ds = "tests/unit/schema__ds.json" schema_file_ms = "tests/unit/schema__ms.json" conf_manager = ConfigManager() mobility_config = conf_manager.get_specific_configuration("mobility").copy() cos_config = conf_manager.get_specific_configuration("cos") mobility_url = mobility_config.pop("url") @pytest.mark.parametrize("data, sensor_id, schema_file", [([1, 2, [3, 4]], "_ms_", schema_file_ms), ([1, 2, 3], "_ms_", schema_file_ds), ([1, 2, 3], "_ds_", schema_file_ms), (1, "_ms_", schema_file_ds), (1, "_ds_", schema_file_ms)]) def test_unsupported_data(data, sensor_id, schema_file): cos_client = mbcos.CosObject(cos_config['object_type'], mobility_url, **mobility_config) cos_client.create_object() cos_client.set_schema_from_file(schema_file) ret = subsystem.generate_schema(data, cos_config, sensor_id)
import subsystem import pkg_resources tiltmeter_cos_schema = pkg_resources.resource_filename( "pws_common", "tiltmeter_cos_schema.json") logger = logging.getLogger("connector_main") app = Flask(__name__) cos_client = None conf_manager = ConfigManager() pws_cos_api = conf_manager.get_specific_configuration("pws_cos_api") @app.route("/<nw_id>/write", methods=["POST"]) def rx_gw_data(nw_id): timestamp = datetime.datetime.strftime( datetime.datetime.utcnow().replace(tzinfo=pytz.utc), "%Y-%m-%dT%H:%M:%SZ") token = request.args.get("auth") logger.info("Received data from LoRa gateway in network [" + nw_id + "] with token [" + token + "]") data = subsystem.get_data(request.json, timestamp) if data is not None: sensor_name = data["loadsensing_tiltmeter_id"]["value"]
def connector_loop(): cos_client = {} conf_manager = ConfigManager() mobility_config = conf_manager.get_specific_configuration( "mobility").copy() cos_config = conf_manager.get_specific_configuration("cos") frost_config = conf_manager.get_specific_configuration("sensorthings") mobility_url = mobility_config.pop("url") frost_url = frost_config.pop("url") frost_port = frost_config.pop("port") query_sleep = frost_config.pop("query_sleep") sensorthings_path = frost_config.pop("schema_dir") pws_cos_api = conf_manager.get_specific_configuration("pws_cos_api") last_t = subsystem.get_most_recent_timestamp(pws_cos_api) while True: time.sleep( query_sleep) # Timer to prevent over-loading the FROST server responses = subsystem.query_frost(frost_url, frost_port, last_t) status_code = responses.status_code if status_code != 200: logger.info("Received {} status code".format(status_code)) logger.info("Response content: {}".format(responses.content)) continue responses_dict = json.loads(responses.content) for response in responses_dict["value"]: # TODO: handle phenomenonTime not present t = dateutil.parser.parse(response["phenomenonTime"] ) # Converts from ISO 8601 timeformat if last_t is None or t > last_t: # Update last_t in first occation and if newer data received last_t = t sensor_id = subsystem.get_sensor_id(response) data = response["result"] epoch = response["phenomenonTime"] if sensor_id not in cos_client: _cos_config = dict(cos_config) _cos_config[ "object_type"] = _cos_config["object_type"] + sensor_id schema_file = subsystem.generate_schema( data, _cos_config, sensor_id) cos_client[sensor_id] = mbcos.CosObject( _cos_config['object_type'], mobility_url, **mobility_config) cos_client[sensor_id].create_object() cos_client[sensor_id].set_schema_from_file(schema_file) else: schema_file = os.path.join(sensorthings_path, "schema_{}.json".format(sensor_id)) formatted_data = subsystem.format_data(data, epoch, schema_file, _cos_config) send_data(cos_client[sensor_id], [formatted_data])