Skip to content

Commit

Permalink
Add geolocation feature
Browse files Browse the repository at this point in the history
  • Loading branch information
selengalp committed Jan 25, 2022
1 parent 626816e commit 965fff0
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions core/modules/monitoring.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
MONITOR_PATH = f"{USER_PATH}/.core/monitor.yaml"
CONFIG_PATH = f"{USER_PATH}/.core/configs/config.yaml"
CONFIGS_REQUEST_PATH = f"{USER_PATH}/.core/configs/request"
GEOLOCATION_PATH = f"{USER_PATH}/.core/geolocation.yaml"


def _check_configuration_requests(mqtt_client, configs):
Expand Down Expand Up @@ -61,6 +62,7 @@ def loop(mqttClient, configs):
last_monitoring_data = {}
last_system_data = {}
last_config_data = {}
last_geolocation_data = {}


def callback(client, userdata, msg):
Expand Down Expand Up @@ -95,6 +97,11 @@ def callback(client, userdata, msg):
last_config_data.update(data["data"])
logger.debug("Updated last config data")

elif data["type"] == "data_geolocation":
data["data"].pop("last_update", None)
last_geolocation_data.update(data["data"])
logger.debug("Updated last geolocation data")



configs["callbacks"].append(callback)
Expand Down Expand Up @@ -233,6 +240,44 @@ def callback(client, userdata, msg):
logger.debug("Skipping config data, couldn't find any changes.")


# GEOLOCATION DATA
new_geolocation_data = None
try:
with open(GEOLOCATION_PATH) as geolocation_data:
new_geolocation_data = yaml.load(geolocation_data, Loader=Loader) or {}
except Exception:
logger.exception("Geolocation data not exists!")

if new_geolocation_data:
new_geolocation_data.pop("last_update", None)
data_to_send = {}

for key, value in new_geolocation_data.items():
last_value = last_geolocation_data.get(key, "N/A")

if value != last_value:
data_to_send[key] = value

if data_to_send:
mid = uuid4().hex[-4:]

message_body = dict(
type="data_geolocation",
data=data_to_send,
mid=mid
)

message_response = mqttClient.publish(
f"device/{configs['token']}/hive",
json.dumps(message_body)
)
print(message_body, message_response)
message_cache[mid] = message_body

logger.debug("Sending new geolocation data")
else:
logger.debug("Skipping geolocation data, couldn't find any changes.")


time.sleep(CONTROL_INTERVAL)

Expand Down

0 comments on commit 965fff0

Please sign in to comment.