def add_sensors(): # Creating an instance of sensor my_camera = CameraStreaming(camera_no=0, name="camera", output_path="./output") # Creating an instance of device coordinator device_coordinator = DeviceCoordinator() # Adding sensor to device coordinator device_coordinator.add_devices([my_camera]) experiment_id = "p01" stimuli_id = "S00" input("Press a button to start data recording") # Starts data recording device_coordinator.dispatch(start_message(experiment_id, stimuli_id)) time.sleep(5) # Stops deta recording device_coordinator.dispatch(stop_message(experiment_id, stimuli_id)) time.sleep(0.5) # Terminate, This step is necessary to close the connection with added devices device_coordinator.terminate()
def simple_scenario(stimuli_path): # Reading image stimuli and assigning an ID to them based on their alphabetical order stimuli_list = os.listdir(stimuli_path) stimuli_list.sort() stimuli = {} i = 0 for item in stimuli_list: stimuli[i] = item i += 1 print("initializing") # Creating an instance of simmer3 my_shimmer = Shimmer3Streaming(name="Shimmer3_sensor", output_path="./output") # Creating an instance of camera. by uncommenting this line and adding it to the dive_coordinator # you can record video data as well # my_camera = CameraStreaming(0, name="camera", output_path="./output", ) # Creating an instance of device coordinator device_coordinator = DeviceCoordinator() # Adding sensor to device coordinator device_coordinator.add_devices([my_shimmer]) experiment_id = "p01" # A delay to be sure initialing devices have finished time.sleep(3) input("\nPress a key to run the scenario") for stimuli_id, stmulus_name in stimuli.items(): # Starts data recording by displaying the image device_coordinator.dispatch(start_message(experiment_id, stimuli_id)) # Displaying image may start with some miliseconds delay after data recording # because of GTK initialization in show_image_standalone. If this delay is important to you, # use other tools for displaying image stimuli # Since image is displaying in another thread we have to manually create the same delay in current # thread to record data for 10 seconds timeout = 5 stimulus = ImageStimulus(stimuli_id, os.path.join(stimuli_path, stmulus_name), 5) stimulus.show_standalone() time.sleep(timeout) # IF the stimuli is a video we are displaying stimuli as follows #stimulus = VideoStimulus(stimuli_id, os.path.join(stimuli_path, stmulus_name)) #stimulus.show() # Stops data recording by closing image device_coordinator.dispatch(stop_message(experiment_id, stimuli_id)) input("\nPress a key to continue") # Terminate, This step is necessary to close the connection with added devices device_coordinator.terminate()
def server(): ''' Starts a server and sends several messages and terminates ''' device_coordinator = DeviceCoordinator() socket_device = SocketNetworkDevice("0.0.0.0", 5002) device_coordinator.add_devices([socket_device]) time.sleep(2) input("Press enter to start sending marker") message = start_message("test", "00") device_coordinator.dispatch(message) time.sleep(2) message = stop_message("test", "00") device_coordinator.dispatch(message) time.sleep(2) message = start_message("test", "01") device_coordinator.dispatch(message) time.sleep(2) message = stop_message("test", "01") device_coordinator.dispatch(message) time.sleep(3) device_coordinator.terminate()
def test_http_network_device_happy_path(): coordinator = DeviceCoordinator() device = HttpNetworkDevice(["http://localhost:5003/"], name="test-http-network-device", timeout=15) coordinator.add_device(device) server = http.server.ThreadingHTTPServer(("localhost", 5003), Handler) threading.Thread(target=server.serve_forever, daemon=True).start() # To ensure device and server are started time.sleep(1) coordinator.dispatch(start_message("exp1", "stim1")) coordinator.dispatch(stop_message("exp1", "stim2")) # Should send TERMINATE message coordinator.terminate()
def __server(): ''' Starts a server and send several messages and terminates ''' device_coordinator = DeviceCoordinator() socket_device = SocketNetworkDevice("localhost", 5002) device_coordinator.add_devices([socket_device]) time.sleep(5) message = start_message("test", "00") device_coordinator.dispatch(message) time.sleep(2) message = stop_message("test", "00") device_coordinator.dispatch(message) time.sleep(2) message = start_message("test", "01") device_coordinator.dispatch(message) time.sleep(2) message = stop_message("test", "01") device_coordinator.dispatch(message) time.sleep(3) device_coordinator.terminate()
def test_system_health(mocked): '''Runs the whole system to roughly check everything is working together.''' # To ensure mocks are applied, we import these modules here. import octopus_sensing.devices.openbci_streaming as openbci_streaming import octopus_sensing.devices.shimmer3_streaming as shimmer3_streaming from octopus_sensing.device_coordinator import DeviceCoordinator from octopus_sensing.common.message_creators import start_message, stop_message, terminate_message from octopus_sensing.monitoring_endpoint import MonitoringEndpoint output_dir = tempfile.mkdtemp(prefix="octopus-sensing-test") coordinator = DeviceCoordinator() openbci = openbci_streaming.OpenBCIStreaming(name="eeg", output_path=output_dir) coordinator.add_device(openbci) shimmer = shimmer3_streaming.Shimmer3Streaming(name="shimmer", output_path=output_dir) coordinator.add_device(shimmer) monitoring_endpoint = MonitoringEndpoint(coordinator) monitoring_endpoint.start() try: coordinator.dispatch(start_message("int_test", "stimulus_1")) # Allowing data collection for five seconds time.sleep(5) coordinator.dispatch(stop_message("int_test", "stimulus_1")) http_client = http.client.HTTPConnection("127.0.0.1:9330") http_client.request("GET", "/") response = http_client.getresponse() assert response.status == 200 monitoring_data = pickle.loads(response.read()) assert isinstance(monitoring_data, dict) assert isinstance(monitoring_data["eeg"], list) # three seconds * data rate assert len(monitoring_data["eeg"]) == 3 * 128 assert len(monitoring_data["eeg"][0]) in (34, 35) assert len(monitoring_data["eeg"][-1]) in (34, 35) assert isinstance(monitoring_data["shimmer"], list) assert len(monitoring_data["shimmer"]) == 3 * 128 assert len(monitoring_data["shimmer"][0]) in (8, 9) assert len(monitoring_data["shimmer"][-1]) in (8, 9) finally: coordinator.dispatch(terminate_message()) monitoring_endpoint.stop() # To ensure termination is happened. time.sleep(0.5) eeg_output = os.path.join(output_dir, "eeg") assert os.path.exists(eeg_output) assert len(os.listdir(eeg_output)) == 1 assert os.listdir(eeg_output)[0] == "eeg-int_test.csv" shimmer_output = os.path.join(output_dir, "shimmer") assert os.path.exists(shimmer_output) assert len(os.listdir(shimmer_output)) == 1 assert os.listdir(shimmer_output)[0] == "shimmer-int_test.csv"