This time, I will use Java function node that can execute Java code on Node-RED. Here are some examples of flows that developers may find useful.
** Java function node (node-red-contrib-java-function) ** https://flows.nodered.org/node/node-red-contrib-java-function
Since the function node provided as standard in Node-RED requires writing code in JavaScript, developers who are not familiar with JavaScript may struggle. To solve this problem, the Node-RED library has a function node that can be written in other languages such as python. ) Is open to the public. The Java function node introduced this time is a function node that can execute Java code. By using this node, you can call the abundant standard library of Java from Nodde-RED.
Here are 6 examples using Java function nodes. By using the Java function node, you can easily realize functions that were difficult with conventional Node-RED.
Try to make the OS system sound "Pororon" for Windows 10 and "Tsun" for mac. It can be implemented simply by writing the beep () method of the Toolkit class in the Java function node.
In this flow, clicking the button on the inject node will make a system sound. It seems that you can also play mp3 files using JavaFx.
[{"id":"7668777e.580ae8","type":"inject","z":"3cd542b9.3985de","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":"","x":160,"y":260,"wires":[["f5974570.d119d8"]]},{"id":"f5974570.d119d8","type":"javafunction","z":"3cd542b9.3985de","name":"beep sound","func":"Toolkit.getDefaultToolkit().beep();\nreturn msg;","outputs":1,"x":310,"y":260,"wires":[["279508d1.f18468"]]},{"id":"279508d1.f18468","type":"debug","z":"3cd542b9.3985de","name":"","active":true,"console":false,"complete":"false","x":470,"y":260,"wires":[]}]
Try blinking the Scroll Lock LED on your keyboard (red circle in the photo below). It's an LED that I rarely use, so let's use it for notifications from Node-RED.
Write the code to press the Scroll Lock key using the Robot class in the Java function node.
Every time you click the inject node, the LED will blink. By combining it with the e-mail node, it seems that you can create a flow that notifies you that an email has arrived by blinking the LED.
[{"id":"44c7ddf6.403c24","type":"inject","z":"3cd542b9.3985de","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":180,"y":480,"wires":[["e38dcfdf.cc86e"]]},{"id":"5a9065ca.2d930c","type":"debug","z":"3cd542b9.3985de","name":"","active":true,"console":false,"complete":"false","x":510,"y":480,"wires":[]},{"id":"e38dcfdf.cc86e","type":"javafunction","z":"3cd542b9.3985de","name":"blink scroll lock","func":"Robot rb = new Robot();\nrb.keyPress(KeyEvent.VK_SCROLL_LOCK);\nrb.keyRelease(KeyEvent.VK_SCROLL_LOCK);\nreturn msg;","outputs":1,"x":340,"y":480,"wires":[["5a9065ca.2d930c"]]}]
When you receive a message with the URL, you can also use the Desktop class to describe the process of opening the page in the browser.
In the above flow, if the default browser is Google Chrome, when you run it, www.google.co.jp will open in Google Chrome (if you are operating the flow editor in Google Chrome, it will open in a new tab ).
When using a personal computer, I think that the operation of copying the URL and pasting it on the browser to open the page is often performed. If you combine it with the clipboard linkage function described later, you can write a convenient flow that opens the browser a little earlier when the character string starting with "http: //" or "https: //" is saved in the clipboard.
[{"id":"b5368af2.afcbc8","type":"inject","z":"3cd542b9.3985de","name":"","topic":"","payload":"http://www.google.co.jp","payloadType":"str","repeat":"","crontab":"","once":false,"x":200,"y":380,"wires":[["272fb242.1067ee"]]},{"id":"272fb242.1067ee","type":"javafunction","z":"3cd542b9.3985de","name":"open browser","func":"URI uri = new URI(msg.get(\"payload\").getAsString());\nDesktop.getDesktop().browse(uri);\nreturn msg;","outputs":1,"x":400,"y":380,"wires":[["68407010.0f3ac"]]},{"id":"68407010.0f3ac","type":"debug","z":"3cd542b9.3985de","name":"","active":true,"console":false,"complete":"false","x":570,"y":380,"wires":[]}]
You can also access the clipboard using the Java function node. In the flow below, the character string "data" received in the message is saved in the clipboard, and the contents of the clipboard are called in another flow.
In the Java function node, the Clipboard class is used to read and write the clipboard.
If you combine it with the file node, you can write a flow to manage the history of the clipboard.
[{"id":"aee0f3ab.fadef","type":"inject","z":"3cd542b9.3985de","name":"","topic":"","payload":"data","payloadType":"str","repeat":"","crontab":"","once":false,"x":110,"y":360,"wires":[["8c33e48c.c2cac8"]]},{"id":"8c33e48c.c2cac8","type":"javafunction","z":"3cd542b9.3985de","name":"save to clipboard","func":"String str = msg.get(\"payload\").getAsString();\nToolkit tk = Toolkit.getDefaultToolkit();\nClipboard cb = tk.getSystemClipboard();\ncb.setContents(new StringSelection(str), null);\nreturn msg;","outputs":1,"x":270,"y":360,"wires":[["ce731dd.2cdc4e"]]},{"id":"ce731dd.2cdc4e","type":"debug","z":"3cd542b9.3985de","name":"","active":true,"console":false,"complete":"false","x":450,"y":360,"wires":[]},{"id":"51b942a1.110d0c","type":"inject","z":"3cd542b9.3985de","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":120,"y":420,"wires":[["2e071160.2678ce"]]},{"id":"2e071160.2678ce","type":"javafunction","z":"3cd542b9.3985de","name":"read clipboard","func":"Toolkit tk = Toolkit.getDefaultToolkit();\nClipboard cb = tk.getSystemClipboard();\nObject oj = cb.getData(DataFlavor.stringFlavor);\nmsg.addProperty(\"payload\", oj.toString());\nreturn msg;","outputs":1,"x":280,"y":420,"wires":[["93c1a28a.e100a"]]},{"id":"93c1a28a.e100a","type":"debug","z":"3cd542b9.3985de","name":"","active":true,"console":false,"complete":"false","x":450,"y":420,"wires":[]}]
You can also get the coordinates of the mouse cursor and move the mouse cursor to the specified coordinates. First, get the X and Y coordinates of the mouse cursor and display them on the debug tab.
Use the PointerInfo class to get the position of the mouse cursor. If you set the inject node to run periodically and move the mouse cursor, the X and Y coordinate values on the debug tab will change from moment to moment.
Next, let's write a flow to move the mouse cursor.
Use the Robot class to move the mouse cursor. Click the button on the inject node to jump the mouse cursor to the specified coordinates.
If you use this mechanism, you can also operate the mouse using the sensor value received via MQTT. It seems that you can easily develop an interesting mouse.
[{"id":"84d59034.87a96","type":"inject","z":"3cd542b9.3985de","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":120,"y":480,"wires":[["df3edce2.04cfa"]]},{"id":"df3edce2.04cfa","type":"javafunction","z":"3cd542b9.3985de","name":"get mouse location","func":"PointerInfo pi = MouseInfo.getPointerInfo();\nPoint pt = pi.getLocation();\nJsonObject jo = new JsonObject();\njo.addProperty(\"x\", pt.getX());\njo.addProperty(\"y\", pt.getY());\nmsg.add(\"payload\", jo);\nreturn msg;","outputs":1,"x":290,"y":480,"wires":[["3b3449cb.5889e6"]]},{"id":"3b3449cb.5889e6","type":"debug","z":"3cd542b9.3985de","name":"","active":true,"console":false,"complete":"false","x":470,"y":480,"wires":[]},{"id":"d80fd109.6c26f","type":"javafunction","z":"3cd542b9.3985de","name":"move mouse","func":"Robot rb = new Robot();\nJsonObject jo = (JsonObject)msg.get(\"payload\");\nint x = Integer.parseInt(jo.get(\"x\").toString());\nint y = Integer.parseInt(jo.get(\"y\").toString());\nrb.mouseMove(x, y);\nreturn msg;","outputs":1,"x":310,"y":540,"wires":[["d10b91b3.4141"]]},{"id":"888f6d50.e793f","type":"inject","z":"3cd542b9.3985de","name":"","topic":"","payload":"{\"x\":245,\"y\":530}","payloadType":"json","repeat":"","crontab":"","once":false,"x":140,"y":540,"wires":[["d80fd109.6c26f"]]},{"id":"d10b91b3.4141","type":"debug","z":"3cd542b9.3985de","name":"","active":true,"console":false,"complete":"false","x":470,"y":540,"wires":[]}]
Try to display a confirmation dialog containing the message received from the previous node. Bring up the window in the screenshot below.
To display the dialog, use the JOptionPane class. It's a little long, but the essence is only the 3rd and 4th lines.
In the Node-RED standard, events are often used in online services such as Twitter and e-mail nodes, but a smartphone or email notification mechanism is required. The confirmation dialog method is convenient because it can be used by Node-RED alone.
[{"id":"80733fc8.a636b","type":"debug","z":"3cd542b9.3985de","name":"","active":true,"console":false,"complete":"false","x":490,"y":700,"wires":[]},{"id":"73578fbe.9ba63","type":"javafunction","z":"3cd542b9.3985de","name":"show dialog","func":"String lf = UIManager.getSystemLookAndFeelClassName();\nUIManager.setLookAndFeel(lf);\nString payload = msg.get(\"payload\").getAsString();\nJOptionPane op = new JOptionPane(payload,\n JOptionPane.PLAIN_MESSAGE,\n JOptionPane.DEFAULT_OPTION);\nJDialog dg = op.createDialog(null, null);\ndg.setAlwaysOnTop(true);\ndg.setVisible(true);\nreturn msg;","outputs":1,"x":330,"y":700,"wires":[["80733fc8.a636b"]]},{"id":"6467a0e7.53bfc","type":"inject","z":"3cd542b9.3985de","name":"","topic":"","payload":"message","payloadType":"str","repeat":"","crontab":"","once":false,"x":180,"y":700,"wires":[["73578fbe.9ba63"]]}]
The example introduced this time has been confirmed to work on Node-RED installed on Windows and Mac. To enable the Java function node, perform the following three steps. For details, refer to the document linked to each link and install it.
(1) Install Java JDK (2) Set environment variables so that java and javac commands can be executed (3) Install "node-red-contrib-java-function" from "Barrett Management" of Node-RED
The Java standard library is so rich that it seems to have interesting uses other than the examples introduced here. Please play with the Java function node.