Skip to main content

Python implementation of Femtican client

Project description

lib-femtican.py

Environment setup

Consult DevBook section 10-50.

If you have conda installed correctly, run conda env create in the root project folder.

How to build this as a package

Change the package version in the pyproject.toml file according to semantic versioning rules.

Inside a conda environment, run:

python -m build . --sdist

Importing the package

pip install [path]/femtican-[version].tar.gz

Importing the package in Thonny IDE

Tools -> Manage packages -> Install from local file -> locate femtican-[version].tar.gz

Usage

Listening to broadcasts

import time
from femtican import FemticanClient

client = FemticanClient()
# default port is 60200. If you want to use a different IP or port, connect like this:
# e.g. client.connect("172.168.1.2", 50000)
client.connect("cmp.local")

def global_handler(device_id, register_id, value):
    match device_id, register_id:
        case 208, 49:
            print(f"Filtered data {value} from Device: 208, Register: 49")

client.broadcast_handler = global_handler
time.sleep(2)

client.disconnect()

Listening to broadcast (using per-register handlers)

import time
from femtican import FemticanClient

client = FemticanClient()
client.connect("cmp.local")  # default port: 60200

def aries_handler(device_id, register_id, value):
    print(f"{device_id}-{register_id}----{value} -- aries")

def random_handler(device_id, register_id, value):
    print(f"{device_id}-{register_id}----{value} -- random")

client.add_broadcast_handler(162, 50, aries_handler)
client.add_broadcast_handler(162, 48, random_handler)

# optional: trigger a few broadcasts (example)
for _ in range(10):
    client.trigger_broadcast(162, 50)
    time.sleep(0.05)

# wait for incoming broadcasts
time.sleep(2)

client.remove_broadcast_handler(162, 50)
client.remove_broadcast_handler(162, 48)

client.disconnect()

Triggering a broadcast on a device

from femtican import FemticanClient

client = FemticanClient()
client.connect("cmp.local")

client.trigger_broadcast(208, 49) # device_id, register_id

client.disconnect()

Setting a value to a register

from femtican import FemticanClient

client = FemticanClient()
client.connect("cmp.local")

client.set_register(208, 49, 0) # device_id, register_id, value. Value can be text (string), float or None type
client.set_register_index(208, 49, 1, 3.0) # device_id, register_id, index, value. Value can only be float type

client.disconnect()

Error handling

import time
from femtican import FemticanClient

client = FemticanClient()
client.connect("cmp.local")

def error_handler(error_message):
    print(f"Error: {error_message}")

client.error_handler = error_handler
time.sleep(2)

client.disconnect()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

femtican-0.3.7.tar.gz (11.7 kB view details)

Uploaded Source

File details

Details for the file femtican-0.3.7.tar.gz.

File metadata

  • Download URL: femtican-0.3.7.tar.gz
  • Upload date:
  • Size: 11.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.7

File hashes

Hashes for femtican-0.3.7.tar.gz
Algorithm Hash digest
SHA256 2afcb2e19f13263d1aa07f636989ae5e82965d01c48d2731d832dd282d8eae31
MD5 60369d39c2e62c41e57f64e2c9f30946
BLAKE2b-256 6a8c06f7dc4eee993ac70a4ad2671ecefc82c96cafc83e36785c7a0ac81c4f1d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page