| 1 |
|
| 2 |
|
| 3 |
var serverTitle= null; |
| 4 |
var client = null; |
| 5 |
subscriptionTitles = {}; |
| 6 |
subscriptionMessages = {}; |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
const mqtturl = 'ws://iot.expeyes.in:9001/'; |
| 13 |
var mqttoptions = { |
| 14 |
|
| 15 |
clean: true, |
| 16 |
connectTimeout: 2000, |
| 17 |
|
| 18 |
clientId: 'emqx_test', |
| 19 |
username: '', |
| 20 |
password: '', |
| 21 |
} |
| 22 |
|
| 23 |
function connectToCloud(name,callback){ |
| 24 |
mqttoptions.clientId = name; |
| 25 |
myname = name; |
| 26 |
|
| 27 |
client = mqtt.connect(mqtturl, mqttoptions); |
| 28 |
console.log(client); |
| 29 |
client.on('connect', function () { |
| 30 |
console.log('Connected') |
| 31 |
|
| 32 |
client.subscribe('test', function (err) { |
| 33 |
if (!err) { |
| 34 |
serverTitle.setValue("Connected to IoT!"); |
| 35 |
|
| 36 |
}else{ |
| 37 |
serverTitle.setValue("Failed to Connect IoT!") |
| 38 |
console.log('failed to connect to server'); |
| 39 |
} |
| 40 |
}); |
| 41 |
|
| 42 |
|
| 43 |
client.on('message', (channel, message) => { |
| 44 |
|
| 45 |
if(subscriptionMessages.hasOwnProperty(channel)){ |
| 46 |
subscriptionMessages[channel].push(JSON.parse(message)); |
| 47 |
|
| 48 |
subscriptionTitles[channel].fieldGroup_.setHTMLUnsafe(`<text fill="red" font-size="11" x=-5 y=10>[${subscriptionMessages[channel].length}]</text><text class="blocklyText" x=15 y=20>Subscribed To : ${channel}</text>`); |
| 49 |
} |
| 50 |
}); |
| 51 |
|
| 52 |
callback(); |
| 53 |
|
| 54 |
|
| 55 |
}); |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
function mqttPublish(channel, data){ |
| 60 |
client.publish(channel,data, {qos:2}); |
| 61 |
} |
| 62 |
|
| 63 |
function mqttSubscribe(channel, callback){ |
| 64 |
|
| 65 |
console.log('subscribe to : ',channel); |
| 66 |
client.subscribe(channel, function (err) { |
| 67 |
if (!err) { |
| 68 |
console.log('subscribe successful: '+channel); |
| 69 |
console.log(subscriptionTitles); |
| 70 |
subscriptionTitles[channel].setValue("Subscribed to "+channel) |
| 71 |
subscriptionMessages[channel] = []; |
| 72 |
}else{ |
| 73 |
console.log('subscribe failed: ',channel); |
| 74 |
subscriptionTitles[channel].setValue("Subscription failed") |
| 75 |
delete subscriptionMessages[channel]; |
| 76 |
} |
| 77 |
callback(); |
| 78 |
}, {qos: 2}); |
| 79 |
} |
| 80 |
|
| 81 |
function mqttHasData(channel){ |
| 82 |
if(subscriptionMessages.hasOwnProperty(channel)) |
| 83 |
return subscriptionMessages[channel].length; |
| 84 |
else |
| 85 |
return 0 |
| 86 |
} |
| 87 |
function mqttFetchData(channel){ |
| 88 |
if(subscriptionMessages.hasOwnProperty(channel)) |
| 89 |
return subscriptionMessages[channel].pop(); |
| 90 |
else |
| 91 |
return 0 |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
function stopMQTT(){ |
| 96 |
if(serverTitle != null) |
| 97 |
serverTitle.setValue("Connect to IOT Server"); |
| 98 |
if(client != null){ |
| 99 |
for( let a in subscriptionTitles){ |
| 100 |
console.log('unsub:',a); |
| 101 |
client.unsubscribe(a); |
| 102 |
} |
| 103 |
client.end(); |
| 104 |
} |
| 105 |
subscriptionTitles = {}; |
| 106 |
subscriptionMessages = {}; |
| 107 |
|
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
Blockly.Blocks['mqtt_connect'] = { |
| 114 |
init: function() { |
| 115 |
this.appendDummyInput("TITLE") |
| 116 |
.appendField("Connect IOT Server") |
| 117 |
.appendField(new Blockly.FieldImage("media/remote.png", 30, 30, { alt: "*", flipRtl: "FALSE" })); |
| 118 |
this.appendDummyInput() |
| 119 |
.appendField("Your Name:") |
| 120 |
.appendField(new Blockly.FieldTextInput(""), "NAME"); |
| 121 |
|
| 122 |
this.setColour(230); |
| 123 |
this.setTooltip("Connect to IoT"); |
| 124 |
this.setInputsInline(false); |
| 125 |
this.setPreviousStatement(true, null); |
| 126 |
this.setNextStatement(true, null); |
| 127 |
this.setHelpUrl(""); |
| 128 |
} |
| 129 |
}; |
| 130 |
|
| 131 |
Blockly.JavaScript['mqtt_connect'] = function(block) { |
| 132 |
var name = block.getFieldValue('NAME'); |
| 133 |
serverTitle = this.inputList[0].fieldRow[0]; |
| 134 |
console.log(this); |
| 135 |
var code = 'mqttConnect(\'' + name + '\');\n'; |
| 136 |
return code; |
| 137 |
}; |
| 138 |
|
| 139 |
|
| 140 |
Blockly.Python['mqtt_connect'] = function(block) { |
| 141 |
var code = 'mqttConnect()\n'; |
| 142 |
return code; |
| 143 |
}; |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
Blockly.Blocks['mqtt_publish'] = { |
| 151 |
init: function() { |
| 152 |
this.appendValueInput("CHANNEL") |
| 153 |
.setAlign(Blockly.ALIGN_LEFT) |
| 154 |
.appendField("MQTT Channel Name: "); |
| 155 |
this.appendValueInput("DATA") |
| 156 |
.setAlign(Blockly.ALIGN_LEFT) |
| 157 |
.appendField("Transmit: "); |
| 158 |
this.setInputsInline(false); |
| 159 |
this.setPreviousStatement(true, null); |
| 160 |
this.setNextStatement(true, null); |
| 161 |
this.setColour(230); |
| 162 |
this.setTooltip("Send Data"); |
| 163 |
this.setHelpUrl(""); |
| 164 |
} |
| 165 |
}; |
| 166 |
|
| 167 |
|
| 168 |
Blockly.JavaScript['mqtt_publish'] = function(block) { |
| 169 |
var channel = Blockly.JavaScript.valueToCode(block, 'CHANNEL', Blockly.JavaScript.ORDER_NONE); |
| 170 |
var data = Blockly.JavaScript.valueToCode(block, 'DATA', Blockly.JavaScript.ORDER_NONE); |
| 171 |
var code = 'mqttPublish(' + channel + ',' + data + ');\n'; |
| 172 |
return code; |
| 173 |
}; |
| 174 |
|
| 175 |
Blockly.Python['mqtt_publish'] = function(block) { |
| 176 |
var code = '\n'; |
| 177 |
return code; |
| 178 |
}; |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
Blockly.Blocks['mqtt_subscribe'] = { |
| 185 |
init: function() { |
| 186 |
this.appendDummyInput() |
| 187 |
.appendField("Subscribe To Channel") |
| 188 |
this.appendDummyInput() |
| 189 |
.appendField("Channel Name:") |
| 190 |
.appendField(new Blockly.FieldTextInput("TEST"), "CHANNEL"); |
| 191 |
|
| 192 |
this.setColour(230); |
| 193 |
this.setTooltip("Subscribe to IoT Channel"); |
| 194 |
this.setInputsInline(false); |
| 195 |
this.setPreviousStatement(true, null); |
| 196 |
this.setNextStatement(true, null); |
| 197 |
this.setHelpUrl(""); |
| 198 |
} |
| 199 |
}; |
| 200 |
|
| 201 |
Blockly.JavaScript['mqtt_subscribe'] = function(block) { |
| 202 |
var chan = block.getFieldValue('CHANNEL'); |
| 203 |
subscriptionTitles[chan] = this.inputList[0].fieldRow[0]; |
| 204 |
console.log(subscriptionTitles); |
| 205 |
console.log(chan); |
| 206 |
var code = 'mqttSubscribe(\'' + chan + '\');\n'; |
| 207 |
return code; |
| 208 |
}; |
| 209 |
|
| 210 |
|
| 211 |
Blockly.Python['mqtt_subscribe'] = function(block) { |
| 212 |
var code = 'mqttSubscribe()\n'; |
| 213 |
return code; |
| 214 |
}; |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
Blockly.Blocks['mqtt_hasdata'] = { |
| 219 |
init: function() { |
| 220 |
this.appendDummyInput() |
| 221 |
.appendField("IoT Channel:") |
| 222 |
.appendField(new Blockly.FieldTextInput("TEST"), "CHANNEL"); |
| 223 |
this.appendDummyInput() |
| 224 |
.appendField("Has Data?") |
| 225 |
.appendField(new Blockly.FieldImage("media/mail.png", 30, 30, { alt: "*", flipRtl: "FALSE" })); |
| 226 |
|
| 227 |
this.setOutput(true, null); |
| 228 |
this.setColour(230); |
| 229 |
this.setTooltip("check if data is available to read from IoT Channel"); |
| 230 |
this.setHelpUrl(""); |
| 231 |
} |
| 232 |
}; |
| 233 |
|
| 234 |
Blockly.JavaScript['mqtt_hasdata'] = function(block) { |
| 235 |
var chan = block.getFieldValue('CHANNEL'); |
| 236 |
var code = 'mqttHasData(\'' + chan + '\')'; |
| 237 |
return [code, Blockly.JavaScript.ORDER_NONE]; |
| 238 |
}; |
| 239 |
|
| 240 |
|
| 241 |
Blockly.Python['mqtt_hasdata'] = function(block) { |
| 242 |
var chan = block.getFieldValue('CHANNEL'); |
| 243 |
var code = 'mqttHasData(\'' + chan + '\')'; |
| 244 |
return [code, Blockly.Python.ORDER_NONE]; |
| 245 |
}; |
| 246 |
|
| 247 |
|
| 248 |
Blockly.Blocks['mqtt_fetchdata'] = { |
| 249 |
init: function() { |
| 250 |
this.appendDummyInput() |
| 251 |
.appendField("IoT Channel:") |
| 252 |
.appendField(new Blockly.FieldTextInput("TEST"), "CHANNEL"); |
| 253 |
this.appendDummyInput() |
| 254 |
.appendField("Latest Data") |
| 255 |
|
| 256 |
this.setOutput(true, null); |
| 257 |
this.setColour(230); |
| 258 |
this.setTooltip("check if data is available to read from IoT Channel"); |
| 259 |
this.setHelpUrl(""); |
| 260 |
} |
| 261 |
}; |
| 262 |
|
| 263 |
Blockly.JavaScript['mqtt_fetchdata'] = function(block) { |
| 264 |
var chan = block.getFieldValue('CHANNEL'); |
| 265 |
var code = 'mqttFetchData(\'' + chan + '\')'; |
| 266 |
return [code, Blockly.JavaScript.ORDER_NONE]; |
| 267 |
}; |
| 268 |
|
| 269 |
|
| 270 |
Blockly.Python['mqtt_fetchdata'] = function(block) { |
| 271 |
var chan = block.getFieldValue('CHANNEL'); |
| 272 |
var code = 'mqttFetchData(\'' + chan + '\')'; |
| 273 |
return [code, Blockly.Python.ORDER_NONE]; |
| 274 |
}; |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
Blockly.Blocks['mqtt_json'] = { |
| 280 |
init: function() { |
| 281 |
this.appendDummyInput() |
| 282 |
.appendField("Parse JSON Objects") |
| 283 |
this.appendValueInput("RAWDATA") |
| 284 |
.setAlign(Blockly.ALIGN_RIGHT) |
| 285 |
.appendField("JSON Object:"); |
| 286 |
this.appendValueInput("PARAMETER") |
| 287 |
.setAlign(Blockly.ALIGN_RIGHT) |
| 288 |
.appendField("Variable Name:"); |
| 289 |
this.setOutput(true, null); |
| 290 |
this.setInputsInline(false); |
| 291 |
this.setColour(230); |
| 292 |
this.setTooltip("Send Data"); |
| 293 |
this.setHelpUrl(""); |
| 294 |
} |
| 295 |
}; |
| 296 |
|
| 297 |
|
| 298 |
Blockly.JavaScript['mqtt_json'] = function(block) { |
| 299 |
var rawdata = Blockly.JavaScript.valueToCode(block, 'RAWDATA', Blockly.JavaScript.ORDER_NONE); |
| 300 |
var parameter = Blockly.JavaScript.valueToCode(block, 'PARAMETER', Blockly.JavaScript.ORDER_NONE); |
| 301 |
var code = 'mqttParseJson(' + rawdata + ',' + parameter+')'; |
| 302 |
return [code, Blockly.JavaScript.ORDER_NONE]; |
| 303 |
}; |
| 304 |
|
| 305 |
Blockly.Python['mqtt_json'] = function(block) { |
| 306 |
var rawdata = Blockly.Python.valueToCode(block, 'RAWDATA', Blockly.Python.ORDER_NONE); |
| 307 |
var parameter = Blockly.Python.valueToCode(block, 'PARAMETER', Blockly.Python.ORDER_NONE); |
| 308 |
return [rawdata[parameter], Blockly.Python.ORDER_NONE]; |
| 309 |
}; |
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
function initMQTT(interpreter, scope) { |
| 316 |
|
| 317 |
interpreter.setProperty(scope, 'mqttConnect', interpreter.createAsyncFunction( |
| 318 |
function(name,callback) { |
| 319 |
serverTitle.setValue("Connecting...") |
| 320 |
connectToCloud(name,callback); |
| 321 |
} |
| 322 |
)); |
| 323 |
|
| 324 |
interpreter.setProperty(scope, 'mqttPublish', interpreter.createNativeFunction( |
| 325 |
function(channel,data) { |
| 326 |
mqttPublish(channel,JSON.stringify(data)); |
| 327 |
} |
| 328 |
)); |
| 329 |
|
| 330 |
interpreter.setProperty(scope, 'mqttSubscribe', interpreter.createAsyncFunction( |
| 331 |
function(channel, callback) { |
| 332 |
mqttSubscribe(channel, callback); |
| 333 |
} |
| 334 |
)); |
| 335 |
|
| 336 |
interpreter.setProperty(scope, 'mqttHasData', interpreter.createNativeFunction( |
| 337 |
function(channel,) { |
| 338 |
return mqttHasData(channel); |
| 339 |
} |
| 340 |
)); |
| 341 |
|
| 342 |
interpreter.setProperty(scope, 'mqttFetchData', interpreter.createNativeFunction( |
| 343 |
function(channel) { |
| 344 |
return mqttFetchData(channel); |
| 345 |
} |
| 346 |
)); |
| 347 |
interpreter.setProperty(scope, 'mqttParseJson', interpreter.createNativeFunction( |
| 348 |
function(rawdata,parameter) { |
| 349 |
return rawdata[parameter]; |
| 350 |
} |
| 351 |
)); |
| 352 |
|
| 353 |
|
| 354 |
} |
| 355 |
|
| 356 |
|
| 357 |
|