Move parameters to a configuration file + add onStop
This commit is contained in:
46
run.py
46
run.py
@@ -1,19 +1,36 @@
|
|||||||
|
import os
|
||||||
import time
|
import time
|
||||||
import Domoticz
|
import Domoticz
|
||||||
import plugin
|
import plugin
|
||||||
|
|
||||||
Parameters = {
|
def function_read_properties(filename:str, sep:str='=', comment_char:str='#')->dict:
|
||||||
"Address":"localhost",
|
"""Convert a .properties file into a dictionary"""
|
||||||
"Port":"8080",
|
dictionary = {}
|
||||||
"Username":"",
|
|
||||||
"Password":"",
|
with open(filename, "r", encoding="utf-8") as fproperties:
|
||||||
"Mode1":"",
|
for line in fproperties:
|
||||||
"Mode2":"",
|
lstrip = line.strip()
|
||||||
"Mode3":"",
|
if lstrip and not lstrip.startswith(comment_char):
|
||||||
"Mode4":"",
|
key_value = lstrip.split(sep)
|
||||||
"Mode5":"",
|
key = key_value[0].strip()
|
||||||
"Mode6":0
|
value = sep.join(key_value[1:]).strip().strip('"')
|
||||||
}
|
dictionary[key] = value
|
||||||
|
|
||||||
|
return dictionary
|
||||||
|
|
||||||
|
"""Read Parameter file? Format should be:
|
||||||
|
Address=xxx
|
||||||
|
Port=xxx
|
||||||
|
Username=xxx
|
||||||
|
Password=xxx
|
||||||
|
Mode1=xxx
|
||||||
|
Mode2=xxx
|
||||||
|
Mode3=xxx
|
||||||
|
Mode4=xxx
|
||||||
|
Mode5=xxx
|
||||||
|
Mode6=xxx
|
||||||
|
"""
|
||||||
|
Parameters = function_read_properties(os.path.join(os.path.dirname(__file__), "parameters.properties"))
|
||||||
|
|
||||||
Images = Domoticz.Images
|
Images = Domoticz.Images
|
||||||
Devices = Domoticz.Devices
|
Devices = Domoticz.Devices
|
||||||
@@ -23,7 +40,10 @@ with open("./plugin.py", 'rb') as fileobj:
|
|||||||
|
|
||||||
onStart()
|
onStart()
|
||||||
|
|
||||||
while True:
|
try:
|
||||||
|
while True:
|
||||||
Domoticz.Log("## Plugin Heartbeat ##")
|
Domoticz.Log("## Plugin Heartbeat ##")
|
||||||
onHeartbeat()
|
onHeartbeat()
|
||||||
time.sleep(Domoticz.domo.GetHeartbeat())
|
time.sleep(Domoticz.domo.GetHeartbeat())
|
||||||
|
finally:
|
||||||
|
onStop()
|
||||||
|
|||||||
Reference in New Issue
Block a user