• Recent
  • Tags
  • Unsolved
  • Solved
  • MagicMirror² Repository
  • Documentation
  • 3rd-Party-Modules
  • Donate
  • Discord
  • Register
  • Login
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.

Timer based on python script

Scheduled Pinned Locked Moved Development
5 Posts 2 Posters 1.2k 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.
  • S Offline
    scotchi
    last edited by Nov 20, 2018, 9:10 PM

    Hello,
    I’ve just got my MM up and running but sadly struggle with making a timer showing how long until screen goes off.

    My MM has python script that controls when to turn on/off screen based on some sensors and time. It starts internal timer 60s when there is nobody in front of the mirror, turn screen off when timer goes off and resets it when it registers movement.

    Although I’m terrible with JS I have somehow managed to create new module, that simply show remaining time.

    I’d love to ask for some tips how to get get remaining time from python script to MM module. So far my only idea was to write the number into a file and let the module read it, but I’m sure there must be better way as this just sounds terrible considering I’d like to update it at least once a second.

    ? 1 Reply Last reply Nov 20, 2018, 10:21 PM Reply Quote 0
    • ? Offline
      A Former User @scotchi
      last edited by Nov 20, 2018, 10:21 PM

      @scotchi
      I’m not with my pc at this moment, so I cannot
      give you the details, but google these;

      • nodejs child-process exec and spawn

      Your python script might spit stdout as a result of running. exec or spawn can control runnig and termination of child-process(e.g your script) and get stdout or stdere from it. You can make to display those in your mm module.

      1 Reply Last reply Reply Quote 0
      • S Offline
        scotchi
        last edited by scotchi Nov 21, 2018, 10:49 AM Nov 21, 2018, 10:37 AM

        Thank for reply, but you can’t used it for already running script, can you? What I understood is, that is runs new py script.

        Although it would probably work, I was looking for something that can listen/request data from already running script.

        I was hoping I can call some function MagicFunction(localhost, port?, data) from python… and my module could have something like listen(localhost,port?); .onReceive(){ data = fromPython; }

        I hope it makes sense.

        ps: I understand there are better and simpler solutions how to turn on/off monitor, but this is supposed to be only beginning of what I’m trying to accomplish

        EDIT: I just discovered there is “Socket.IO”. It sounds like exactly what I’m looking for. But honestly the more I read about it the more I’m confused how would I used it in my module.

        ? 1 Reply Last reply Nov 21, 2018, 10:49 AM Reply Quote 0
        • ? Offline
          A Former User @scotchi
          last edited by A Former User Nov 21, 2018, 10:51 AM Nov 21, 2018, 10:49 AM

          @scotchi
          Hmmm. I think controlling external script in nodeJs with child-process is better, but choice is yours. :D
          As you said, via HTTP could be one solution.

          • Your Python(Python 3) sender sript :
          import urllib.request
          contents = urllib.request.urlopen("http://localhost:8080/GATEWAY?something").read()
          
          • MM module
            • your module XXXX.js
          ...
          socketNotificationReceived: function(noti, payload) {
            if (noti == "RECEIVED_DATA") {
              console.log(payload)
            }
          }
          ...
          
          • it’s node_helper.js
          const NodeHelper = require("node_helper")
          const bodyParser = require("body-parser")
          
          module.exports = NodeHelper.create({
            start: function() {
              this.expressApp.use(bodyParser.json())
              this.expressApp.use(bodyParser.urlencoded({extended: true}))
          
              this.expressApp.get("/GATEWAY", (req, res) => {
                this.sendSocketNotification("RECEIVED_DATA", req.body)
                res.status(200).send({status: 200})
              })
            }
          })
          

          If you think handling node_helper.js is somehow difficult, consider MMM-NotificationTrigger. It has already a feature to receive from HTTP request then relay to other module via notification.

          1 Reply Last reply Reply Quote 0
          • S Offline
            scotchi
            last edited by Nov 21, 2018, 10:56 AM

            This is perfect!

            I need few hours on google now, to at least begin to comprehend what you just wrote…

            Thank you!! :) I’ll post when I get it somehow work (or get stuck :D )

            1 Reply Last reply Reply Quote 0
            • 1 / 1
            1 / 1
            • First post
              1/5
              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