Server

class c104.Server

This class represents a local server and provides access to meta information and containing stations

__init__(self: c104.Server, ip: str = '0.0.0.0', port: int = 2404, tick_rate_ms: int = 100, select_timeout_ms=100, max_connections: int = 0, transport_security: c104.TransportSecurity = None) None

create a new 104er server

Parameters:
  • ip (str) – listening server ip address

  • port (int) – listening server port

  • tick_rate_ms (int) – server thread update interval

  • select_timeout_ms (int) – execution for points in SELECT_AND_EXECUTE mode must arrive within this interval to succeed

  • max_connections (int) – maximum number of clients allowed to connect

  • transport_security (c104.TransportSecurity, optional) – TLS configuration object

Example

>>> my_server = c104.Server(ip="0.0.0.0", port=2404, tick_rate_ms=100, select_timeout_ms=100, max_connections=0)
add_station(self: c104.Server, common_address: int) c104.Station | None

add a new station to this server and return the new station object

Parameters:

common_address (int) – station common address (value between 1 and 65534)

Returns:

station object, if station was added, else None

Return type:

c104.Station, optional

Example

>>> station_1 = my_server.add_station(common_address=15)
get_station(self: c104.Server, common_address: int) c104.Station | None

get a station object via common address

Parameters:

common_address (int) – station common address (value between 1 and 65534)

Returns:

station object, if found, else None

Return type:

c104.Station, optional

Example

>>> station_2 = my_server.get_connection(common_address=14)
on_clock_sync(self: c104.Server, callable: collections.abc.Callable[[c104.Server, str, datetime.datetime], c104.ResponseState]) None

set python callback that will be executed on incoming clock sync command

Parameters:

callable (collections.abc.Callable[[c104.Server, str, datetime.datetime], c104.ResponseState]) – callback function reference

Return type:

None

Raises:
  • ValueError – callable signature does not match exactly

  • **Callable signature**

  • Callable Parameters

  • -------------------

  • server – c104.Server: server instance

  • ip – str: client connection request ip

  • date_time – datetime.datetime: clients current clock time

  • Callable Returns

  • ----------------

  • c104.ResponseState – success or failure of clock sync command

Example

>>> import datetime
>>>
>>> def sv_on_clock_sync(server: c104.Server, ip: str, date_time: datetime.datetime) -> c104.ResponseState:
>>>     print("->@| Time {0} from {1} | SERVER {2}:{3}".format(date_time, ip, server.ip, server.port))
>>>     return c104.ResponseState.SUCCESS
>>>
>>> my_server.on_clock_sync(callable=sv_on_clock_sync)
on_connect(self: c104.Server, callable: collections.abc.Callable[[c104.Server, ip], bool]) None

set python callback that will be executed on incoming connection requests

Parameters:

callable (collections.abc.Callable[[c104.Server, ip], bool]) – callback function reference

Return type:

None

Raises:
  • ValueError – callable signature does not match exactly

  • **Callable signature**

  • Callable Parameters

  • -------------------

  • server – c104.Server: server instance

  • ip – str: client connection request ip

  • Callable Returns

  • ----------------

  • bool – accept or reject the connection request

Example

>>> def sv_on_connect(server: c104.Server, ip: str) -> bool:
>>>     print("<->| {0} | SERVER {1}:{2}".format(ip, server.ip, server.port))
>>>     return ip == "127.0.0.1"
>>>
>>> my_server.on_connect(callable=sv_on_connect)
on_receive_raw(self: c104.Server, callable: collections.abc.Callable[[c104.Server, bytes], None]) None

set python callback that will be executed on incoming message

Parameters:

callable (collections.abc.Callable[[c104.Server, bytes], None]) – callback function reference

Return type:

None

Raises:
  • ValueError – callable signature does not match exactly

  • **Callable signature**

  • Callable Parameters

  • -------------------

  • server – c104.Server: server instance

  • data – bytes: raw message bytes

  • Callable Returns

  • ----------------

  • None

Example

