Skip to content

Commit

Permalink
Add client functionality #225
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickjaigner committed Jul 15, 2024
1 parent 121d65d commit a31200f
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 16 deletions.
8 changes: 4 additions & 4 deletions config/thingsboard.default.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"host": "10.0.0.4",
"port": 8883,
"user": "...",
"password": "..."
"host": "0.0.0.0",
"access_token": "...",
"ca_cert": null,
"cert_file": null
}
56 changes: 51 additions & 5 deletions packages/core/threads/thingsboard_thread.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from .abstract_thread import AbstractThread
from packages.core import types, utils, interfaces
from tb_device_mqtt import TBDeviceMqttClient # type: ignore
import threading
import time

Expand All @@ -10,11 +11,11 @@ class ThingsBoardThread(AbstractThread):
def should_be_running(config: types.Config) -> bool:
"""Based on the config, should the thread be running or not?"""

# only upload when upload is configured
# only publish data when Thingsboard is configured
if config.thingsboard is None:
return False

# don't upload in test mode
# don't publish in test mode
if config.general.test_mode:
return False

Expand All @@ -30,10 +31,55 @@ def main(headless: bool = False) -> None:
"""Main entrypoint of the thread. In headless mode,
don't write to log files but print to console."""

logger = utils.Logger(origin="upload", just_print=headless)
logger = utils.Logger(origin="thingsboard", just_print=headless)
config = types.Config.load()
assert config.thingsboard is not None

# initialize MQTT client
client = TBDeviceMqttClient(
config.thingsboard.host, username=config.thingsboard.access_token
)

# connect to MQTT broker
if config.thingsboard.ca_cert and config.thingsboard.cert_file:
client.connect(
tls=True,
ca_certs=config.thingsboard.ca_cert,
cert_file=config.thingsboard.cert_file,
)
else:
client.connect()

logger.info("Succesfully connected to Thingsboard Broker.")

while True:
print(1)
time.sleep(60)
# Read latest config
config = types.Config.load()
if not ThingsBoardThread.should_be_running(config):
logger.info("ThingsboardThread shall not be running anymore.")
client.disconnect()
return

# publish if client is connected
if not client.is_connected():
logger.warning(
"Client is currently not connected. Waiting for reconnect."
)
else:
# read latest state file
current_state = interfaces.StateInterface.load_state()

# TODO: inset state file parameters into telemetry payload
telemetry_with_ts = {
"ts": int(round(time.time() * 1000)),
"values": {"temperature": 42.1, "humidity": 70},
}

try:
# Sending telemetry without checking the delivery status (QoS0)
client.send_telemetry(telemetry_with_ts)
except Exception as e:
logger.exception(e)
logger.info("Failed to publish last telemetry data.")

time.sleep(config.general.seconds_per_core_interval)
12 changes: 6 additions & 6 deletions packages/core/types/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,16 +259,16 @@ class PartialUploadConfig(StricterBaseModel):

class ThingsBoardConfig(StricterBaseModel):
host: str
port: int
user: str
password: str
access_token: int
ca_cert: Optional[str]
cert_file: Optional[str]


class PartialThingsBoardConfig(StricterBaseModel):
host: Optional[str] = None
port: Optional[int] = None
user: Optional[str] = None
password: Optional[str] = None
access_token: Optional[int] = None
ca_cert: Optional[str] = None
cert_file: Optional[str] = None


class Config(StricterBaseModel):
Expand Down
36 changes: 35 additions & 1 deletion pdm.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ dependencies = [
"filelock==3.14.0",
"psutil==5.9.8",
"tb-mqtt-client>=1.9.9",
"mmh3>=4.1.0",
"pymmh3>=0.0.5",
]
requires-python = "==3.10.*"
readme = "README.md"
Expand Down

0 comments on commit a31200f

Please sign in to comment.