MagicMirror Forum
    • Recent
    • Tags
    • Unsolved
    • Solved
    • MagicMirror² Repository
    • Documentation
    • 3rd-Party-Modules
    • Donate
    • Discord
    • Register
    • Login
    1. Home
    2. bimbim2401
    3. Best
    A New Chapter for MagicMirror: The Community Takes the Lead
    Read the statement by Michael Teeuw here.
    B
    Offline
    • Profile
    • Following 0
    • Followers 0
    • Topics 4
    • Posts 12
    • Groups 0

    Posts

    Recent Best Controversial
    • RE: Module Sending notification to a python project

      Hey,

      i have succeed to make it works with the following in the main.py, my problem was due to the port which was already in use, and then i have to take another one.

      add the import

      from threading import Thread
      from urllib.parse import unquote
      

      then just before the main function add

      class WebSocketsListener(Thread):
      """Thread chargé d'ecouter les messages sur la websocket."""
      
      def __init__(self, assistant):
          Thread.__init__(self)
          self.websocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
          self.websocket.bind(('127.0.0.1', 8081))
          self.assistant = assistant
      
      
      def run(self):
          print ("Open socket")
      
          while True:
              self.websocket.listen(5)
              client, address = self.websocket.accept()
              print (format( address ))
      
              response = client.recv(255)
              if response != "":
                  print (response)
                  str_response = response.decode("utf-8")
                  if 'assistant' in str_response:
                      
                      if 'mode' in str_response:
                          if 'start' in str_response:
                              print("start the assistant") 
                              self.assistant.start_conversation()
                          if 'stop' in str_response: 
                              print("stop  the assistant") 
                              self.assistant.stop_conversation()
      
                      elif 'say' in str_response:
                          token = str_response[str_response.find("say=")+4:str_response.find("HTTP")-1]
                          token = unquote(token)
                          say(token)
      
          print ("Close socket")
          client.close()
          stock.close()
      

      in the main function just before the for event in events

          webSocketsListener = WebSocketsListener(assistant)
          webSocketsListener.start()
      

      With this, open a web browser and write
      http://127.0.0.1:8081/assistant?mode=start
      it will start manually the assistant like with the hotword"ok google"
      http://127.0.0.1:8081/assistant?mode=stop
      to stop the assistant if the conversation is too long
      http://127.0.0.1:8081/assistant?say=Hello karim how are you
      and then gassistPI will say what you write

      in the magic mirror module, you have to send this request in the node helper.js

       socketNotificationReceived: function(notification, payload) {
      
          var res = "";
          var ipadress = "127.0.0.1";
          var port = "8081";
      
      
      console.log("google assistant Node Helper Received a socket notification: " + notification + " payload:" + payload);
      if (notification === "ASSISTANT_START"){
      	request("http://"+ipadress+":"+port+"/assistant?mode=start", { json: true }, (err, res, body) => {
      		if (err) { return console.log(err); }
      		console.log(body.url);
      		console.log(body.explanation);
      	});
      }
      else if (notification === "ASSISTANT_STOP"){
      	request("http://"+ipadress+":"+port+"/assistant?mode=stop", { json: true }, (err, res, body) => {
      		if (err) { return console.log(err); }
      		console.log(body.url);
      		console.log(body.explanation);
      	});
      	
      }
      else if (notification === "ASSISTANT_SAY"){
      	console.log("assistant payload:"+payload);
      	request("http://"+ipadress+":"+port+"/assistant?say="+payload, { json: true }, (err, res, body) => {
      		if (err) { return console.log(err); }
      		console.log(body.url);
      		console.log(body.explanation);
      	});
      }		
      }	
      

      Thanks for your help

      posted in Requests
      B
      bimbim2401
    • 1 / 1