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 linkto 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 projectResolve dependencies > npm iBundle > gulp bundle --shipPackage > gulp package-solution --shipUpload package into SharePoint App catalog and deploy. You could find package under SharePoint folder.A…

PowerApps Delegation warning

Warning:Delegation warning. This part "Filter" of this formula might not work correctly on large datasets. The data source might not be able to process the formula and might return an incomplete data set. Your application might not return correct results or behave correctly if the data set is incomplete
"Working with large datasets requires using data sources and formulas that can be delegated" - PowerApps 










Solution!!!500 is the default number of records to pull, but you can change this number for an entire app. 

By altering the number of delegation queries (not recommended), you will be able to pull items up to 2000 (as of 2018). 
On the File tab, select App settings.Find Experimental features, change the Data row limit for non-delegable queries setting from 1 to 2000.










Data sources that will help with delegation:Common Data Service

📢 Update -Microsoft Teams IP Phones and Intune Enrollment

For customers who require desk phones and conference room phones to make and receive audio calls or join meetings, Microsoft Teams provides a growing portfolio of devices that can be purchased from our Teams Marketplace. For Teams phones including the Yealink T56A/T58A/CP960 and the Crestron Flex series IP phones that run on Android 5.x or later, there may be specific configurations that need to be enabled in the customer's tenant for the phones to successfully enroll into Intune. Allowing successful Intune enrollment for Android versions 5.x and upIf all the following conditions below are true, you will need to enable a specific configuration setting in the Intune admin console to allow for a successful enrollment:You are deploying a Teams IP phone with Android OS version 5.x or later.You have connected your Intune tenant with managed Google Play in order to manage Android Enterprise devices.You have configured your enrollment restrictions such that Android work profile enrollmen…