Skip to content

Commit

Permalink
Merge pull request #111 from rascaraficci/bugfix/device-update-event
Browse files Browse the repository at this point in the history
Changed to publish event for devices when they are indirectly updated
  • Loading branch information
rascaraficci committed Mar 18, 2019
2 parents 4490451 + fd0e336 commit dfad1c8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
13 changes: 10 additions & 3 deletions DeviceManager/TemplateHandler.py
Expand Up @@ -6,7 +6,7 @@
from sqlalchemy.sql import text, collate, func

from DeviceManager.DatabaseHandler import db
from DeviceManager.DatabaseModels import handle_consistency_exception, assert_template_exists
from DeviceManager.DatabaseModels import handle_consistency_exception, assert_template_exists, assert_device_exists
from DeviceManager.DatabaseModels import DeviceTemplate, DeviceAttr, DeviceTemplateMap
from DeviceManager.SerializationModels import template_list_schema, template_schema
from DeviceManager.SerializationModels import attr_list_schema, attr_schema
Expand All @@ -20,6 +20,10 @@

from DeviceManager.Logger import Log
from datetime import datetime

from DeviceManager.BackendHandler import KafkaHandler
from DeviceManager.DeviceHandler import serialize_full_device

import time
import json

Expand Down Expand Up @@ -264,7 +268,7 @@ def update_attr(attrs_from_db, attrs_from_request):
attrs_from_db.static_value = attrs_from_request.get('static_value', None)

def validate_attr(attr_from_request):
attr_schema.load(attr_from_request)
attr_schema.load(attr_from_request)

def analyze_attrs(attrs_from_db, attrs_from_request, parentAttr=None):
for attr_from_db in attrs_from_db:
Expand Down Expand Up @@ -292,7 +296,7 @@ def analyze_attrs(attrs_from_db, attrs_from_request, parentAttr=None):
LOGGER.debug(f" Adding new attribute {attr}")
if "id" in attr:
del attr["id"]
child = DeviceAttr(template=old, **attr)
child = DeviceAttr(template=old, **attr)
db.session.add(child)
if "metadata" in attr and attr["metadata"] is not None:
for metadata in attr["metadata"]:
Expand All @@ -313,7 +317,10 @@ def analyze_attrs(attrs_from_db, attrs_from_request, parentAttr=None):
.all()

affected_devices = []
kafka_handler = KafkaHandler()
for device in affected:
orm_device = assert_device_exists(device.device_id)
kafka_handler.update(serialize_full_device(orm_device, service), meta={"service": service})
affected_devices.append(device.device_id)

event = {
Expand Down
3 changes: 2 additions & 1 deletion docs/kafka-messages.rst
Expand Up @@ -108,7 +108,8 @@ And its attributes are:
Update message
--------------

This message is published whenever a new device is updated.
This message is published whenever a new device is directly or indirectly updated.
The indirectly case happens when a template associated with the device is updated.
Its payload looks very similar to device creation:

.. code-block:: json
Expand Down

0 comments on commit dfad1c8

Please sign in to comment.