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

80 lines
3.5 KiB
Python
Raw Normal View History

2018-02-02 00:21:59 +01:00
import os.path
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):
##-----------------------------------------------------------------------------------##
data = None
if(type == "settings"):
print("Reading 'settings.json'")
if(os.path.isfile("config/settings.json")):
with open('config/settings.json', encoding='utf-8') as data_file:
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'")
if(os.path.isfile("config/devices.json")):
with open('config/devices.json', encoding='utf-8') as data_file:
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):
##-----------------------------------------------------------------------------------##
if(type == "settings"):
print("Writing 'settings.json'")
with open('config/settings.json', 'w') as outfile:
json.dump(data, outfile, indent=4)
print("Settings written")
else:
print("Writing 'devices.json'")
with open('config/devices.json', 'w') as outfile:
json.dump(data, outfile, indent=4)
print("Devices written")