2014-01-25 00:38:56 +01:00
|
|
|
'use strict';
|
2014-01-18 00:05:36 +01:00
|
|
|
/*************************************************************************
|
|
|
|
## 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...
|
|
|
|
|
2014-02-04 22:36:00 +01:00
|
|
|
```sh
|
2014-02-28 11:10:05 +01:00
|
|
|
java -classpath sc-mqtt.jar;craftbukkit.jar org.bukkit.craftbukkit.Main
|
2014-02-04 22:36:00 +01:00
|
|
|
```
|
|
|
|
|
2014-01-18 00:05:36 +01:00
|
|
|
If you're using Mac OS, create a new craftbukkit-sc-mqtt.command
|
|
|
|
file and edit it (using TextWrangler or another text editor) ...
|
|
|
|
|
2014-02-04 22:36:00 +01:00
|
|
|
```sh
|
2014-02-28 11:10:05 +01:00
|
|
|
java -classpath sc-mqtt.jar:craftbukkit.jar org.bukkit.craftbukkit.Main
|
2014-02-04 22:36:00 +01:00
|
|
|
```
|
2014-01-18 00:05:36 +01:00
|
|
|
|
|
|
|
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]
|
|
|
|
|
2014-02-04 22:36:00 +01:00
|
|
|
```javascript
|
|
|
|
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 );
|
|
|
|
});
|
|
|
|
|
|
|
|
```
|
2014-01-18 00:05:36 +01:00
|
|
|
|
|
|
|
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' +
|
2014-01-25 00:38:56 +01:00
|
|
|
'Make sure sc-mqtt.jar is in the classpath.\n' +
|
|
|
|
'See http://github.com/walterhiggins/scriptcraft-extras-mqtt for details.\n';
|
2014-01-18 00:05:36 +01:00
|
|
|
|
2014-01-29 20:49:15 +01:00
|
|
|
function Client( brokerUrl, clientId ) {
|
2014-01-18 00:05:36 +01:00
|
|
|
|
2014-01-25 00:38:56 +01:00
|
|
|
var Callback = org.walterhiggins.scriptcraft.ScriptCraftMqttCallback;
|
|
|
|
var MqttClient = org.eclipse.paho.client.mqttv3.MqttClient;
|
|
|
|
|
|
|
|
var callback = new Callback(
|
2014-01-29 20:49:15 +01:00
|
|
|
function( err ) {
|
|
|
|
console.log( 'connectionLost: ' + err );
|
2014-01-25 00:38:56 +01:00
|
|
|
},
|
2014-01-29 20:49:15 +01:00
|
|
|
function( topic, message ) {
|
|
|
|
console.log( 'messageArrived ' + topic + '> ' + message );
|
2014-01-25 00:38:56 +01:00
|
|
|
},
|
2014-01-29 20:49:15 +01:00
|
|
|
function( token ) {
|
|
|
|
console.log( 'deliveryComplete:' + token );
|
2014-01-18 00:05:36 +01:00
|
|
|
}
|
2014-01-25 00:38:56 +01:00
|
|
|
);
|
|
|
|
|
2014-01-29 20:49:15 +01:00
|
|
|
if ( !brokerUrl ) {
|
2014-01-25 00:38:56 +01:00
|
|
|
brokerUrl = 'tcp://localhost:1883';
|
|
|
|
}
|
2014-01-29 20:49:15 +01:00
|
|
|
if ( !clientId ) {
|
2014-01-25 00:38:56 +01:00
|
|
|
clientId = 'scriptcraft' + new Date().getTime();
|
|
|
|
}
|
2014-01-29 20:49:15 +01:00
|
|
|
var client = new MqttClient( brokerUrl, clientId, null );
|
|
|
|
client.setCallback( callback );
|
2014-01-25 00:38:56 +01:00
|
|
|
return {
|
2014-01-29 20:49:15 +01:00
|
|
|
connect: function( options ) {
|
|
|
|
if ( typeof options === 'undefined' ) {
|
2014-01-25 00:38:56 +01:00
|
|
|
client.connect();
|
|
|
|
}else{
|
|
|
|
client.connect(options);
|
|
|
|
}
|
|
|
|
return client;
|
|
|
|
},
|
|
|
|
|
2014-01-29 20:49:15 +01:00
|
|
|
disconnect: function( quiesceTimeout ) {
|
|
|
|
if ( typeof quiesceTimeout == 'undefined' ) {
|
2014-01-25 00:38:56 +01:00
|
|
|
client.disconnect();
|
2014-01-29 20:49:15 +01:00
|
|
|
} else {
|
|
|
|
client.disconnect( quiesceTimeout );
|
|
|
|
}
|
2014-01-25 00:38:56 +01:00
|
|
|
return client;
|
|
|
|
},
|
|
|
|
|
2014-01-29 20:49:15 +01:00
|
|
|
publish: function( topic, message, qos, retained ) {
|
|
|
|
if ( typeof message == 'string' ) {
|
|
|
|
message = new java.lang.String( message ).bytes;
|
2014-01-25 00:38:56 +01:00
|
|
|
}
|
|
|
|
if (typeof qos == 'undefined'){
|
|
|
|
qos = 1;
|
|
|
|
}
|
|
|
|
if (typeof retained == 'undefined'){
|
|
|
|
retained = false;
|
|
|
|
}
|
2014-01-29 20:49:15 +01:00
|
|
|
client.publish( topic, message,qos, retained );
|
2014-01-25 00:38:56 +01:00
|
|
|
return client;
|
|
|
|
},
|
|
|
|
|
2014-01-29 20:49:15 +01:00
|
|
|
subscribe: function( topic ) {
|
|
|
|
client.subscribe( topic );
|
2014-01-25 00:38:56 +01:00
|
|
|
return client;
|
|
|
|
},
|
|
|
|
|
2014-01-29 20:49:15 +01:00
|
|
|
unsubscribe: function( topic ) {
|
|
|
|
client.unsubscribe( topic );
|
2014-01-25 00:38:56 +01:00
|
|
|
return client;
|
|
|
|
},
|
|
|
|
|
2014-01-29 20:49:15 +01:00
|
|
|
onMessageArrived: function( fn ) {
|
|
|
|
callback.setMesgArrived( fn );
|
2014-01-25 00:38:56 +01:00
|
|
|
return client;
|
|
|
|
},
|
|
|
|
|
2014-01-29 20:49:15 +01:00
|
|
|
onDeliveryComplete: function( fn ) {
|
|
|
|
callback.setDeliveryComplete( fn );
|
2014-01-25 00:38:56 +01:00
|
|
|
return client;
|
|
|
|
},
|
|
|
|
|
2014-01-29 20:49:15 +01:00
|
|
|
onConnectionLost: function( fn ) {
|
|
|
|
callback.setConnLost( fn );
|
2014-01-25 00:38:56 +01:00
|
|
|
return client;
|
2014-01-18 00:05:36 +01:00
|
|
|
}
|
2014-01-25 00:38:56 +01:00
|
|
|
};
|
|
|
|
}
|
2014-01-29 20:49:15 +01:00
|
|
|
/*
|
|
|
|
Return a new MQTT Client
|
|
|
|
*/
|
|
|
|
exports.client = function( brokerUrl, clientId, options ) {
|
|
|
|
if ( typeof org.walterhiggins.scriptcraft.ScriptCraftMqttCallback != 'function' ) {
|
2014-01-25 00:38:56 +01:00
|
|
|
throw MISSING_MQTT;
|
|
|
|
}
|
2014-01-29 20:49:15 +01:00
|
|
|
return new Client( brokerUrl, clientId, options );
|
2014-01-18 00:05:36 +01:00
|
|
|
};
|
|
|
|
|