MagicMirror Forum
    • Recent
    • Tags
    • Unsolved
    • Solved
    • MagicMirror² Repository
    • Documentation
    • 3rd-Party-Modules
    • Donate
    • Discord
    • Register
    • Login
    A New Chapter for MagicMirror: The Community Takes the Lead
    Read the statement by Michael Teeuw here.

    Module Sending notification to a python project

    Scheduled Pinned Locked Moved Solved Requests
    12 Posts 2 Posters 6.1k Views 2 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • B Offline
      bimbim2401
      last edited by

      Hello,

      can someone help me to create a simple module that send all notification from the magic mirror module to a python script. I need also some help to create the code in python to intercept the notification.

      Which goal ? I have a google assistant develop in python, i am using the facial recognition and i want to send notification from MMM-facial or other module to the google assistant which is develop in python to say something when a notification is received.

      here is the link of the google assistant project : https://github.com/shivasiddharth/GassistPi

      One other way is to create a module based on the python project but i do not know how to do it, and then it will received all the notification.

      I know there is a lot of assistant module for the magic mirror, but this one works very well, it has a lot of functionnality and i can configure it easyly in french.

      thanks so much.

      idoodlerI 1 Reply Last reply Reply Quote 0
      • idoodlerI Offline
        idoodler Module Developer @bimbim2401
        last edited by

        @bimbim2401 You just have to establish a Websocket connection from your Python module to the MagicMirror at port 8080.

        1 Reply Last reply Reply Quote 0
        • B Offline
          bimbim2401
          last edited by bimbim2401

          so i just need to create a websocket in a python script like this below ? and it will listen to all notification from the magic mirror sending by the method sendnotification ?

          import SocketServer

          class MyTCPHandler(SocketServer.BaseRequestHandler):
          “”"
          The RequestHandler class for our server.

          It is instantiated once per connection to the server, and must
          override the handle() method to implement communication to the
          client.
          """
          
          def handle(self):
              # self.request is the TCP socket connected to the client
              self.data = self.request.recv(1024).strip()
              print "{} wrote:".format(self.client_address[0])
              print self.data
              # just send back the same data, but upper-cased
              self.request.sendall(self.data.upper())
          

          if name == “main”:
          HOST, PORT = “localhost”, 8080

          # Create the server, binding to localhost on port 8080
          server = SocketServer.TCPServer((HOST, PORT), MyTCPHandler)
          
          # Activate the server; this will keep running until you
          # interrupt the program with Ctrl-C
          server.serve_forever()
          

          or do i have to add the MMM-WEBSOCKET module found here :
          https://github.com/JanLoebel/MMM-websocket

          idoodlerI 1 Reply Last reply Reply Quote 0
          • idoodlerI Offline
            idoodler Module Developer @bimbim2401
            last edited by

            @bimbim2401 Well, in Chrome I can see. That the MagicMirror Webclient establishes a Websocket connection to ws://IP:8080/socket.io/?EIO=3&transport=websocket&sid=SOME_ID. You can clearly see the communication, as the communication is not encrypted at all.

            1 Reply Last reply Reply Quote 0
            • B Offline
              bimbim2401
              last edited by bimbim2401

              Thanks both for your response

              Do you think that the python code I have wrote is ok or can you give me the direction for the python code for listening to the notification

              idoodlerI 1 Reply Last reply Reply Quote 0
              • idoodlerI Offline
                idoodler Module Developer @bimbim2401
                last edited by

                @bimbim2401 Well, my Python times are a long time ago. Just try to run this script ans log the output. I also highly recogmend you to open Chrome and look at the Websocket frames.

                1 Reply Last reply Reply Quote 0
                • B Offline
                  bimbim2401
                  last edited by

                  F12 and then network ? Right ?

                  idoodlerI 1 Reply Last reply Reply Quote 0
                  • idoodlerI Offline
                    idoodler Module Developer @bimbim2401
                    last edited by idoodler

                    @bimbim2401 Yep, on Windows its F12, on macOS its alt + cmd + i.

                    • Open the Network-Tab
                    • Look for the status code 101 (Switching Protocols) or just look for the type websocket
                    • Open the request
                    • Inspect the Socket as you wish (View frames, headers…)
                    1 Reply Last reply Reply Quote 0
                    • B Offline
                      bimbim2401
                      last edited by bimbim2401

                      Sorry but i cannot succeed to launch multiple script i found to intercept the get request from the module in python.

                      i need some help … sorry this is my first program in python language

                      Here is the script i want to modify : https://github.com/shivasiddharth/GassistPi/blob/master/src/main.py

                      idoodlerI 1 Reply Last reply Reply Quote 0
                      • idoodlerI Offline
                        idoodler Module Developer @bimbim2401
                        last edited by

                        @bimbim2401 I am not a Python guy either. Start small, create a simple Python script that establishes a Websocket connection, then interprete the websocket messages and so on…

                        1 Reply Last reply Reply Quote 0
                        • B Offline
                          bimbim2401
                          last edited by bimbim2401

                          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

                          idoodlerI 1 Reply Last reply Reply Quote 1
                          • idoodlerI Offline
                            idoodler Module Developer @bimbim2401
                            last edited by

                            @bimbim2401 Congratulations!

                            1 Reply Last reply Reply Quote 0

                            Hello! It looks like you're interested in this conversation, but you don't have an account yet.

                            Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

                            With your input, this post could be even better 💗

                            Register Login
                            • 1 / 1
                            • First post
                              Last post
                            Enjoying MagicMirror? Please consider a donation!
                            MagicMirror created by Michael Teeuw.
                            Forum managed by Sam, technical setup by Karsten.
                            This forum is using NodeBB as its core | Contributors
                            Contact | Privacy Policy