robot.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. # -*- coding: utf-8 -*-
  2. from __future__ import division
  3. from .__load__ import *
  4. class Robot(object):
  5. def init(self, profiling=False, logout=False):
  6. # 大脑
  7. self.brain = Demeter.service('brain').init(self)
  8. # 耳朵
  9. self.ear = Demeter.service('ear').init(self)
  10. # 嘴
  11. self.mouth = Demeter.service('mouth').init(self)
  12. # 眼睛
  13. self.eye = Demeter.service('eye').init(self)
  14. # 行为工具
  15. self.tool = {}
  16. # 数据文件夹
  17. self.data = Demeter.config['vecan']['data']
  18. # 日志
  19. Demeter.logger = self.logger = Log.init(__name__, logout)
  20. # 是否正在录音
  21. self.isRecording = False
  22. # 插件验证--暂时无用
  23. self.matchPlugin = None
  24. # 勿扰模式
  25. self.immersiveMode = None
  26. # 优化
  27. self.profiling = profiling
  28. # 醒来 启动
  29. self.wakeup()
  30. def wakeup(self):
  31. # 机器人配置,后续改成从数据库读取
  32. self.config = Demeter.config['robot']
  33. for item in self.config:
  34. setting = self.setting(self.config[item])
  35. if item not in self.tool:
  36. self.logger.info('使用' + self.config[item] + ' ' + item + '引擎')
  37. self.tool[item] = Demeter.service(self.config[item], 'api')
  38. self.tool[item].setting(**setting)
  39. self.brain.wakeup()
  40. def wait(self):
  41. try:
  42. self.ear.wait()
  43. except AttributeError:
  44. self.logger.critical('初始化离线唤醒功能失败')
  45. def sleep(self, signal, frame):
  46. clean()
  47. self.brain.pause()
  48. self.ear.close()
  49. self.mouth.close()
  50. self.eye.close()
  51. Demeter.dog.stop()
  52. def say(self, msg, cache=True, plugin='', completed=None):
  53. self.mouth.say(msg, cache, plugin, completed)
  54. def checkRestore(self):
  55. if self.immersiveMode:
  56. self.brain.restore()
  57. def setting(self, name):
  58. # 后续要改成数据库读取
  59. config = Demeter.config[name]
  60. return config
  61. # 中断、停止
  62. def stop(self):
  63. self.mouth.close()
  64. if self.immersiveMode:
  65. self.brain.pause()
  66. def doParse(self, query, **args):
  67. return self.tool['nlu'].nlu(query, **args)
  68. def setImmersiveMode(self, slug):
  69. self.immersiveMode = slug
  70. def getImmersiveMode(self):
  71. return self.immersiveMode