-
Notifications
You must be signed in to change notification settings - Fork 0
/
configManager.py
126 lines (113 loc) · 5.39 KB
/
configManager.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/python3
# Copyright (c) 2017 Johannes Leupolz
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
# of the Software, and to permit persons to whom the Software is furnished to do
# so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import asyncio
import os
import yaml
class BluetoothMonitorConfigManager:
def __init__(self, loop):
self.loop = loop
self.trace = lambda level,msg: print(msg)
self.appConfigFilePath = None
self.lastTimeConfigOnDriveUpdated=-1
self.appConfig={}
self.Continue = True
self.WatchingFuture=None
self.onLoadConfigHandler = lambda appConfig: None
def loadConfig(self):
if not self.appConfigFilePath==None:
print("Try to use configuration file "+self.appConfigFilePath)
if os.path.isfile(self.appConfigFilePath):
configFile=open(self.appConfigFilePath,"r")
self.appConfig=yaml.load(configFile)
self.updateLastTimeConfigOnDriveUpdated()
else:
print("Configuration file not found. Using default configuration")
else:
print("Cannot locate path of configuration file. Need $XDG_CONFIG_HOME or custom path to configuration file as --config parameter. Using default configuration")
if "traceLevel" not in self.appConfig:
self.appConfig["traceLevel"]=0
if "pollingCycle" not in self.appConfig:
self.appConfig["pollingCycle"]=2
if "useMqtt" not in self.appConfig:
self.appConfig["useMqtt"]=False
if "updateConfig" not in self.appConfig:
self.appConfig["updateConfig"]=False
if "saveConfigOnExit" not in self.appConfig:
self.appConfig["saveConfigOnExit"]=False
if "bluetoothDevices" not in self.appConfig:
self.appConfig["bluetoothDevices"]={}
if "other_a2dp_sinks" not in self.appConfig["bluetoothDevices"]:
self.appConfig["bluetoothDevices"]["other_a2dp_sinks"]={}
for device,deviceConfig in self.appConfig["bluetoothDevices"].items():
if "onConnectCommand" not in deviceConfig:
deviceConfig["onConnectCommand"]=None
if "onDisconnectCommand" not in deviceConfig:
deviceConfig["onDisconnectCommand"]=None
print("trace level (higher means more output): "+str(self.appConfig["traceLevel"]))
print("polling cycle: "+str(self.appConfig["pollingCycle"]))
print("use mqtt: "+str(self.appConfig["useMqtt"]))
print("update configuration file: "+str(self.appConfig["updateConfig"]))
print("save configuration file on exit: "+str(self.appConfig["saveConfigOnExit"]))
for device,deviceConfig in self.appConfig["bluetoothDevices"].items():
print("bluetooth device (must be in upper case form, e.g. A0_14_...): "+str(device))
print("onConnectCommand: "+str(deviceConfig["onConnectCommand"]))
print("onDisconnectCommand: "+str(deviceConfig["onDisconnectCommand"]))
self.onLoadConfigHandler(self.appConfig)
def updateLastTimeConfigOnDriveUpdated(self):
if (not self.appConfigFilePath==None):
self.lastTimeConfigOnDriveUpdated=os.stat(self.appConfigFilePath).st_mtime
def checkIfConfigOnDriveWasUpdated(self):
if (not self.appConfigFilePath==None):
stamp=os.stat(self.appConfigFilePath).st_mtime
if (stamp != self.lastTimeConfigOnDriveUpdated):
self.updateLastTimeConfigOnDriveUpdated()
return True
else:
return False
return False
def saveConfig(self):
if (not self.appConfigFilePath==None):
print("update settings file")
configFile=open(self.appConfigFilePath,"w")
yaml.dump(self.appConfig,configFile)
async def watchConfig(self):
if self.WatchingFuture != None:
return
self.WatchingFuture=self.loop.create_future()
while self.Continue:
self.trace(3,"checking for updated configuraton")
if self.checkIfConfigOnDriveWasUpdated():
self.trace(0,"reloading configuration")
try:
self.loadConfig()
except FileNotFoundError:
pass
self.trace(0,"reloading configuration finished")
await asyncio.sleep(1)
self.WatchingFuture.set_result(True)
async def startWatchConfig(self):
self.trace(1,"watching changes on local configuration file")
asyncio.ensure_future(self.watchConfig())
async def stopWatchConfig(self):
self.Continue=False
if (self.WatchingFuture):
await self.WatchingFuture
self.WatchingFuture = None