Play with Java function nodes that can use Java with Node-RED

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

What is a Java function node?

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.

Java function node development example

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.

(1) Make sound from the speaker

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.

beep.png

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.

Flow of sound from speaker

[{"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":[]}]

(2) Blink the LED on the keyboard

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.

scrolllock2.png

Write the code to press the Scroll Lock key using the Robot class in the Java function node.

scrolllock.png

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.

Flow to blink the LED of the keyboard

[{"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"]]}]

(3) Open the specified URL in a browser

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.

browser.png

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 ).

google.png

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.

Flow to open the specified URL in the browser

[{"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":[]}]

(4) Read and write the clipboard

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.

write_read_clipboard.png

In the Java function node, the Clipboard class is used to read and write the clipboard.

write_clipboard.png

read_clipboard.png

If you combine it with the file node, you can write a flow to manage the history of the clipboard.

Flow to read and write 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":[]}]

(5) Get and change the position of the mouse cursor

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.

get_mouse_location2.png

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.

get_mouse_location.png

Next, let's write a flow to move the mouse cursor.

move_mouse2.png

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.

move_mouse.png

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.

Flow to get and change the position of the mouse cursor

[{"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":[]}]

(6) Display a dialog

Try to display a confirmation dialog containing the message received from the previous node. Bring up the window in the screenshot below.

dialog2.png

To display the dialog, use the JOptionPane class. It's a little long, but the essence is only the 3rd and 4th lines.

dialog.png

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.

Flow to display dialog

[{"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"]]}]

How to install Java function node

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

Finally

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.

Recommended Posts

Play with Java function nodes that can use Java with Node-RED
Java to play with Function
Use Lambda Layers with Java
Use SpatiaLite with Java / JDBC
Use java with MSYS and Cygwin
Use Microsoft Graph with standard Java
Use Azure Bing SpellCheck with Java
Use JDBC with Java and Scala.
Use Java 11 with Google Cloud Functions
Play with Markdown in Java flexmark-java
Introduction to Java that can be understood even with Krillin (Part 1)
I tried learning Java with a series that beginners can understand clearly
I made a class that can use JUMAN and KNP from Java
How to use BootStrap with Play Framework
Authentication function with Play Framework [Registration and authentication]
[JaCoCo (Java Code Coverage)] Use with NetBeans
[Processing × Java] How to use the function
Find out if there is a font that can use Japanese (Hiragana, Katakana, Kanji) with AWS Lambda + Java
I can't use SQS that can read the questionnaire with a scanner → I could use it after changing the java ver
When is it said that you can use try with a Swift error?
[Good news] You can still go with Java 8 !!
Scala's Implicit that even Java shops can understand
With Tomcat you can use placeholders ($ {...}) in web.xml
How to use Java framework with AWS Lambda! ??
I want to use java8 forEach with index
How to use Java API with lambda expression
Use Matplotlib from Java or Scala with Matplotlib4j
Organize methods that can be used with StringUtils
[JAVA] [Spring] [MyBatis] Use IN () with SQL Builder
[Ruby] Methods that can be used with strings
Compiled kotlin with cli with docker and created an environment that can be executed with java