termux / termux-api

Termux add-on app which exposes device functionality as API to command line programs.
https://f-droid.org/en/packages/com.termux.api/
2.36k stars 462 forks source link

Idea/Question: REST-API #49

Open Spiritdude opened 8 years ago

Spiritdude commented 8 years ago

I like to make/have termux-api available via REST, for example:

Android:

% termux-rest &

PC:

% curl 'http://192.168.2.100:8091/camera-photo?c=1'

retrieves JPEG direct

% curl http://192.168.2.100:8091/camera-info

retrieves JSON

etc. essentially http://x.x.x.x:port/(\w+)?(.+) -> termux-$1 $2 (with some proper argument parsing)

Question: what would be the easiest way (programming framework) to implement this lightweight http server providing the functionality?

I quickly coded (2hr) perl script which implements basic web-server which calls termux- (with simple argument parsing) and takes output to return to http GET request, yet, it might be more suitable to use termux-api.c direct and put a small web-server into it, which connects to the internal API instead via termux- -> termux-api -> am -> tcp.

I like to probe multiple android phones for battery info (actually the battery temperature is what I care about) and take photos via REST, turning Android smartphones into IoT devices.

My quick hack does this, accessing http://192.168.2.100:8091/ gives an overview of the services:

termux-rest (0.0.2) services:
   battery-status
   camera-info
   camera-photo: c=<...>
   location
   telephony-cellinfo
   telephony-deviceinfo
   tts-engines
   tts-speak: speak=<...>
   vibrate

with Content-Type: text/html, each service listed is a link.

and http://192.168.2.100:8091/battery-status:

{
  "health": "GOOD",
  "percentage": 97,
  "plugged": "UNPLUGGED",
  "status": "DISCHARGING",
  "temperature": 23.600000381469727
}

with Content-Type: application/json and CORS set so XHR JavaScript requests can be made.

jaromil commented 8 years ago

http://Kore.io would be a good candidate for the programming framework, all in C and portable, I'm using it in other embedded projects and works fine and is well documented. I am not aware of the status of higher language interpreters implementations in termux.

GonzaloRizzo commented 8 years ago

This is exactly what I was planning to do with this API! But I don't know C so I'll try to do it in nodejs

sax1johno commented 8 years ago

If you're using node js you can throw something like this together in literally a few minutes with node-red. Install / run, drag an http node onto the canvas, and use the exec node to execute commands.

fornwall commented 8 years ago

Sounds like a good suggestion! We could create a termux-api-server package, which can depend on what's needed (perl/node/...). Perl is a bit heavyweight as a dependency. Something in go would be nice (as it compiles to a executable without runtime dependencies), but node is fine too.

imikod commented 7 years ago

I gave it a go https://github.com/imikod/termux-api-server