Пример #1
0
def plant():
    db = DB()
    action = request.args.get('action')

    if action == 'read':
        valveConfigs = db.loadValveConfigs()
        # print 'READ VALVE CONFIG:'
        # print valveConfigs
        response = json.dumps({ 'plant': valveConfigs })

    elif action == 'create':
        jsonValveConfigs = request.form['plant']
        valveConfig = json.loads(jsonValveConfigs)
        # check if valves can be added (system_settings.valve_amount)
        maxValves = db.getMaxValveCountSetting()
        actualValves = db.getValveCount()
        if not maxValves or actualValves < maxValves:
            # print 'CREATED VALVE CONFIG:'
            # print valveConfig
            newRow = db.addValveConfig(valveConfig)
            if len(newRow):
                restartJobManager()
                responseObj = { 'success': 'true', 'plant': newRow }
            else:
                responseObj = { 'success': 'false' }
        else:
            responseObj = { 'success': 'false', 'message': 'No more entrys to add, maximum entries can be configured in settings.' }
        response = json.dumps(responseObj)

    elif action == 'update':
        jsonValveConfigs = request.form['plant']
        valveConfig = json.loads(jsonValveConfigs)
        # print 'UPDATED VALVE CONFIG:'
        # print valveConfig
        success = db.saveValveConfig(valveConfig)
        if success == True:
            restartJobManager()
            responseObj = { 'success': 'true' }
        else:
            responseObj = { 'success': 'false', 'message': 'Valve already used by another entry.' }
        response = json.dumps(responseObj)#{'success': 'false', 'message': }#, 500 #'metaData': { 'messageProperty': 'msg', 'successProperty': 'success' }

    elif action == 'destroy':
        jsonValveConfigs = request.form['plant']
        valveConfig = json.loads(jsonValveConfigs)
        # print 'DELETED VALVE CONFIG:'
        # print valveConfig
        success = db.deleteValveConfig(valveConfig['id'])
        restartJobManager()
        response = json.dumps({ 'success': str(success).lower() })

    return response
Пример #2
0
def setting():
    db = DB()
    action = request.args.get('action')
    if action == 'read':
        settings = {}
        # print 'READ SYSTEM CONF:'
        for line in db.loadSystemSettings():
            if line['setting_name'] == 'password':
                continue
            settings.update({ line['setting_name']: line['setting_value'] })
        response = json.dumps({ 'setting': [settings] })
        # print response
    elif action == 'update':
        if request.method == 'POST':
            jsonCredentials = request.form['setting']
            params = json.loads(jsonCredentials)
            response = json.dumps({ 'success': 'false' })
            if 'username' in params:
                credentialUsername = params['username']
                db.updateSystemSettings('username', credentialUsername)
                response = json.dumps({ 'success': 'true' })
            if 'password' in params:
                credentialPassword = params['password']
                db.updateSystemSettings('password', credentialPassword)
                response = json.dumps({ 'success': 'true' })
            if 'valve_amount' in params:
                valveAmount = int(params['valve_amount'])
                actualValves = db.getValveCount()
                if actualValves <= valveAmount:
                    db.updateSystemSettings('valve_amount', valveAmount)
                    response = json.dumps({ 'success': 'true' })
                else:
                    response = json.dumps({ 'success': 'false', 'message': 'There are more valves set up than you want to allow. Please remove some of them first.' })
    elif action == 'destroy':
        if request.method == 'POST':
            jsonCredentials = request.form['setting']
            params = json.loads(jsonCredentials)
            # print params
            #for setting in params:
            for key, value in params.items():
                # print 'checking: %s / %s' % (key, value)
                if value == '-DELETE-':
                    db.deleteSystemSetting(key)
            response = json.dumps({ 'success': 'true' })
    return response