import struct import time from .BxCmd import baseBxCmd from .BxCmdCode import CMD_WRITE_FILE, CMD_START_WRITE_FILE from .BxUtils import CRC16, Uint2BCD class bxFile: """普通文本文件""" def __init__(self, fileName, diedLine, areas): self.type_ = 0x00 # 默认0x00 self.name = fileName # 4字节ASCII self.len = 0 self.content = "" self.Priority = 0xff self.DisplayType = 0 # 播放方式节目播放方式,0——顺序播放,其他——定长播放的 时间,单位为秒 self.PlayTimes = 1 self.ProgramLife = diedLine # 节目生命周期 self.ProgramWeek = 1 # 节目的星期属性 self.ProgramTime = 0 # 定时节目位 0 非定时 self.PlayPeriodGrpNum = 0 # 节目播放时段组数 self.Areas = areas if areas else [] # 区域列表 def NewCmdWriteFile(self): """创建文件写入命令""" return CmdWriteFile(self) def encodeProgramLife(self): """编码节目生命周期""" if self.ProgramLife == "": return bytes([0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]) now = time.localtime() y = Uint2BCD(now.tm_year, False) m = Uint2BCD(now.tm_mon, False) d = Uint2BCD(now.tm_mday, False) result = bytearray() result.append(y) result.append(m) result.append(d) try: # 解析过期时间 end = time.strptime(self.ProgramLife, "%Y-%m-%d") endY = Uint2BCD(end.tm_year, False) endM = Uint2BCD(end.tm_mon, False) endD = Uint2BCD(end.tm_mday, False) result.append(endY) result.append(endM) result.append(endD) except Exception as e: print(f"时间解析错误: {e}") return bytes([0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]) return bytes(result) class CmdWriteFile(baseBxCmd): """文件写入命令""" def __init__(self, file): super().__init__(CMD_WRITE_FILE.group, CMD_WRITE_FILE.code) self.state = 0 self.file = file self.LastBlockFlag = 0x01 # 是否是最后一包,0x00——不是最后一包 0x01——最后一包 self.BlockNum = 0x00 # 包号,如果是单包发送,则默认为 0x00 self.BlockLen = 0 # 包长,若是单包发送,此处为文件长度 self.BlockAddr = 0 # 本包数据在文件中的起始位置,如果是单包发送,此处默认为 0 self.temp = [] def Build(self): """构建命令数据""" if self.state == 0: # 先计算区域数据及长度 w0 = bytearray() for v in self.file.Areas: b = v.Build() w0.extend(struct.pack('