<commit_before>#!/usr/bin/env python

import datetime
import json
import random as rand
import sys
from threading import Thread
import time

import unicodedata,socket
from isc_dhcp_leases.iscdhcpleases import Lease, IscDhcpLeases
from flask import Flask, render_template, request, jsonify, g, session, flash, url_for, redirect, abort
from flask.ext.sqlalchemy import SQLAlchemy
from sqlalchemy import create_engine, inspect
from sqlalchemy.orm import sessionmaker
from flask.ext.login import LoginManager, login_user, logout_user, current_user, login_required
from config import SQLALCHEMY_DATABASE_URI
from astral import Astral
import model as model
import schedule

engine = create_engine(SQLALCHEMY_DATABASE_URI)
Session = sessionmaker(bind=engine)

a = Astral()
geo = Astral().geocoder

app = Flask(__name__)
app.config.from_object('config')

# Login system initialization
login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view = 'login'
app.secret_key = 'super_secret_key'

@app.route("/")
def index():
    return render_template('index.html', groups=model.Group.query.filter_by(parent_id=None), lights=model.Light.query.filter_by(parent_id=None))

@app.route("/settings")
def settings():
    return render_template('settings.html', geo=sorted(geo))

@app.route('/buttons')
@login_required
def buttons():
    return render_template('buttons.html', commands=AVAILABLE_COMMANDS)

@app.route('/buttons_proc',methods=['POST'])
def command(cmd=None):
    light_command = request.json['command']
    mac = request.json['mac']
    unicodedata.normalize('NFKD',light_command).encode('ascii','ignore')
    unicodedata.normalize('NFKD',mac).encode('ascii','ignore')
    enlighten(light_command,mac)
    return light_command+"@"+mac, 200, {'Content-Type': 'text/plain'}

def str2bool(st):
    try:
        return ['false', 'true'].index(st.lower())
    except (ValueError, AttributeError):
        raise ValueError('no Valid Conversion Possible')

def send_command(light,action):
    print "Sending command to ", light.device.ipaddr, " on ", light.port, " turning it to ", action
    ip = str(light.device.ipaddr)
    tcp_port = 9999
    command = ""
    for lights in range(0,4):
        print light.device.lights[lights].status
        if light.device.lights[lights].port == light.port:
            print "SEND TO ", light.port
            command += str(str2bool(action))
            lightUpdate = model.Light.query.filter_by(id=light.device.lights[lights].id).first()
            lightUpdate.status = int(str2bool(action))
            model.db.session.commit()
        elif light.device.lights[lights].status is None:
            command += '0'
        else:
            command += str(light.device.lights[lights].status)
    print command
    #sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    #result = sock.connect_ex((ip,tcp_port))
    #sock.send(command)
    return "OK"

@app.route('/enlighten',methods=['POST'])
def enlighten():
    light_type = request.form['type']
    action = request.form['state']
    if light_type == 'group':
        group = model.Group.query.filter_by(id=request.form['group']).first()
        print group.groups, " with ", len(group.groups) , " children"
        for item in xrange(0,len(group.lights)):
            result = send_command(group.lights[item],action)
    if light_type == 'light':
        light = model.Light.query.filter_by(id=request.form['light']).first()
        print "Light at ", light.device_mac
        result = send_command(light,action)
    return result
@app.route("/advanced")
def advanced():<commit_msg>web: remove demo features (buttons page, enlighten function)<commit_after>#!/usr/bin/env python

import datetime
import json
import random as rand
import sys
from threading import Thread
import time

import unicodedata,socket
from isc_dhcp_leases.iscdhcpleases import Lease, IscDhcpLeases
from flask import Flask, render_template, request, jsonify, g, session, flash, url_for, redirect, abort
from flask.ext.sqlalchemy import SQLAlchemy
from sqlalchemy import create_engine, inspect
from sqlalchemy.orm import sessionmaker
from flask.ext.login import LoginManager, login_user, logout_user, current_user, login_required
from config import SQLALCHEMY_DATABASE_URI
from astral import Astral
import model as model
import schedule

engine = create_engine(SQLALCHEMY_DATABASE_URI)
Session = sessionmaker(bind=engine)

a = Astral()
geo = Astral().geocoder

app = Flask(__name__)
app.config.from_object('config')

# Login system initialization
login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view = 'login'
app.secret_key = 'super_secret_key'

@app.route("/")
def index():
    return render_template('index.html', groups=model.Group.query.filter_by(parent_id=None), lights=model.Light.query.filter_by(parent_id=None))

@app.route("/settings")
def settings():
    return render_template('settings.html', geo=sorted(geo))

def str2bool(st):
    try:
        return ['false', 'true'].index(st.lower())
    except (ValueError, AttributeError):
        raise ValueError('no Valid Conversion Possible')

def send_command(light,action):
    print "Sending command to ", light.device.ipaddr, " on ", light.port, " turning it to ", action
    ip = str(light.device.ipaddr)
    tcp_port = 9999
    command = ""
    for lights in range(0,4):
        print light.device.lights[lights].status
        if light.device.lights[lights].port == light.port:
            print "SEND TO ", light.port
            command += str(str2bool(action))
            lightUpdate = model.Light.query.filter_by(id=light.device.lights[lights].id).first()
            lightUpdate.status = int(str2bool(action))
            model.db.session.commit()
        elif light.device.lights[lights].status is None:
            command += '0'
        else:
            command += str(light.device.lights[lights].status)
    print command
    #sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    #result = sock.connect_ex((ip,tcp_port))
    #sock.send(command)
    return "OK"

@app.route("/advanced")
def advanced():
{'fraction_non_alphanumeric': 0.1230340760157274, 'fraction_numerical': 0.003931847968545216, 'mean_word_length': 4.231362467866324, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1399757', 'n_tokens_mistral': 1903, 'n_tokens_neox': 1861, 'n_words': 426}