>>> def sv_on_receive_raw(server: c104.Server, data: bytes) -> None:
>>>     print("-->| {1} [{0}] | SERVER {2}:{3}".format(data.hex(), c104.explain_bytes(apdu=data), server.ip, server.port))
>>>
>>> my_server.on_receive_raw(callable=sv_on_receive_raw)
on_send_raw(self: c104.Server, callable: collections.abc.Callable[[c104.Server, bytes], None]) None

set python callback that will be executed on outgoing message

Parameters:

callable (collections.abc.Callable[[c104.Server, bytes], None]) – callback function reference

Return type:

None

Raises:
  • ValueError – callable signature does not match exactly

  • **Callable signature**

  • Callable Parameters

  • -------------------

  • server – c104.Server: server instance

  • data – bytes: raw message bytes

  • Callable Returns

  • ----------------

  • None

Example

>>> def sv_on_send_raw(server: c104.Server, data: bytes) -> None:
>>>     print("<--| {1} [{0}] | SERVER {2}:{3}".format(data.hex(), c104.explain_bytes(apdu=data), server.ip, server.port))
>>>
>>> my_server.on_send_raw(callable=sv_on_send_raw)
on_unexpected_message(self: c104.Server, callable: collections.abc.Callable[[c104.Server, c104.IncomingMessage, c104.Umc], None]) None

set python callback that will be executed on unexpected incoming messages

Parameters:

callable (collections.abc.Callable[[c104.Server, c104.IncomingMessage, c104.Umc], None]) – callback function reference

Return type:

None

Raises:
  • ValueError – callable signature does not match exactly

  • **Callable signature**

  • Callable Parameters

  • -------------------

  • server – c104.Server: server instance

  • message – c104.IncomingMessage: incoming message

  • cause – c104.Umc: unexpected message cause

  • Callable Returns

  • ----------------

  • None

Example

>>> def sv_on_unexpected_message(server: c104.Server, message: c104.IncomingMessage, cause: c104.Umc) -> None:
>>>     print("->?| {1} from CLIENT OA {0} | SERVER {2}:{3}".format(message.originator_address, cause, server.ip, server.port))
>>>
>>> my_server.on_unexpected_message(callable=sv_on_unexpected_message)
remove_station(self: c104.Server, common_address: int) bool

removes an existing station from this server

Parameters:

common_address (int) – station common address (value between 1 and 65534)

Returns:

True if the station was successfully removed, otherwise False.

Return type:

bool

Example

>>> station_3.remove_station(common_address=12)
start(self: c104.Server) None

open local server socket for incoming connections

Raises:

RuntimeError – server thread failed to start

Example

>>> my_server.start()
stop(self: c104.Server) None

stop local server socket

Example

>>> my_server.stop()
transmit_batch(self: c104.Server, batch: c104.Batch) bool

transmit a batch object

Parameters:

batch (c104.Batch) – batch object to transmit

Returns:

send success

Return type:

bool

Example

>>> success = my_server.transmit_batch(c104.Batch([point1, point2, point3]))
property active_connection_count

get number of active (open and not muted) connections to clients (read-only)

Type:

int

property has_active_connections

test if server has active (open and not muted) connections to clients (read-only)

Type:

bool

property has_open_connections

test if server has open connections to clients (read-only)

Type:

bool

property has_stations

test if server has at least one station (read-only)

Type:

bool

property ip

ip address the server will accept connections on, “0.0.0.0” = any (read-only)

Type:

str

property is_running

test if server is running (read-only)

Type:

bool

property max_connections

maximum number of open connections, 0 = no limit

Type:

int

property open_connection_count

represents the number of open connections to clients (read-only)

Type:

int

property port

port number the server will accept connections on (read-only)

Type:

int

property protocol_parameters

read and update protocol parameters

Type:

c104.ProtocolParameters

property stations

list of all local Station objects (read-only)

Type:

list[c104.Station]

property tick_rate_ms

the servers tick rate in milliseconds (read-only)

Type:

int