Skip to content

Commit

Permalink
🎨 Bunch of cleanups, updates etc
Browse files Browse the repository at this point in the history
  • Loading branch information
Psychokiller1888 committed Jan 31, 2022
1 parent 056f0da commit be572d0
Show file tree
Hide file tree
Showing 13 changed files with 275 additions and 160 deletions.
16 changes: 8 additions & 8 deletions core/ProjectAlice.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ def __init__(self, restartHandler: callable):

self._superManager.initManagers()

if self._superManager.configManager.getAliceConfigByName('useHLC'):
self._superManager.commons.runRootSystemCommand(['systemctl', 'start', 'hermesledcontrol'])
if self._superManager.ConfigManager.getAliceConfigByName('useHLC'):
self._superManager.Commons.runRootSystemCommand(['systemctl', 'start', 'hermesledcontrol'])

self._superManager.onStart()

Expand Down Expand Up @@ -69,8 +69,8 @@ def onStop(self, withReboot: bool = False):
self._logger.logInfo('Shutting down')
self._shuttingDown = True
self._superManager.onStop()
if self._superManager.configManager.getAliceConfigByName('useHLC'):
self._superManager.commons.runRootSystemCommand(['systemctl', 'stop', 'hermesledcontrol'])
if self._superManager.ConfigManager.getAliceConfigByName('useHLC'):
self._superManager.Commons.runRootSystemCommand(['systemctl', 'stop', 'hermesledcontrol'])

self._booted = False
self.INSTANCE = None
Expand All @@ -82,20 +82,20 @@ def onStop(self, withReboot: bool = False):


def onFullHour(self):
if not self._superManager.configManager.getAliceConfigByName('aliceAutoUpdate'):
if not self._superManager.ConfigManager.getAliceConfigByName('aliceAutoUpdate'):
return
self.updateProjectAlice()


def updateProjectAlice(self):
self._logger.logInfo('Checking for satellite updates')
self._isUpdating = True
req = requests.get(url=f'{constants.GITHUB_API_URL}/ProjectAliceSatellite/branches', auth=SuperManager.getInstance().configManager.getGithubAuth())
req = requests.get(url=f'{constants.GITHUB_API_URL}/ProjectAliceSatellite/branches', auth=SuperManager.getInstance().ConfigManager.getGithubAuth())
if req.status_code != 200:
self._logger.logWarning('Failed checking for updates')
return

userUpdatePref = SuperManager.getInstance().configManager.getAliceConfigByName('aliceUpdateChannel')
userUpdatePref = SuperManager.getInstance().ConfigManager.getAliceConfigByName('aliceUpdateChannel')

if userUpdatePref == 'master':
candidate = 'master'
Expand All @@ -114,7 +114,7 @@ def updateProjectAlice(self):
candidate = repoVersion

self._logger.logInfo(f'Checking on "{str(candidate)}" update channel')
commons = SuperManager.getInstance().commons
commons = SuperManager.getInstance().Commons

currentHash = subprocess.check_output(['git', 'rev-parse', '--short HEAD'])

Expand Down
6 changes: 3 additions & 3 deletions core/ProjectAliceExceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ def __init__(self, skillName: str = '', error: str = ''):
self._logger.logWarning(f'[{skillName}] ❗ Error starting skill: {error}')

if skillName:
SuperManager.getInstance().skillManager.deactivateSkill(skillName)
SuperManager.getInstance().SkillManager.deactivateSkill(skillName)


class SkillStartDelayed(ProjectAliceException):
def __init__(self, skillName):
super().__init__(skillName)
self._logger.logWarning(f'[{skillName}] ⌛ Delaying skill start')
SuperManager.getInstance().skillManager.getSkillInstance(skillName).delayed = True
SuperManager.getInstance().SkillManager.getSkillInstance(skillName).delayed = True


