def test_get_simulated_camera(self): from cam_server import CamClient from cam_server.utils import get_host_port_from_stream_address from bsread import source, SUB # Change to match your camera server server_address = "http://0.0.0.0:8888" # Initialize the client. camera_client = CamClient(server_address) # Get stream address of simulation camera. Stream address in format tcp://hostname:port. camera_stream_address = camera_client.get_instance_stream("simulation") # Extract the stream hostname and port from the stream address. camera_host, camera_port = get_host_port_from_stream_address( camera_stream_address) # Subscribe to the stream. with source(host=camera_host, port=camera_port, mode=SUB) as stream: # Receive next message. data = stream.receive() image_width = data.data.data["width"].value image_height = data.data.data["height"].value image_bytes = data.data.data["image"].value print("Image size: %d x %d" % (image_width, image_height)) print("Image data: %s" % image_bytes) x_size, y_size = get_simulated_camera().get_geometry() self.assertIsNotNone(data) self.assertEqual(image_width, x_size) self.assertIsNotNone(image_height, y_size)
def get_axis(screen): camera_client = CamClient() camera_stream_adress = camera_client.get_instance_stream(screen) host, port = get_host_port_from_stream_address(camera_stream_adress) max_tries = 3 for _try in range(max_tries): try: with source(host=host, port=port, mode=SUB) as stream: data = stream.receive() print('Received image') x_axis = np.array(data.data.data['x_axis'].value) * 1e-6 y_axis = np.array(data.data.data['y_axis'].value) * 1e-6 except Exception as e: print('Try %i' % _try, e) pass else: break else: raise return x_axis, y_axis
class CameraClientProxyTest(unittest.TestCase): def setUp(self): test_base_dir = os.path.split(os.path.abspath(__file__))[0] self.config_folder = os.path.join(test_base_dir, "camera_config/") self.user_scripts_folder = os.path.join(test_base_dir, "user_scripts/") self.host = "0.0.0.0" self.cam_worker_port = 8880 self.cam_manager_port = 8888 cam_server_address = "http://%s:%s" % (self.host, self.cam_worker_port) self.process_camserver = Process(target=start_camera_worker, args=(self.host, self.cam_worker_port, self.user_scripts_folder)) self.process_camserver.start() self.cam_proxy_host = "0.0.0.0" self.process_camproxy = Process(target=start_camera_manager, args=(self.host, self.cam_manager_port, cam_server_address, self.config_folder, self.user_scripts_folder)) self.process_camproxy.start() sleep(1.0) # Give it some time to start. server_address = "http://%s:%s" % (self.host, self.cam_manager_port) self.client = CamClient(server_address) def tearDown(self): test_cleanup([self.client], [self.process_camproxy,self.process_camserver ], [ os.path.join(self.config_folder, "testing_camera.json"), os.path.join(self.config_folder, "simulation_temp.json") , ]) def test_client(self): server_info = self.client.get_server_info() self.assertIsNot(server_info["active_instances"], "There should be no running instances.") expected_cameras = set(["camera_example_1", "camera_example_2", "camera_example_3", "camera_example_4", "simulation", "simulation2"]) print (self.client.get_cameras()) #self.assertSetEqual(set(self.client.get_cameras()), expected_cameras, "Not getting all expected cameras") for camera in expected_cameras: self.assertIn(camera, set(self.client.get_cameras()), "Not getting expected camera: " + camera) print (self.client.get_camera_aliases()) print(self.client.get_camera_groups()) camera_stream_address = self.client.get_instance_stream("simulation") self.assertTrue(bool(camera_stream_address), "Camera stream address cannot be empty.") self.assertTrue(self.client.is_instance_running("simulation"), "Simulation camera not present in server info.") # Check if we can connect to the stream and receive data (in less than 2 seconds). host, port = get_host_port_from_stream_address(camera_stream_address) with source(host=host, port=port, receive_timeout=2000, mode=SUB) as stream: data = stream.receive() self.assertIsNotNone(data, "Received data was none.") required_fields = config.CAMERA_STREAM_REQUIRED_FIELDS self.assertSetEqual(required_fields, set(data.data.data.keys()), "Required fields missing.") image = data.data.data["image"].value x_size, y_size = get_simulated_camera().get_geometry() self.assertListEqual(list(image.shape), [y_size, x_size], "Original and received image are not the same.") self.assertEqual(data.data.data["width"].value, x_size, "Width not correct.") self.assertEqual(data.data.data["height"].value, y_size, "Height not correct.") # Stop the simulation instance. self.client.stop_instance("simulation") self.assertTrue(not self.client.is_instance_running("simulation"), "Camera simulation did not stop.") self.client.get_instance_stream("simulation") self.assertTrue(self.client.is_instance_running("simulation"), "Camera simulation did not start.") self.client.stop_all_instances() self.assertTrue(not self.client.is_instance_running("simulation"), "Camera simulation did not stop.") example_1_config = self.client.get_camera_config("camera_example_1") self.assertTrue(bool(example_1_config), "Cannot retrieve config.") # Change the name to reflect tha camera. example_1_config["name"] = "testing_camera" self.client.set_camera_config("testing_camera", example_1_config) testing_camera_config = self.client.get_camera_config("testing_camera") self.assertDictEqual(example_1_config, testing_camera_config, "Saved and loaded configs are not the same.") geometry = self.client.get_camera_geometry("simulation") simulated_camera = get_simulated_camera() size_x, size_y = simulated_camera.get_geometry() self.assertListEqual(geometry, [size_x, size_y], 'The geometry of the simulated camera is not correct.') self.assertTrue("testing_camera" in self.client.get_cameras(), "Testing camera should be present.") self.client.delete_camera_config("testing_camera") self.assertTrue("testing_camera" not in self.client.get_cameras(), "Testing camera should not be present.") # Test if it fails quickly enough. with self.assertRaisesRegex(ValueError, "Camera with prefix EPICS_example_1 is offline"): self.client.get_instance_stream("camera_example_1") self.assertTrue(self.client.is_camera_online("simulation"), "Simulation should be always online") self.assertFalse(self.client.is_camera_online("camera_example_1"), "Epics not working in this tests.") self.client.set_camera_config("simulation_temp", self.client.get_camera_config("simulation")) stream_address = self.client.get_instance_stream("simulation_temp") camera_host, camera_port = get_host_port_from_stream_address(stream_address) sim_x, sim_y = get_simulated_camera().get_geometry() instance_info = self.client.get_server_info()["active_instances"]["simulation_temp"] self.assertTrue("last_start_time" in instance_info) self.assertTrue("statistics" in instance_info) # Collect from the pipeline. with source(host=camera_host, port=camera_port, mode=SUB) as stream: data = stream.receive() x_size = data.data.data["width"].value y_size = data.data.data["height"].value self.assertEqual(x_size, sim_x) self.assertEqual(y_size, sim_y) x_axis_1 = data.data.data["x_axis"].value y_axis_1 = data.data.data["y_axis"].value self.assertEqual(x_axis_1.shape[0], sim_x) self.assertEqual(y_axis_1.shape[0], sim_y) camera_config = self.client.get_camera_config("simulation_temp") camera_config["rotate"] = 1 self.client.set_camera_config("simulation_temp", camera_config) sleep(0.5) # Collect from the pipeline. with source(host=camera_host, port=camera_port, mode=SUB) as stream: data = stream.receive() x_size = data.data.data["width"].value y_size = data.data.data["height"].value # We rotate the image for 90 degrees - X and Y size should be inverted. self.assertEqual(x_size, sim_y) self.assertEqual(y_size, sim_x) x_axis_2 = data.data.data["x_axis"].value y_axis_2 = data.data.data["y_axis"].value # We rotate the image for 90 degrees - X and Y size should be inverted. self.assertEqual(x_axis_2.shape[0], sim_y) self.assertEqual(y_axis_2.shape[0], sim_x) self.client.delete_camera_config("simulation_temp") image = self.client.get_camera_image("simulation") self.assertGreater(len(image.content), 0) image = self.client.get_camera_image_bytes("simulation") dtype = image["dtype"] shape = image["shape"] bytes = base64.b64decode(image["bytes"].encode()) x_size, y_size = get_simulated_camera().get_geometry() self.assertEqual(shape, [y_size, x_size]) image_array = numpy.frombuffer(bytes, dtype=dtype).reshape(shape) self.assertIsNotNone(image_array) #Check logs self.assertIsInstance(self.client.get_logs(False), list) self.assertIsInstance(self.client.get_logs(True), str) self.client.stop_all_instances()
class CameraClientProxyTest(unittest.TestCase): def setUp(self): test_base_dir = os.path.split(os.path.abspath(__file__))[0] self.config_folder = os.path.join(test_base_dir, "camera_config/") self.user_scripts_folder = os.path.join(test_base_dir, "user_scripts/") self.host = "0.0.0.0" self.cam_server_ports = [8880, 8881] self.cam_manager_port = 8888 self.cam_server_address = [] self.process_camserver = [] port_range = config.CAMERA_STREAM_PORT_RANGE for port in self.cam_server_ports: print(port, is_port_available(port)) for p in self.cam_server_ports: port_range = [port_range[0] + 10000, port_range[1] + 10000] self.cam_server_address.append("http://%s:%s" % (self.host, p)) process = Process(target=start_camera_worker, args=(self.host, p, self.user_scripts_folder, None, port_range)) self.process_camserver.append(process) process.start() self.cam_proxy_host = "0.0.0.0" self.process_camproxy = Process( target=start_camera_manager, args=(self.host, self.cam_manager_port, ",".join(self.cam_server_address), self.config_folder, self.user_scripts_folder)) self.process_camproxy.start() sleep(1.0) # Give it some time to start. server_address = "http://%s:%s" % (self.host, self.cam_manager_port) self.client = CamClient(server_address) self.proxy_client = ProxyClient(server_address) def tearDown(self): test_cleanup([self.client], [ self.process_camproxy, ] + self.process_camserver, []) def test_manager(self): stream_address_1 = self.client.get_instance_stream("simulation") stream_address_2 = self.client.get_instance_stream("simulation2") #Check if streams are alive camera_host, camera_port = get_host_port_from_stream_address( stream_address_1) with source(host=camera_host, port=camera_port, mode=SUB) as stream: data = stream.receive() for key in ["image", "width", "height"]: self.assertIn(key, data.data.data.keys()) camera_host, camera_port = get_host_port_from_stream_address( stream_address_2) with source(host=camera_host, port=camera_port, mode=SUB) as stream: data = stream.receive() for key in ["image", "width", "height"]: self.assertIn(key, data.data.data.keys()) server_info = self.proxy_client.get_servers_info() status_info = self.proxy_client.get_status_info() instance_info = self.proxy_client.get_instances_info() #Check if streams are equally distributed self.assertEqual(server_info[self.cam_server_address[0]]["load"], 1) self.assertEqual(server_info[self.cam_server_address[1]]["load"], 1) # Check if instance information is available for each server instance for instance in server_info[self.cam_server_address[0]]["instances"]: self.assertIn(instance, instance_info) for instance in server_info[self.cam_server_address[1]]["instances"]: self.assertIn(instance, instance_info) self.client.stop_all_instances() #Server Config self.assertEqual( self.proxy_client.get_config(), { 'http://0.0.0.0:8880': { 'expanding': True }, 'http://0.0.0.0:8881': { 'expanding': True } }) self.proxy_client.set_config({ 'http://0.0.0.0:8880': { 'expanding': True }, 'http://0.0.0.0:8881': { "instances": ["DUMMY"], 'expanding': False } }) stream_address_1 = self.client.get_instance_stream("simulation") stream_address_2 = self.client.get_instance_stream("simulation2") #Check if streams are alive camera_host, camera_port = get_host_port_from_stream_address( stream_address_1) camera_host, camera_port = get_host_port_from_stream_address( stream_address_2) server_info = self.proxy_client.get_servers_info() status_info = self.proxy_client.get_status_info() instance_info = self.proxy_client.get_instances_info() self.assertEqual(server_info[self.cam_server_address[0]]["load"], 2) self.assertEqual(server_info[self.cam_server_address[1]]["load"], 0) """