import json import os import subprocess import time import datetime import syslog import Activity import fsio import FilePaths import logConfig import notify_activity import status_mod import m7z_mod SIGNALING_FILENAME = '/home/tool/config/Signaling.json' SIGNALING_DEFAULT = '/home/tool/config/default/Signaling.json' if False: response = None request = None session = None db = None status_mod.sessionUserMgr.setLastVisit(session, request) if logConfig.signalingSessFrgt: session.forget(response) def fileEmpty(path): return os.stat(path).st_size == 0 def read_adjustment(data): data['type'] = 'SLIDERSWITCHOFFDELAY' data['min'] = 0 data['max'] = 61 if data['myValue'] == "-1": data['myValue'] = "61" def write_adjustment(data): if data['myValue'] == "61": data['myValue'] = "-1" def find_element_recursive(data, func): if isinstance(data, dict): for k, v in data.items(): #if k == 'myText': # print k, v if isinstance(v, dict) or isinstance(v, list): find_element_recursive(v, func) if k == "myText" and v == "Afterglow time": func(data) elif isinstance(data, list): for e in data: find_element_recursive(e, func) @auth.requires(request.ajax==True, requires_login=True) def processCfg(): data = None if request.env.request_method == "GET": if os.path.isfile(SIGNALING_FILENAME) and not fileEmpty(SIGNALING_FILENAME): data = fsio.jsonload(SIGNALING_FILENAME) find_element_recursive(data, read_adjustment) else: data = {"active": False, "signaling": [ {"expanded": True, "children": [ {"expanded": False, "leaf": False, "myText": "Phase before tightening", "type": "CHECKBOX", "myValue": "", "children": [ { "leaf": True, "myText": "Triggermode", "type": "COMBOBOX", "myValue": "", "choice": [[0, "Startbutton" ], [ 1, "????"]]}, { "leaf": True, "myText": "Intensity white LED", "Intensity": True, "type": "SLIDERTIP", "myValue": "50", "min": 0, "max": 100}, { "leaf": False, "myText": "RGB", "color": True, "children": [ { "leaf": True, "myText": "Red", "type": "SLIDERTIP", "myValue": "255", "min": 0, "max": 255}, { "leaf": True, "myText": "Green", "type": "SLIDERTIP", "myValue": "0", "min": 0, "max": 255}, { "leaf": True, "myText": "Blue", "type": "SLIDERTIP", "myValue": "0", "min": 0, "max": 255} ]}]}, {"expanded": False, "leaf": False, "myText": "Phase during tightening", "type": "CHECKBOX", "myValue": "", "children": [ { "leaf": True, "myText": "Intensity white LED", "Intensity": True, "type": "SLIDERTIP", "myValue": "50", "min": 0, "max": 100}, { "leaf": False, "myText": "RGB", "color": True, "children": [ { "leaf": True, "myText": "Red", "type": "SLIDERTIP", "myValue": "255", "min": 0, "max": 255}, { "leaf": True, "myText": "Green", "type": "SLIDERTIP", "myValue": "0", "min": 0, "max": 255}, { "leaf": True, "myText": "Blue", "type": "SLIDERTIP", "myValue": "0", "min": 0, "max": 255} ]}]}, {"expanded": False, "leaf": False, "myText": "Phase after OK result", "type": "CHECKBOX", "myValue": "", "children": [ { "leaf": True, "myText": "Afterglow time", "type": "SLIDERSWITCHOFFDELAY", "myValue": "10", "min": 0, "max": 61, "step": 1}, { "leaf": True, "myText": "Intensity white LED", "Intensity": True, "type": "SLIDERTIP", "myValue": "50", "min": 0, "max": 100}, { "leaf": False, "myText": "RGB", "color": True, "children": [ { "leaf": True, "myText": "Red", "type": "SLIDERTIP", "myValue": "255", "min": 0, "max": 255}, { "leaf": True, "myText": "Green", "type": "SLIDERTIP", "myValue": "0", "min": 0, "max": 255}, { "leaf": True, "myText": "Blue", "type": "SLIDERTIP", "myValue": "0", "min": 0, "max": 255} ]}, { "leaf": True, "myText": "Flashing cycle", "type": "SLIDERMULTI", "myValue": "50", "min": 0, "max": 100}, { "leaf": False, "myText": "Sound", "children": [ { "leaf": True, "myText": "Volume", "type": "SLIDERTIP", "myValue": "50", "min": 0, "max": 100}, { "leaf": True, "myText": "Duration", "type": "NUMBERFIELD", "myValue": "10", "min": 0, "max": 30, "step": 1} ]} ]}, {"expanded": False, "leaf": False, "myText": "Phase after NOK result", "type": "CHECKBOX", "myValue": "", "children": [ { "leaf": True, "myText": "Afterglow time", "type": "SLIDERSWITCHOFFDELAY", "myValue": "10", "min": 0, "max": 61, "step": 1}, { "leaf": True, "myText": "Intensity white LED", "Intensity": True, "type": "SLIDERTIP", "myValue": "50", "min": 0, "max": 100}, { "leaf": False, "myText": "RGB", "color": True, "children": [ { "leaf": True, "myText": "Red", "type": "SLIDERTIP", "myValue": "255", "min": 0, "max": 255}, { "leaf": True, "myText": "Green", "type": "SLIDERTIP", "myValue": "0", "min": 0, "max": 255}, { "leaf": True, "myText": "Blue", "type": "SLIDERTIP", "myValue": "0", "min": 0, "max": 255} ]}, { "leaf": True, "myText": "Flashing cycle", "type": "SLIDERMULTI", "myValue": "50", "min": 0, "max": 100}, { "leaf": False, "myText": "Sound", "children": [ { "leaf": True, "myText": "Volume", "type": "SLIDERTIP", "myValue": "50", "min": 0, "max": 100}, { "leaf": True, "myText": "Duration", "type": "NUMBERFIELD", "myValue": "10", "min": 0, "max": 30, "step": 1} ]} ]}, {"expanded": False, "leaf": False, "myText": "System error", "type": "CHECKBOX", "myValue": "", "children": [ { "leaf": True, "myText": "Intensity white LED", "Intensity": True, "type": "SLIDERTIP", "myValue": "50", "min": 0, "max": 100}, { "leaf": False, "myText": "RGB", "color": True, "children": [ { "leaf": True, "myText": "Red", "type": "SLIDERTIP", "myValue": "255", "min": 0, "max": 255}, { "leaf": True, "myText": "Green", "type": "SLIDERTIP", "myValue": "0", "min": 0, "max": 255}, { "leaf": True, "myText": "Blue", "type": "SLIDERTIP", "myValue": "0", "min": 0, "max": 255} ]}, { "leaf": True, "myText": "Flashing cycle", "type": "SLIDERMULTI", "myValue": "50", "min": 0, "max": 100}, { "leaf": False, "myText": "Sound", "children": [ { "leaf": True, "myText": "Volume", "type": "SLIDERTIP", "myValue": "50", "min": 0, "max": 100}, { "leaf": True, "myText": "Duration", "type": "NUMBERFIELD", "myValue": "10", "min": 0, "max": 30, "step": 1} ]} ]} ]}]} return response.json(data) if request.env.request_method == "POST": data = json.loads(request.body.read()) find_element_recursive(data, write_adjustment) fsio.jsondump(SIGNALING_FILENAME, data) notify_activity.configChanged(SIGNALING_FILENAME, Activity.SIGNALING_CHANGED, session=session) return response.json(data) @auth.requires(request.ajax==True, requires_login=True) def active(): sig = fsio.jsonload(SIGNALING_FILENAME) return response.json(sig.get('active')) @auth.requires(request.ajax==True, requires_login=True) def _genSignalingFileName(): ts = time.time() dateStamp = datetime.datetime.fromtimestamp(ts).strftime('%Y_%m_%d') return dateStamp + '_LED.nxled' @auth.requires(request.ajax==True, requires_login=True) def exportSignalingFile(): res = {'success': False, 'msg': 'Unknown error'} fsio.fsLock.acquire() try: expfname = _genSignalingFileName() res = {'success': True, 'msg': '/tmp/' + expfname} pwd = FilePaths.gen_rev_pwd("Signaling.json") syslog.syslog(syslog.LOG_INFO, "START exportSignalingFile.subprocess to " + expfname + " at " + time.asctime()) p = subprocess.Popen(['/home/tool/bin/7za', 'a', '-p{0}'.format(pwd), '{0}/{1}'.format('/tmp', expfname), '/home/tool/config/Signaling.json'], stdout=subprocess.PIPE, stderr=subprocess.PIPE) (stdout, stderr) = p.communicate() syslog.syslog(syslog.LOG_INFO, "DONE exportSignalingFile.subprocess to " + expfname + " at " + time.asctime()) syslog.syslog(syslog.LOG_INFO, "RESULT exportSignalingFile.subprocess to stdout: " + str(stdout)) syslog.syslog(syslog.LOG_INFO, "RESULT exportSignalingFile.subprocess to stderr: " + str(stderr)) if p.returncode != 0: msg = getSevenZipErrorMsg(p.returncode) # Failure res['success'] = False res['msg'] = msg except Exception, e: res['success'] = False res['msg'] = str(e) finally: fsio.fsLock.release() return response.json(res) @auth.requires(request.ajax==True, requires_login=True) def sevenZipErrorMsgs(): res = { 0: lambda: "No error", 1: lambda: "Warning", 2: lambda: "Fatal error", 7: lambda: "Command line error", 8: lambda: "Not enough memory for operation", 255: lambda: "User stopped the process" } return res def getSevenZipErrorMsg(errorcode): try: msg = sevenZipErrorMsgs()[errorcode]() except: msg = "Unexpected error" return msg @auth.requires_login() def importSignalingFile(): res = {'success': False, 'msg': 'Unknown error'} destRootDir = '/tmp/' destDir = '/home/tool/config' filename = 'signaling.zip' fsio.fsXngLock.acquire() try: with open(destRootDir + filename, 'w') as f: f.write(request.vars.fileupload.value) pwd = FilePaths.gen_rev_pwd("Signaling.json") returncode = m7z_mod.test_pwd(os.path.join(destRootDir, filename), pwd) if returncode != 0: return response.json(m7z_mod.get_import_impossible_msg()) p = subprocess.Popen( [ '/home/tool/bin/7za', 'x', '-p' + pwd, os.path.join(destRootDir, filename), '-o' + destDir, '-y' ], stdout=subprocess.PIPE, stderr=subprocess.PIPE) (stdout, stderr) = p.communicate() if p.returncode != 0: msg = getSevenZipErrorMsg(p.returncode) # Failure res['success'] = False res['msg'] = msg else: notify_activity.configChanged("/home/tool/config/Signaling.json", Activity.SIGNALING_CHANGED, session=session) return response.json({'success': True, 'msg': ':-)'}) except Exception, e: res['success'] = False res['msg'] = str(e) finally: fsio.fsXngLock.release() return response.json(res) @auth.requires(request.ajax==True, requires_login=True) def resetToFactory(): fsio.shcopyfile(SIGNALING_DEFAULT, SIGNALING_FILENAME) notify_activity.configChanged(SIGNALING_FILENAME, Activity.SIGNALING_CHANGED, session=session) return ''