class IntentError(ProjectAliceException):
Expand Down Expand Up @@ -123,5 +123,5 @@ class PlayBytesStopped(ProjectAliceException):
class VitalConfigMissing(ProjectAliceException):
def __init__(self, message: str = None):
super().__init__(message)
self._logger.logWarning(f'A vital configuration --{message}-- is missing. Make sure the following configurations are set: {" / ".join(SuperManager.getInstance().configManager.vitalConfigs)}')
self._logger.logWarning(f'A vital configuration --{message}-- is missing. Make sure the following configurations are set: {" / ".join(SuperManager.getInstance().ConfigManager.vitalConfigs)}')
SuperManager.getInstance().projectAlice.onStop()
169 changes: 102 additions & 67 deletions core/base/SuperManager.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from __future__ import annotations

from core.commons import constants
from core.util.model.Logger import Logger


Expand All @@ -21,61 +20,70 @@ def __init__(self, mainClass):
self._managers = dict()

self.projectAlice = mainClass
self.commons = None
self.commonsManager = None
self.configManager = None
self.databaseManager = None
self.threadManager = None
self.mqttManager = None
self.timeManager = None
self.networkManager = None
self.hotwordManager = None
self.skillManager = None
self.internetManager = None
self.audioManager = None
self.wakewordManager = None
self.subprocessManager = None
self.Commons = None
self.CommonsManager = None
self.ConfigManager = None
self.DatabaseManager = None
self.ThreadManager = None
self.MqttManager = None
self.TimeManager = None
self.NetworkManager = None
self.HotwordManager = None
self.SkillManager = None
self.InternetManager = None
self.AudioManager = None
self.WakewordManager = None
self.SubprocessManager = None


def onStart(self):
commons = self._managers.pop('CommonsManager')
commons.onStart()
try:
commons = self._managers.pop('CommonsManager')
commons.onStart()

configManager = self._managers.pop('ConfigManager')
configManager.onStart()
configManager = self._managers.pop('ConfigManager')
configManager.onStart()

subprocessManager = self._managers.pop('SubprocessManager')
subprocessManager.onStart()
subprocessManager = self._managers.pop('SubprocessManager')
subprocessManager.onStart()

internetManager = self._managers.pop('InternetManager')
internetManager.onStart()
internetManager = self._managers.pop('InternetManager')
internetManager.onStart()

databaseManager = self._managers.pop('DatabaseManager')
databaseManager.onStart()
databaseManager = self._managers.pop('DatabaseManager')
databaseManager.onStart()

networkManager = self._managers.pop('NetworkManager')
networkManager.onStart()
networkManager = self._managers.pop('NetworkManager')
networkManager.onStart()

mqttManager = self._managers.pop('MqttManager')
mqttManager.onStart()
mqttManager = self._managers.pop('MqttManager')
mqttManager.onStart()

for manager in self._managers.values():
if manager:
manager.onStart()
for manager in self._managers.values():
if manager:
manager.onStart()

self._managers[commons.name] = commons
self._managers[configManager.name] = configManager
self._managers[subprocessManager.name] = subprocessManager
self._managers[databaseManager.name] = databaseManager
self._managers[mqttManager.name] = mqttManager
self._managers[networkManager.name] = networkManager
self._managers[internetManager.name] = internetManager
self._managers[commons.name] = commons
self._managers[configManager.name] = configManager
self._managers[subprocessManager.name] = subprocessManager
self._managers[databaseManager.name] = databaseManager
self._managers[mqttManager.name] = mqttManager
self._managers[networkManager.name] = networkManager
self._managers[internetManager.name] = internetManager
except Exception as e:
import traceback

traceback.print_exc()
Logger().logFatal(f'Error while starting managers: {e}')

def onBooted(self):
for manager in self._managers.values():
if manager:
manager.onBooted()
manager = None
try:
for manager in self._managers.values():
if manager:
manager.onBooted()
except Exception as e:
Logger().logError(f'Error while sending onBooted to manager **{manager.name}**: {e}')


@staticmethod
Expand All @@ -98,43 +106,70 @@ def initManagers(self):
from core.server.AudioServer import AudioManager
from core.voice.WakewordManager import WakewordManager

