oldMain.txt 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. from snipsTools import SnipsConfigParser
  2. from xiSnipsTools import Personality
  3. from hermes_python.ontology import *
  4. from hermes_python.hermes import Hermes
  5. from hermes_python.ontology import MqttOptions
  6. import common
  7. from Action_Verlassen import Action_Verlassen
  8. from Action_Begruessen import Action_Begruessen
  9. from Action_Schlafenszeit import Action_Schlafenszeit
  10. from Action_Device_Programmierer import Action_Device_Programmierer
  11. from Action_Device_Kompliment import Action_Device_Kompliment
  12. from Action_Device_Wer_Bist_Du import Action_Device_Wer_Bist_Du
  13. from Action_Device_Hobbies import Action_Device_Hobbies
  14. from Action_wie_geht_es_dir import Action_wie_geht_es_dir
  15. from Action_Device_Alter import Action_Device_Alter
  16. from Action_Beleidigend import Action_Beleidigend
  17. from Action_Ankommen import Action_Ankommen
  18. from Action_Device_Hungrig import Action_Device_Hungrig
  19. from Action_Guten_Morgen import Action_Guten_Morgen
  20. from Action_Device_Gedanken import Action_Device_Gedanken
  21. import io
  22. import os
  23. import toml
  24. import datetime
  25. MQTT_IP_ADDR = "localhost"
  26. MQTT_PORT = 1883
  27. MQTT_ADDR = "{}:{}".format(MQTT_IP_ADDR, str(MQTT_PORT))
  28. class KuriisSmallTalk(object):
  29. """Action Code for the snips App "SmallTalk"
  30. Smalltalk ist my first test app for snips and in the python prgramming language
  31. """
  32. config = None
  33. def __init__(self):
  34. snips_config = toml.load('/etc/snips.toml')
  35. mqtt_username = None
  36. mqtt_password = None
  37. mqtt_broker_address = "localhost:1883"
  38. if 'mqtt' in snips_config['snips-common'].keys():
  39. mqtt_broker_address = snips_config['snips-common']['mqtt']
  40. if 'mqtt_username' in snips_config['snips-common'].keys():
  41. mqtt_username = snips_config['snips-common']['mqtt_username']
  42. if 'mqtt_password' in snips_config['snips-common'].keys():
  43. mqtt_password = snips_config['snips-common']['mqtt_password']
  44. mqtt_opts = MqttOptions(username=mqtt_username, password=mqtt_password, broker_address=mqtt_broker_address)
  45. self.init_callbacks(Hermes(mqtt_options=mqtt_opts))
  46. # --> Master callback function, triggered everytime an intent is recognized
  47. def init_callbacks(self,hermes):
  48. with hermes as h:
  49. h\
  50. .subscribe_intent("BP-Kurii:Device_Programmierer", Action_Device_Programmierer.callback) \
  51. .subscribe_intent("BP-Kurii:Verlassen", Action_Verlassen.callback) \
  52. .subscribe_intent("BP-Kurii:Schlafenszeit", Action_Schlafenszeit.callback) \
  53. .subscribe_intent("BP-Kurii:Device_Kompliment", Action_Device_Kompliment.callback) \
  54. .subscribe_intent("BP-Kurii:Begruessen", Action_Begruessen.callback) \
  55. .subscribe_intent("BP-Kurii:Device_Wer_Bist_Du", Action_Device_Wer_Bist_Du.callback) \
  56. .subscribe_intent("BP-Kurii:Device_Hobbies", Action_Device_Hobbies.callback) \
  57. .subscribe_intent("BP-Kurii:wie_geht_es_dir", Action_wie_geht_es_dir.callback) \
  58. .subscribe_intent("BP-Kurii:Device_Alter", Action_Device_Alter.callback) \
  59. .subscribe_intent("BP-Kurii:Beleidigend", Action_Beleidigend.callback) \
  60. .subscribe_intent("BP-Kurii:Ankommen", Action_Ankommen.callback) \
  61. .subscribe_intent("BP-Kurii:Device_Hungrig", Action_Device_Hungrig.callback) \
  62. .subscribe_intent("BP-Kurii:Guten_Morgen", Action_Guten_Morgen.callback) \
  63. .subscribe_intent("BP-Kurii:Device_Gedanken", Action_Device_Gedanken.callback) \
  64. .start()
  65. # --> Sub callback function, one per intent
  66. def howareyou_callback(self, hermes, intent_message):
  67. # terminate the session first if not continue
  68. hermes.publish_end_session(intent_message.session_id, "")
  69. # action code goes here...
  70. print('[Received] intent: {}'.format(intent_message.intent.intent_name))
  71. message = "null"
  72. # Read CPU temperature
  73. cpu_temp = os.popen("sh getcputemp.sh").readline()
  74. message = "null"
  75. try:
  76. cpu_temp2 = float(cpu_temp)
  77. if cpu_temp2 < 60:
  78. message = hay_personality.get_AnswerToTopic("well").format(cpu_temp)
  79. else:
  80. message = hay_personality.get_AnswerToTopic("notwell").format(cpu_temp)
  81. except :
  82. message = hay_personality.get_AnswerToTopic("null")
  83. print("Fehler bei der Ermittlung der cpu Temperatur \n Moegliche Ursachen:\n 1. Nutzer _snips-skills nicht in Rechtegruppe video\n 2. Skill wird nicht auf Raspberry ausgefuehrt")
  84. # if need to speak the execution result by tts
  85. hermes.publish_start_session_notification(intent_message.site_id, message, "SnipsSmallTalkAPP")
  86. def whatdoyouthink_callback(self, hermes, intent_message):
  87. # terminate the session first if not continue
  88. hermes.publish_end_session(intent_message.session_id, "")
  89. # action code goes here...
  90. print('[Received] intent: {}'.format(intent_message.intent.intent_name))
  91. message = "null"
  92. topic = "null"
  93. if len(intent_message.slots.topic) > 0:
  94. topic = str(intent_message.slots.topic.first().value)
  95. print("Erkannter Topic Wert: " + topic)
  96. message = wdyt_personality.get_AnswerToTopic(topic)
  97. print(message)
  98. # if need to speak the execution result by tts
  99. hermes.publish_start_session_notification(intent_message.site_id, message, "SnipsSmallTalkAPP")
  100. def completeidiom_callback(self, hermes, intent_message):
  101. # terminate the session first if not continue
  102. hermes.publish_end_session(intent_message.session_id, "")
  103. # action code goes here...
  104. print('[Received] intent: {}'.format(intent_message.intent.intent_name))
  105. message = "null"
  106. idiom = str(intent_message.input)
  107. print("Erkannter Message Idiom Wert: " + idiom)
  108. message = ci_personality.get_AnswerToTopic(idiom)
  109. print(message)
  110. # if need to speak the execution result by tts
  111. hermes.publish_start_session_notification(intent_message.site_id, message, "SnipsSmallTalkAPP")
  112. def tellmeajoke_callback(self, hermes, intent_message):
  113. # terminate the session first if not continue
  114. hermes.publish_end_session(intent_message.session_id, "")
  115. # action code goes here...
  116. print('[Received] intent: {}'.format(intent_message.intent.intent_name))
  117. message = "null"
  118. category = "null"
  119. if len(intent_message.slots.category) > 0:
  120. category = str(intent_message.slots.category.first().value)
  121. message = tmaj_personality.get_AnswerToTopic(category)
  122. else:
  123. message = tmaj_personality.get_RandomContent()
  124. print("Erkannte Witz Kategorie: " + category)
  125. print(message)
  126. # if need to speak the execution result by tts
  127. hermes.publish_start_session_notification(intent_message.site_id, message, "SnipsSmallTalkAPP")
  128. hermes.publish_end_session(intent_message.session_id, "[[sound:test]]")
  129. if __name__ == "__main__":
  130. common();
  131. KuriisSmallTalk()