refactor: put datatype formatting in a separate function
This commit is contained in:
		
							parent
							
								
									a1f8328b50
								
							
						
					
					
						commit
						620cdaf880
					
				
					 1 changed files with 42 additions and 22 deletions
				
			
		
							
								
								
									
										64
									
								
								itho-wpu.py
									
										
									
									
									
								
							
							
						
						
									
										64
									
								
								itho-wpu.py
									
										
									
									
									
								
							| 
						 | 
					@ -259,35 +259,55 @@ def process_datalog(response, wpu):
 | 
				
			||||||
    measurements = {}
 | 
					    measurements = {}
 | 
				
			||||||
    for d in datalog:
 | 
					    for d in datalog:
 | 
				
			||||||
        if d.type == 0x0 or d.type == 0xC:
 | 
					        if d.type == 0x0 or d.type == 0xC:
 | 
				
			||||||
            m = message[d.index : d.index + 1]  # noqa: E203
 | 
					            length = 1
 | 
				
			||||||
            num = int(m[0], 0)
 | 
					        elif d.type == 0x10 or d.type == 0x12 or d.type == 0x90 or d.type == 0x92:
 | 
				
			||||||
        elif d.type == 0x10:
 | 
					            length = 2
 | 
				
			||||||
            m = message[d.index : d.index + 2]  # noqa: E203
 | 
					 | 
				
			||||||
            num = (int(m[0], 0) << 8) + int(m[1], 0)
 | 
					 | 
				
			||||||
        elif d.type == 0x12:
 | 
					 | 
				
			||||||
            m = message[d.index : d.index + 2]  # noqa: E203
 | 
					 | 
				
			||||||
            num = round((int(m[0], 0) << 8) + int(m[1], 0) / 100, 2)
 | 
					 | 
				
			||||||
        elif d.type == 0x90:
 | 
					 | 
				
			||||||
            m = message[d.index : d.index + 2]  # noqa: E203
 | 
					 | 
				
			||||||
            num = (int(m[0], 0) << 8) + int(m[1], 0)
 | 
					 | 
				
			||||||
            if num >= 32768:
 | 
					 | 
				
			||||||
                num -= 65536
 | 
					 | 
				
			||||||
        elif d.type == 0x92:
 | 
					 | 
				
			||||||
            m = message[d.index : d.index + 2]  # noqa: E203
 | 
					 | 
				
			||||||
            num = (int(m[0], 0) << 8) + int(m[1], 0)
 | 
					 | 
				
			||||||
            if num >= 32768:
 | 
					 | 
				
			||||||
                num -= 65536
 | 
					 | 
				
			||||||
            num = round(num / 100, 2)
 | 
					 | 
				
			||||||
        elif d.type == 0x20:
 | 
					        elif d.type == 0x20:
 | 
				
			||||||
            m = message[d.index : d.index + 4]  # noqa: E203
 | 
					            length = 4
 | 
				
			||||||
            num = (int(m[0], 0) << 24) + (int(m[1], 0) << 16) + (int(m[2], 0) << 8) + int(m[3], 0)
 | 
					 | 
				
			||||||
        else:
 | 
					        else:
 | 
				
			||||||
            logger.error(f"Unknown message type for datalog {d.name}: {d.type}")
 | 
					            logger.error(f"Unknown message type for datalog {d.label}: {d.type}")
 | 
				
			||||||
 | 
					        num = format_datatype(d.label, message[d.index : d.index + length], d.type)  # noqa: E203
 | 
				
			||||||
        logger.info(f"{d.description}: {num}")
 | 
					        logger.info(f"{d.description}: {num}")
 | 
				
			||||||
        measurements[d.label] = num
 | 
					        measurements[d.label] = num
 | 
				
			||||||
    return measurements
 | 
					    return measurements
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def format_datatype(name, m, dt):
 | 
				
			||||||
 | 
					    """
 | 
				
			||||||
 | 
					    Transform a list of bytes to a readable number based on the datatype.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    :param str name: Name/label of the data
 | 
				
			||||||
 | 
					    :param list[str] m: List of bytes in hexadecimal string format
 | 
				
			||||||
 | 
					    :param dt: Datatype
 | 
				
			||||||
 | 
					    :type dt: str or int
 | 
				
			||||||
 | 
					    """
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    num = None
 | 
				
			||||||
 | 
					    if type(dt) is str:
 | 
				
			||||||
 | 
					        dt = int(dt, 0)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if dt == 0x0 or dt == 0xC:
 | 
				
			||||||
 | 
					        num = int(m[-1], 0)
 | 
				
			||||||
 | 
					    elif dt == 0x10:
 | 
				
			||||||
 | 
					        num = (int(m[-2], 0) << 8) + int(m[-1], 0)
 | 
				
			||||||
 | 
					    elif dt == 0x12:
 | 
				
			||||||
 | 
					        num = round((int(m[-2], 0) << 8) + int(m[-1], 0) / 100, 2)
 | 
				
			||||||
 | 
					    elif dt == 0x90:
 | 
				
			||||||
 | 
					        num = (int(m[-2], 0) << 8) + int(m[-1], 0)
 | 
				
			||||||
 | 
					        if num >= 32768:
 | 
				
			||||||
 | 
					            num -= 65536
 | 
				
			||||||
 | 
					    elif dt == 0x92:
 | 
				
			||||||
 | 
					        num = (int(m[-2], 0) << 8) + int(m[-1], 0)
 | 
				
			||||||
 | 
					        if num >= 32768:
 | 
				
			||||||
 | 
					            num -= 65536
 | 
				
			||||||
 | 
					        num = round(num / 100, 2)
 | 
				
			||||||
 | 
					    elif dt == 0x20:
 | 
				
			||||||
 | 
					        num = (int(m[-4], 0) << 24) + (int(m[-3], 0) << 16) + (int(m[-2], 0) << 8) + int(m[-1], 0)
 | 
				
			||||||
 | 
					    else:
 | 
				
			||||||
 | 
					        logger.error(f"Unknown datatype for '{name}': 0x{dt:X}")
 | 
				
			||||||
 | 
					    return num
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
if __name__ == "__main__":
 | 
					if __name__ == "__main__":
 | 
				
			||||||
    args = parse_args()
 | 
					    args = parse_args()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue