Skip to content

Commit

Permalink
Enable drawing saving for administrator
Browse files Browse the repository at this point in the history
  • Loading branch information
ymollard committed Sep 25, 2018
1 parent ead1065 commit 2df8509
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 6 deletions.
34 changes: 30 additions & 4 deletions arbalet/frontage/apps/drawing.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
from apps.actions import Actions
from utils.tools import Rate
from utils.colors import name_to_rgb, rgb_to_hsv, rgb255_to_rgb
from scheduler_state import SchedulerState

from db.models import FappModel
from db.base import session_factory
from json import dumps

class Drawing(Fap):
PLAYABLE = True
Expand Down Expand Up @@ -43,10 +48,31 @@ def handle_message(self, json_data, path=None): # noqa

self.model.set_pixel(px_x, px_y, rgb255_to_rgb(color['red'], color['green'], color['blue']))

def set_default_drawing(self):
# Set the current model as the default drawing in database
session = session_factory()
try:
fap = session.query(FappModel).filter_by(name='Drawing').first()
if not fap:
return False
fap.default_params = dumps({"model": self.model.json()})
session.commit()
finally:
session.close()

def run(self, params, expires_at=None):
self.start_socket()
while True:
self.send_model()
self.rate.sleep()
if 'model' in params and params['model'] != "":
# Replay saved drawing
self.model.set_from_json(params['model'])
while True:
self.send_model()
self.rate.sleep()
else:
# Regular real-time drawing
self.start_socket()
while True:
if SchedulerState.get_default_drawing_request():
self.set_default_drawing()
self.send_model()
self.rate.sleep()

3 changes: 1 addition & 2 deletions arbalet/frontage/db/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,13 @@ def cln_str(s):
'')
return ''


class FappModel(Base):
__tablename__ = 'fapp'

uniqid = Column(String(36), primary_key=True)
name = Column(String(36), unique=True)
is_scheduled = Column(Boolean)
default_params = Column(String(36))
default_params = Column(String(4096))
position = Column(Integer)
duration = Column(Integer)
created_at = Column(DateTime, unique=False)
Expand Down
16 changes: 16 additions & 0 deletions arbalet/frontage/scheduler_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class SchedulerState(object):
KEY_ON_TIME = 'astronomical_twilight_end'
KEY_OFF_TIME = 'astronomical_twilight_begin'
KEY_DEFAULT_APP_CURRENT_INDEX = 'key_default_app_current_index'
KEY_SET_DEFAULT_DRAWING = 'key_set_default_drawing'

# admin override app
KEY_FORCED_APP = 'frontage_forced_app'
Expand Down Expand Up @@ -107,6 +108,13 @@ def get_close_app_request():
return True, request['userid']
return False, None

@staticmethod
def get_default_drawing_request():
# Return true if a request has been initiated to set the current drawing as the default
request = redis_get(SchedulerState.KEY_SET_DEFAULT_DRAWING, False) == 'True'
redis.set(SchedulerState.KEY_SET_DEFAULT_DRAWING, 'False')
return request

@staticmethod
def get_forced_app_request():
return json.loads(redis_get(SchedulerState.KEY_FORCED_APP_REQUEST, '{}'))
Expand Down Expand Up @@ -144,6 +152,14 @@ def set_registered_apps(apps):
struct[fap] = apps[fap].jsonify()
redis.set(SchedulerState.KEY_REGISTERED_APP, json.dumps(struct))

@staticmethod
def set_default_drawing():
c_app = SchedulerState.get_current_app()
if('name' in c_app and c_app['name'] == "Drawing"):
redis.set(SchedulerState.KEY_SET_DEFAULT_DRAWING, "True")
return True
return False

@staticmethod
def get_available_apps():
return json.loads(redis.get(SchedulerState.KEY_REGISTERED_APP))
Expand Down
7 changes: 7 additions & 0 deletions arbalet/frontage/server/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,12 @@ def get(self, user):
def post(self, user):
return True

class DrawingAppDefault(Resource):
@authentication_required
def post(self, user):
if not is_admin(user):
abort(403, "Forbidden Bru")
return jsonify(done=SchedulerState.set_default_drawing())

class AppDefaultParamView(Resource):
@authentication_required
Expand Down Expand Up @@ -317,6 +323,7 @@ def restart_service(user):
rest_api.add_resource(AppDefaultView, '/b/apps/default/')
rest_api.add_resource(AppDefaultParamView, '/b/apps/default/<string:app_name>')
rest_api.add_resource(AppDefaultListView, '/b/apps/default')
rest_api.add_resource(DrawingAppDefault, '/b/apps/drawing/default')

rest_api.add_resource(AppRunningView, '/b/apps/running')
rest_api.add_resource(AppListView, '/b/apps')

0 comments on commit 2df8509

Please sign in to comment.