Node-Red亂數產生 暨 函數的寫法
const MIN = 1; const MAX = 99; //The maximum is exclusive and the minimum is inclusive msg.payload =function getRandomInt(min = MIN, max = MAX) { min = Math.ceil(min); max = Math.floor(max); return Math.floor(Math.random() * (max - min)) + min; } return msg; |
const MIN = 1; const MAX = 99; //The maximum is exclusive and the minimum is inclusive function getRandomInt(min = MIN, max = MAX) { min = Math.ceil(min); max = Math.floor(max); return Math.floor(Math.random() * (max - min)) + min; }
var no1=getRandomInt(min = MIN, max = MAX); var no2=getRandomInt(min = MIN, max = MAX); var no3=getRandomInt(min = MIN, max = MAX); var no4=getRandomInt(min = MIN, max = MAX);
var payload=[]; var obj={"steve":no1,"Bella":no2}; payload.push(obj);
var obj={"shanna":no3,"saskia":no4}; payload.push(obj); msg.payload=payload; return msg; |
//===setting=== var hpoint = 100; var lpoint = 0; var cent = 10; //centralize number //===============
var mpoint = 0; var output = context.get('output')||0; var addpoint = context.get('addpoint')||0;
//count var count = context.get('count')||0; count += 1; context.set('count',count);
//rcount var rcount = context.get('rcount')||0; if(count >= rcount){ for(i=0;i<cent;i++){ //centralize mpoint = mpoint + Math.floor(Math.random() * hpoint); } rcount = parseInt(Math.floor(Math.random() * 100)/5); addpoint = ((mpoint/cent)-output)/rcount; context.set('addpoint',addpoint); context.set('rcount',rcount); context.set('count',0); }else{ output = parseFloat((output + addpoint).toFixed(1)); context.set('output',output); } msg.payload = output; return msg; |
const MIN = 1; const MAX = 99; //The maximum is exclusive and the minimum is inclusive
switch (msg.payload) {
case "undefined" : msg.payload = undefined; break;
case "null" : msg.payload = null; break;
case "random" : msg.payload =function getRandomInt(min = MIN, max = MAX) { min = Math.ceil(min); max = Math.floor(max); return Math.floor(Math.random() * (max - min)) + min; }(); break; }
return msg; |
Node-Red程式
[{"id":"b49f314d.e60eb","type":"ui_template","z":"a9c9090b.478498","group":"c7f6cd2081a9366a","name":"","order":13,"width":0,"height":0,"format":"<style>\n \n.table\n{\n\n width:500px;\n background:lightblue;\n border:1px solid black;\n//font-size:10px;\n}\n.main\n{\n
height:400px;\n}\n\n</style>\n<div
class=\"main\">\n
<table class=\"table\">\n <tr ng-repeat = \"payload in
msg.payload\"><td ng-repeat =\"(key,value) in
payload\">{{key}}: {{value}}</td></tr>\n\n </table>\n \n</div>","storeOutMessages":true,"fwdInMessages":true,"resendOnRefresh":true,"templateScope":"local","className":"","x":560,"y":240,"wires":[["4db0093b.a252f8"]]},{"id":"4db0093b.a252f8","type":"debug","z":"a9c9090b.478498","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":750,"y":240,"wires":[]},{"id":"89def998.97df18","type":"inject","z":"a9c9090b.478498","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"2","crontab":"","once":false,"onceDelay":0.1,"topic":"","payloadType":"date","x":200,"y":240,"wires":[["15b1e814.b5de"]]},{"id":"15b1e814.b5de","type":"function","z":"a9c9090b.478498","name":"","func":"const
MIN = 1;\nconst MAX = 99;\n//The maximum is exclusive and the minimum is
inclusive\nfunction getRandomInt(min = MIN, max = MAX) {\n min = Math.ceil(min);\n max = Math.floor(max);\n return Math.floor(Math.random() * (max -
min)) + min;\n}\n\nvar no1=getRandomInt(min = MIN, max = MAX);\nvar no2=getRandomInt(min
= MIN, max = MAX);\nvar no3=getRandomInt(min = MIN, max = MAX);\nvar
no4=getRandomInt(min = MIN, max = MAX);\n\nvar payload=[];\nvar
obj={\"steve\":no1,\"Bella\":no2};\npayload.push(obj);\n\nvar
obj={\"shanna\":no3,\"saskia\":no4};\npayload.push(obj);\nmsg.payload=payload;\nreturn
msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":350,"y":240,"wires":[["b49f314d.e60eb"]]},{"id":"59e7f383.6f34ec","type":"comment","z":"a9c9090b.478498","name":"Random","info":"","x":180,"y":320,"wires":[]},{"id":"a38db82b.383e78","type":"inject","z":"a9c9090b.478498","name":"","repeat":"2","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":180,"y":400,"wires":[["f49a2ffd.11127"]]},{"id":"a99544a8.554ed8","type":"debug","z":"a9c9090b.478498","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":590,"y":460,"wires":[]},{"id":"f49a2ffd.11127","type":"function","z":"a9c9090b.478498","name":"random","func":"//===setting===
\nvar hpoint = 100;\nvar lpoint = 0;\nvar cent = 10; //centralize
number\n//===============\n\nvar mpoint = 0;\nvar output =
context.get('output')||0;\nvar addpoint =
context.get('addpoint')||0;\n\n//count\nvar count =
context.get('count')||0;\ncount +=
1;\ncontext.set('count',count);\n\n//rcount\nvar rcount =
context.get('rcount')||0;\nif(count >= rcount){\n for(i=0;i<cent;i++){ //centralize\n mpoint = mpoint +
Math.floor(Math.random() * hpoint);\n
}\n rcount = parseInt(Math.floor(Math.random()
* 100)/5);\n addpoint =
((mpoint/cent)-output)/rcount;\n
context.set('addpoint',addpoint);\n
context.set('rcount',rcount);\n
context.set('count',0);\n}else{\n
output = parseFloat((output + addpoint).toFixed(1));\n context.set('output',output);\n}\nmsg.payload
= output;\nreturn
msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":340,"y":400,"wires":[["a99544a8.554ed8","7f105c44.bf22e4"]]},{"id":"7f105c44.bf22e4","type":"ui_chart","z":"a9c9090b.478498","name":"","group":"d0b72a74.f9d968","order":0,"width":"10","height":"6","label":"chart","chartType":"line","legend":"false","xformat":"HH:mm:ss","interpolate":"linear","nodata":"","dot":false,"ymin":"0","ymax":"100","removeOlder":1,"removeOlderPoints":"50","removeOlderUnit":"3600","cutout":0,"useOneColor":false,"useUTC":false,"colors":["#1f77b4","#aec7e8","#ff7f0e","#2ca02c","#98df8a","#d62728","#ff9896","#9467bd","#c5b0d5"],"outputs":1,"useDifferentColor":false,"className":"","x":590,"y":400,"wires":[[]]},{"id":"04abeb5155a8efcf","type":"debug","z":"a9c9090b.478498","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":570,"y":540,"wires":[]},{"id":"d7978ee7f6986217","type":"inject","z":"a9c9090b.478498","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"random","payloadType":"str","x":190,"y":540,"wires":[["ac21dd5ac5ace135"]]},{"id":"ac21dd5ac5ace135","type":"function","z":"a9c9090b.478498","name":"","func":"const
MIN = 1;\nconst MAX = 99;\n//The maximum is exclusive and the minimum is
inclusive\n\nswitch (msg.payload) {\n\ncase \"undefined\" :\n msg.payload = undefined;\n break;\n\ncase \"null\"
:\n msg.payload = null;\n break;\n\ncase \"random\"
:\n msg.payload =function
getRandomInt(min = MIN, max = MAX) {\n
min = Math.ceil(min);\n max =
Math.floor(max);\n return
Math.floor(Math.random() * (max - min)) + min;\n }();\n break;\n}\n\nreturn
msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":350,"y":540,"wires":[["04abeb5155a8efcf"]]},{"id":"aaa9d8e5fed27dd0","type":"template","z":"a9c9090b.478498","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"\n亂數產生器 : {{payload}}
!","output":"str","x":490,"y":160,"wires":[["328474e5c2bd33dd"]]},{"id":"328474e5c2bd33dd","type":"ui_template","z":"a9c9090b.478498","group":"c7f6cd2081a9366a","name":"","order":1,"width":"6","height":"3","format":"<div
ng-bind-html=\"msg.payload\"></div>","storeOutMessages":true,"fwdInMessages":true,"resendOnRefresh":true,"templateScope":"local","className":"","x":640,"y":160,"wires":[[]]},{"id":"f765fee776e41b4e","type":"inject","z":"a9c9090b.478498","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"3","crontab":"","once":false,"onceDelay":0.1,"topic":"","payloadType":"date","x":200,"y":160,"wires":[["f343f355d2815477"]]},{"id":"f343f355d2815477","type":"function","z":"a9c9090b.478498","name":"","func":"const
MIN = 1;\nconst MAX = 99;\n//The maximum is exclusive and the minimum is
inclusive\nmsg.payload =function getRandomInt(min = MIN, max = MAX) {\n min = Math.ceil(min);\n max = Math.floor(max);\n return Math.floor(Math.random() * (max -
min)) + min;\n}\nreturn
msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":350,"y":160,"wires":[["aaa9d8e5fed27dd0"]]},{"id":"c7f6cd2081a9366a","type":"ui_group","name":"Default","tab":"e01b6f.a9d07c9","order":2,"disp":true,"width":"12","collapse":false,"className":""},{"id":"d0b72a74.f9d968","type":"ui_group","name":"Chart","tab":"e01b6f.a9d07c9","order":1,"disp":true,"width":"10","collapse":true,"className":""},{"id":"e01b6f.a9d07c9","type":"ui_tab","name":"Home","icon":"dashboard","disabled":false,"hidden":false}] |
沒有留言:
張貼留言