def test_last_sensors_updates(self): import flask from core.data.sensors import get_sensors_arrays_with_children with self.__class__.app.app_context( ), self.__class__.app.test_request_context(): response = self.test_client.get( flask.url_for('webapp_api.last_sensors_updates'), follow_redirects=True) self.assertEqual(response.status_code, 200) json_response = json.loads(response.data) sensors_arrays = get_sensors_arrays_with_children() for sensors_array_key, sensors_array in sensors_arrays.items(): for child in sensors_array.get("children"): child_name = child.get("name") [child_last_reading] = [ reading for reading in json_response if reading.get("sensor") == child_name ] if child.get("sensor_type") == "temperature": self.assertGreaterEqual( child_last_reading.get("last_value"), 10) self.assertLessEqual( child_last_reading.get("last_value"), 30) else: self.assertGreaterEqual( child_last_reading.get("last_value"), 100) self.assertLessEqual( child_last_reading.get("last_value"), 130)
def test_sensors(self): from core.data.sensors import get_sensors_arrays_with_children import flask with self.__class__.app.app_context( ), self.__class__.app.test_request_context(): sensors_arrays = get_sensors_arrays_with_children() response = self.test_client.get('/sensors.html', follow_redirects=True) self.assertEqual(response.status_code, 200) response_txt = str(response.data) for sensors_array_key, sensors_array in sensors_arrays.items(): sensors_array_name = sensors_array.get("name") self.assertIn( flask.url_for('webapp.room_overview', sensors_array_name=sensors_array_name), response_txt) for child in sensors_array.get("children"): child_name = child.get("name") self.assertIn( flask.url_for('webapp.room_overview', selected_sensor=child_name), response_txt)
def test_dump_all_aggregated_data(self): import flask from core.data.sensors import get_sensors_arrays_with_children with self.__class__.app.app_context( ), self.__class__.app.test_request_context(): response = self.test_client.get(flask.url_for( 'webapp_api.dump_all_aggregated_data', group_by="1h"), follow_redirects=True) self.assertEqual(response.status_code, 200) json_response = json.loads(response.data) sensors_arrays = get_sensors_arrays_with_children() for sensors_array_key, sensors_array in sensors_arrays.items(): for child in sensors_array.get("children"): child_name = child.get("name") self.assertIn(child_name, json_response.get("sensors_data")) child_means = json_response.get("sensors_data").get( child_name).get("means") self.assertEqual(len(child_means), 25) self.assertEqual( len([v for v in child_means if v is not None]), 13)
def test_sensor_data(self): from core.data.sensors import get_sensors_arrays_with_children import flask with self.__class__.app.app_context( ), self.__class__.app.test_request_context(): sensors_arrays = get_sensors_arrays_with_children() for sensors_array_key, sensors_array in sensors_arrays.items(): for child in sensors_array.get("children"): child_name = child.get("name") response = self.test_client.get(flask.url_for( 'webapp_api.sensor_data', sensor_name=child_name), follow_redirects=True) self.assertEqual(response.status_code, 200) json_dict = json.loads(response.data) self.assertEqual(len(json_dict.get("values")), 241) if child.get("sensor_type") == "temperature": self.assertGreaterEqual( numpy.mean(json_dict.get("values")), 10) self.assertLessEqual( numpy.mean(json_dict.get("values")), 30) else: self.assertGreaterEqual( numpy.mean(json_dict.get("values")), 100) self.assertLessEqual( numpy.mean(json_dict.get("values")), 130)
def detect_unresponsive_temperature_sensors(): print("Detecting unresponsive temperature detectors") last_updates = db_last_sensors_updates() sensors_arrays_with_children = get_sensors_arrays_with_children() now_time = time.time() for sensors_array_key, sensors_array in sensors_arrays_with_children.items( ): for child in sensors_array["children"]: child_last_update = [ x for x in last_updates if x.get("sensor") == child.get("name") ] if len(child_last_update) > 0: last_update_since_epoch = int( time.mktime( parser.parse(child_last_update[0] ["time"]).timetuple())) - time.timezone time_since_last_update_secs = now_time - last_update_since_epoch if time_since_last_update_secs > 40: print( "%s is unresponsive since %s seconds: I increment his error counter" % (child.get("name"), time_since_last_update_secs)) redis_increment_sensor_error_count(child.get("name"))
def test_sensors(self): from core.data.sensors import get_sensors_arrays_with_children import flask with self.__class__.app.app_context( ), self.__class__.app.test_request_context(): response = self.test_client.get( flask.url_for('webapp_api.sensors'), follow_redirects=True) self.assertEqual(response.status_code, 200) json_dict = json.loads(response.data) sensors_arrays = get_sensors_arrays_with_children() for sensors_array_key, sensors_array in sensors_arrays.items(): for child in sensors_array.get("children"): child_name = child.get("name") self.assertIn(child_name, json_dict.get("sensors"))
def sensors(): last_updates = db_last_sensors_updates() sensors_arrays_with_children = get_sensors_arrays_with_children() now_time = time.time() for (sensors_array_name, sensors_array) in sensors_arrays_with_children.items(): for child in sensors_array["children"]: child_last_update = [x for x in last_updates if x.get("sensor") == child.get("name")] if len(child_last_update) > 0: last_update_since_epoch = int( time.mktime(parser.parse(child_last_update[0]["time"]).timetuple())) - time.timezone time_since_last_update_secs = now_time - last_update_since_epoch displayed_time_text = _display_time(time_since_last_update_secs) child_last_update[0]["nice_last_udpate_text"] = displayed_time_text child["last_update"] = child_last_update[0] else: child["last_update"] = None return render_template("sensors.html.jinja2", last_updates=last_updates, sensors_arrays_with_children=sensors_arrays_with_children)
def test_sensors_array(self): from core.data.sensors import get_sensors_arrays_with_children import flask sensors_arrays = get_sensors_arrays_with_children() all_sensors = _load_sensors_data() sensors = [*all_sensors.get("temperature").values()] \ + [*all_sensors.get("socomec").values()] \ + [*all_sensors.get("flukso").values()] \ + [*all_sensors.get("pdus").values()] with self.__class__.app.app_context( ), self.__class__.app.test_request_context(): for sensors_array_key, sensors_array in sensors_arrays.items(): sensors_array_name = sensors_array.get("name") response = self.test_client.get(flask.url_for( 'webapp.sensors_array', sensors_array_name=sensors_array_name), follow_redirects=True) self.assertEqual(response.status_code, 200) response_txt = str(response.data) self.assertIn( f"""<title>View sensor array linked to {sensors_array_name}</title>""", response_txt) for child in sensors_array.get("children"): child_name = child.get("name") response = self.test_client.get(flask.url_for( 'webapp.sensors_array', sensors_array_name=sensors_array_name, selected_sensor=child_name), follow_redirects=True) self.assertEqual(response.status_code, 200) response_txt = str(response.data) self.assertIn( f"""data-marker="station" data-labelpos="e">{child_name}</li>""", response_txt)
def test_room_overview(self): from core.data.sensors import get_sensors_arrays_with_children import flask sensors_arrays = get_sensors_arrays_with_children() all_sensors = _load_sensors_data() sensors = [*all_sensors.get("temperature").values()] \ + [*all_sensors.get("socomec").values()] \ + [*all_sensors.get("flukso").values()] \ + [*all_sensors.get("pdus").values()] with self.__class__.app.app_context( ), self.__class__.app.test_request_context(): for sensors_array_key, sensors_array in sensors_arrays.items(): sensors_array_name = sensors_array.get("name") response = self.test_client.get(flask.url_for( 'webapp.room_overview', sensors_array_name=sensors_array_name), follow_redirects=True) self.assertEqual(response.status_code, 200) response_txt = str(response.data) self.assertIn( f"""<iframe style="border: none; width: 100%;" id="mapIframe" src="/sensors_array.html/{ sensors_array_name }" onload="iframeLoaded()"></iframe>""", response_txt) for child in sensors_array.get("children"): child_name = child.get("name") response = self.test_client.get(flask.url_for( 'webapp.room_overview', selected_sensor=child_name), follow_redirects=True) self.assertEqual(response.status_code, 200) response_txt = str(response.data) self.assertIn( f"""<iframe style="border: none; width: 100%;" id="mapIframe" src="/sensors_array.html/{ sensors_array_name }/{ child_name }" onload="iframeLoaded()"></iframe>""", response_txt)