corrade-lsl-templates – Diff between revs 15 and 24

Subversion Repositories:
Rev:
Show entire fileIgnore whitespace
Rev 15 Rev 24
Line 177... Line 177...
177 string configuration = ""; 177 string configuration = "";
178 // callback URL 178 // callback URL
179 string URL = ""; 179 string URL = "";
180 // store message over state. 180 // store message over state.
181 string data = ""; 181 string data = "";
-   182 string jump_table = "";
-   183 string messageHash = "";
Line 182... Line 184...
182   184  
183 default { 185 default {
184 state_entry() { 186 state_entry() {
185 llOwnerSay("[AI] Starting..."); 187 llOwnerSay("[AI] Starting...");
186 llSetTimerEvent(10); 188 llSetTimerEvent(10);
187 } 189 }
188 link_message(integer sender, integer num, string message, key id) { 190 link_message(integer sender, integer num, string message, key id) {
189 if(id != "configuration") return; 191 if(id != "configuration") return;
190 llOwnerSay("[AI] Got configuration..."); 192 llOwnerSay("[AI] Got configuration...");
-   193 configuration = message;
-   194
-   195 // Subscribe to MQTT messages.
191 configuration = message; 196 jump_table = "subscribe";
192 state listen_group; 197 state url;
193 } 198 }
194 timer() { 199 timer() {
195 llOwnerSay("[AI] Requesting configuration..."); 200 llOwnerSay("[AI] Requesting configuration...");
196 llMessageLinked(LINK_THIS, 0, "configuration", NULL_KEY); 201 llMessageLinked(LINK_THIS, 0, "configuration", NULL_KEY);
Line 208... Line 213...
208 state_exit() { 213 state_exit() {
209 llSetTimerEvent(0); 214 llSetTimerEvent(0);
210 } 215 }
211 } 216 }
Line -... Line 217...
-   217  
-   218 state url {
-   219 state_entry() {
-   220 // DEBUG
-   221 llOwnerSay("[AI] Requesting URL...");
-   222 llRequestURL();
-   223 }
-   224 http_request(key id, string method, string body) {
-   225 if(method != URL_REQUEST_GRANTED) return;
-   226 URL = body;
-   227
-   228 // DEBUG
-   229 llOwnerSay("[AI] Got URL...");
-   230
-   231 if(jump_table == "subscribe")
-   232 state subscribe;
-   233 if(jump_table == "publish")
-   234 state publish;
-   235 }
-   236 on_rez(integer num) {
-   237 llResetScript();
-   238 }
-   239 changed(integer change) {
-   240 if((change & CHANGED_INVENTORY) ||
-   241 (change & CHANGED_REGION_START) ||
-   242 (change & CHANGED_OWNER)) {
-   243 llResetScript();
-   244 }
-   245 }
-   246 }
-   247  
-   248 state subscribe {
-   249 state_entry() {
-   250 // DEBUG
-   251 llOwnerSay("[AI] Subscribing to Corrade AI...");
-   252 llInstantMessage(
-   253 wasKeyValueGet(
-   254 "corrade",
-   255 configuration
-   256 ),
-   257 wasKeyValueEncode(
-   258 [
-   259 "command", "MQTT",
-   260 "group", wasURLEscape(
-   261 wasKeyValueGet(
-   262 "group",
-   263 configuration
-   264 )
-   265 ),
-   266 "password", wasURLEscape(
-   267 wasKeyValueGet(
-   268 "password",
-   269 configuration
-   270 )
-   271 ),
-   272 // Subscribe to Corrade AI
-   273 "action", "subscribe",
-   274 "id", wasURLEscape(
-   275 wasKeyValueGet(
-   276 "ai subscription",
-   277 configuration
-   278 )
-   279 ),
-   280 // Corrade AI listening host.
-   281 "host", wasURLEscape(
-   282 wasKeyValueGet(
-   283 "ai host",
-   284 configuration
-   285 )
-   286 ),
-   287 // Corrade AI listening port.
-   288 "port", wasURLEscape(
-   289 wasKeyValueGet(
-   290 "ai port",
-   291 configuration
-   292 )
-   293 ),
-   294 // Corrade AI credentials.
-   295 "username", wasURLEscape(
-   296 wasKeyValueGet(
-   297 "ai username",
-   298 configuration
-   299 )
-   300 ),
-   301 "secret", wasURLEscape(
-   302 wasKeyValueGet(
-   303 "ai secret",
-   304 configuration
-   305 )
-   306 ),
-   307 // Use the SIML module of Corrade AI.
-   308 "topic", "SIML",
-   309 // Send the result of the MQTT command to this URL.
-   310 "callback", wasURLEscape(URL)
-   311 ]
-   312 )
-   313 );
-   314 }
-   315
-   316 http_request(key id, string method, string body) {
-   317 llHTTPResponse(id, 200, "OK");
-   318 llReleaseURL(URL);
-   319 if(wasKeyValueGet("command", body) != "MQTT" ||
-   320 wasKeyValueGet("success", body) != "True") {
-   321 // DEBUG
-   322 llOwnerSay("[AI] Unable to subscribe to MQTT topic: " +
-   323 wasURLUnescape(
-   324 wasKeyValueGet("error", body)
-   325 )
-   326 );
-   327 llResetScript();
-   328 }
-   329
-   330 state listen_group;
-   331 }
-   332 on_rez(integer num) {
-   333 llResetScript();
-   334 }
-   335 changed(integer change) {
-   336 if((change & CHANGED_INVENTORY) ||
-   337 (change & CHANGED_REGION_START) ||
-   338 (change & CHANGED_OWNER)) {
-   339 llResetScript();
-   340 }
-   341 }
-   342 state_exit() {
-   343 llSetTimerEvent(0);
-   344 }
-   345 }
212   346  
213 state listen_group { 347 state listen_group {
214 state_entry() { 348 state_entry() {
215 // DEBUG 349 // DEBUG
216 llOwnerSay("[AI] Waiting for group messages..."); 350 llOwnerSay("[AI] Waiting for group messages...");
217 } 351 }
218 link_message(integer sender, integer num, string message, key id) { 352 link_message(integer sender, integer num, string message, key id) {
219 // We only care about notifications now. 353 // We only care about notifications now.
220 if(id != "notification") 354 if(id != "notification")
Line 221... Line 355...
221 return; 355 return;
222 356
223 // This script only processes group notifications. 357 // Listen to group message notifications.
Line 224... Line 358...
224 if(wasKeyValueGet("type", message) != "group") 358 if(wasKeyValueGet("type", message) != "group")
225 return; 359 return;
Line 249... Line 383...
249 383
250 // Dump the rest of the message. 384 // Dump the rest of the message.
Line 251... Line 385...
251 data = llDumpList2String(command, " "); 385 data = llDumpList2String(command, " ");
-   386  
252   387 // Get an URL.
253 // Get an URL. 388 jump_table = "publish";
254 state url; 389 state url;
255 } 390 }
256 on_rez(integer num) { 391 on_rez(integer num) {
Line 263... Line 398...
263 llResetScript(); 398 llResetScript();
264 } 399 }
265 } 400 }
266 } 401 }
Line 267... Line 402...
267   402  
268 state url { -  
269 state_entry() { -  
270 // DEBUG -  
271 llOwnerSay("[AI] Requesting URL..."); -  
272 llRequestURL(); -  
273 } -  
274 http_request(key id, string method, string body) { -  
275 if(method != URL_REQUEST_GRANTED) return; -  
276 URL = body; -  
277 // DEBUG -  
278 llOwnerSay("[AI] Got URL..."); -  
279 state version; -  
280 } -  
281 on_rez(integer num) { -  
282 llResetScript(); -  
283 } -  
284 changed(integer change) { -  
285 if((change & CHANGED_INVENTORY) || -  
286 (change & CHANGED_REGION_START) || -  
287 (change & CHANGED_OWNER)) { -  
288 llResetScript(); -  
289 } -  
290 } -  
291 } -  
292   -  
293 state version { 403 state publish {
294 state_entry() { 404 state_entry() {
295 // DEBUG 405 // DEBUG
-   406 llOwnerSay("[AI] Sending to AI for processing...");
-   407
-   408 messageHash = llSHA1String(data);
296 llOwnerSay("[AI] Sending to AI for processing..."); 409
297 llInstantMessage( 410 llInstantMessage(
298 wasKeyValueGet( 411 wasKeyValueGet(
299 "corrade", 412 "corrade",
300 configuration 413 configuration
301 ), 414 ),
302 wasKeyValueEncode( 415 wasKeyValueEncode(
303 [ 416 [
304 "command", "ai", 417 "command", "MQTT",
305 "group", wasURLEscape( 418 "group", wasURLEscape(
306 wasKeyValueGet( 419 wasKeyValueGet(
307 "group", 420 "group",
308 configuration 421 configuration
Line 312... Line 425...
312 wasKeyValueGet( 425 wasKeyValueGet(
313 "password", 426 "password",
314 configuration 427 configuration
315 ) 428 )
316 ), 429 ),
317 "action", "process", 430 "action", "publish",
-   431 // Corrade AI listening host.
-   432 "host", wasURLEscape(
-   433 wasKeyValueGet(
-   434 "ai host",
-   435 configuration
-   436 )
-   437 ),
-   438 // Corrade AI listening port.
-   439 "port", wasURLEscape(
-   440 wasKeyValueGet(
-   441 "ai port",
-   442 configuration
-   443 )
-   444 ),
-   445 // Corrade AI credentials.
318 "message", wasURLEscape(data), 446 "username", wasURLEscape(
-   447 wasKeyValueGet(
-   448 "ai username",
-   449 configuration
-   450 )
-   451 ),
-   452 "secret", wasURLEscape(
-   453 wasKeyValueGet(
-   454 "ai secret",
-   455 configuration
-   456 )
-   457 ),
-   458 // Use the SIML module of Corrade AI.
-   459 "topic", "SIML",
-   460 "payload", wasURLEscape(
-   461 wasKeyValueEncode(
-   462 [
-   463 // The hash is an identifier that will allow responses from Corrade AI
-   464 // for various messages to be distinguished. It can be any identifier
-   465 // but a handy way of generating an identifier is to hash the message.
-   466 "Hash", messageHash,
-   467 // Note the double escaping!
-   468 "Message", wasURLEscape(data)
-   469 ]
-   470 )
-   471 ),
319 "callback", wasURLEscape(URL) 472 "callback", wasURLEscape(URL)
320 ] 473 ]
321 ) 474 )
322 ); 475 );
323 llSetTimerEvent(60); 476 llSetTimerEvent(60);
324 } 477 }
325 http_request(key id, string method, string body) { 478 http_request(key id, string method, string body) {
326 llHTTPResponse(id, 200, "OK"); 479 llHTTPResponse(id, 200, "OK");
327 llReleaseURL(URL); 480 llReleaseURL(URL);
328 if(wasKeyValueGet("command", body) != "ai" || 481 if(wasKeyValueGet("command", body) != "MQTT" ||
329 wasKeyValueGet("success", body) != "True") { 482 wasKeyValueGet("success", body) != "True") {
330 // DEBUG 483 // DEBUG
331 llOwnerSay("[AI] Unable to get processed message: " + 484 llOwnerSay("[AI] Unable to publish message: " +
332 wasURLUnescape( 485 wasURLUnescape(
333 wasKeyValueGet("error", body) 486 wasKeyValueGet("data", body)
334 ) 487 )
335 ); 488 );
336 state listen_group; 489 state listen_group;
337 } 490 }
Line -... Line 491...
-   491
-   492 // DEBUG
-   493 llOwnerSay("[AI] Message published successfully...");
-   494 }
-   495 link_message(integer sender, integer num, string message, key id) {
-   496 // We only care about notifications now.
-   497 if(id != "notification")
-   498 return;
-   499
-   500 // Listen to MQTT messages.
-   501 if(wasKeyValueGet("type", message) != "MQTT")
-   502 return;
338 503
-   504 // Get the sent message.
-   505 data = wasURLUnescape(
-   506 wasKeyValueGet(
-   507 "payload",
-   508 message
-   509 )
-   510 );
-   511
-   512 string hash = wasURLUnescape(
-   513 wasKeyValueGet(
-   514 "Hash",
-   515 data
-   516 )
-   517 );
-   518
-   519 string serverMessage = wasURLUnescape(
-   520 wasKeyValueGet(
-   521 "ServerMessage",
-   522 data
-   523 )
-   524 );
-   525
-   526 // Skip generated messages that are not for the published message.
-   527 if(hash != messageHash ||
-   528 serverMessage != "True")
-   529 return;
-   530
339 // Get the processed message. 531 data = wasURLUnescape(
-   532 wasKeyValueGet(
-   533 "Message",
-   534 data
-   535 )
Line 340... Line 536...
340 data = wasKeyValueGet("data", body); 536 );
341 537
342 state tell; 538 state tell;
343 } 539 }
Line 383... Line 579...
383 "password", 579 "password",
384 configuration 580 configuration
385 ) 581 )
386 ), 582 ),
387 "entity", "group", 583 "entity", "group",
388 "message", data // message is already encoded 584 "message", wasURLEscape(data)
389 ] 585 ]
390 ) 586 )
391 ); 587 );
392 state listen_group; 588 state listen_group;
393 } 589 }