import json

class file_proc: 
    def __init__(self, path) -> None:
        self._data = None
        try:
            with open(path, 'r') as load_f:
                context = load_f.read()
                self._data = json.loads(context)
        except Exception as exp:
            print(exp)
    
    
    def getMqttInfo(self):
        mqtt_info = {
            'broker': 'www.witium.com.cn',
            'port': 12883,
            'user': 'witcd',
            'password': 'Witium37774020'
        }
        
        try:
           if self._data is not None:
                mqtt_info['broker'] = str(self._data['ip'])
                mqtt_info['port'] = int(self._data['port'])
                mqtt_info['user'] = str(self._data['user'])
                mqtt_info['password'] = str(self._data['pwd'])
           return mqtt_info
        except Exception as exp:
            print(exp)
            return mqtt_info

    def getOpDataPeriod(self):
        period_s = 300
        try:
           if self._data is not None:
                period_s = self._data['period']
           return period_s
        except Exception as exp:
            print(exp)
            return period_s
        
    def setOpDatafile(self, path, data):
        self._data = data
        try:
            with open(path ,"w") as f:
                json.dump(data, f)
        except Exception as exp:
            print(exp)

    def getConfigData(self):
        data = {"gateway":"WTG93RF","sn": "99991234","COM": "COM7"}
        if self._data is not None:
            return self._data
        else:
           return data 
    
    def getWtLinkConfigData(self):
        if self._data is not None:
            return self._data
        else:
           return None
    

    
