def test_should_receive_data_from_attached_sensor(): snapper = Snapper() sensor = Sensor() snapper.add_sensor(sensor) sensor.publish(2) assert snapper.dataBuffer[sensor.uuid] == [2]
def test_get_all_relationship(): manager = Manager() sensor1 = Sensor() sensor2 = Sensor() manager.add_sensor(sensor1) manager.add_sensor(sensor2) assert manager.get_all_relationships() is not None
def test_get_relationship_from_sensors(): manager = Manager() sensor1 = Sensor() sensor2 = Sensor() manager.matrix.get_relationship_from_sensors = MagicMock() manager.get_relationship_from_sensors(sensor1, sensor2) manager.matrix.get_relationship_from_sensors.assert_called_with(sensor1, sensor2)
def test_should_use_none_for_sensors_not_pushing(): snapper = Snapper() sensor_a = Sensor() snapper.add_sensor(sensor_a) sensor_b = Sensor() snapper.add_sensor(sensor_b) sensor_b.publish(3) snapshot = snapper.get_snapshot() assert sensor_a.uuid in snapshot assert snapshot[sensor_a.uuid] is None assert round(snapshot[sensor_b.uuid] - 3, 5) == 0
def test_should_remove_sensor(): snapper = Snapper() sensor = Sensor() snapper.add_sensor(sensor) snapper.remove_sensor(sensor) assert snapper.sensors == []
def tick_loop(): sensors, sensor_data = formatted_csv_reader() for sensor_name in sensors: AEManager.add_sensor(Sensor(name=sensor_name)) subscribe_sensors() simtimestamp = sensor_data[0][0] lastTimestamp = sensor_data[-1][0] print("Starting at timestamp", simtimestamp) print("Ending at timestamp", lastTimestamp) print("Length of", lastTimestamp - simtimestamp) print("Starting Simulation") i = 0 # I'm indexing sensor_data instead of popping to save the rams while i < len(sensor_data): while simtimestamp > sensor_data[i][0]: timestamp, sensorIndex, value = sensor_data[i] #print("Publishing", value, "on sensor", sensorIndex, "at time", # timestamp) AEManager.sensors[sensorIndex].publish( value, timestamp=timestamp.timestamp()) i += 1 if i >= len(sensor_data): break simtimestamp += datetime.timedelta(hours=1) print("stepping to:", simtimestamp) io.sleep(.5) print("Simulation Complete")
def test_add_multiple_sensors(): manager = Manager() sensor1 = Sensor() sensor2 = Sensor() manager.add_sensor(sensor1) manager.add_sensor(sensor2) assert manager.sensors == [sensor1, sensor2] assert isinstance(manager.variables[0], Variable) assert isinstance(manager.variables[1], Variable) assert isinstance(manager.route_map[sensor1.uuid], Variable) assert isinstance(manager.route_map[sensor2.uuid], Variable) manager.remove_sensor(sensor1) manager.remove_sensor(sensor2) assert manager.sensors == [] assert manager.variables == [] assert manager.route_map == {}
def test_push_snapshot(): manager = Manager() sensor1 = Sensor() sensor2 = Sensor() manager.add_sensor(sensor1) manager.add_sensor(sensor2) variable1 = manager.route_map[sensor1.uuid] variable2 = manager.route_map[sensor2.uuid] variable1.on_data = MagicMock() variable2.on_data = MagicMock() snapshot = {"start": 20, "end": 40, sensor1.uuid: 1, sensor2.uuid: 2} manager.on_data(snapshot) variable1.on_data.assert_called_with(1, 20, 40) variable2.on_data.assert_called_with(2, 20, 40)
def test_should_generate_and_return_snapshot(): snapper = Snapper() sensor_a = Sensor() snapper.add_sensor(sensor_a) sensor_a.publish(1) sensor_b = Sensor() snapper.add_sensor(sensor_b) sensor_b.publish(3) snapshot = snapper.get_snapshot() assert round(snapshot[sensor_a.uuid] - 1, 5) == 0 assert round(snapshot[sensor_b.uuid] - 3, 5) == 0
def test_should_forward_snapshot_once_data_complete(): manager = Manager() manager.on_data = MagicMock() snapper = Snapper(manager) sensor1 = Sensor() snapper.add_sensor(sensor1) sensor1.publish(2, 1) sensor2 = Sensor() snapper.add_sensor(sensor2) sensor2.publish(3, 1) startTime = snapper.windowStart endTime = snapper.windowEnd # Force a snapshot publish by pushing value with time past window sensor2.publish(3, 11) manager.on_data.assert_called_with({ "start": startTime, "end": endTime, sensor1.uuid: 2, sensor2.uuid: 3 })
def test_add_and_remove_sensor_variable_pair(): manager = Manager() sensor = Sensor() manager.add_sensor(sensor) assert manager.sensors == [sensor] assert isinstance(manager.variables[0], Variable) assert isinstance(manager.route_map[sensor.uuid], Variable) manager.remove_sensor(sensor) assert manager.sensors == [] assert manager.variables == [] assert manager.route_map == {}
def get_manager_and_sensor_data_from_cvs(filename): # List of (datetime, value) but datetime is still a string sensors_data = [] with open(filename, newline='') as csvfile: csvreader = csv.reader(csvfile) for row in csvreader: sensors_data.append(row) manager = Manager() for _ in range(len(sensors_data)): sensor = Sensor() manager.add_sensor(sensor) return manager, sensors_data
def test_should_receive_data_from_multiple_sensors(): snapper = Snapper() sensor1 = Sensor() snapper.add_sensor(sensor1) sensor1.publish(2) sensor2 = Sensor() snapper.add_sensor(sensor2) startTime = snapper.windowStart endTime = snapper.windowEnd sensor2.publish(3) assert snapper.dataBuffer == {sensor1.uuid: [2], sensor2.uuid: [3]} assert snapper.get_snapshot() == { "start": startTime, "end": endTime, sensor1.uuid: 2, sensor2.uuid: 3 }
def test_should_properly_generate_snapshot_from_complete_data(): snapper = Snapper() sensor1 = Sensor() snapper.add_sensor(sensor1) sensor2 = Sensor() snapper.add_sensor(sensor2) sensor1.publish(2) assert snapper.snapshot == {} startTime = snapper.windowStart endTime = snapper.windowEnd sensor2.publish(3) assert snapper.get_snapshot() == { "start": startTime, "end": endTime, sensor1.uuid: 2, sensor2.uuid: 3 }
def test_should_store_sensor(): snapper = Snapper() sensor = Sensor() snapper.add_sensor(sensor) assert snapper.sensors == [sensor]
def test_unnamed_Sensor_returns_uuid(): s = Sensor() assert str(s) == str(s.uuid)
def test_Sensor_gets_named(): s = Sensor(name="Name") assert str(s) == "Name"
def test_aggregation_averaging(): snapper = Snapper() sensor_a = Sensor() snapper.add_sensor(sensor_a) sensor_b = Sensor() snapper.add_sensor(sensor_b) sensor_a.publish(1, 1) sensor_a.publish(5, 2) sensor_a.publish(6, 2) sensor_a.publish(4, 2) sensor_b.publish(3, 2) sensor_b.publish(7, 2) sensor_b.publish(2, 2) sensor_b.publish(4, 2) sensor_b.publish(5, 2) sensor_b.publish(2, 2) sensor_b.publish(6, 2) sensor_b.publish(9, 2) snapshot = snapper.get_snapshot() assert round(snapshot[sensor_a.uuid] - 4, 5) == 0 assert round(snapshot[sensor_b.uuid] - 4.75, 5) == 0
def test_should_generate_and_return_multiple_snapshots_correctly(): snapper = Snapper() sensor_a = Sensor() snapper.add_sensor(sensor_a) sensor_a.publish(1, 1) sensor_a.publish(5, 6) sensor_b = Sensor() snapper.add_sensor(sensor_b) sensor_b.publish(3, 7) sensor_b.publish(9, 8) snapshot1 = snapper.get_snapshot() assert round(snapshot1[sensor_a.uuid] - 3, 5) == 0 assert round(snapshot1[sensor_b.uuid] - 6, 5) == 0 sensor_a.publish(5, 11) sensor_a.publish(10, 12) sensor_a.publish(15, 13) sensor_b.publish(6, 13) sensor_b.publish(12, 14) sensor_b.publish(2, 15) sensor_b.publish(30, 16) snapshot2 = snapper.get_snapshot() assert round(snapshot2[sensor_a.uuid] - 10, 5) == 0 assert round(snapshot2[sensor_b.uuid] - 12.5, 5) == 0
def test_should_use_changed_time_window_for_snapshot_after_change(): manager = Manager() manager.on_data = MagicMock() snapper = Snapper(manager) sensor1 = Sensor() snapper.add_sensor(sensor1) sensor1.publish(2, 1) # Change window size in middle of data transmission snapper.set_window_size(15.0) sensor2 = Sensor() snapper.add_sensor(sensor2) sensor2.publish(3, 1) # Force a snapshot publish by pushing value with time past original window sensor2.publish(3, 11) # Should finish original window and push the resulting snapshot before # transitioning manager.on_data.assert_called_with({ 'start': 1, 'end': 10, sensor1.uuid: 2, sensor2.uuid: 3 }) # Push values within the new window sensor1.publish(3, 15) sensor2.publish(5, 16) # Force a snapshot push by publishing out of bounds data sensor1.publish(3, 26) manager.on_data.assert_called_with({ 'start': 10, 'end': 24, sensor1.uuid: 3, sensor2.uuid: 4 })