English 中文(简体)
dec式有效载荷
原标题:Hexadecimal format payload decoding

我有一个回家任务,我需要代用六dec有效载荷。 我一直在试图对有效载荷进行编码,但无法具体为时地产生正确的产出。 我很赞赏在编码上提供援助。

Description about the task:

为了监测电池的健康状况,我们决定从电池中向云层发送数据。 这一数据以六dec形式传送,并在我们的阿拉伯首脑会议账户收到。

这些数据作为六dec星传输。 每个有效载荷由8个 by组成。 由于空间优化,信息不按部就班。 实地可以按部就开始。 因此,我们需要借机操作,以编码有效载荷。 有效载荷没有在低端签署和编码。 下表介绍了有效载荷及其轨道位置所载数据领域。

enter image description here enter image description here

For instance, type is encoded on 4 bits in the first byte. state of charge is encoded on 8 bits (1 byte) on the 6th byte.

Time: time represents the timestamp of the data. It is defined in seconds since UNIX epoch.

State: state is astring, with the followingreci Value:

0: "power off"

1. “权力”

2. “收费”

3. “收费”

4: “全部”

5: "host mode"

6: "shutdown"

7: "error"

8: “未定义”

www.un.org/Depts/DGACM/index_spanish.htm 责任国: 责任状态是电池的收费。 该浮标值为0至100,精度为0.5。 为了将它储存起来,它增加了2倍。

电池温度:电池温度表示电池温度。 数值在20至100之间可能有所不同。 精确度为0.5。 为了储存它,我们增加了20个,将其增加到2个。

Sample Test data

投入:F1E63676C75000

产出:{“时间”:1668181615,“状态”:“频率”:99.5,“温度”:20.0}

http://www.ohchr.org。

import base64
import struct
import json
from datetime import datetime


def lambda_handler(event):
    # Extract device and payload from the event
    device = event["device"]
    payload_hex = event["payload"]

    # Convert hexadecimal payload to bytes
    payload_bytes = bytes.fromhex(payload_hex)

    # Unpack the payload using struct
    unpacked_data = struct.unpack( <I2Bh , payload_bytes)

    # Extract individual fields
    time = unpacked_data[0]

    state = unpacked_data[1]
    state = (state >> 4) & 0x0F

    state_of_charge = unpacked_data[2] / 2.0
    temperature = (unpacked_data[3] / 2.0) - 20.0

    # Mapping state values to corresponding strings
    state_mapping = {
    0: "power off",
    1. “权力”,
    2. “收费”,
    3. “收费”,
    4: “全部”,
    5: "host mode",
    6: "shutdown",
    7: "error",
    8: “未定义”
    }

    # Create the output dictionary
    output_data = {
    "device": device,
    "time": time,
    "state": state_mapping.get(state, "unknown"),
    "state_of_charge": round(state_of_charge, 1),
    "temperature": round(temperature, 1)
    }

    # Log the output data to stdout
    print(json.dumps(output_data))

event = { device :  device1 ,  payload :  6188293726C75C00 }
lambda_handler(event)

我在努力争取获得正确产出,因为当时不仅依赖基于上述假设的未加装的数据。

问题回答

您可使用rel=“nofollow noreferer”>ctypes, 用于支持借方的机构。 在该类别中添加物,以计算:

import ctypes as ct
import struct

class _Data(ct.Structure):
    # Bitfield definitions
    _fields_ = (( type , ct.c_uint64, 4),
                ( time , ct.c_uint64, 32),
                ( state , ct.c_uint64, 4),
                ( soc , ct.c_uint64, 8),
                ( temp , ct.c_uint64, 8))

    states = ( power off ,  power on ,  discharge ,  charge ,
               charge complete ,  host mode ,  shutdown ,
               error ,  undefined )

class Data(ct.Union):
    _fields_ = (( _u , ct.c_uint64),
                ( _d , _Data))

    def __init__(self, data):
        # Take raw byte data and unpack as 64-bit little-endian
        self._u = struct.unpack( <Q , data)[0]

    @property
    def type(self):
        return self._d.type

    @property
    def time(self):
        return self._d.time

    @property
    def state(self):
        try:
            return self._d.states[self._d.state]
        except IndexError:
            return  invalid 

    @property
    def state_of_charge(self):
        return self._d.soc / 2

    @property
    def battery_temperature(self):
        return self._d.temp / 2 - 20

    def __repr__(self):
        return (f Data(type={self.type},  
                     f time={self.time},  
                     f state={self.state!r},  
                     f state_of_charge={self.state_of_charge},  
                     f temperature={self.battery_temperature}) )

    def as_dict(self):
        return { type : self.type,
                 time : self.time,
                 state : self.state,
                 state_of_charge : self.state_of_charge,
                 temperature : self.battery_temperature}

data = Data(bytes.fromhex( F1E6E63676C75000 ))
print(data)
print(data.as_dict())

产出:

Data(type=1, time=1668181615, state= error , state_of_charge=99.5, temperature=20.0)
{ type : 1,  time : 1668181615,  state :  error ,  state_of_charge : 99.5,  temperature : 20.0}




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签