Skip to main content

IoT Sample with NodeRed functions and HTTP request handlers



Scenario :

"/time" GET request returns server time in JSON format(Format: {"time" : "hh:mm"}).


 

"/random" GET request returns generated random number in JSON format(Format: {"random" : number}).


 

"/page" GET request returns last request date & time, generated last number in HTML format. Output values as below;



  1. when the last time was requested
  2. when the last random number was requested
  3. the value of the last random number generated


Twist:


  • Work with HTTP requests and responses
  • Generate random numbers and work with those
  • Switch responses based on logic


Mustache view templating



 

Code as below:



 

[{"id":"ad8758cf.4b2dc8","type":"tab","label":"Flow 1"},{"id":"2b2cf6bf.d6f44a","type":"template","z":"ad8758cf.4b2dc8","name":"ContainsHistory","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"Time last server time request received at {{time}}\nLast random number request returned {{number}}, which was received at {{time}}\n","x":669.5001373291016,"y":400.60013580322266,"wires":[["9e751b7c.6d4818"]]},{"id":"3f9df85.189ed08","type":"http response","z":"ad8758cf.4b2dc8","name":"","x":954.5000152587891,"y":160.60002899169922,"wires":[]},{"id":"e5693da4.654c","type":"http in","z":"ad8758cf.4b2dc8","name":"/random","url":"/random","method":"get","swaggerDoc":"","x":74.5,"y":203.60001373291016,"wires":[["f4dc09d8.95a4e8"]]},{"id":"803bc5e3.cbe218","type":"function","z":"ad8758cf.4b2dc8","name":"setHTTPheader-JSON","func":"// If sending JSON data the content type\nmsg.headers={\"Content-Type\":\"application/json\"}\n\nreturn msg;","outputs":1,"noerr":0,"x":797.5000762939453,"y":101.5999984741211,"wires":[["3f9df85.189ed08"]]},{"id":"f4dc09d8.95a4e8","type":"random","z":"ad8758cf.4b2dc8","name":"RandomNumber","low":"1","high":"100","inte":"true","x":266.5,"y":203.60001373291016,"wires":[["59d88bde.a671b4","fba26957.70eeb8"]]},{"id":"4e94a6a5.21a4d8","type":"function","z":"ad8758cf.4b2dc8","name":"ReturnTime","func":"if ( !msg.timestamp ) \n msg.timestamp = Math.round(+new Date());\n\nvar dt = new Date(msg.timestamp);\nvar hour = dt.getHours();\nvar minute = dt.getMinutes();\n\nif(hour.toString().length == 1) {\n hour = '0'+hour;\n}\nif(minute.toString().length == 1) {\n minute = '0'+minute;\n}\n\nmsg.payload = '{\"time\":\"'+hour + \":\" + minute+'\"}';\n\nreturn msg;","outputs":1,"noerr":0,"x":292.50001525878906,"y":64.60000610351562,"wires":[["cdee7677.f9c488"]]},{"id":"2a1f832a.3c389c","type":"comment","z":"ad8758cf.4b2dc8","name":"\"/time\" GET request returns server time in JSON format(Format: {\"time\" : \"hh:mm\"}).","info":"\"/time\" GET request returns server time in JSON format(Format: {\"time\" : \"hh:mm\"}).","x":302,"y":20,"wires":[]},{"id":"5fe025ed.d17e0c","type":"http in","z":"ad8758cf.4b2dc8","name":"/time","url":"/time","method":"get","swaggerDoc":"","x":63,"y":66.99999237060547,"wires":[["4e94a6a5.21a4d8"]]},{"id":"cdee7677.f9c488","type":"json","z":"ad8758cf.4b2dc8","name":"ConvertToJSON","x":592.9999542236328,"y":56.400047302246094,"wires":[["803bc5e3.cbe218"]]},{"id":"7e33a06e.cb226","type":"comment","z":"ad8758cf.4b2dc8","name":" \"/random\" GET request returns generated random number in JSON format(Format: {\"random\" : number}).","info":"\"/random\" GET request returns generated random number in JSON format(Format: {\"random\" : number}).","x":373.5,"y":153.60001373291016,"wires":[]},{"id":"f035558a.f54fb8","type":"comment","z":"ad8758cf.4b2dc8","name":"\"/page\" GET request returns last request date & time, generated last number in HTML format.","info":"\"/page\" GET request returns last request date & time, generated last number in HTML format.","x":333.3999938964844,"y":270.6000442504883,"wires":[]},{"id":"4243d625.950ee8","type":"switch","z":"ad8758cf.4b2dc8","name":"Switch HTML","property":"payload","propertyType":"msg","rules":[{"t":"null"},{"t":"else"}],"checkall":"true","outputs":2,"x":477.0000762939453,"y":339.00003814697266,"wires":[["811ef4f9.686138"],["2b2cf6bf.d6f44a"]]},{"id":"811ef4f9.686138","type":"template","z":"ad8758cf.4b2dc8","name":"No History","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"No server time requests have been received\nNo requests for random numbers have been received\n","x":679.0000152587891,"y":313.0000457763672,"wires":[["9e751b7c.6d4818"]]},{"id":"59d88bde.a671b4","type":"function","z":"ad8758cf.4b2dc8","name":"BuildJSON","func":"msg.payload = '{\"random\":\"'+ msg.payload +'\"}';\nreturn msg;","outputs":1,"noerr":0,"x":534.9999847412109,"y":195.60015106201172,"wires":[["cdee7677.f9c488"]]},{"id":"fba26957.70eeb8","type":"function","z":"ad8758cf.4b2dc8","name":"Build&StoreToGlobalContext","func":"if ( !msg.timestamp ) \n msg.timestamp = Math.round(+new Date());\n\nvar dt = new Date(msg.timestamp);\nvar hour = dt.getHours();\nvar minute = dt.getMinutes();\n\nif(hour.toString().length == 1) {\n hour = '0'+hour;\n}\nif(minute.toString().length == 1) {\n minute = '0'+minute;\n}\n\n//msg.payload = '{\"random\":\"'+ msg.payload +'\", \"time\":\"'+hour + \":\" + minute+'\"}';\nvar jsonObj = msg.payload = '{\"random\":\"'+ msg.payload +'\", \"time\":\"'+hour + \":\" + minute+'\"}';\n//\n//set to global - access from anywhere\nglobal.set(\"randomJSON\",jsonObj);\n\n//return msg;","outputs":1,"noerr":0,"x":726.9091339111328,"y":233.96366119384766,"wires":[[]]},{"id":"c3b19b52.861488","type":"debug","z":"ad8758cf.4b2dc8","name":"time","active":false,"console":"false","complete":"time","x":401.0000457763672,"y":445.80003356933594,"wires":[]},{"id":"6d233601.4670b8","type":"function","z":"ad8758cf.4b2dc8","name":"AccessGlobalContext","func":"//Get gloabal variables\nvar globalValue = global.get(\"randomJSON\");\nnode.warn(globalValue);\n\nif(globalValue)\n{ globalValue = JSON.parse(globalValue);\n msg.time = globalValue.time;\n msg.number = globalValue.random;\n \n}\nelse {\n msg.payload = null;\n}\n\nreturn msg;","outputs":1,"noerr":0,"x":245.00006103515625,"y":325.00003814697266,"wires":[["4243d625.950ee8","c3b19b52.861488"]]},{"id":"32f866bd.fb62ea","type":"http response","z":"ad8758cf.4b2dc8","name":"","x":951.0000762939453,"y":448.00003814697266,"wires":[]},{"id":"ed21455e.d83e58","type":"http in","z":"ad8758cf.4b2dc8","name":"/page","url":"/page","method":"get","swaggerDoc":"","x":55,"y":327.4000015258789,"wires":[["6d233601.4670b8"]]},{"id":"9e751b7c.6d4818","type":"function","z":"ad8758cf.4b2dc8","name":"setHTTPheader-HTML","func":"// If sending JSON data the content type is:\n//msg.headers={\"Content-Type\":\"application/json\"}\n\n// For HTML use the content type line below:\nmsg.headers={\"Content-Type\":\"text/html\"}\nreturn msg;","outputs":1,"noerr":0,"x":879.5000152587891,"y":352.6001739501953,"wires":[["32f866bd.fb62ea"]]}]


<

p style="margin-left:36pt;">

Comments

Popular posts from this blog

Turn off/ Hide Details panel/ Information Panel on modern SharePoint lists

Not always we require to show changes done by other which is a mandatory feature in SharePoint online.  What is details pane (aka. Information Pane)? Detail pane/ Information shows information regarding the document if you selected a one or its showing recent changes within a list or library. Follow link to Microsoft documentation about details pane. Bad news: Until Microsoft listen to User Voice , there is no straightforward way to enable disable this even you don't want. Good news: We could write a SharePoint framework extension to hack styles until Microsoft give us a permanent solution. How? I found this sample project (Inject CSS into modern SharePoint pages with React) which could reuse to our purpose. Thanks to Hugo for saving my time.  Steps to awesomeness:  Clone the project Resolve dependencies >  npm i Bundle >  gulp bundle --ship Package >  gulp package-solution --ship Upload package into SharePoint App catalog  

📢 Update -Top 10 Microsoft Teams questions from customers around the world

Over the last 4 months, we’ve traveled to 9 countries on Microsoft Ignite | The Tour meeting and speaking with users like you about Microsoft Teams. The insights and feedback at every location has been invaluable — Thank you. During our travels, there have been 10 questions that have repeatedly surfaced. Below are all of the questions — including the answers too, of course!   Can you restrict access to a specific channel in a team? What are best practices for governance, specifically around team creation, naming, and retention? Can I add guests to my team? If so, how? How does external access/federation work? How is this different to guest access? What functionalities do I get in a hybrid environment? Is Teams available for on-prem environments? How do I configure Direct Routing in Microsoft Teams? Why should I move from Skype for Business to Teams? How do I make this transition? I have existing SharePoint sites that I would like to bring into Teams. Can I do that? I need t

Eliminate Duplicates in Microsoft Flow and Azure Logic Apps

There is no straight forward method or Microsoft Flow action  to eliminate values in a collection like Microsft Excel (as of March 2019). This little hack will help you to eliminate/ remove duplicates and create a collection of unique values. Sample scenario: Need to send daily summarized notification to assigned approved with pending approval items. Sample workflow: Initialize an array Iterate collection of data and append items that are not there in the filtered array Side note: This won't be an option to consider if you too concerned about the performance of the workflow but for the time being its an option to consider.