Skip to content
This repository was archived by the owner on Apr 5, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Dockerfile.template
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ RUN install_packages \
kmod \
libiio0 \
libiio-utils \
python3-libiio
python3-libiio

WORKDIR /usr/src/app

Expand Down
21 changes: 20 additions & 1 deletion sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import idetect
from reading import IIO_READER
from information import Information
import requests


def mqtt_detect():
Expand Down Expand Up @@ -95,15 +96,25 @@ def background_web(server_socket):
client_connection.sendall(response.encode())
client_connection.close()


# sends to fleet analytics transport service
def sent_to_fleet_analytics(endpoint_url, message):
try:
print("Sending log {} to fleet analytics at {}.".format(message, endpoint_url))
res = requests.post(endpoint_url, json = message)
print("Send status was {}".format(res))
except Exception as e:
print("Error connecting to mqtt. ({0})".format(str(e)))

if __name__ == "__main__":

mqtt_address = os.getenv('MQTT_ADDRESS', 'none')
use_httpserver = os.getenv('ALWAYS_USE_HTTPSERVER', 0)
publish_interval = os.getenv('MQTT_PUB_INTERVAL', '8')
publish_topic = os.getenv('MQTT_PUB_TOPIC', 'sensors')
publish_fleet_analytics = os.getenv('PUBLISH_FLEET_ANALYTICS', 0)

FLEET_ANALYTICS_PORT = 5000

try:
interval = float(publish_interval)
except Exception as e:
Expand Down Expand Up @@ -152,4 +163,12 @@ def background_web(server_socket):
while True:
if mqtt_address != "none":
client.publish(publish_topic, json.dumps(balenasense.sample()))
if publish_fleet_analytics:
FLEET_ANALYTICS_SERVER_HOST = "0.0.0.0"
# just a placeholder if we end up exposing multiple transport services
FLEET_ANALYICS_SERVICE_NAME = "kafka-rest"
sent_to_fleet_analytics("{}:{}/{}".format(FLEET_ANALYTICS_SERVER_HOST, FLEET_ANALYTICS_PORT, FLEET_ANALYICS_SERVICE_NAME),
json.dumps(balenasense.sample()))

time.sleep(interval)