hi evryone,
i have been tiring for a few days now to create a flow that will use a light sensor to check a color and i need it too find closest color match to none samples stored in mysql
so fare i have most of it working however i cant get the function too work that finds the closest match, if i make a array within the function it will compare sample to this array fine.
but what i need it to do is inport the array from the DB and compare it too that and the other problem the number of columns in the DB can change that will change the array size witch is making it harder for me to create the code needed
any help with this would be gratefull
[{"id":"5af8a725.9a8b4","type":"tab","label":"Results","disabled":false,"info":""},{"id":"865ea1fc.6fe4","type":"mysql","z":"5af8a725.9a8b4","mydb":"42a8cc88.db9fcc","name":"","x":610,"y":260,"wires":[["7f732a3f.bf7f64"]]},{"id":"ae1b973a.53d9e8","type":"function","z":"5af8a725.9a8b4","d":true,"name":"","func":"nearest = -1;\nInteger = 200;\nvar i;\nbestDistanceFoundYet = Integer.MAX_INTEGER;\narray = msg.payload.color[i];\n// We iterate on the array...\nfor (i = 0; i < array.length; i++) {\n // if we found the desired number, we return it.\n if (array[i] == desiredNumber) {\n return array[i];\n } else {\n // else, we consider the difference between the desired number and the current number in the array.\n d = Math.abs(desiredNumber - array[i]);\n if (d < bestDistanceFoundYet) {\n // For the moment, this value is the nearest to the desired number...\n bestDistanceFoundYet = d; // Assign new best distance...\n nearest = array[i];\n }\n }\n}\nreturn nearest;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1820,"y":440,"wires":[[]]},{"id":"bbf957c.5b1f428","type":"function","z":"5af8a725.9a8b4","name":"","func":"msg.topic = \"SELECT hue From ph_results\";\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":440,"y":260,"wires":[["865ea1fc.6fe4"]]},{"id":"6e69f675.2122b","type":"inject","z":"5af8a725.9a8b4","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":240,"y":260,"wires":[["bbf957c.5b1f428","157268ec.8dd1f7"]]},{"id":"157268ec.8dd1f7","type":"function","z":"5af8a725.9a8b4","name":"","func":"msg.payload = \"test\";\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":420,"y":320,"wires":[["c886cf3a.7c2a9"]]},{"id":"c886cf3a.7c2a9","type":"serial request","z":"5af8a725.9a8b4","name":"","serial":"118f6787.4d53f","x":600,"y":320,"wires":[["2204ad23.743b9a"]]},{"id":"2204ad23.743b9a","type":"json","z":"5af8a725.9a8b4","name":"","property":"payload","action":"","pretty":true,"x":750,"y":320,"wires":[["fc876bd0.f75b78"]]},{"id":"fc876bd0.f75b78","type":"function","z":"5af8a725.9a8b4","name":"","func":"red = msg.payload[0].Red;\ngreen = msg.payload[1].Green;\nblue = msg.payload[2].Blue;\n\nreturn {payload:[red,green,blue]}\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":880,"y":320,"wires":[["4a0ed567.2d14d4"]]},{"id":"4a0ed567.2d14d4","type":"color-convert","z":"5af8a725.9a8b4","input":"rgb","output":"hsv","outputType":"array","scaleInput":false,"x":1050,"y":320,"wires":[["a0322a38.566a3","7f732a3f.bf7f64"]]},{"id":"a0322a38.566a3","type":"change","z":"5af8a725.9a8b4","d":true,"name":"","rules":[{"t":"move","p":"payload","pt":"msg","to":"result","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1230,"y":360,"wires":[["7f732a3f.bf7f64"]]},{"id":"d62b7fda.cacba8","type":"mysql","z":"5af8a725.9a8b4","d":true,"mydb":"42a8cc88.db9fcc","name":"","x":2070,"y":300,"wires":[["c37dde6a.6813f"]]},{"id":"c37dde6a.6813f","type":"debug","z":"5af8a725.9a8b4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":2150,"y":200,"wires":[]},{"id":"7f732a3f.bf7f64","type":"join","z":"5af8a725.9a8b4","name":"","mode":"custom","build":"array","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"num","reduceFixup":"","x":1420,"y":260,"wires":[["b684eacd.cc61a8","17297fc3.5110d8"]]},{"id":"17297fc3.5110d8","type":"debug","z":"5af8a725.9a8b4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1710,"y":80,"wires":[]},{"id":"b684eacd.cc61a8","type":"function","z":"5af8a725.9a8b4","name":"","func":"\n\n//var counts = [msg.payload[0][0],msg.payload[0][1],msg.payload[0][2],msg.payload[0][3],msg.payload[0][4],msg.payload[0][5],msg.payload[0][6],msg.payload[0][7]]; //only checks first entry\nvar counts =[10,50,100,150,300,360]; //works\n\n goal = msg.payload[1][0]; // works\n //goal = 6;\n\nvar closest = counts.reduce(function(prev, curr) {\n return (Math.abs(curr - goal) < Math.abs(prev - goal) ? curr : prev);\n});\n//msg.topic = \"SELECT value From ph_results WHERE hue='\"+closest+\"'\";\nmsg.payload = closest;\nreturn msg;\n//return counts;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1660,"y":240,"wires":[["17297fc3.5110d8","5132d256.072394"]]},{"id":"2b9352fb.fc604e","type":"inject","z":"5af8a725.9a8b4","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":1390,"y":140,"wires":[["b684eacd.cc61a8"]]},{"id":"5132d256.072394","type":"function","z":"5af8a725.9a8b4","name":"","func":"var hue = msg.payload.hue\nmsg.topic = \"SELECT value From ph_results WHERE hue='\"+hue+\"'\";\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1840,"y":260,"wires":[["d62b7fda.cacba8","c37dde6a.6813f"]]},{"id":"42a8cc88.db9fcc","type":"MySQLdatabase","name":"tests","host":"127.0.0.1","port":"3306","db":"test","tz":"","charset":"UTF8"},{"id":"118f6787.4d53f","type":"serial-port","serialport":"/dev/ttyACM0","serialbaud":"115200","databits":"8","parity":"none","stopbits":"1","waitfor":"","dtr":"none","rts":"none","cts":"none","dsr":"none","newline":"\\n","bin":"false","out":"char","addchar":"\\n","responsetimeout":"15000"}]