Common

Message

class octopus_sensing.common.message.Message(message_type: str, payload: Any, experiment_id: Optional[str] = None, stimulus_id: Optional[str] = None)[source]

Bases: object

Message class. It is being used for communicating between DeviceCoordinator and various devices. DeviceCoordinator uses message objects to inform devices about an event

Parameters:
  • type (str) – The type of message

  • payload (Any) – the message data that can have differnt values

  • experiment_id (str, default: None) – A unique ID for each participant and task in the study

  • stimulus_id (str, default: None) – A unique ID for each stimulus

Example

We can create a customized message using this class, or use prepared messages in the common/message_creators. In this example, we created an instance of message, which its type is start. When DeviceCoordinator dispath this message, it will be sent to all devices in its list. Using this message we inform all devices that start of stimulus_00 is happened.

>>> message = Message("START",
...                   "study_1_p10",
...                   "stimulus_00")
>>> device_coordinator.dispatch(message)

Predefined Messages

class octopus_sensing.common.message_creators.MessageType[source]

Bases: object

Predefined message types including:

1- START: start of a stimulus

2- STOP: End of each stimulus

3- TERMINATE: terminating the process of each device

4- SAVE: Save the data in the file

START = 'START'
STOP = 'STOP'
TERMINATE = 'TERMINATE'
SAVE = 'SAVE'
octopus_sensing.common.message_creators.start_message(experiment_id: str, stimulus_id: str, payload: Any = None)[source]

Creates a message to inform device of starting the stimulus

Parameters:
  • experiment_id (str, default: None) – A unique ID for each participant and task in the study

  • stimulus_id (str, default: None) – A unique ID for each stimulus

  • payload (Any, default: None) – the message data that can have differnt values

Returns:

message – A start message

Return type:

Message

Example

In this example, we created a stop message. When DeviceCoordinator dispath this message, it will be sent to all devices in its list. Using this message we inform all devices that stimulus stimulus_00 has started.

>>> message = start_message("study_1_p10", "stimulus_00")
>>> device_coordinator.dispatch(message)
octopus_sensing.common.message_creators.stop_message(experiment_id: str, stimulus_id: str)[source]

Creates a message to inform device of stopping the stimulus

Parameters:
  • experiment_id (str, default: None) – A unique ID for each participant and task in the study

  • stimulus_id (str, default: None) – A unique ID for each stimulus

Returns:

message – A start message

Return type:

Message

Example

In this example, we created a start message. When DeviceCoordinator dispath this message, it will be sent to all devices in its list. Using this message we inform all devices that stimulus stimulus_00 is finished.

>>> message = stop_message("study_1_p10", "stimulus_00")
>>> device_coordinator.dispatch(message)
octopus_sensing.common.message_creators.save_message(experiment_id: str)[source]

Creates a message to inform device of saving the data in the file This message is used when the data is saved in a continuous mode for partial save of data several times during the experiment. After receiving this message, the device will save the data in a file with the name and clear the data in memory. It will takes some time to save the data in the file, so after sending a message, wait for a short time to make sure IO is done. it is recommended to use this message several times in the long duration experiments to avoid losing data in case of unexpected termination of the program. The device will continue data recording by sending the next start message.

Parameters:

experiment_id (str, default: None) – A unique ID for each participant and task in the study

Returns:

message – A save message

Return type:

Message

Example

In this example, we created a save message. When DeviceCoordinator dispath this message, it will be sent to all devices in its list. Using this message we inform all devices to save the data in file.

>>> message = save_message("study_1_p10")
>>> device_coordinator.dispatch(message)
octopus_sensing.common.message_creators.terminate_message()[source]

Creates a message to inform device of terminating the program

Returns:

message – A terminate message

Return type:

Message

Example

In this example, we created a terminate message. When DeviceCoordinator dispath this message, it will be sent to all devices in its list. Using this message we inform all devices to terminate.

>>> message = terminate_message()
>>> device_coordinator.dispatch(message)