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.


#4

The node service system is really more built to run dynamically, but one way of getting a service to run "indefinitely" is to call activitymanager/create to grab a new activity, subscribe to it, and only let go of it when you're actually ready to close the service. If I didn't want to edit any startup files, to run something at boot, for example, I'd probably do this way. I believe you can also setup an activitymanager function to trigger on startup, so if you want that service to be pretty much always running, you can use service.register('startServer'...) like you are, call activitymanager/create immediately to hold the service open... and you can trigger it using an activitymanager event triggered at startup.

What I'm not seeing, is the part in the docs for activitymanager that allows it to trigger events at certain conditions that aren't triggers or schedules... i know there used to be something in there, but it may not be documented.. or it may not be in there anymore.

In any case, I think the best way to do it, if you're competing with activitymanager trying to kill your process, is to create an activity so it knows you're still doing something.