Luna Bus Static Javascript Service


#1

How do I setup a static javascript service?

I want remote ip's to send a message via http and this message then get put onto the Luna service bus.

root@raspberrypi3:/media/internal/downloads# node
> require('webos-service');
Error: Cannot find module 'webos-service'
    at Function.Module._resolveFilename (module.js:469:15)
    at Function.Module._load (module.js:417:25)
    at Module.require (module.js:497:17)
    at require (internal/module.js:20:19)
    at repl:1:1
    at sigintHandlersWrap (vm.js:22:35)
    at sigintHandlersWrap (vm.js:73:12)
    at ContextifyScript.Script.runInThisContext (vm.js:21:12)
    at REPLServer.defaultEval (repl.js:340:29)
    at bound (domain.js:280:14)
>

#2

This kicks off a dynamic built in javascript service Node.js server. It works for my simple use case. It would be nice if the documentation explained how to create a static javascript service.

var Service = require('webos-service');
var http = require('http');
var url  = require('url');
const { execSync } = require('child_process');

// Register com.example.service.js
var service = new Service("com.example.service.js");
service.activityManager.idleTimeout = 60 * 60 * 24 * 7; // 1 week
var server = null;

// start server
service.register("startServer", function(message) {
if(server != null) {
  var response = "HTTP server already started";
  message.respond({message: response});
  return;
}
server = http.createServer(function(request, response) {
  var url_parts = url.parse(request.url, true);
  var query = url_parts.query;

  response.writeHead(200, "OK", {'Content-Type': 'text/plain'});

	    if(typeof(query.getForegroundAppInfo) !== 'undefined') {
    let stdout = execSync("luna-send -n 1 -f luna://com.webos.service.applicationManager/getForegroundAppInfo '{}'");
    console.log(stdout);
    response.write(stdout);
    response.end();
  }
  else if(typeof(query.listApps) !== 'undefined') {
    let stdout = execSync("luna-send -n 1 -f luna://com.webos.service.applicationManager/listApps '{}'");
    console.log(stdout);
    response.write(stdout);
    response.end();
  }
  else if(typeof(query.launchApp) !== 'undefined') {
    let stdout = execSync("luna-send -n 1 -f luna://com.webos.service.applicationmanager/launch '{\"id\":\"" + query.id + "\"}'");
    console.log(stdout);
    response.write(stdout);
    response.end();
  }
  else if(typeof(query.installApp) !== 'undefined') {
    let stdout = execSync("luna-send -n 1 -f luna://com.webos.appInstallService/install '{\"id\":\"" + query.id + "," + "\"ipkUrl\":\"" + query.ipkUrl + "}'");
    console.log(stdout);
    response.write(stdout);
    response.end();
  }
  else if(typeof(query.locale) !== 'undefined') {
    service.call("luna://com.webos.settingsservice/getSystemSettings", {"key":"localeInfo"}, function(m2) {
      console.log(m2);
      response.write(JSON.stringify(m2));
  		    response.end();
    });
  }
  else {
    console.log("Error logic in GET!");
    response.end("Error logic in GET!");
  }

	  });
server.listen(8080); //the server object listens on port 8080

var response = "Started HTTP server";
message.respond({message: response});
});

// stop server
service.register("stopServer", function(message) {
if(server != null) {
  server.close();
}
var response = "Stopped HTTP server";
message.respond({message: response});
});

#3

Thanks for posting. it will be helpful to us.