def switch_light(number, state): if state: print( "************************************************************************" ) print(" User {} (local identifier: {})".format( weblab_user.username, weblab_user.data['local_identifier'])) print( " Imagine that light {} is turning on! " .format(number)) print( "************************************************************************" ) redis.set('hardware:lights:{}'.format(number), 'off') else: print( "************************************************************************" ) print( " Imagine that light {} is turning off! " .format(number)) print( "************************************************************************" ) redis.set('hardware:lights:{}'.format(number), 'on')
def program_device(code): if weblab_user.time_left < 10: print("************************************************************************") print("Error: typically, programming the device takes around 10 seconds. So if ") print("the user has less than 10 seconds (%.2f) to use the laboratory, don't start " % weblab_user.time_left) print("this task. Otherwise, the user session will still wait until the task") print("finishes, delaying the time assigned by the administrator") print("************************************************************************") return { 'success': False, 'reason': "Too few time: {}".format(weblab_user.time_left) } print("************************************************************************") print("You decided that you wanted to program the robot, and for some reason, ") print("this takes time. In weblablib, you can create a 'task': something that ") print("you can start, and it will be running in a different thread. In this ") print("case, this is lasting for 10 seconds from now ") print("************************************************************************") if redis.set('hardware:microcontroller:programming', 0) == 0: # Just in case two programs are sent at the very same time return { 'success': False, 'reason': "Already programming" } socketio.emit('board-status', hardware_status(), namespace='/mylab') for step in range(10): time.sleep(1) redis.set('hardware:microcontroller:programming', step) socketio.emit('board-status', hardware_status(), namespace='/mylab') print("Still programming...") if code == 'division-by-zero': print("************************************************************************") print("Oh no! It was a division-by-zero code! Expect an error!") print("************************************************************************") pipeline = redis.pipeline() pipeline.set('hardware:microcontroller:state', 'failed') pipeline.delete('hardware:microcontroller:programming') pipeline.execute() socketio.emit('board-status', hardware_status(), namespace='/mylab') 10 / 0 # Force an exception to be raised print("************************************************************************") print("Yay! the robot has been programmed! Now you can retrieve the result ") print("************************************************************************") pipeline = redis.pipeline() pipeline.set('hardware:microcontroller:state', 'programmed') pipeline.delete('hardware:microcontroller:programming') pipeline.execute() socketio.emit('board-status', hardware_status(), namespace='/mylab') return { 'success': True }
def clean_resources(): """ This code could be in dispose(). However, since we want to call this low-level code from outside any request and we can't (since we're using weblab_user.username in dispose())... we separate it. This way, this code can be called from outside using 'flask clean-resources' """ redis.set('hardware:microcontroller', 'empty') print("Microcontroller restarted")
def start(client_data, server_data): print("************************************************************************") print("Preparing laboratory for user {}...".format(weblab_user.username)) print() print(" - Typically, here you prepare resources.") print(" - Since this method is run *before* the user goes to the lab, you can't") print(" store information on Flask's 'session'. But you can store it on:") print(" weblab_user.data") weblab_user.data['local_identifier'] = weblab.create_token() print(" In this case: {}".format(weblab_user.data['local_identifier'])) print() print("************************************************************************") for light in range(LIGHTS): redis.set('hardware:lights:{}'.format(light), 'off') redis.set('hardware:microcontroller:state', 'empty') redis.delete('hardware:microcontroller:programming')