MagicMirror Forum

    • Register
    • Login
    • Search
    • Recent
    • Tags
    • Unsolved
    • Solved
    • MagicMirror² Repository
    • Documentation
    • Donate
    • Discord

    SOLVED Snowbound on node.js

    Troubleshooting
    3
    11
    1547
    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.
    • G
      grantc66 last edited by

      I’ve created a python script to interface with the mirror to do a follow a few simple instruction (play internet radio, adjust volume etc). This works well, but it is a bit irritating that every phone in the local area tries to respond as well as the mirror.

      So I integrated snowboy as a front end to activate Google’s listener. This works perfectly when running from a terminal but doesn’t respond when the python is called from the node helper.

      Can somebody explain why the same code doesn’t run when it’s initiated from the node helper?

      Sorry if it’s a bit of a basic question. I’ve tried googling but I can’t find anything that appears relevent.

      1 Reply Last reply Reply Quote 0
      • ?
        A Former User @grantc66 last edited by

        @grantc66
        Of course, Choice is yours. I fully cheer for your trials to skill up.

        By the way, these things I want to point out;

        • Generally NodeJS is dozens of times faster than Python 3
        • Snowboy detection engine is somekind of compiled-binaries from C(C++?) The each language is using its own wrapper, but that is the only difference among them.

        I modified code to measure the time for catching sound, distinguishing hotword, then sending Notification to other modules.
        0_1544688788772_pi.png
        I executed 10 times. Range was 15ms ~ 100ms, Average was around 40ms. This might be not the exact and professional measuring. But I think this is hard to say “VERY SLOW” for real usage.

        Anyway, I wish you good luck. Leave the result of this approach, I have an interest also.

        G 1 Reply Last reply Reply Quote 0
        • S
          sdetweil last edited by

          how do you execute this with node?

          Sam

          Create a working config
          How to add modules

          1 Reply Last reply Reply Quote 0
          • G
            grantc66 last edited by grantc66

            Node_helper.js runs a pythonshell when getting a notification from the module.

            Ah! It’s not running in the same environment as it does from the command line.

            Is that fixable? Would running it as a child process help?

            S 1 Reply Last reply Reply Quote 0
            • S
              sdetweil @grantc66 last edited by

              @grantc66 you would have to to use exec or spawn

              Sam

              Create a working config
              How to add modules

              1 Reply Last reply Reply Quote 1
              • G
                grantc66 last edited by grantc66

                Got it launching with spawn, the code without snowboy works well (have an issue with getting status messages passed, but I’ll fix that later)

                The version using the snowboy listener works until the snowboy decoder call. Then it stalls, doesn’t crash just freezes. Like the code is running but not communicating back to the python script.

                Couldn’t find anything on exec, this is the spawn version.

                node_helper.js

                const NodeHelper = require("node_helper");
                const spawn = require("child_process").spawn;
                var pythonStarted = false
                
                module.exports = NodeHelper.create({
                
                	socketNotificationReceived: function(notification, payload) {
                		if (notification === "MMM-PIRadio-NOTIFICATION_TEST") {
                			console.log("Working notification system. Notification:", notification, "payload: ", payload);
                
                			this.sendNotificationTest(this.anotherFunction()); //Is possible send objects :)
                		}
                		if (notification === "CONFIG") {
                			console.log("Got a request to start Python:", notification, "payload: ", payload);
                				if(!pythonStarted) {
                					pythonStarted = true;
                					this.python_start();
                				}
                		}
                	},
                
                	sendNotificationTest: function(payload) {
                		this.sendSocketNotification("MMM-PIRadio-NOTIFICATION_TEST", payload);
                	},
                	
                	sendData: function(payload) {
                		this.sendSocketNotification("PiRadiodata", payload);
                	},
                
                	extraRoutes: function() {
                		var self = this;
                		this.expressApp.get("/MMM-PIRadio/extra_route", function(req, res) {
                			// call another function
                			values = self.anotherFunction();
                			res.send(values);
                		});
                	},
                	python_start: function () {
                		const self = this
                
                                const callPy = spawn("/usr/bin/python3.5",["./modules/MMM-PIRadio/PiRadioSnow.py"]);
                                callPy.stdout.on('message', function (message) {
                
                			if (message.hasOwnProperty('status')){
                			console.log("[" + self.name + "]" + message.status);
                			}
                
                			if (message.hasOwnProperty('data')){
                			console.log("[" + self.name + "]" + message.data.PlrStat + " : " + message.data.Station + " : " + message.data.Said + " : " + message.data.volume);
                			self.sendData({"PlrStat": message.data.PlrStat, "TrkNxt":message.data.TrkNext, "Station": message.data.Station, "Said": message.data.Said, "volume": message.data.volume});
                			}
                		});
                
                	},
                
                });
                

                What am I doing wrong? Sorry about the formatting.

                S 1 Reply Last reply Reply Quote 0
                • S
                  sdetweil @grantc66 last edited by

                  @grantc66 don’t see anything obvious. But there is also an on.error() callback for spawn you should use. If the running script has a problem

                  Sam

                  Create a working config
                  How to add modules

                  G 1 Reply Last reply Reply Quote 0
                  • ?
                    A Former User last edited by

                    There is already snowboy integrated module. (MMM-Hotword). I suggest not to re-invent wheels twice.
                    You can emit notifications from MMM-Hotword to your module when your snowboy hotwords are detected. I think that is easier.

                    G 1 Reply Last reply Reply Quote 1
                    • G
                      grantc66 @Guest last edited by

                      @sean said in Snowbound on node.js:

                      MMM-Hotword

                      Tried that, it works but its very slow to respond. With the python script, you can say the hotword and give it instructions seamlessly, passing notifications to seems to take forever to respond.

                      What I’m trying to get to is understanding what node is doing with the python that stops external modules working & hopefully find away around it. Bit more of a learning exercise, if I can crack this it’ll open up my ability to expand the functionality.

                      ? 1 Reply Last reply Reply Quote 0
                      • G
                        grantc66 @sdetweil last edited by

                        @sdetweil Thanks, had it in there for the pythonshell forgot about it on this version.

                        Any pointers on trying exec, my googlefoo is failing to find any help.

                        1 Reply Last reply Reply Quote 0
                        • ?
                          A Former User @grantc66 last edited by

                          @grantc66
                          Of course, Choice is yours. I fully cheer for your trials to skill up.

                          By the way, these things I want to point out;

                          • Generally NodeJS is dozens of times faster than Python 3
                          • Snowboy detection engine is somekind of compiled-binaries from C(C++?) The each language is using its own wrapper, but that is the only difference among them.

                          I modified code to measure the time for catching sound, distinguishing hotword, then sending Notification to other modules.
                          0_1544688788772_pi.png
                          I executed 10 times. Range was 15ms ~ 100ms, Average was around 40ms. This might be not the exact and professional measuring. But I think this is hard to say “VERY SLOW” for real usage.

                          Anyway, I wish you good luck. Leave the result of this approach, I have an interest also.

                          G 1 Reply Last reply Reply Quote 0
                          • 1
                          • 2
                          • 1 / 2
                          • First post
                            Last post
                          Enjoying MagicMirror? Please consider a donation!
                          MagicMirror created by Michael Teeuw.
                          Forum managed by Paul-Vincent Roll and Rodrigo Ramírez Norambuena.
                          This forum is using NodeBB as its core | Contributors
                          Contact | Privacy Policy