self.commonsManager = CommonsManager()
self.commons = self.commonsManager
self.configManager = ConfigManager()
self.databaseManager = DatabaseManager()
self.threadManager = ThreadManager()
self.mqttManager = MqttManager()
self.timeManager = TimeManager()
self.networkManager = NetworkManager()
self.hotwordManager = HotwordManager()
self.skillManager = SkillManager()
self.internetManager = InternetManager()
self.audioManager = AudioManager()
self.wakewordManager = WakewordManager()
self.subprocessManager = SubprocessManager()
self.CommonsManager = CommonsManager()
self.Commons = self.CommonsManager
self.ConfigManager = ConfigManager()
self.DatabaseManager = DatabaseManager()
self.ThreadManager = ThreadManager()
self.MqttManager = MqttManager()
self.TimeManager = TimeManager()
self.NetworkManager = NetworkManager()
self.HotwordManager = HotwordManager()
self.SkillManager = SkillManager()
self.InternetManager = InternetManager()
self.AudioManager = AudioManager()
self.WakewordManager = WakewordManager()
self.SubprocessManager = SubprocessManager()

self._managers = {name[0].upper() + name[1:]: manager for name, manager in self.__dict__.items() if name.endswith('Manager')}
self._managers = {name: manager for name, manager in self.__dict__.items() if name.endswith('Manager')}


def onStop(self):
managerName = constants.UNKNOWN_MANAGER
try:
mqttManager = self._managers.pop('MqttManager', None)

for managerName, manager in self._managers.items():
manager.onStop()

if mqttManager:
mqttManager = self._managers.pop('MqttManager', None) # Mqtt goes down last with bug reporter
bugReportManager = self._managers.pop('BugReportManager', None) # bug reporter goes down as last

skillManager = self._managers.pop('SkillManager', None) # Skill manager goes down first, to tell the skills
if skillManager:
try:
skillManager.onStop()
except Exception as e:
Logger().logError(f'Error stopping SkillManager: {e}')

for managerName, manager in self._managers.items():
try:
if manager.isActive:
manager.onStop()
except Exception as e:
Logger().logError(f'Error while shutting down manager **{managerName}**: {e}')

if mqttManager:
try:
mqttManager.onStop()
except Exception as e:
Logger().logError(f'Error stopping MqttManager: {e}')

except Exception as e:
Logger().logError(f'Error while shutting down manager "{managerName}": {e}')
if bugReportManager:
try:
bugReportManager.onStop()
except Exception as e:
Logger().logError(f'Error stopping BugReportManager: {e}')


def getManager(self, managerName: str):
return self._managers.get(managerName, None)


def restartManager(self, manager: str):
managerInstance = self._managers.get(manager, None)
if not managerInstance:
Logger().logWarning(f'Was asking to restart manager **{manager}** but it doesn\'t exist')
return

managerInstance.onStop()
managerInstance.onStart()
managerInstance.onBooted()


@property
def managers(self) -> dict:
return self._managers
4 changes: 2 additions & 2 deletions core/base/model/HotwordDownloadThread.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def run(self):
try:
self._logger.logInfo('Cleaning up')

rootPath = Path(SuperManager.getInstance().commons.rootDir(), 'trained/hotwords/snips_hotword')
rootPath = Path(SuperManager.getInstance().Commons.rootDir(), 'trained/hotwords/snips_hotword')
hotwordPath = rootPath / f'{self._hotwordName}'
zipPath = hotwordPath.with_suffix('.zip')

Expand Down Expand Up @@ -71,4 +71,4 @@ def run(self):
finally:
sock.close()
os.remove(zipPath)
SuperManager.getInstance().wakewordManager.restartEngine()
SuperManager.getInstance().WakewordManager.restartEngine()
2 changes: 1 addition & 1 deletion core/base/model/Manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def onStop(self):

def _initDB(self):
if self._databaseSchema:
return SuperManager.getInstance().databaseManager.initDB(schema=self._databaseSchema, callerName=self.name)
return SuperManager.getInstance().DatabaseManager.initDB(schema=self._databaseSchema, callerName=self.name)
return True


Expand Down
Loading

0 comments on commit be572d0

Please sign in to comment.