Skip to content

Idea/Question: REST-API #49

Open
Open
@Spiritdude

Description

@Spiritdude

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions