Skip to content

Commit

Permalink
add buildout config
Browse files Browse the repository at this point in the history
  • Loading branch information
cekk committed Dec 29, 2019
1 parent 8f3139b commit 8b4e878
Show file tree
Hide file tree
Showing 11 changed files with 176 additions and 215 deletions.
9 changes: 8 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,11 @@ instance
.vscode
gunicorn_config.py
function.zip
lambda_function/package
lambda_function/package
.installed.cfg
__pycache__/
bin/
buildout.cfg
eggs/
parts/
var/
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
.PHONY: install
install:
pipenv install
pipenv run buildout -N

.PHONY: buildout
buildout:
pipenv run buildout -N

.PHONY: dev
dev:
Expand Down
7 changes: 1 addition & 6 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,7 @@ verify_ssl = true
[dev-packages]

[packages]
flask = "*"
flask-mqtt = "*"
flask-restful = "*"
gunicorn = "*"
flask-socketio = "*"
gevent = "*"
zc-buildout = "*"

[requires]
python_version = "3.7"
204 changes: 4 additions & 200 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

53 changes: 51 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,55 @@
# Roller Shutter
# Roller Shutter Manager

TODO
This is a Flask app that allows to manage a set of Shelly 2.5 switches mounted to control roller shutters.àà#

## Prerequisites

- mqtt broker running (i use mosquitto)

## App configuration

We use [Instance folders](https://flask.palletsprojects.com/en/1.1.x/config/#instance-folders) configuration,
so you need to create a configuration file in `instance/config.py` with informations like these::

MQTT_BROKER_URL = "127.0.0.1"
MQTT_BROKER_PORT = 1883
FLASK_APP = "blinds_manager"

BLINDS = [
{"id": "blind1", "name": "Kitchen"},
]

Where `MQTT_*` are config informations for MQTT broker connection.

BLINDS is a list of dictionaries where `id` is the blind id set into MQTT settings into Shelly device.

## Installation

This app runs with a buildout configuration, to handle dependencies and helper scripts.

First of all you need to create a virtualenv and install zc.buildout.

If you are using `pipenv`, just run::

> pipenv

It will create a new virtual environment with Python 3.7 and zc.buildout.

After that, you have to run the buildout to install all the needed dependencies.
There are two main config files (development.cfg and production.cfg). Symlink buildout.cfg with one of these
depending on the environment where you are working on.::

> ln -s development.cfg buildout.cfg

And finally, run buildout::

pipenv run buildout -N

Or with Makefile::

> make buildout

This will install all Flask dependencies and supervisor.

## Production mode

Expand Down
19 changes: 13 additions & 6 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
import os
import re

app = Flask(__name__, instance_relative_config=True, static_folder="static/build")
app = Flask(
__name__, instance_relative_config=True, static_folder="static/build"
)
app.config.from_pyfile("config.py")

api = Api(app)
Expand All @@ -23,7 +25,9 @@ def handle_connect(client, userdata, flags, rc):
# mqtt.subscribe("shellies/command")
for blind in app.config.get("BLINDS", []):
mqtt.subscribe("shellies/{id}/online".format(id=blind.get("id", "")))
mqtt.subscribe("shellies/{id}/roller/0/pos".format(id=blind.get("id", "")))
mqtt.subscribe(
"shellies/{id}/roller/0/pos".format(id=blind.get("id", ""))
)
mqtt.subscribe("shellies/{id}/roller/0".format(id=blind.get("id", "")))


Expand Down Expand Up @@ -77,7 +81,12 @@ def publish(self, id, action):

def get(self, id, action):
if action not in ["close", "open", "stop"]:
return {"message": 'Valid actions are "close", "open", "stop" or "rc"'}, 400
return (
{
"message": 'Valid actions are "close", "open", "stop" or "rc"'
},
400,
)
if id == "all":
for blind in app.config.get("BLINDS", []):
self.publish(id=blind.get("id", ""), action=action)
Expand All @@ -100,6 +109,4 @@ def get(self, id, action):
api.add_resource(Action, "/roller/<string:id>/<string:action>")

if __name__ == "__main__":
socketio.run(
app, debug=False, host="0.0.0.0", threaded=True,
)
socketio.run(app, debug=True, host="0.0.0.0", threaded=False)
27 changes: 27 additions & 0 deletions base.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[buildout]

extends =
flask.cfg
versions.cfg

eggs-directory = eggs
versions = versions
show-picked-versions = true

parts =
flask
supervisor
remove_typings

[supervisor]
recipe = collective.recipe.supervisor
http-socket = unix
user = admin
password = admin
file = ${buildout:directory}/var/supervisor.sock
programs =

[remove_typings]
recipe = collective.recipe.cmd
cmds =
"rm -rf eggs/typing*"
Loading

0 comments on commit 8b4e878

Please sign in to comment.