This repository has been archived on 2023-06-10. You can view files and clone it, but cannot push or open issues or pull requests.
HomeControl/app/config.py

83 lines
3.7 KiB
Python
Raw Normal View History

2018-02-02 17:50:05 +01:00
import os.path, os
2018-02-02 00:21:59 +01:00
import json, codecs
##---------------------------------------------------------------------------------------##
class Config(object):
##---------------------------------------------------------------------------------------##
##-----------------------------------------------------------------------------------##
def __init__(self):
##-----------------------------------------------------------------------------------##
print("Config initialisiert")
##-----------------------------------------------------------------------------------##
def defaultSettings(self):
##-----------------------------------------------------------------------------------##
print("Returning Default-Settings")
data = {
"yeelight" : {
"dunkel" : "1",
"hell" : "100",
"warm" : "1700",
"normal" : "3000",
"kalt" : "6500"
}
}
return data
##-----------------------------------------------------------------------------------##
def defaultDevices(self):
##-----------------------------------------------------------------------------------##
print("Returning Default-Devices")
data = {}
return data
##-----------------------------------------------------------------------------------##
def read(self, type):
##-----------------------------------------------------------------------------------##
2018-02-02 17:54:28 +01:00
path = os.path.dirname(os.path.abspath(__file__))
2018-02-02 00:21:59 +01:00
data = None
if(type == "settings"):
print("Reading 'settings.json'")
2018-02-02 17:52:51 +01:00
if(os.path.isfile(path + "/config/settings.json")):
with open(path + '/config/settings.json', encoding='utf-8') as data_file:
2018-02-02 00:21:59 +01:00
try:
data = json.loads(data_file.read())
except:
print("settings.json konnte nicht gelesen werden")
data = self.defaultSettings()
self.writeConfig("settings", data)
print("Settings loaded")
else:
data =self.defaultSettings()
self.writeConfig("settings", data)
else:
print("Reading 'devices.json'")
2018-02-02 17:52:51 +01:00
if(os.path.isfile(path + "/config/devices.json")):
with open(path + '/config/devices.json', encoding='utf-8') as data_file:
2018-02-02 00:21:59 +01:00
try:
data = json.loads(data_file.read())
except:
print("devices.json konnte nicht gelesen werden")
data = self.defaultDevices()
self.writeConfig("devices", data)
print("Devices loaded")
else:
data = self.defaultDevices()
self.writeConfig("devices", data)
return data
##-----------------------------------------------------------------------------------##
def writeConfig(self, type, data):
##-----------------------------------------------------------------------------------##
2018-02-02 17:54:28 +01:00
path = os.path.dirname(os.path.abspath(__file__))
2018-02-02 17:51:15 +01:00
print(path)
2018-02-02 00:21:59 +01:00
if(type == "settings"):
print("Writing 'settings.json'")
2018-02-02 17:52:51 +01:00
with open(path + '/config/settings.json', 'w') as outfile:
2018-02-02 00:21:59 +01:00
json.dump(data, outfile, indent=4)
print("Settings written")
else:
print("Writing 'devices.json'")
2018-02-02 17:52:51 +01:00
with open(path + '/config/devices.json', 'w') as outfile:
2018-02-02 00:21:59 +01:00
json.dump(data, outfile, indent=4)
print("Devices written")