Skip to content

Commit

Permalink
add interface
Browse files Browse the repository at this point in the history
  • Loading branch information
cekk committed Dec 1, 2019
1 parent 41ed321 commit 044b57e
Show file tree
Hide file tree
Showing 37 changed files with 10,270 additions and 31 deletions.
2 changes: 2 additions & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ verify_ssl = true
flask = "*"
flask-mqtt = "*"
flask-restful = "*"
gunicorn = "*"
flask-socketio = "*"

[requires]
python_version = "3.7"
32 changes: 31 additions & 1 deletion Pipfile.lock

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

81 changes: 51 additions & 30 deletions app.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
from flask import Flask
from flask import Flask, render_template, send_from_directory
from flask_restful import Resource, Api, reqparse
from flask_mqtt import Mqtt
from flask_socketio import SocketIO
from utils import createSocketMessage

app = Flask(__name__, instance_relative_config=True)
import os
import re

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

api = Api(app)
socketio = SocketIO(app)
mqtt = Mqtt(app)

parser = reqparse.RequestParser()
Expand All @@ -15,18 +22,37 @@
def handle_connect(client, userdata, flags, rc):
mqtt.subscribe("shellies/command")
for blind in app.config.get("BLINDS", []):
# mqtt.subscribe("shellies/{id}".format(id=blind.get("id", "")))
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".format(id=blind.get("id", "")))


@mqtt.on_message()
def handle_mqtt_message(client, userdata, message):
print("{} - {}".format(message.topic, message.payload))
if message.payload == "announce":
#  skip it
return
socket_message = createSocketMessage(message)
if not socket_message:
return
socketio.emit(socket_message["event"], socket_message["data"])


@socketio.on("connect")
def connect_handler():
mqtt.publish("shellies/command", "announce")


@app.route("/", defaults={"path": ""})
@app.route("/<path:path>")
def serve(path):
if path != "" and os.path.exists(app.static_folder + "/" + path):
return send_from_directory(app.static_folder, path)
else:
return send_from_directory(app.static_folder, "index.html")


class HomePage(Resource):
class Blinds(Resource):
def get(self):
return app.config.get("BLINDS", [])

Expand All @@ -45,38 +71,33 @@ def get(self):
return "", 204


class Open(Resource):
def get(self, id):
mqtt.publish("shellies/{id}/roller/0/command".format(id=id), "open")
return "", 204


class Close(Resource):
def get(self, id):
mqtt.publish("shellies/{id}/roller/0/command".format(id=id), "close")
return "", 204

class Action(Resource):
def publish(self, id, action):
mqtt.publish("shellies/{id}/roller/0/command".format(id=id), action)

class Stop(Resource):
def get(self, id):
mqtt.publish("shellies/{id}/roller/0/command".format(id=id), "stop")
def get(self, id, action):
if action not in ["close", "open", "stop"]:
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)
else:
self.publish(id=id, action=action)
return "", 204


class Command(Resource):
def post(self):
args = parser.parse_args()
mqtt.publish("shellies/shellyswitch25-68E5F8/roller/0/command", "close")
return {todo_id: todos[todo_id]}
# class Command(Resource):
# def post(self):
# args = parser.parse_args()
# mqtt.publish("shellies/shellyswitch25-68E5F8/roller/0/command", "close")
# return {todo_id: todos[todo_id]}


api.add_resource(HomePage, "/")
api.add_resource(Blinds, "/blinds")
api.add_resource(Announce, "/announce")
api.add_resource(Update, "/update")
api.add_resource(Command, "/command")
api.add_resource(Close, "/<string:id>/close")
api.add_resource(Open, "/<string:id>/open")
api.add_resource(Stop, "/<string:id>/stop")
# api.add_resource(Command, "/command")
api.add_resource(Action, "/roller/<string:id>/<string:action>")

if __name__ == "__main__":
app.run(debug=False)
socketio.run(app, debug=False)
20 changes: 20 additions & 0 deletions static/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*
68 changes: 68 additions & 0 deletions static/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).

## Available Scripts

In the project directory, you can run:

### `yarn start`

