• 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.

Building my first module, using MMM-API

Scheduled Pinned Locked Moved Development
6 Posts 2 Posters 2.3k 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.
  • ? Offline
    A Former User @PTcrusher
    last edited by Oct 9, 2018, 5:56 PM

    @ptcrusher
    As I read the document of MMM-API, ACTION would be the name of notification of target module. (It’s obvious whether notification could be case-insensitive.)
    By example,
    MMM-Glance module works with notification GLANCE_ON, Thus MMM-API doc says

    curl -X POST 'http://localhost:3001/api/v1/modules/mmm-glance/glance_on?name=news&time=1000'
    

    will activate MMM-Glance. This curl execution will be equivalent with the notification GLANCE_ON with payload value {name:"news", time:1000}.

    P 1 Reply Last reply Oct 9, 2018, 6:06 PM Reply Quote 0
    • P Offline
      PTcrusher @Guest
      last edited by Oct 9, 2018, 6:06 PM

      @sean Got it! Thanks

      1 Reply Last reply Reply Quote 0
      • P Offline
        PTcrusher
        last edited by Nov 5, 2018, 5:32 PM

        @Sean Did some progress on the module. I removed the dependency on MMM-API since we can define extra routes in node_helper.js.

        Now i have a doubt. Should i keep the values that i wish to retrieve in a variable and use the socketNotificationSend/socketNotificationReceived logic or… should i call the extra-routes from my module (MMM-PiggyBank.js) pretending that i’m accessing an external API?

        Could you kindly tell me which is the recommended approach?

        extraRoutes: function() {
                        var self = this;
                        this.expressApp.get("/MMM-PiggyBank/WITHDRAW", function(req, res) {
                                self.withdrawCash().then((rsp) => res.status(rsp.status).send(rsp.response));
                        });
                        this.expressApp.post("/MMM-PiggyBank/CASH_IN", function(req, res) {
                                self.updateCash(req.query.target,req.query.current).then((rsp) => res.status(rsp.status).send(rsp.response));
                        });
                },
        
               updateCash: async function(target,current) {
                        var self = this;
                        try {
                                await self.checkExistance(self.fullyQualFileName());
                                var oldcontent = await readFile(self.fullyQualFileName());
                                var contentcandidate = await self.valChanges(target, current, JSON.parse(oldcontent));
                                var newcontent = await writeFile(self.fullyQualFileName(), JSON.stringify(contentcandidate));
                                return self.buildSuccessRsp("Successfully saved!");
                        }
                        catch(err) {
                                return self.buildErrRsp(err);
                        }
                },
        
                withdrawCash: async function() {
                        var self = this;
                        try {
                                await self.checkExistance(self.fullyQualFileName());
                                var oldcontent = await readFile(self.fullyQualFileName());
                                return self.buildSuccessRsp(JSON.parse(oldcontent));
                        }
                        catch(err) {
                                return self.buildErrRsp(err);
                        }
                },
        
        ? 1 Reply Last reply Nov 5, 2018, 9:08 PM Reply Quote 0
        • ? Offline
          A Former User @PTcrusher
          last edited by Nov 5, 2018, 9:08 PM

          @ptcrusher
          I think, there might be no silver bullet or Deus Ex Machina about that kinds of question. I believe anything be good if you can handle it.

          1 Reply Last reply Reply Quote 0
          • P Offline
            PTcrusher
            last edited by Dec 18, 2018, 11:38 AM

            Finally finished my first module.
            Nothing too fancy but fits my requirements.

            GitHub Link

            1 Reply Last reply Reply Quote 0
            • 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