def from_bytes(data): """Creates a Picture instance from the given bytes""" fields = struct.unpack_from(f'<LLL{Picture._PATH_MAX}s', data) size, time_ms, _, path = fields pic = Picture() pic._size = size pic._time = utils.secs_to_time(time_ms // 1000) pic._path = utils.cstr2str(path) pic._data = data[Picture._HDR_LEN:] return pic
def from_bytes(data): """Creates a Config instance from the given bytes""" if len(data) != Config._LEN: raise ValueError('invalid config length') fields = struct.unpack('<BBB32s32sQBQQ48s', data) (wifi_chan, flip, wifi_sec, wifi_name, wifi_pass, time, sdc_mounted, sdc_size, sdc_free, version) = fields conf = Config() conf._wifi_chan = wifi_chan conf._flip = CameraFlip(flip) conf._wifi_sec = ConfigWiFiSec(wifi_sec) conf._wifi_name = utils.cstr2str(wifi_name) conf._wifi_pass = utils.cstr2str(wifi_pass) conf._time = datetime.datetime.fromtimestamp(time) conf._sdc_mounted = sdc_mounted == 1 conf._sdc_size = sdc_size conf._sdc_free = sdc_free conf._version = utils.cstr2str(version) return conf
def iter_from_bytes(data): """returns an iterator of PictureListItem instances from the given raw data""" if len(data) % PictureListItem._LEN != 0: raise ValueError('invalid picture list data') for i in range(0, len(data), PictureListItem._LEN): flag, size, _, path = struct.unpack_from( f'<LL16s{PictureListItem._PATH_MAX}s', data, i) if flag != 1: raise ValueError('invalid picture list entry') yield PictureListItem(size, utils.cstr2str(path)) return
def iter_from_bytes(data): """returns an iterator of RecordListItem instances from the given raw data""" if len(data) % RecordListItem._LEN != 0: raise ValueError('invalid record list data') for i in range(0, len(data), RecordListItem._LEN): stime, tlen, _, path = struct.unpack_from( f'<LL8s{RecordListItem._PATH_MAX}s', data, i) # cam returns epoch in GMT+8, so need TZ=-8 to get back to UTC tz = datetime.timezone(datetime.timedelta(hours=-8)) start = datetime.datetime.fromtimestamp(stime, tz) start = start.replace(tzinfo=datetime.timezone.utc) yield RecordListItem(start, tlen, utils.cstr2str(path)) return
def from_bytes(data): """Creates a FileFrame instance from the given bytes""" fields = struct.unpack_from(FileFrame._HDR_FMT, data) flag, size, tot_size, _, path, md5 = fields fframe = FileFrame() fframe._flag = FileFrameFlag(flag) fframe._size = size fframe._tot_size = tot_size fframe._path = utils.cstr2str(path) fframe._md5 = md5.decode() fframe._file_bytes = data[FileFrame._HDR_LEN:] if len(fframe._file_bytes) != fframe._size: raise ValueError('incomple file segment') return fframe