Runs the app in the development mode.<br />
Open [http://localhost:3000](http://localhost:3000) to view it in the browser.

The page will reload if you make edits.<br />
You will also see any lint errors in the console.

### `yarn test`

Launches the test runner in the interactive watch mode.<br />
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.

### `yarn build`

Builds the app for production to the `build` folder.<br />
It correctly bundles React in production mode and optimizes the build for the best performance.

The build is minified and the filenames include the hashes.<br />
Your app is ready to be deployed!

See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.

### `yarn eject`

**Note: this is a one-way operation. Once you `eject`, you can’t go back!**

If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.

Instead, it will copy all the configuration files and the transitive dependencies (Webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own.

You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it.

## Learn More

You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).

To learn React, check out the [React documentation](https://reactjs.org/).

### Code Splitting

This section has moved here: https://facebook.github.io/create-react-app/docs/code-splitting

### Analyzing the Bundle Size

This section has moved here: https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size

### Making a Progressive Web App

This section has moved here: https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app

### Advanced Configuration

This section has moved here: https://facebook.github.io/create-react-app/docs/advanced-configuration

### Deployment

This section has moved here: https://facebook.github.io/create-react-app/docs/deployment

### `yarn build` fails to minify

This section has moved here: https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify
18 changes: 18 additions & 0 deletions static/build/asset-manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"files": {
"main.js": "/static/js/main.0345317c.chunk.js",
"main.js.map": "/static/js/main.0345317c.chunk.js.map",
"runtime-main.js": "/static/js/runtime-main.7db7bb0f.js",
"runtime-main.js.map": "/static/js/runtime-main.7db7bb0f.js.map",
"static/js/2.141055f6.chunk.js": "/static/js/2.141055f6.chunk.js",
"static/js/2.141055f6.chunk.js.map": "/static/js/2.141055f6.chunk.js.map",
"index.html": "/index.html",
"precache-manifest.be10d735018106acbc7980fe9e78375c.js": "/precache-manifest.be10d735018106acbc7980fe9e78375c.js",
"service-worker.js": "/service-worker.js"
},
"entrypoints": [
"static/js/runtime-main.7db7bb0f.js",
"static/js/2.141055f6.chunk.js",
"static/js/main.0345317c.chunk.js"
]
}
Binary file added static/build/favicon.ico
Binary file not shown.
1 change: 1 addition & 0 deletions static/build/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!doctype html><html lang="en"><head><meta charset="utf-8"/><link rel="icon" href="/favicon.ico"/><meta name="viewport" content="width=device-width,initial-scale=1"/><meta name="theme-color" content="#000000"/><meta name="description" content="Web site created using create-react-app"/><link rel="apple-touch-icon" href="logo192.png"/><link rel="manifest" href="/manifest.json"/><title>React App</title></head><body><noscript>You need to enable JavaScript to run this app.</noscript><div id="root"></div><script>!function(a){function e(e){for(var t,r,n=e[0],o=e[1],u=e[2],i=0,l=[];i<n.length;i++)r=n[i],Object.prototype.hasOwnProperty.call(c,r)&&c[r]&&l.push(c[r][0]),c[r]=0;for(t in o)Object.prototype.hasOwnProperty.call(o,t)&&(a[t]=o[t]);for(s&&s(e);l.length;)l.shift()();return p.push.apply(p,u||[]),f()}function f(){for(var e,t=0;t<p.length;t++){for(var r=p[t],n=!0,o=1;o<r.length;o++){var u=r[o];0!==c[u]&&(n=!1)}n&&(p.splice(t--,1),e=i(i.s=r[0]))}return e}var r={},c={1:0},p=[];function i(e){if(r[e])return r[e].exports;var t=r[e]={i:e,l:!1,exports:{}};return a[e].call(t.exports,t,t.exports,i),t.l=!0,t.exports}i.m=a,i.c=r,i.d=function(e,t,r){i.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:r})},i.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},i.t=function(t,e){if(1&e&&(t=i(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var r=Object.create(null);if(i.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var n in t)i.d(r,n,function(e){return t[e]}.bind(null,n));return r},i.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return i.d(t,"a",t),t},i.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},i.p="/";var t=this.webpackJsonpstatic=this.webpackJsonpstatic||[],n=t.push.bind(t);t.push=e,t=t.slice();for(var o=0;o<t.length;o++)e(t[o]);var s=n;f()}([])</script><script src="/static/js/2.141055f6.chunk.js"></script><script src="/static/js/main.0345317c.chunk.js"></script></body></html>
Binary file added static/build/logo192.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added static/build/logo512.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions static/build/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"short_name": "React App",
"name": "Create React App Sample",
"icons": [
{
"src": "favicon.ico",
"sizes": "64x64 32x32 24x24 16x16",
"type": "image/x-icon"
},
{
"src": "logo192.png",
"type": "image/png",
"sizes": "192x192"
},
{
"src": "logo512.png",
"type": "image/png",
"sizes": "512x512"
}
],
"start_url": ".",
"display": "standalone",
"theme_color": "#000000",
"background_color": "#ffffff"
}
18 changes: 18 additions & 0 deletions static/build/precache-manifest.be10d735018106acbc7980fe9e78375c.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
self.__precacheManifest = (self.__precacheManifest || []).concat([
{
"revision": "b742a2ec424447d78484f5070e775376",
"url": "/index.html"
},
{
"revision": "33f3579f9ce2a561a67c",
"url": "/static/js/2.141055f6.chunk.js"
},
{
"revision": "e944288652ad221fdcfb",
"url": "/static/js/main.0345317c.chunk.js"
},
{
"revision": "268f0192eeb15de1a820",
"url": "/static/js/runtime-main.7db7bb0f.js"
}
]);
2 changes: 2 additions & 0 deletions static/build/robots.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# https://www.robotstxt.org/robotstxt.html
User-agent: *
39 changes: 39 additions & 0 deletions static/build/service-worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* Welcome to your Workbox-powered service worker!
*
* You'll need to register this file in your web app and you should
* disable HTTP caching for this file too.
* See https://goo.gl/nhQhGp
*
* The rest of the code is auto-generated. Please don't update this file
* directly; instead, make changes to your Workbox build configuration
* and re-run your build process.
* See https://goo.gl/2aRDsh
*/

importScripts("https://storage.googleapis.com/workbox-cdn/releases/4.3.1/workbox-sw.js");

importScripts(
"/precache-manifest.be10d735018106acbc7980fe9e78375c.js"
);

self.addEventListener('message', (event) => {
if (event.data && event.data.type === 'SKIP_WAITING') {
self.skipWaiting();
}
});

workbox.core.clientsClaim();

/**
* The workboxSW.precacheAndRoute() method efficiently caches and responds to
* requests for URLs in the manifest.
* See https://goo.gl/S9QRab
*/
self.__precacheManifest = [].concat(self.__precacheManifest || []);
workbox.precaching.precacheAndRoute(self.__precacheManifest, {});

workbox.routing.registerNavigationRoute(workbox.precaching.getCacheKeyForURL("/index.html"), {

blacklist: [/^\/_/,/\/[^/?]+\.[^/]+$/],
});
2 changes: 2 additions & 0 deletions static/build/static/js/2.141055f6.chunk.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions static/build/static/js/2.141055f6.chunk.js.map

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions static/build/static/js/main.0345317c.chunk.js

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

Loading

0 comments on commit 044b57e

Please sign in to comment.