Talk to a socketio server
Project description
Smartphone Connector
This simple package exposes methods to interact with smartphones connected to a socket.io server instance.
Socket Server
This package talks with a socket.io server, which defines the following events and actions.
A simple socket io server implementing this interface can be seen on github.com/lebalz/socketio_server. A running server is currently (August 2020) deployed under io.lebalz.ch.
Events
device
{ 'deviceId': str, 'isController': bool, 'deviceNr': int, 'socketId': str }
devices
[ { 'deviceId': str, 'isController': bool, 'deviceNr': int, 'socketId': str }, ... ]
all_data
[ { 'deviceId': str, 'timeStamp': int, # time (seconds) since client loaded page, ... }, ... ]
new_data
{ 'deviceId': str, 'deviceNr': int, 'timeStamp': int, # time (seconds) since client loaded page, 'type': 'key' | 'acceleration' | 'gyro' | 'pointer' | 'notification' ... }
room_joined
{ 'deviceId': str, 'isController': bool, 'deviceNr': int, 'socketId': str }
room_left
{ 'deviceId': str, 'isController': bool, 'deviceNr': int, 'socketId': str }
error_msg
{ 'msg': str, 'err': dict | str }
Actions (for emitting)
emit('clear_data', { 'deviceId': str })
emit('new_device', { 'deviceId': str })
emit('get_all_data', { 'deviceId': str })
emit('get_devices', { 'deviceId': str })
emit('new_data', { 'deviceId': str, timeStamp: int, ... })
emit('new_data', { 'deviceNr': int, timeStamp: int, ... })
emit('join_room', { 'room': str })
emit('leave_room', { 'room': str })
Callbacks (when fired when event is received)
when a function is assigned to one of the following props, this function will be called with the received data as its first argument
on_key
on_pointer
on_acceleration
on_gyro
on_sensor
on_data
on_broadcast_data
on_all_data
on_device
on_devices
on_error
on_room_joined
on_room_left
e.g.
connector = Connector('https://io.lebalz.ch')
connector.on_key = lambda data: print('on key', data)
will print each received key
Helpers
To convert the timestamps (seconds since epoch) to a python timestamp
object, the function to_datetime
may be used.
Class ´Controller`
Attributes
data
data dictionary. key:device_id
, value: List containing all data messagesdevices
all devicesdevice
the connected device (self)server_url
the connected server. (readonly)device_id
the device_id connected to. (readonly)sio
the raw socket io clientroom_members
list of devices and scripts connected to this roomjoined_rooms
all rooms which this client gets events fromdevice_count
the count of all devices and scripts currently connected to the serverclient_count
the count of client devices connected to the server (e.g. Smartphones)room_member_count
count of other clients/scripts connected to this room. All these clients will receive the events of this room too.
Methods
-
emit(event, data={}, broadcast=False)
event
: name of the emitted eventdata
: whatever data you want to sendbroadcast
: when set to true, the event is broadcasted to all currently connected devices. Defaults toFalse
-
broadcast(data: {type: str})
broadcast's anew_data
event. The data must contain two fields:type
and a field equivalent to thetypes
value. e.g.connector.broadcast({'type': 'grid', 'grid': ['red']}) connector.broadcast({'type': 'color', 'color': 'red'})
-
clear_data()
clears all data on the server related to this `device_id`` -
all_broadcast_data(data_type: str = None) -> List[DataMsg]
returns all broadcasted data of the given type. When no type is provided, all broadcasted data is returned. -
latest_broadcast_data(data_type: str = None) -> DataMsg | None
returns the latest received data of the given type. None is returned when no data is present. -
all_data(data_type: str = None, device_id: str = None) -> List[DataMsg]
returns all data with the given type and from the given device_id. -
latest_data(data_type: str = None, device_id: str = None) -> DataMsg | None
returns the latest data (last received) with the given type and from the given device_id. -
set_grid(grid, device_id: str = None, device_nr: int = None, broadcast: bool = False)
sends anew_data
event with the given grid. the grid can be either a1D
or2D
array containing css colors. -
set_color(color: str, device_id: str = None, device_nr: int = None, broadcast: bool = False)
sets the color of the color panel -
disconnect()
-
join_room(deviceId: str)
joins the room of another device. This means that all events from this other device will be streamed to this client too. -
leave_room(deviceId: str)
leaves the room of this device. This means no events from this device are received.
Example
from smartphone_connecter import Connector
connector = Connector('https://io.lebalz.ch', 'FooBar')
# draw a 3x3 checker board
connector.set_grid([
['black','white','black'],
['white','black','white'],
['black','white','black']
], broadcast=True)
results on all devices in the following screen.
When broadcast
is set to False
(default), only the FooBar
devices display the checker board.
Package and upload to pip
@see this tutorial
rm -rf build/ dist/ smartphone_connector.egg-info/
python3 setup.py sdist bdist_wheel
python3 -m twine upload dist/*
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for smartphone-connector-0.0.7.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6b84ce0654d914c61a74a96c72bf6385835aa3f22bdd3df51068d3637a112653 |
|
MD5 | 5cdd27905886825dd731f2a5df4e6ec7 |
|
BLAKE2b-256 | f8c17c15d709668d5384e6e13018a37eac1abe227c0d4f14fc3758beda0e2acb |
Hashes for smartphone_connector-0.0.7-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ee667f9a492ab13f4c1662ffe30a1e9c5eae73686b0abd714044ec5071feef1f |
|
MD5 | 6120253e9b5d0fb4dde2c27e8f26782d |
|
BLAKE2b-256 | 1ad7812bc215ae7b6ef897adfdcaddb7934131aab0f6c172ede35a5ec64aca92 |