karen.Brain

Karen's input/response module.

The Brain leverages a JSON-based TCP server for communication with its input/output devices. The primary purpose of the brain is to accept data and send control commands to the devices to take action.

__init__(self, tcp_port=8080, hostname='localhost', ssl_cert_file=None, ssl_key_file=None, skill_folder=None) special

Brain Server Initialization

Parameters:
  • tcp_port (int) – The TCP port on which the brain's TCP server will listen for incoming connections

  • hostname (str) – The network hostname or IP address for the TCP server daemon

  • ssl_cert_file (str) – The path and file name of the SSL certificate (.crt) for secure communications

  • ssl_key_file (str) – The path and file name of the SSL private key (.key or .pem) for secure communications

  • skill_folder (str) – The path in which the skill modules are located.

Both the ssl_cert_file and ssl_key_file must be present in order for SSL to be leveraged.

addData(self, inType, inData)

Routine to add data to the brain. Stores the most recently added 50 items per type.

Parameters:
  • inType (str) – The data type under which to save the data

  • inData (object) – The data in which to be saved. The specific type (str, dict, list, etc.) is dependent on the type of data being saved and controlled by the caller.

Returns:
  • (bool) – True on success else will raise an exception.

ask(self, in_text, in_callback=None, timeout=0)

Method to create a action/response/reaction via voice interactions.

Parameters:
  • in_text (str) – The message to send to the speaker.

  • in_callback (function) – The function to call when a response is received.

  • timeout (int) – Number of seconds to wait on a response

Returns:
  • (bool) – True on success else will raise an exception.

say(self, text)

Method to send a message to the speaker to be spoken audibly.

Parameters:
  • text (str) – The message to send to the speaker.

Returns:
  • (bool) – True on success or False on failure.

sendRequestToDevices(self, path, payload, inType=None, friendlyName=None)

Sends a JSON request to one or more client devices.

Parameters:
  • path (str) – relative path to call (e.g. "control" or "data").

  • payload (object) – Object to be converted to JSON and sent in the body of the request

  • inType (str) – The type of device to deliver the request to (e.g. "karen.listener.Listener"). This will limit the request to only clients with the specified type of device attached. (optional)

  • friendlyName (str) – The friendly name of the client device (e.g. "living room"). This will limit the request to only clients that a device (of the specified type) with that friendly name. (optional)

Returns:
  • (bool) – True on success or False on failure.

setDataHandler(self, handlerType, handlerCallback, enableWebControl=True, friendlyName=None)

Sets the handler to be used for incoming data requests

Parameters:
  • handlerType (str) – The type of incoming control request to handle (e.g. "AUDIO_INPUT").

  • handlerCallback (function) – The function to call when the control request matches the handler type

  • enableWebControl (bool) – Indicates if the handler type should be listed in the web gui as an option in the drop down

  • friendlyName (str) – The user-readable name of the control. (optional)

Returns:
  • (bool) – True on success else will raise an exception.

setHandler(self, handlerType, handlerCallback, enableWebControl=True, friendlyName=None)

Sets the handler to be used for incoming control requests

Parameters:
  • handlerType (str) – The type of incoming control request to handle (e.g. "KILL").

  • handlerCallback (function) – The function to call when the control request matches the handler type

  • enableWebControl (bool) – Indicates if the handler type should be listed in the web gui as a button

  • friendlyName (str) – The user-readable name of the control. (optional)

Returns:
  • (bool) – True on success else will raise an exception.

start(self, useThreads=True)

Starts the brain TCP daemon, optionally using threads.

Parameters:
  • useThreads (bool) – Indicates if the brain should be started on a new thread.

Returns:
  • (bool) – True on success else will raise an exception.

stop(self)

Stops the brain TCP server daemon.

Returns:
  • (bool) – True on success else will raise an exception.

wait(self, seconds=0)

Waits for any active servers to complete before closing

Parameters:
  • seconds (int) – Number of seconds to wait before calling the "stop()" function

Returns:
  • (bool) – True on success else will raise an exception.