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
def restartJobManager(): # Remove all jobs if len(scheduler.get_jobs()) > 0: for job in scheduler.get_jobs(): scheduler.remove_job(job.id) if RUNNINGONPI: tft.clearJobDisplay() # Add all jobs that are stored in database db = DB() valveConfigs = db.loadValveConfigs() for config in valveConfigs: if config['on_time'] and config['on_duration'] and config['is_active']: if RUNNINGONPI: tft.displayJob(config) timeComponents = map(int, config['on_time'].split(':')) if(config['interval_type'] == 'daily'): scheduler.add_job(valveJob, 'cron', day_of_week = 'mon-sun', hour = timeComponents[0], minute = timeComponents[1], second = timeComponents[2], args = [config]) # print 'Scheduled daily job [%i:%i]' % (timeComponents[0], timeComponents[1]) if(config['interval_type'] == 'weekly'): scheduler.add_job(valveJob, 'cron', day_of_week = 'sun', hour = timeComponents[0], minute = timeComponents[1], second = timeComponents[2], args = [config]) # print 'Scheduled weekly job [sun %i:%i]' % (timeComponents[0], timeComponents[1]) if(config['interval_type'] == 'monthly'): scheduler.add_job(valveJob, 'cron', day = 1, hour = timeComponents[0], minute = timeComponents[1], second = timeComponents[2], args = [config]) # print 'Scheduled monthly job [1st of the month %i:%i]' % (timeComponents[0], timeComponents[1]) # print 'JOBS:' # print scheduler.get_jobs() if RUNNINGONPI: while time.time() - displayTime < 5: time.sleep(1) tft.clear() tft.setBackgroundImage('static/gfx/lcd-ui-background.png', (0, 0)) addTftJob() return