From a13f3badd923ef57565b350085e7fb589a942599 Mon Sep 17 00:00:00 2001 From: walterhiggins Date: Fri, 17 Jan 2014 23:05:36 +0000 Subject: [PATCH] Adding sc-mqtt module for comms with Arduino --- build.xml | 9 +- docs/API-Reference.md | 79 +++++++++- .../scriptcraft/ScriptCraftPlugin.java | 2 - src/main/javascript/modules/sc-mqtt.js | 137 ++++++++++++++++++ 4 files changed, 216 insertions(+), 11 deletions(-) create mode 100644 src/main/javascript/modules/sc-mqtt.js diff --git a/build.xml b/build.xml index 2338a24..4fe8977 100644 --- a/build.xml +++ b/build.xml @@ -22,6 +22,7 @@ + @@ -54,7 +55,12 @@ - + @@ -161,6 +167,7 @@ Walter Higgins + diff --git a/docs/API-Reference.md b/docs/API-Reference.md index 42263b9..5e3fe17 100644 --- a/docs/API-Reference.md +++ b/docs/API-Reference.md @@ -52,6 +52,8 @@ Walter Higgins * [Examples](#examples-1) * [Http Module](#http-module) * [http.request() function](#httprequest-function) + * [sc-mqtt module](#sc-mqtt-module) + * [Usage](#usage) * [Signs Module](#signs-module) * [signs.menu() function](#signsmenu-function) * [signs.getTargetedBy() function](#signsgettargetedby-function) @@ -103,22 +105,22 @@ Walter Higgins * [Drone.hemisphere0() method](#dronehemisphere0-method) * [Drone.spiral_stairs() method](#dronespiral_stairs-method) * [Example Plugin #1 - A simple extension to Minecraft.](#example-plugin-1---a-simple-extension-to-minecraft) - * [Usage:](#usage) - * [Example Plugin #2 - Making extensions available for all players.](#example-plugin-2---making-extensions-available-for-all-players) * [Usage:](#usage-1) - * [Example Plugin #3 - Limiting use of commands to operators only.](#example-plugin-3---limiting-use-of-commands-to-operators-only) + * [Example Plugin #2 - Making extensions available for all players.](#example-plugin-2---making-extensions-available-for-all-players) * [Usage:](#usage-2) - * [Example Plugin #4 - Using parameters in commands.](#example-plugin-4---using-parameters-in-commands) + * [Example Plugin #3 - Limiting use of commands to operators only.](#example-plugin-3---limiting-use-of-commands-to-operators-only) * [Usage:](#usage-3) - * [Example Plugin #5 - Re-use - Using your own and others modules.](#example-plugin-5---re-use---using-your-own-and-others-modules) + * [Example Plugin #4 - Using parameters in commands.](#example-plugin-4---using-parameters-in-commands) * [Usage:](#usage-4) - * [Example Plugin #6 - Re-use - Using 'utils' to get Player objects.](#example-plugin-6---re-use---using-utils-to-get-player-objects) + * [Example Plugin #5 - Re-use - Using your own and others modules.](#example-plugin-5---re-use---using-your-own-and-others-modules) * [Usage:](#usage-5) + * [Example Plugin #6 - Re-use - Using 'utils' to get Player objects.](#example-plugin-6---re-use---using-utils-to-get-player-objects) + * [Usage:](#usage-6) * [Example Plugin #7 - Listening for events, Greet players when they join the game.](#example-plugin-7---listening-for-events-greet-players-when-they-join-the-game) * [Arrows Plugin](#arrows-plugin) - * [Usage:](#usage-6) + * [Usage:](#usage-7) * [Spawn Plugin](#spawn-plugin) - * [Usage](#usage-7) + * [Usage](#usage-8) * [alias Plugin](#alias-plugin) * [Examples](#examples-2) * [Classroom Plugin](#classroom-plugin) @@ -834,6 +836,67 @@ The following example illustrates how to use http.request to make a request to a var jsObj = eval("(" + responseBody + ")"); }); +## sc-mqtt module + +This module provides a simple way to communicate with devices (such as Arduino) +using the popular lightweight [MQTT protocol][mqtt]. + +### Usage + +This module can only be used if the separate `sc-mqtt.jar` file is +present in the CraftBukkit classpath. To use this module, you should +... + + 1. Download sc-mqtt.jar from + 2. Save the file to the same directory where craftbukkit.jar resides. + 3. Create a new batch file (windows-only) called + craftbukkit-sc-mqtt.bat and edit it to include the following + command... + + java -classpath sc-mqtt.jar;craftbukit.jar org.bukkit.craftbukkit.Main + + If you're using Mac OS, create a new craftbukkit-sc-mqtt.command + file and edit it (using TextWrangler or another text editor) ... + + java -classpath sc-mqtt.jar:craftbukkit.jar org.bukit.craftbukkit.Main + + 4. Execute the craftbukkit-sc-mqtt batch file / command file to start + Craftbukkit. You can now begin using this module to send and receive + messages to/from a Net-enabled Arduino or any other device which uses + the [MQTT protocol][mqtt] + + + var mqtt = require('sc-mqtt'); + + // create a new client + + var client = mqtt.client('tcp://localhost:1883', 'uniqueClientId'); + + // connect to the broker + + client.connect({ keepAliveInterval: 15 }); + + // publish a message to the broker + + client.publish('minecraft','loaded'); + + // subscribe to messages on 'arduino' topic + + client.subscribe('arduino'); + + // do something when an incoming message arrives... + + client.onMessageArrived(function(topic, message){ + console.log('Message arrived: topic=' + topic + ', message=' + message); + }); + +The `sc-mqtt` module provides a very simple minimal wrapper around the +[Eclipse Paho MQTT Version 3 Client][pahodocs] java-based MQTT +library. + +[pahodocs]: http://pic.dhe.ibm.com/infocenter/wmqv7/v7r5/index.jsp?topic=/com.ibm.mq.javadoc.doc/WMQMQxrClasses/org/eclipse/paho/client/mqttv3/package-summary.html +[mqtt]: http://mqtt.org/ + ## Signs Module The Signs Module can be used by plugin authors to create interactive diff --git a/src/main/java/net/walterhiggins/scriptcraft/ScriptCraftPlugin.java b/src/main/java/net/walterhiggins/scriptcraft/ScriptCraftPlugin.java index 8fb5a71..ab2082e 100644 --- a/src/main/java/net/walterhiggins/scriptcraft/ScriptCraftPlugin.java +++ b/src/main/java/net/walterhiggins/scriptcraft/ScriptCraftPlugin.java @@ -15,12 +15,10 @@ public class ScriptCraftPlugin extends JavaPlugin implements Listener // need to look at possibly having context/scope per operator //protected Map playerContexts = new HashMap(); protected ScriptEngine engine = null; - @Override public void onEnable() { try{ - ScriptEngineManager factory = new ScriptEngineManager(); this.engine = factory.getEngineByName("JavaScript"); Invocable inv = (Invocable)this.engine; diff --git a/src/main/javascript/modules/sc-mqtt.js b/src/main/javascript/modules/sc-mqtt.js new file mode 100644 index 0000000..82b9ee8 --- /dev/null +++ b/src/main/javascript/modules/sc-mqtt.js @@ -0,0 +1,137 @@ +/************************************************************************* +## sc-mqtt module + +This module provides a simple way to communicate with devices (such as Arduino) +using the popular lightweight [MQTT protocol][mqtt]. + +### Usage + +This module can only be used if the separate `sc-mqtt.jar` file is +present in the CraftBukkit classpath. To use this module, you should +... + + 1. Download sc-mqtt.jar from + 2. Save the file to the same directory where craftbukkit.jar resides. + 3. Create a new batch file (windows-only) called + craftbukkit-sc-mqtt.bat and edit it to include the following + command... + + java -classpath sc-mqtt.jar;craftbukit.jar org.bukkit.craftbukkit.Main + + If you're using Mac OS, create a new craftbukkit-sc-mqtt.command + file and edit it (using TextWrangler or another text editor) ... + + java -classpath sc-mqtt.jar:craftbukkit.jar org.bukit.craftbukkit.Main + + 4. Execute the craftbukkit-sc-mqtt batch file / command file to start + Craftbukkit. You can now begin using this module to send and receive + messages to/from a Net-enabled Arduino or any other device which uses + the [MQTT protocol][mqtt] + + + var mqtt = require('sc-mqtt'); + + // create a new client + + var client = mqtt.client('tcp://localhost:1883', 'uniqueClientId'); + + // connect to the broker + + client.connect({ keepAliveInterval: 15 }); + + // publish a message to the broker + + client.publish('minecraft','loaded'); + + // subscribe to messages on 'arduino' topic + + client.subscribe('arduino'); + + // do something when an incoming message arrives... + + client.onMessageArrived(function(topic, message){ + console.log('Message arrived: topic=' + topic + ', message=' + message); + }); + +The `sc-mqtt` module provides a very simple minimal wrapper around the +[Eclipse Paho MQTT Version 3 Client][pahodocs] java-based MQTT +library. + +[pahodocs]: http://pic.dhe.ibm.com/infocenter/wmqv7/v7r5/index.jsp?topic=/com.ibm.mq.javadoc.doc/WMQMQxrClasses/org/eclipse/paho/client/mqttv3/package-summary.html +[mqtt]: http://mqtt.org/ + +***/ +var MISSING_MQTT = '\nMissing class org.walterhiggins.scriptcraft.ScriptCraftMqttCallback.\n' + + 'Make sure sc-mqtt.jar is in the classpath.\n' + + 'See http://github.com/walterhiggins/scriptcraft-extras-mqtt for details.\n'; + +function Client(brokerUrl, clientId){ + + var Callback = org.walterhiggins.scriptcraft.ScriptCraftMqttCallback; + var MqttClient = org.eclipse.paho.client.mqttv3.MqttClient; + + var callback = new Callback( + function(err){ + console.log('connectionLost: ' + err); + }, + function(topic, message){ + console.log('messageArrived ' + topic + '> ' + message); + }, + function(token){ + console.log('deliveryComplete:' + token); + } + ); + + if (!brokerUrl){ + brokerUrl = 'tcp://localhost:1883'; + } + if (!clientId){ + clientId = 'scriptcraft'; + } + var client = new MqttClient(brokerUrl, clientId, null); + client.setCallback(callback); + return { + connect: function(options){ + if (typeof options === 'undefined'){ + client.connect(); + }else{ + client.connect(options); + } + }, + publish: function(topic, message, qos, retained){ + if (typeof message == 'string'){ + message = new java.lang.String(message).bytes; + } + if (typeof qos == 'undefined'){ + qos = 1; + } + if (typeof retained == 'undefined'){ + retained = false; + } + client.publish(topic, message,qos, retained); + }, + subscribe: function(topic){ + client.subscribe(topic); + }, + unsubscribe: function(topic){ + client.unsubscribe(topic); + }, + onMessageArrived: function(fn){ + callback.setMesgArrived(fn); + }, + onDeliveryComplete: function(fn){ + callback.setDeliveryComplete(fn); + }, + onConnectionLost: function(fn){ + callback.setConnLost(fn); + } + }; +}; + +exports.client = function(brokerUrl, clientId, options){ + if (typeof org.walterhiggins.scriptcraft.ScriptCraftMqttCallback != 'function'){ + throw MISSING_MQTT; + } + return new Client(brokerUrl, clientId, options); +}; +