# Path: thingsboard_gateway/storage/file/event_storage_files.py
# class EventStorageFiles:
#     def __init__(self, state_file, data_files):
#         self.state_file = state_file
#         self.data_files = data_files
# 
#     def get_state_file(self):
#         return self.state_file
# 
#     def get_data_files(self):
#         return sorted(self.data_files)
# 
#     def set_data_files(self, data_files):
#         self.data_files = data_files
# 
# Path: thingsboard_gateway/storage/file/event_storage_reader_pointer.py
# class EventStorageReaderPointer:
#     def __init__(self, file, line):
#         self.file = file
#         self.line = line
# 
#     def __eq__(self, other):
#         return self.file == other.file and self.line == other.line
# 
#     def __hash__(self):
#         return hash((self.file, self.line))
# 
#     def get_file(self):
#         return self.file
# 
#     def get_line(self):
#         return self.line
# 
#     def set_file(self, file):
#         self.file = file
# 
#     def set_line(self, line):
#         self.line = line
# 
# Path: thingsboard_gateway/storage/file/file_event_storage.py
# class FileEventStorage(EventStorage):
#     def __init__(self, config):
#     def put(self, event):
#     def get_event_pack(self):
#     def event_pack_processing_done(self):
#     def init_data_folder_if_not_exist(self):
#     def init_data_files(self):
#     def create_new_datafile(self):
#     def create_file(self, prefix, filename):
#     def stop(self):
# 
# Path: thingsboard_gateway/storage/file/file_event_storage_settings.py
# class FileEventStorageSettings:
#     def __init__(self, config):
#         self.data_folder_path = config.get("data_folder_path", "./")
#         self.max_files_count = config.get("max_file_count", 5)
#         self.max_records_per_file = config.get("max_records_per_file", 3)
#         self.max_records_between_fsync = config.get("max_records_between_fsync", 1)
#         self.max_read_records_count = config.get("max_read_records_count", 1000)
# 
#     def get_data_folder_path(self):
#         return self.data_folder_path
# 
#     def get_max_files_count(self):
#         return self.max_files_count
# 
#     def get_max_records_per_file(self):
#         return self.max_records_per_file
# 
#     def get_max_records_between_fsync(self):
#         return self.max_records_between_fsync
# 
#     def get_max_read_records_count(self):
#         return self.max_read_records_count


from base64 import b64decode
from io import BufferedReader, FileIO
from os import remove
from os.path import exists
from simplejson import JSONDecodeError, dumps, load
from thingsboard_gateway.storage.file.event_storage_files import EventStorageFiles
from thingsboard_gateway.storage.file.event_storage_reader_pointer import EventStorageReaderPointer
from thingsboard_gateway.storage.file.file_event_storage import log
from thingsboard_gateway.storage.file.file_event_storage_settings import FileEventStorageSettings
                        try:
                            self.current_batch.append(b64decode(line).decode("utf-8"))
                            records_to_read -= 1
                        except IOError as e:
                            log.warning("Could not parse line [%s] to uplink message! %s", line, e)
                        except Exception as e:
                            log.exception(e)
                            current_line_in_file += 1
                            self.new_pos.set_line(current_line_in_file)
                            self.write_info_to_state_file(self.new_pos)
                            break
                        finally:
                            current_line_in_file += 1
                            if records_to_read > 0:
                                line = self.buffered_reader.readline()
                        self.new_pos.set_line(current_line_in_file)
                        if records_to_read == 0:
                            break

                    if (self.settings.get_max_records_per_file() >= current_line_in_file >= 0) or \
                            (line == b'' and current_line_in_file >= self.settings.get_max_records_per_file() - 1):
                        previous_file = self.current_pos
                        next_file = self.get_next_file(self.files, self.new_pos)
                        # self.write_info_to_state_file(self.new_pos)
                        if next_file is None:
                            break
                        self.delete_read_file(previous_file)
                        if self.buffered_reader is not None:
                            self.buffered_reader.close()
                        # self.buffered_reader = None

