Skip to content

Commit

Permalink
Add support for auto connect where all the devices within a profile a…
Browse files Browse the repository at this point in the history
…re connected after they are started
  • Loading branch information
knro committed Dec 4, 2018
1 parent c7cb1a8 commit d552716
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
*.egg-info
*~
__pycache__/**

/build
/dist
.idea
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ URL | Method | Return | Format
/api/profiles | GET | Returns all profiles | [{"port": number, "id": ID, "autostart": number, "name": profile_name}, ...]

**Example:** curl http://localhost:8624/api/profiles
**Reply:** [{"port": 7624, "id": 1, "autostart": 0, "name": "Simulators"}, {"port": 7624, "id": 2, "autostart": 0, "name": "EQ5"}]
**Reply:** [{"port": 7624, "id": 1, "autostart": 0, "autoconnect": 0, "name": "Simulators"}, {"port": 7624, "id": 2, "autostart": 0, "name": "EQ5"}]

### TODO

Expand Down
31 changes: 25 additions & 6 deletions indiweb/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import sqlite3
import logging

VERSION = "0.1.5"
VERSION = "0.1.6"


def dict_factory(cursor, row):
Expand All @@ -28,12 +28,30 @@ def __init__(self, filename):
self.__conn = sqlite3.connect(filename, check_same_thread=False)
self.__conn.row_factory = dict_factory

# update table for version and any schema updates
self.update()
# create new tables if they doesn't exist
self.create(filename)

def create(self, filename):
def update(self):
c = self.__conn.cursor()
# Check if we have a version table.
try:
c.execute('SELECT version FROM Version')
result = c.fetchone()
# Add autoconnect to profile before 0.1.6
if result['version'] < '0.1.6':
c.execute('ALTER TABLE profile ADD COLUMN autoconnect INTEGER DEFAULT 0')
except sqlite3.Error:
pass

try:
c.execute('UPDATE Version SET version=?', VERSION)
except sqlite3.Error:
pass

def create(self, filename):
c = self.__conn.cursor()
# Check if we have a version table. If not, then the scheme is too old and needs updating
try:
c.execute('SELECT version FROM Version')
Expand All @@ -60,7 +78,8 @@ def create(self, filename):
c.execute('CREATE TABLE IF NOT EXISTS '
'profile (id INTEGER PRIMARY KEY AUTOINCREMENT,'
'name TEXT UNIQUE, port INTEGER DEFAULT 7624, '
'autostart INTEGER DEFAULT 0)')
'autostart INTEGER DEFAULT 0, '
'autoconnect INTEGER DEFAULT 0)')
c.execute('UPDATE Version SET version=?', (VERSION,))

self.__conn.commit()
Expand Down Expand Up @@ -136,15 +155,15 @@ def get_profile(self, name):
(name,))
return cursor.fetchone()

def update_profile(self, name, port, autostart=False):
def update_profile(self, name, port, autostart=False, autoconnect=False):
"""Update profile info"""

c = self.__conn.cursor()
if autostart:
# If we have a profile with autostart=1, reset everyone else to 0
c.execute('UPDATE profile SET autostart=0')
c.execute('UPDATE profile SET port=?, autostart=? WHERE name=?',
(port, autostart, name))
c.execute('UPDATE profile SET port=?, autostart=?, autoconnect=? WHERE name=?',
(port, autostart, autoconnect, name))
self.__conn.commit()
c.close()

Expand Down
15 changes: 15 additions & 0 deletions indiweb/indi_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,21 @@ def get_prop(self, dev, prop, element):
def get_state(self, dev, prop):
return self.get_prop(dev, prop, '_STATE')

def auto_connect(self):
cmd = ['indi_getprop', '*.CONNECTION.CONNECT']
output = ""
try:
output = check_output(cmd).decode('utf_8')
except e:
pass
logging.info(output)
output = output.replace("Off", "On")
logging.info(output)
for dev in output.splitlines():
command = ['indi_setprop', dev]
logging.info(command)
call(command)

def get_running_drivers(self):
# drivers = [proc.name() for proc in psutil.process_iter() if
# proc.name().startswith('indi_')]
Expand Down
12 changes: 9 additions & 3 deletions indiweb/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import json
import logging
import argparse
from threading import Timer
from subprocess import call

from bottle import Bottle, run, template, static_file, request, response, BaseRequest, default_app
from .indi_server import IndiServer, INDI_PORT, INDI_FIFO, INDI_CONFIG_DIR
Expand Down Expand Up @@ -80,7 +82,6 @@
saved_profile = None
active_profile = ""


def start_profile(profile):
info = db.get_profile(profile)

Expand All @@ -96,6 +97,10 @@ def start_profile(profile):

if all_drivers:
indi_server.start(info['port'], all_drivers)
# Auto connect drivers in 3 seconds if required.
if info['autoconnect'] == 1:
t = Timer(3, indi_server.auto_connect)
t.start()


@app.route('/static/<path:path>')
Expand Down Expand Up @@ -156,13 +161,14 @@ def delete_profile(name):

@app.put('/api/profiles/<name>')
def update_profile(name):
"""Update profile info (port & autostart)"""
"""Update profile info (port & autostart & autoconnect)"""
response.set_cookie("indiserver_profile", name,
None, max_age=3600000, path='/')
data = request.json
port = data.get('port', args.indi_port)
autostart = bool(data.get('autostart', 0))
db.update_profile(name, port, autostart)
autoconnect = bool(data.get('autoconnect', 0))
db.update_profile(name, port, autostart, autoconnect)


@app.post('/api/profiles/<name>/drivers')
Expand Down
7 changes: 5 additions & 2 deletions indiweb/views/form.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<!-- Set the page to the width of the device and set the zoon level -->
<meta name="viewport" content="width = device-width, initial-scale = 1">
<title>StellarMate Web Manager</title>
<title>INDI Web Manager</title>
<link rel="stylesheet" type="text/css" href="/static/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="/static/css/jquery-ui.min.css">
<link rel="stylesheet" type="text/css" href="/static/css/bootstrap-select.min.css">
Expand Down Expand Up @@ -41,7 +41,10 @@
<button class="btn btn-default" onClick="removeProfile()" data-toggle="tooltip" title="Delete Profile"><span class="glyphicon glyphicon-minus" aria-hidden="true"></span></button>
</span>
</div>
<div class="checkbox"><label><input id="profile_auto" onChange="saveProfileInfo()" type="checkbox" value="Autostart">Auto Start</label></div>
<div>
<label class="checkbox-inline"><input id="profile_auto_start" onChange="saveProfileInfo()" type="checkbox" value="Autostart">Auto Start</label>
<label class="checkbox-inline"><input id="profile_auto_connect" onChange="saveProfileInfo()" type="checkbox" value="Autoconnect">Auto Connect</label>
</div>
</div>
</div>

Expand Down
17 changes: 12 additions & 5 deletions indiweb/views/js/indi.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,16 @@ function saveProfileInfo() {
console.log(name);
var port = $("#profile_port").val();
console.log(port);
var autostart = ($('#profile_auto').is(':checked')) ? 1 : 0;
console.log(autostart);
var autostart = ($('#profile_auto_start').is(':checked')) ? 1 : 0;
var autoconnect = ($('#profile_auto_connect').is(':checked')) ? 1 : 0;
//console.log(autostart);
//var url = "/api/profiles/" + name + "/" + port + "/" + autostart;
var url = "/api/profiles/" + name;

var profileInfo = {
"port": port,
"autostart": autostart
"autostart": autostart,
"autoconnect": autoconnect,
};
profileInfo = JSON.stringify(profileInfo);
console.log("Profile info " + profileInfo);
Expand Down Expand Up @@ -156,9 +158,14 @@ function loadProfileData() {

$.getJSON(url, function(info) {
if (info.autostart == 1)
$("#profile_auto").prop("checked", true);
$("#profile_auto_start").prop("checked", true);
else
$("#profile_auto").prop("checked", false);
$("#profile_auto_start").prop("checked", false);

if (info.autoconnect == 1)
$("#profile_auto_connect").prop("checked", true);
else
$("#profile_auto_connect").prop("checked", false);

$("#profile_port").val(info.port);

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

setup(
name='indiweb',
version='0.1.5',
version='0.1.6',
description='A simple web application to manage INDI server',
long_description=LONG_DESCRIPTION,
author='Jasem Mutlaq, Juan Menendez',
Expand Down

0 comments on commit d552716

Please sign in to comment.