import numpy as np
from myconfig import DATA_DIR,OUTPUT_DIR
from tools.mylogger import log
from pathlib import Path
import time
# from handheld import HandHeldPath
class GatherData(object):
    """对数据进行汇总后分析 csv"""
    def __init__(self, ):
        super(GatherData, self).__init__()
        self.dir = OUTPUT_DIR
        self.extname = ".csv"

    def setDir(self, fpath):
        self.dir = fpath
        pass

    def setExtname(self, extname):
        self.extname = extname
        pass

    def getChildDirs(self, ):
        '''
        按时间排放，每个目录下有五个文件 Esky....Rs
        文件直接保存到 output下的五个文件 Esky  ..... Rs 
        '''
        childdirs = []
        for path in Path(self.dir).iterdir():
            # if path.is_dir():
            #     print(path)  
            if not path.is_dir():
                continue  
            childdirs.append(path)
        return childdirs
        pass

    def gather_handheld(self, ):
        log.info(f"  ",__name__,'gather_handheld')
        childDirs = self.getChildDirs()
        for cdir  in childDirs: 
            tmp_path:Path = cdir.joinpath("Lw"+self.extname)
            self.transfer_data(tmp_path)
            tmp_path:Path = cdir.joinpath("Rs"+self.extname)
            self.transfer_data(tmp_path)
        pass 

    def transfer_data(self, fpath:Path):
        log.info(f"  ",__name__,'transfer_data')
        log.info(fpath)
        old_path:Path = fpath
        new_path:Path = self.dir.joinpath( old_path.stem + self.extname)
        if not new_path.exists():
            new_path.touch()
        if old_path.exists():
            count = 0
            with open( new_path, "a" ) as fnew:
                with open( old_path, "r" ) as f:
                    while True:
                        line = f.readline()
                        if not line :
                            break
                        if count != 0:
                            fnew.write(line)
                        count+=1

    def gather_awrams(self, ):
        '''
        一个文件data.csv包含  Esky   .....Rs 五组数据 
        获得csv文件,转移不同的行到不同文件
        第一列要转为时间
        '''
        log.info(f"  ",__name__,'gather_awrams')
        fs = None
        self.filelist = []
        fs = self.dir.glob( "*/*/*/*.csv" )
        for f in fs:
            # print(f)
            with open(f, 'r') as fhandle:
                while True:
                    line = fhandle.readline()
                    if not line:
                        break
                    self.addLine2File(f.stem, line)
                    pass

    def addLine2File( self, fname, line:str ):
        log.info(f"+++++ {fname}  -- {line}",__name__,'addLine2File')
        tm = fname[0:4] +"-"+ fname[5:7] +"-"+ fname[8:10] +" "+fname[11:13] +":"+fname[14:16] +":"+fname[17:19]  
        func = "Lw"
        if line.startswith(func+";"):
            log.info(f"===== Lw")
            tmp_str = tm  +line[len(func):]
            self.save_str_by_func(tmp_str,func)
        func = "Rs"
        if line.startswith(func+";"):
            log.info(f"=====  Rs")
            tmp_str = tm  +line[len(func):]
            self.save_str_by_func(tmp_str,func)     
        pass

    def save_str_by_func(self, str, func):
        log.info(f"  {func}  -- {str}",__name__,'save_str_by_func')
        savepath = OUTPUT_DIR
        save_fname= savepath.joinpath(func + self.extname)
        if not save_fname.exists():
            save_fname.touch()
        with open(save_fname,"a") as fhandle:
            fhandle.write(str)

        


    # def transfer_line_without_firstline(self, oldpath:Path, newPath:Path):
    #     count = 0
    #     with open( newPath, "a" ) as fnew:
    #         with open( oldpath, "r" ) as f:
    #             line = f.readline()
    #             if count != 0:
    #                 fnew.write(line)
    #         pass
    #     pass

    def getFileList_awrams(self, ):
        '''
        一个文件包含  Esky   .....Rs 五组数据 
        获得csv文件,转移不同的行到不同文件
        '''
        # ret = []
        fs = None
        self.filelist = []
        fs = self.dir.glob( "*/*.csv" )

        pass
    
if __name__ == "__main__":
    gd = GatherData()
    gd.gather_awrams()

