# 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/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 b64encode
from io import BufferedWriter, FileIO
from os import O_CREAT, O_EXCL, close as os_close, linesep, open as os_open
from os.path import exists
from time import time
from thingsboard_gateway.storage.file.event_storage_files import EventStorageFiles
from thingsboard_gateway.storage.file.file_event_storage import log
from thingsboard_gateway.storage.file.file_event_storage_settings import FileEventStorageSettings
#
#     Unless required by applicable law or agreed to in writing, software
#     distributed under the License is distributed on an "AS IS" BASIS,
#     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#     See the License for the specific language governing permissions and
#     limitations under the License.




class DataFileCountError(Exception):
    pass


class EventStorageWriter:
    def __init__(self, files: EventStorageFiles, settings: FileEventStorageSettings):
        self.files = files
        self.settings = settings
        self.buffered_writer = None
        self.current_file = sorted(files.get_data_files())[-1]
        self.current_file_records_count = [0]
        self.previous_file_records_count = [0]
        self.get_number_of_records_in_file(self.current_file)

    def write(self, msg):
        if len(self.files.data_files) <= self.settings.get_max_files_count():
            if self.current_file_records_count[0] >= self.settings.get_max_records_per_file() or not exists(
                    self.settings.get_data_folder_path() + self.current_file):
                try:
                    self.current_file = self.create_datafile()

# 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/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_writer.py
from base64 import b64encode
from io import BufferedWriter, FileIO
from os import O_CREAT, O_EXCL, close as os_close, linesep, open as os_open
from os.path import exists
from time import time
from thingsboard_gateway.storage.file.event_storage_files import EventStorageFiles
from thingsboard_gateway.storage.file.file_event_storage import log
from thingsboard_gateway.storage.file.file_event_storage_settings import FileEventStorageSettings

#
#     Unless required by applicable law or agreed to in writing, software
#     distributed under the License is distributed on an "AS IS" BASIS,
#     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#     See the License for the specific language governing permissions and
#     limitations under the License.




class DataFileCountError(Exception):
    pass


class EventStorageWriter:
    def __init__(self, files: EventStorageFiles, settings: FileEventStorageSettings):
        self.files = files
        self.settings = settings
        self.buffered_writer = None
        self.current_file = sorted(files.get_data_files())[-1]
        self.current_file_records_count = [0]
        self.previous_file_records_count = [0]
        self.get_number_of_records_in_file(self.current_file)

    def write(self, msg):
        if len(self.files.data_files) <= self.settings.get_max_files_count():
            if self.current_file_records_count[0] >= self.settings.get_max_records_per_file() or not exists(
                    self.settings.get_data_folder_path() + self.current_file):
                try:
                    self.current_file = self.create_datafile()

                    log.debug("FileStorage_writer -- Created new data file: %s", self.current_file)