scan_collection.py 2.6 KB
import re
import time
import functools
import logging
import traceback
from app.utils.g import shelfconfig

s_detail = {}
test_state = False

def logtime(func):
    @functools.wraps(func)
    def wrapper(*args, **kwargs):
        start = time.perf_counter()
        res = func(*args, **kwargs)
        end = time.perf_counter()
        print(f'函数 {func.__name__} 耗时 {(end - start) * 1000} ms')
        return res
    return wrapper

@logtime
def first_filter(origin_value):
	cur_dict = transfer(origin_value)
	return cur_dict,s_detail

def production_filter(origin_value):
	cur_dict = transfer(origin_value)
	return cur_dict

def transfer(origin_value):
    cur_dict = {}
    addrs=shelfconfig.addrs.split('@')
    if origin_value:
        try: 
            receive_data=origin_value.decode().split("\n")
            for item in receive_data[::-1]:
                if re.search('^A\d{1,2}\[[0-1]{100}\]$',item.strip())==None:
                    receive_data.remove(item)
                    print (item)
            print("筛选后的数据:", receive_data)
            if receive_data:
                for line in receive_data:
                    line=line.strip()
                    # print ("-----------------line",line)
                    addr = re.search("(?<=A)\d{1,2}",line)
                    led = re.search("\[[0-1]{100}\]", line)
                    if led is None:
                        continue
                    if len(addr.group())==2:
                        if "B"+addr.group()[1] in addrs:
                            addrs.remove("B"+addr.group()[1])
                    else:
                        if "A"+addr.group() in addrs:
                            addrs.remove("A"+addr.group())
                    c_list = list(led.group())
                    # print ("-------------------c_list",c_list)
                    c_list.pop(0)
                    c_list.pop(-1)
                    keyword = 'A' + addr.group()
                    cur_dict[keyword] = c_list
                    print('cur_dict',keyword,cur_dict[keyword])
        except Exception as e:
            logging.warning("读取传感器状态出错:{}".format(repr(e)))
            logging.warning("当前解析值:{}".format(receive_data))
            msg = traceback.format_exc()
            logging.warning(msg)
    if len(addrs)>0:
        logging.warning("当前解析值2:{},{}".format(len(addrs),origin_value))
        return None
    return cur_dict

def change_deatil(old_dict):
	global s_detail
	if old_dict:
		s_detail = old_dict
		# print ('------------------------',s_detail)

def reset_value():
	s_detail = {}
	print (s_detail)