Adding sc-mqtt module for comms with Arduino

This commit is contained in:
walterhiggins 2014-01-17 23:05:36 +00:00
parent 5f7193d3ee
commit a13f3badd9
4 changed files with 216 additions and 11 deletions

View file

@ -22,6 +22,7 @@
<target name="test" depends="package" description="Perform unit tests"> <target name="test" depends="package" description="Perform unit tests">
</target> </target>
<target name="server-setup" depends="init" description="Downloads the latest bukkit dev jar" <target name="server-setup" depends="init" description="Downloads the latest bukkit dev jar"
unless="minecraft.present"> unless="minecraft.present">
<mkdir dir="${minecraft.dir}" /> <mkdir dir="${minecraft.dir}" />
@ -54,7 +55,12 @@
</target> </target>
<target name="compile" depends="init, server-setup" description="compile bukkit plugin source"> <target name="compile" depends="init, server-setup" description="compile bukkit plugin source">
<javac includeantruntime="false" srcdir="${src}" source="1.6" target="1.6" destdir="${build}" classpath="${minecraft.dir}/craftbukkit.jar" /> <javac includeantruntime="false"
srcdir="${src}"
source="1.6"
target="1.6"
destdir="${build}"
classpath="${minecraft.dir}/craftbukkit.jar" />
</target> </target>
<target name="gendocs" depends="construct-ypgpm, construct-api-ref" description="Generate API documentation"> <target name="gendocs" depends="construct-ypgpm, construct-api-ref" description="Generate API documentation">
@ -161,6 +167,7 @@ Walter Higgins
<!-- Put everything in ${build} into the MyProject-${DSTAMP}.jar file --> <!-- Put everything in ${build} into the MyProject-${DSTAMP}.jar file -->
<mkdir dir="${dist}/${DSTAMP}" /> <mkdir dir="${dist}/${DSTAMP}" />
<jar jarfile="${dist}/${DSTAMP}/scriptcraft.jar" basedir="${build}"/> <jar jarfile="${dist}/${DSTAMP}/scriptcraft.jar" basedir="${build}"/>
</target> </target>

View file

@ -52,6 +52,8 @@ Walter Higgins
* [Examples](#examples-1) * [Examples](#examples-1)
* [Http Module](#http-module) * [Http Module](#http-module)
* [http.request() function](#httprequest-function) * [http.request() function](#httprequest-function)
* [sc-mqtt module](#sc-mqtt-module)
* [Usage](#usage)
* [Signs Module](#signs-module) * [Signs Module](#signs-module)
* [signs.menu() function](#signsmenu-function) * [signs.menu() function](#signsmenu-function)
* [signs.getTargetedBy() function](#signsgettargetedby-function) * [signs.getTargetedBy() function](#signsgettargetedby-function)
@ -103,22 +105,22 @@ Walter Higgins
* [Drone.hemisphere0() method](#dronehemisphere0-method) * [Drone.hemisphere0() method](#dronehemisphere0-method)
* [Drone.spiral_stairs() method](#dronespiral_stairs-method) * [Drone.spiral_stairs() method](#dronespiral_stairs-method)
* [Example Plugin #1 - A simple extension to Minecraft.](#example-plugin-1---a-simple-extension-to-minecraft) * [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) * [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) * [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) * [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) * [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) * [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) * [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) * [Arrows Plugin](#arrows-plugin)
* [Usage:](#usage-6) * [Usage:](#usage-7)
* [Spawn Plugin](#spawn-plugin) * [Spawn Plugin](#spawn-plugin)
* [Usage](#usage-7) * [Usage](#usage-8)
* [alias Plugin](#alias-plugin) * [alias Plugin](#alias-plugin)
* [Examples](#examples-2) * [Examples](#examples-2)
* [Classroom Plugin](#classroom-plugin) * [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 + ")"); 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 <http://scriptcraftjs.org/download/extras/>
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 ## Signs Module
The Signs Module can be used by plugin authors to create interactive The Signs Module can be used by plugin authors to create interactive

View file

@ -15,12 +15,10 @@ public class ScriptCraftPlugin extends JavaPlugin implements Listener
// need to look at possibly having context/scope per operator // need to look at possibly having context/scope per operator
//protected Map<CommandSender,ScriptCraftEvaluator> playerContexts = new HashMap<CommandSender,ScriptCraftEvaluator>(); //protected Map<CommandSender,ScriptCraftEvaluator> playerContexts = new HashMap<CommandSender,ScriptCraftEvaluator>();
protected ScriptEngine engine = null; protected ScriptEngine engine = null;
@Override @Override
public void onEnable() public void onEnable()
{ {
try{ try{
ScriptEngineManager factory = new ScriptEngineManager(); ScriptEngineManager factory = new ScriptEngineManager();
this.engine = factory.getEngineByName("JavaScript"); this.engine = factory.getEngineByName("JavaScript");
Invocable inv = (Invocable)this.engine; Invocable inv = (Invocable)this.engine;

View file

@ -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 <http://scriptcraftjs.org/download/extras/>
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);
};