# Path: thingsboard_gateway/storage/file/event_storage_files.py
# class EventStorageFiles:
#     def __init__(self, state_file, data_files):
#         self.state_file = state_file
#         self.data_files = data_files
# 
#     def get_state_file(self):
#         return self.state_file
# 
#     def get_data_files(self):
#         return sorted(self.data_files)
# 
#     def set_data_files(self, data_files):
#         self.data_files = data_files
# 
# Path: thingsboard_gateway/storage/file/event_storage_reader_pointer.py
# class EventStorageReaderPointer:
#     def __init__(self, file, line):
#         self.file = file
#         self.line = line
# 
#     def __eq__(self, other):
#         return self.file == other.file and self.line == other.line
# 
#     def __hash__(self):
#         return hash((self.file, self.line))
# 
#     def get_file(self):
#         return self.file
# 
#     def get_line(self):
#         return self.line
# 
#     def set_file(self, file):
#         self.file = file
# 
#     def set_line(self, line):
#         self.line = line
# 
# Path: thingsboard_gateway/storage/file/file_event_storage.py
# class FileEventStorage(EventStorage):
#     def __init__(self, config):
#     def put(self, event):
#     def get_event_pack(self):
#     def event_pack_processing_done(self):
#     def init_data_folder_if_not_exist(self):
#     def init_data_files(self):
#     def create_new_datafile(self):
#     def create_file(self, prefix, filename):
#     def stop(self):
# 
# Path: thingsboard_gateway/storage/file/file_event_storage_settings.py
# class FileEventStorageSettings:
#     def __init__(self, config):
#         self.data_folder_path = config.get("data_folder_path", "./")
#         self.max_files_count = config.get("max_file_count", 5)
#         self.max_records_per_file = config.get("max_records_per_file", 3)
#         self.max_records_between_fsync = config.get("max_records_between_fsync", 1)
#         self.max_read_records_count = config.get("max_read_records_count", 1000)
# 
#     def get_data_folder_path(self):
#         return self.data_folder_path
# 
#     def get_max_files_count(self):
#         return self.max_files_count
# 
#     def get_max_records_per_file(self):
#         return self.max_records_per_file
# 
#     def get_max_records_between_fsync(self):
#         return self.max_records_between_fsync
# 
#     def get_max_read_records_count(self):
#         return self.max_read_records_count


# Path: thingsboard_gateway/storage/file/event_storage_reader.py
from base64 import b64decode
from io import BufferedReader, FileIO
from os import remove
from os.path import exists
from simplejson import JSONDecodeError, dumps, load
from thingsboard_gateway.storage.file.event_storage_files import EventStorageFiles
from thingsboard_gateway.storage.file.event_storage_reader_pointer import EventStorageReaderPointer
from thingsboard_gateway.storage.file.file_event_storage import log
from thingsboard_gateway.storage.file.file_event_storage_settings import FileEventStorageSettings

                        try:
                            self.current_batch.append(b64decode(line).decode("utf-8"))
                            records_to_read -= 1
                        except IOError as e:
                            log.warning("Could not parse line [%s] to uplink message! %s", line, e)
                        except Exception as e:
                            log.exception(e)
                            current_line_in_file += 1
                            self.new_pos.set_line(current_line_in_file)
                            self.write_info_to_state_file(self.new_pos)
                            break
                        finally:
                            current_line_in_file += 1
                            if records_to_read > 0:
                                line = self.buffered_reader.readline()
                        self.new_pos.set_line(current_line_in_file)
                        if records_to_read == 0:
                            break

                    if (self.settings.get_max_records_per_file() >= current_line_in_file >= 0) or \
                            (line == b'' and current_line_in_file >= self.settings.get_max_records_per_file() - 1):
                        previous_file = self.current_pos
                        next_file = self.get_next_file(self.files, self.new_pos)
                        # self.write_info_to_state_file(self.new_pos)
                        if next_file is None:
                            break
                        self.delete_read_file(previous_file)
                        if self.buffered_reader is not None:
                            self.buffered_reader.close()
                        # self.buffered_reader = None

                        self.new_pos = EventStorageReaderPointer(next_file, 0)