def process_event()

in functions/source/attributes-updater/handler.py [0:0]


def process_event(event, mappings):
    mapping = get_mapping(event, mappings)

    logger.info(f"Mapping: {mapping}")

    if mapping is None:
        return None

    asset = assets_cache.get(f"{mapping['Model']}-{event['reading']['id']}")

    logger.info(f"Asset {asset}")
    
    if asset is None:
        return None

    if asset.get('AssetData') is None:
        asset["AssetData"] = {}

    entries = []
    
    for prop in mapping['Mappings']:
        value = None

        if 'value' in prop:
            value = prop['value']
        elif 'path' in prop:
            jsonpath_expression = parse(prop['path'])
            match = jsonpath_expression.find(event)
            if len(match) == 0:
                continue
            else:
                value = match[0].value

        if prop['type'] == 'integer':
            value = int(value)
        elif prop['type'] == 'double':
            value = float(value)
        elif prop['type'] == 'boolean':
            value = (value in ['True', 'true'])
        else:
            value = value

        if asset.get('AssetData').get(prop['name']) == value:
            continue

        asset['AssetData'][prop['name']] = value

        entries.append(
            {
                'entryId': f"{event['reading']['id']}-{prop['name']}".replace(" ", ""),
                'propertyAlias': f"/urbanio/{mapping['Model']}/{event['reading']['id']}/{prop['name']}",
                'propertyValues': [
                    {
                        'value': {
                            f"{prop['type']}Value": value,
                        },
                        'timestamp': {
                            'timeInSeconds': int(datetime.datetime.now().timestamp()),
                        }
                    }
                ]
            }
        )

    if len(entries) > 0:
        if sitewise_asset.update_values(entries=entries):
            assets_cache.save(asset)