Simple remote control for daemon.
This is an example of how to write REST API client for android on Python. However if you are interested on the improvements - create an issue and describe your idea.
Android app based on the Kivy framework. Works on Python 2.7 and 3.x. Server-side code based on Django (tested on 1.9.6), uses Django REST framework.
https://www.youtube.com/watch?v=EqxDIJOSegw
Project content:
- ui_app - Android app folder
- main.py - app implementation
- android.txt - settings for Kivy launcher
- buildozer.spec - configuration for build .apk
- web - Django project folder
- remotecontrol - Django app with REST API implementation
- interface - server-side API
- control.py - implementation of IRemoteControl class
- user_commands.py - custom commands definition
- another Django app modules
- interface - server-side API
- daemon_example.py - demo daemon with IRemoteControl usage
- remotecontrol - Django app with REST API implementation
Usage example:
class MyDaemon(IRemoteControl):
def core(self):
# daemon logic
def main_loop(self):
while True:
self.check_commands()
self.core()
Current version supports following service commands:
- CODE_PAUSE - Pausing daemon execution (check_commands() has a loop until CODE_RESUME is received)
- CODE_RESUME - Resuming daemon execution
- CODE_RESTART - Restarting daemon (requires custom logic)
- CODE_REMOTE_OFF - Disabling remote control (all the next commands are ignored)
NOTE:
Receiving CODE_RESTART has no effect. Implement method _restart_stuff() in your subclass to get effect.
Otherwise CODE_RESTART is ignored.
If you want to use custom commands, you need:
-
write custom commands definition (user_commands.py):
USER_COMMANDS = ( (101, 'Command #1'), (102, 'Command #2'), )
-
implement logic for each command:
class MyDaemon(IRemoteControl): def __init__(self): self.user_commands = { 101: self.command1, 102: self.command2, } super(MyDaemon, self).__init__() def command1(self, command): # command #1 logic self._done_command(command) def command2(self, command): # command #2 logic self._done_command(command)
NOTE:
The custom command logic must mark command as 'done' (._done_command()) or as 'declined' (._ignore_command()).
That's all!
SERVER-SIDE
-
Python 2.7 (or higher) or Python 3.4 (or higher)
-
Django (1.9.6 recommended)
-
Django apps
pip install django-ipware pip install djangorestframework
-
Download and unpack
RemoteControlInterface
-
Make preparations
python web\manage.py makemigrations remotecontrol python web\manage.py migrate remotecontrol
-
Run server:
python web\manage.py runserver
-
Run demo daemon:
python mysite\daemon_example.py
CLIENT-SIDE (on android via Kivy launcher)
-
Install
Kivy launcher
(http://play.google.com/store/apps/details?id=org.kivy.pygame) -
Download and unpack
XPopup
toui_app
folder (https://github.com/kivy-garden/garden.xpopup) -
Copy
ui_app
folder to/sdcard/kivy/
CLIENT-SIDE (as android app)
-
Install
Kivy
library (https://kivy.org/#download) -
Install
Kivy Garden
(if not already installed)pip install kivy-garden
-
Install
Buildozer
(http://kivy.org/docs/guide/packaging-android.html#buildozer) -
Build app from
ui_app
folderbuildozer android debug
-
The resulting .apk install on your android device
CLIENT-SIDE (on desktop)
-
Install
Kivy
library (https://kivy.org/#download) -
Install
Kivy Garden
(if not already installed)pip install kivy-garden
-
Install
XPopup
extensiongarden install xpopup
-
Run
RemoteControl
apppython ui_app\main.py
-
0.1
Initial release