Skip to content

nikhilsharma99/DataConverterServerless

Repository files navigation

Project Overview

This project implements an AWS Lambda function designed to convert and process battery data from a specific device, transforming raw hexadecimal payloads into human-readable formats. The function primarily handles the conversion of little-endian encoded data to big-endian format for ease of processing and interprets the data to extract relevant battery status information. The final output is a structured JSON object containing the battery's operational state, time, state of charge, and temperature.

Code Description

  1. Imports:

    • json: Used for encoding and decoding JSON objects.
    • logging: Provides a flexible framework for emitting log messages from Python programs.
    • sys: Used to write processed data directly to the standard output stream.
  2. Function: convertbatterydata(payload)

    • Purpose: This function is the core processing unit that decodes the incoming battery data payload.
    • State Mapping: The function defines a dictionary state_map to convert numeric state codes into readable battery states like "power off," "charge," etc.
    • Endian Conversion:
      • convert_little_endian_to_big_indian_encoding(hex): A helper function that reverses the byte order of the hexadecimal string, converting it from little-endian to big-endian format.
    • Payload Processing:
      • The payload is first converted to big-endian encoding.
      • The function then extracts specific information by interpreting certain segments of the hexadecimal string:
        • Type: Extracted from the 15th character of the payload.
        • Time: Extracted from characters 7 to 15.
        • State: Mapped from the 6th character using state_map.
        • State of Charge: Extracted from characters 4 to 6, and halved for accuracy.
        • Temperature: Extracted from characters 2 to 4, halved, and adjusted by subtracting 20 to get the final temperature value.
    • Return: The processed data is encapsulated in a dictionary format with keys time, state, state_of_charge, and temperature.
  3. Function: battery_device_wrapper(device, payload)

    • Purpose: This function wraps the convertbatterydata function, adding device-specific information to the processed data.
    • Process:
      • It calls convertbatterydata(payload) to process the payload.
      • It adds the device identifier to the resulting dictionary.
    • Return: A dictionary containing both the device ID and the processed battery data.
  4. Function: handler(event, context)

    • Purpose: This is the AWS Lambda handler function that acts as the entry point for the Lambda execution.
    • Process:
      • It extracts the device and payload from the incoming event.
      • Processes the data using battery_device_wrapper.
      • Logs the processed data using sys.stdout.write.
    • Return: The processed data is returned as a JSON object.
  5. Main Block:

    • Purpose: The if __name__ == "__main__": pass block is included for future extensibility. Currently, it does not execute any code when the script is run directly, as the Lambda function is designed to be triggered by an event, not executed as a standalone script.

This Lambda function is suitable for processing real-time battery data in applications such as IoT device monitoring, where accurate and timely information on battery status is crucial.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published