This repository has been archived by the owner on Feb 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
flamo.py
158 lines (130 loc) · 4.24 KB
/
flamo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/usr/bin/env python3
'''
The MIT License (MIT)
Copyright (c) 2016 Stefan Lohmaier
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
'''
import os
import time
from queue import Queue
from threading import Thread
import flask
from flask import Flask, render_template, request
from flask_socketio import SocketIO
from flask_login import LoginManager, UserMixin
from flask_login import login_required, login_user, logout_user
from flask_reverse_proxy import FlaskReverseProxied
from easysettings import EasySettings
from flashforge import FlashForge, FlashForgeError
'''
Setup the app and all subsytems
'''
#some default values
DEFAULT_PASSWORD = 'flamo'
#create the app
app = Flask('flamo')
app.config['SECRET_KEY'] = os.environ.get('FLAMO_SECRET_KEY', 'flamo')
proxied = FlaskReverseProxied(app)
#login manager
login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view = 'login'
#socket io server
socketio = SocketIO(app)
#settings
settings = EasySettings('flamo.conf')
'''
Implementation
'''
class FlashForgeIO(Thread):
_instance = None
def __init__(self, reconnect_timeout=5, vendorid=0x2b71, deviceid=0x0001):
Thread.__init__(self)
self.queue = Queue()
def run(self):
app.logger.info('[FlashforgeIO] started')
ff = FlashForge()
while True:
app.logger.info('[FlashForgeIO] Waiting for next GCode command')
command = self.queue.get()
if not command.endswith('\n'):
command += '\n'
socketio.emit('terminal', '> ' + command)
app.logger.info('[FlashForgeIO] Executing command: {0}'.format(command))
try:
data = ff.gcodecmd(command)
if not data.endswith('\n'):
data += '\n'
socketio.emit('terminal', '< ' + data)
self.queue.task_done()
except FlashForgeError as error:
socketio.emit('terminal', 'COMERROR: {0}'.format(error.message))
#default route index route
@app.route('/', methods=['GET'])
@login_required
def index():
return render_template('index.html', streamurl=settings.get('streamurl'))
'''
SocketIO callbacks
'''
@socketio.on('gcodecmd')
def socketio_machine_state(cmd):
if FlashForgeIO._instance is None or not FlashForgeIO._instance.is_alive():
FlashForgeIO._instance = FlashForgeIO()
FlashForgeIO._instance.start()
print('LALA {0}'.format(cmd))
FlashForgeIO._instance.queue.put(cmd)
'''
Authentication methods
'''
#dummy user class for flask-login
class User(UserMixin):
def get_id(self):
return 'user'
#function to load user for login-manager
@login_manager.user_loader
def load_user(id):
return User()
#load user from request header
@login_manager.request_loader
def load_user_request(request):
token = request.headers.get('Authorization')
if token is None:
token = request.args.get('token')
if token == settings.get('password'):
return User()
else:
return None
#login-view to show when not authenticated
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
if request.form['password'] == settings.get('password', 'flamo'):
login_user(User())
return flask.redirect('/', code=302)
return render_template('login.html')
#route to logout
@app.route('/logout')
@login_required
def logout():
logout_user()
return flask.redirect('/login', code=302)
'''
main-function? run devserver
'''
if __name__ == '__main__':
socketio.run(app, host='0.0.0.0', port=5002, debug=True)