Ejemplo n.º 1
0
def flame_patterns():
    ''' GET /flame/patterns: Get list of all flame patterns, whether active
         or not
        POST /flame/patterns: Creates a new flame pattern from json patterndata
    '''
    if request.method == 'GET':
        return JSONResponse(json.dumps(get_flame_patterns()))
    else:
        if not "patternData" in request.values:
            return CORSResponse("'patternData' must be present", 400)
        else:
            set_flame_pattern(request.values["patternData"])
            return CORSResponse("", 200)
Ejemplo n.º 2
0
def jar_status():
    ''' GET  Get current foreground, background patterns and intensity 
    '''
    if request.method == 'GET':
        try:
            values = my_rpc("get_current_status", None)
            if values is None:
                raise ValueError("No response")
            if "foreground" not in values \
                or "background" not in values \
                or "intensity" not in values:
                raise ValueError("Bad response")
            response = {
                "foreground": values['foreground'],
                "background": values['background'],
                "intensity": values['intensity']
            }
            return JSONResponse(json.dumps(response))
        except Exception as e:
            print(f"Warning Exception {e} in GET")
            return CORSResponse("Internal Error", 500)
    elif request.method == 'POST':
        try:
            params = {}
            params['foreground'] = request.values[
                'foreground'] if 'foreground' in request.values else None
            params['background'] = request.values[
                'background'] if 'background' in request.values else None
            params['intensity'] = request.values[
                'intensity'] if 'intensity' in request.values else None
            resp = my_rpc("set_current_status", params)
            return CORSResponse("Success", 200)
        except Exception as e:
            print(f"jar status post has exception {e}")
            if e.args[0] == "Invalid State":
                return CORSResponse("Pattern In Transition", 409)
            else:
                return CORSResponse("Internal Error", 500)
    else:
        return CORSResponse("Method Not Allowed", 405)
Ejemplo n.º 3
0
def specific_flame_status(poofer_id):
    ''' GET /flame/poofers/<poofername>. Get status of particular poofer.
        POST /flame/poofers/<poofername> enabled=[true|false]. Set enabled
         state for individual poofers.
    '''
    if not poofer_id_valid(poofer_id):
        abort(400)
    if request.method == 'POST':
        if not "enabled" in request.values:
            return CORSResponse("'enabled' must be present", 400)

        enabled = request.values["enabled"].lower()
        if enabled == 'true':
            flames_controller.enablePoofer(poofer_id)
        elif enabled == 'false':
            flames_controller.disablePoofer(poofer_id)
        else:
            return CORSResponse("Invalid 'enabled' value", 400)

        return ""  # XXX check for errors as a matter of course
    else:
        return JSONResponse(json.dumps(get_poofer_status(poofer_id)))
Ejemplo n.º 4
0
def get_available_patterns():
    ''' Get available foreground and background patterns
    '''
    if request.method == 'GET':
        try:
            values = my_rpc("get_patterns", None)
            if values is None:
                raise ValueError("No response")
            if "foregrounds" not in values \
                or "backgrounds" not in values:
                raise ValueError("Bad response")

            response = {'backgrounds': [], 'foregrounds': []}
            response['backgrounds'] = [{
                'name': background_name,
                'url': IMAGE_DIR + "/" + background_name
            } for background_name in values['backgrounds']]
            response['foregrounds'] = values['foregrounds']
            return JSONResponse(json.dumps(response))
        except:
            return CORSResponse("Internal Error", 500)
    else:
        return CORSResponse("Method Not Allowed", 405)
Ejemplo n.º 5
0
def flame_status():
    ''' GET /flame. Get status of all poofers, any active patterns. (Poofer status 
          is [on|off], [enabled|disabled].)
        POST /flame playState=[pause|play]. Whole sculpture gross control. 
          Pause/Play: Pause all poofing and flame effects (should terminate any 
          current patterns, prevent any poofing until Play is called]
    '''
    if request.method == 'POST':
        if "playState" in request.values:
            playState = request.values["playState"].lower()
            if playState == "pause":
                flames_controller.globalPause()
            elif playState == "play":
                flames_controller.globalRelease()
            else:
                return CORSResponse("Invalid 'playState' value", 400)
        else:
            return CORSResponse("Must have 'playState' value", 400)

        return CORSResponse("", 200)

    else:
        return JSONResponse(json.dumps(get_status()))
Ejemplo n.º 6
0
def makeJsonResponse(jsonString, respStatus=200):
    return CORSResponse(jsonString,
                        status=respStatus,
                        mimetype='application/json')
Ejemplo n.º 7
0
def flame_pattern(patternName):
    ''' POST /flame/patterns/<patternName> active=[true|false] enabled=[true|false] pattern=[pattern]
          active - Start an individual pattern (or stop it if it is currently running).
          enabled - enable/disable a pattern.
          pattern - pattern data, modify existing pattern
    '''
    includesPattern = "pattern" in request.values
    includesEnabled = "enabled" in request.values
    includesActive = "active" in request.values

    if request.method == 'POST':
        # pattern create - pattern data included, but pattern name not in system
        if (not includesPattern) and (not patternName_valid(patternName)):
            return CORSResponse("Must have valid 'patternName'", 400)

        if includesPattern:
            patternData = json.loads(request.values["pattern"])
            oldPatternData = None
            for pattern in patternList:
                if pattern["name"] == patternData["name"]:
                    oldPatternData = pattern
                    break
            if oldPatternData == None:
                patternList.append(patternData)
            else:
                oldPatternData["events"] = patternData["events"]
            savePatternData()

        if includesEnabled:
            enabled = request.values["enabled"].lower()
            enabledValid = param_valid(enabled, ["true", "false"])
        else:
            enabledValid = False
        if includesActive:
            active = request.values["active"].lower()
            activeValid = param_valid(active, ["true", "false"])
        else:
            activeValid = False

        if (not enabledValid and not activeValid):
            abort(400)

        if enabledValid:
            if (enabled == "true"):
                flames_controller.enableFlameEffect(patternName)
            elif (enabled == "false"):
                flames_controller.disableFlameEffect(patternName)
        if activeValid:
            if (active == "true"):
                flames_controller.doFlameEffect(patternName)
            elif (active == "false"):
                flames_controller.stopFlameEffect(patternName)

        return ""
    elif request.method == "DELETE":
        pattern_manager.deletePattern(patternName)
        pattern_manager.savePatterns()
        return ""

    else:
        if (not patternName_valid(patternName)):
            return CORSResponse("Must have valid 'patternName'", 400)
        else:
            return JSONResponse(json.dumps(get_pattern_status(patternName)))