import os import Activity import fsio import notify_activity from master import DefaultTmpl from master import DefaultManualTmpl from master.InitJob import InitJob from master.AssignTmpl import IdAssign from master.OpModesTmpl import OpModes from master.IdCodeAssignTmpl import IdCodeAssign from collections import defaultdict import shutil MASTER_PATH = '/home/tool/config/jobs/masterjob' if False: response = None request = None session = None db = None idcode = {0: 'noidcode', 1: 'cyclecounter', 2: 'openprotocol', 3: 'scanner', 4: 'job', 5: 'vwxml'} scancodes = {'Code 128': 'Code128', 'UCC/EAN-128': 'UccEan128', 'Code 39': 'Code39', 'Code 93': 'Code93', 'Code 11': 'Code11', 'Interleaved 2 of 5': 'Interleaved2of5', 'Codabar': 'Codabar', 'MSI': 'Msi'} def serializeArray(t): content = unicode("") for v in t: if v: if isinstance(v, unicode): content = content + v + "\n" else: content = content + v.decode('utf8') + "\n" elif v == '': content += "\n" return content.encode('utf8') def initJob(_steps, filename, _user, _comment, _date): idSrc = "" delIDCode=False display_time = str(1) timeout1 = 600 timeout2 = 600 display_text1 = "" display_text2 = "" steps = _steps for step in steps: if step['Type'] == 0: for options in step.get('children'): if options.get('myText') == 'ID Code Source': idSrc = idcode.get(int(options.get('children')[0].get('myValue'))) delIDCode = options.get('children')[1].get('myValue') if "true" in delIDCode: delIDCode = 1 else: delIDCode = 0 user = _user comment = _comment date = _date if steps[0].get('Type') == 0: display_time = str(steps[0].get('children')[0].get('children')[0].get('myValue')) useIdleEn = 0 first_id_input_options = None if len(steps) > 2: first_id_input_step = steps[1] first_id_input_options = first_id_input_step.get('children')[2] timeout1 = int(first_id_input_options.get('children')[6].get('children')[0].get('myValue')) display_text1 = (first_id_input_options.get('children')[4].get('myValue')) if len(steps) == 3: if first_id_input_options.get('children')[1].get('myValue') == "1": useIdleEn = 1 else: useIdleEn = 0 if len(steps) == 4: second_id_input_step = steps[2] second_id_input_options = second_id_input_step.get('children')[2] timeout2 = int(second_id_input_options.get('children')[6].get('children')[0].get('myValue')) display_text2 = (second_id_input_options.get('children')[4].get('myValue')) initJob = InitJob() InitFile = initJob.create(idSrc, delIDCode, user, comment, date, display_time, useIdleEn, timeout1, timeout2, display_text1, display_text2) fsio.write(filename, serializeArray(InitFile)) def default_op_modes(): op_modes = OpModes() op_modes = op_modes.create_default() fsio.write("/home/tool/config/jobs/opmodes_declare.lua", serializeArray(op_modes)) def declare_op_modes(_steps, filename, _user, _comment, _date, _active_col): col = 0 op_mode = 0 trigger = {} prg_sel = {} job_sel = {} active = {} pin = {0: "", 1: "", 2: ""} for step in _steps: col = step.get('Column') for options in step.get('children'): if options.get('myText') == 'Operation Mode Settings': for op in options.get('children'): if op.get('myText') == "Operation Mode": op_mode = int(op.get("myValue")) trigger[op_mode] = [] active[op_mode] = _active_col == col elif op.get('myText') == "Trigger": for tri in op.get('children'): #if tri.get('myText') == "Pin for HMI (optional)": # pin[op_mode] = tri.get('myValue') if tri.get('myValue') == "true": trigger[op_mode].append(tri.get('myText')) elif op.get('myText') == "Allow Prg selection": prg_sel[op_mode] = op.get("myValue") elif op.get('myText') == "Allow Job selection": job_sel[op_mode] = op.get("myValue") op_modes = OpModes() op_modes = op_modes.create(user=_user, comment=_comment, date=_date, active=active, trigger=trigger, prgsel=prg_sel, jobsel=job_sel, pin=pin) fsio.write(filename, serializeArray(op_modes)) def idle_en(_steps, filename, _user, _comment, _date, col): prg = False job = False steps = _steps user = _user comment = _comment date = _date display_time = str(steps[0].get('children')[0].get('children')[0].get('myValue')) # future use! default_idle_en = False if(len(steps) == 2): default_idle_en = True if(len(steps) == 3): target = int(steps[1].get('children')[2].get('children')[1].get('myValue')) if target == 4 or 5: prg = True job = True if(len(steps) == 4): target = int(steps[2].get('children')[2].get('children')[1].get('myValue')) if target == 4: prg = True job = True if col > 0: prg = True job = True from master.IdleEnTmpl import IdleEnTmpl idleEn = IdleEnTmpl() IdleCw = idleEn.create(prg, job, user, comment, date, display_time) fsio.write(filename, serializeArray(IdleCw)) def idle_en_idcode(_steps, filename, _user, _comment, _date): user = _user comment = _comment date = _date steps = _steps start_step = steps[0] first_id_input_step = steps[1] first_id_input_options = first_id_input_step.get('children')[2] general = first_id_input_step.get('children')[0] id_input_name1 = general.get('children')[0].get('myValue') id_input_comment1 = general.get('children')[1].get('myValue') display_time = str(start_step.get('children')[0].get('children')[0].get('myValue')) source = int(first_id_input_options.get('children')[0].get('myValue')) extjobctrl = (first_id_input_options.get('children')[5].get('myValue')) target = int(first_id_input_options.get('children')[1].get('myValue')) timeout = int(first_id_input_options.get('children')[6].get('children')[0].get('myValue')) thetxt = first_id_input_options.get('children')[6].get('children')[1].get('myValue') if isinstance(thetxt, unicode): thetxt = thetxt.encode('utf8') timeouttext = str(thetxt) laserontime = float(first_id_input_options.get('children')[7].get('children')[1].get('myValue')) trigger = int(first_id_input_options.get('children')[7].get('children')[0].get('myValue')) codes = "" for code in first_id_input_options.get('children')[7].get('children')[3].get('children'): if code.get('myValue') == "true": codes += 'codes.' + str(scancodes.get(code.get('myText'))) + ',' codes = codes[:-1] idrange = serializeScanRange(str(first_id_input_options.get('children')[2].get('myValue'))) play_sound = (first_id_input_options.get('children')[7].get('children')[2].get('myValue')) if play_sound == 'true': play_sound = 1 else: play_sound = 0 if extjobctrl == 'true': extjobctrl = 1 else: extjobctrl = 0 thetxt= first_id_input_options.get('children')[4].get('myValue') if isinstance(thetxt, unicode): thetxt = thetxt.encode('utf8') display_text = str(thetxt) from master.IdleIdCodeTmpl import IdleIdCodeTmpl idleIdCode = IdleIdCodeTmpl() IdleCw = idleIdCode.create(id_input_name1, id_input_comment1, target, trigger, timeout, codes, idrange, timeouttext, user, comment, date, display_time, display_text, source, play_sound, extjobctrl, laserontime) fsio.write(filename, serializeArray(IdleCw)) def idle_default(filename, op_mode): user = "" comment = "" date = 0 display_time = 1 IdleCw = None if op_mode == 0: masterjobDefault = DefaultTmpl.MasterjobDefaultTmpl() IdleCw = masterjobDefault.create(user, comment, date, display_time) elif op_mode == 2: masterjobDefault = DefaultManualTmpl.MasterjobDefaultManualTmpl() IdleCw = masterjobDefault.create(user, comment, date, display_time) fsio.write(filename, serializeArray(IdleCw)) def idle_assign(_steps, filename, _user, _comment, _date, _op_mode, use_as_idcode=False): user = _user comment = _comment date = _date steps = _steps start_step = steps[0] first_id_input_step = steps[1] first_id_input_options = first_id_input_step.get('children')[2] general = first_id_input_step.get('children')[0] id_input_name1 = general.get('children')[0].get('myValue') id_input_comment1 = general.get('children')[1].get('myValue') display_time = str(start_step.get('children')[0].get('children')[0].get('myValue')) source = int(first_id_input_options.get('children')[0].get('myValue')) target = int(first_id_input_options.get('children')[1].get('myValue')) timeout = int(first_id_input_options.get('children')[6].get('children')[0].get('myValue')) thetxt = first_id_input_options.get('children')[6].get('children')[1].get('myValue') if isinstance(thetxt, unicode): thetxt = thetxt.encode('utf8') timeouttext = str(thetxt) laserontime = float(first_id_input_options.get('children')[7].get('children')[1].get('myValue')) trigger = int(first_id_input_options.get('children')[7].get('children')[0].get('myValue')) codes = "" for code in first_id_input_options.get('children')[7].get('children')[3].get('children'): if code.get('myValue') == "true": codes += 'codes.' + str(scancodes.get(code.get('myText'))) + ',' codes = codes[:-1] range = ["0-255"] id_range = ["0-255"] if target == 5: id_range = serializeScanRange(str(first_id_input_options.get('children')[2].get('myValue'))) range = serializeScanRange(str(first_id_input_options.get('children')[3].get('myValue'))) elif target == 4: range = serializeScanRange(str(first_id_input_options.get('children')[3].get('myValue'))) else: range = serializeScanRange(str(first_id_input_options.get('children')[2].get('myValue'))) play_sound = (first_id_input_options.get('children')[7].get('children')[2].get('myValue')) if play_sound == 'true': play_sound = 1 else: play_sound = 0 thetxt = first_id_input_options.get('children')[4].get('myValue') if isinstance(thetxt, unicode): thetxt = thetxt.encode('utf8') display_text = str(thetxt) # CW version of single ID Input with IDCode for manual mode if use_as_idcode: from master.IdleIdCodeTmpl import IdleIdCodeManualTmpl idCodeManual = IdleIdCodeManualTmpl() IdleCw = idCodeManual.create(id_input_name1, id_input_comment1, target, trigger, timeout, codes, id_range, timeouttext, user, comment, date, display_time, display_text, range, source, play_sound, laserontime) fsio.write(filename, serializeArray(IdleCw)) else: idAssign = IdAssign() IdleCw = idAssign.create(id_input_name1, id_input_comment1, target, trigger, timeout, codes, id_range, timeouttext, user, comment, date, display_time, display_text, range, source, play_sound, laserontime, _op_mode) fsio.write(filename, serializeArray(IdleCw)) def serializeScanRange(step): if len(step) > 0: idarray = step.split(';') idarray2 = map(lambda val: val.split('-'), idarray) for id in idarray2: if len(id) == 1: id[0] = int(id[0]) id[0] -= 1 id.append(int(id[0])) else: id[0] = int(id[0]) id[1] = int(id[1]) id[0] -= 1 id[1] -= 1 if id[0] > id[1]: tmp = id[0] id[0] = id[1] id[1] = tmp idarray = map(lambda a: "\"" + a + "\"", map(lambda a: "-".join(map(lambda a: str(a), a)), idarray2)) else: idarray = ["0-255"] from functools import reduce res = reduce(lambda a, b: a + ", " + b, idarray) range = "{" + res + "}" return range def idcodeAssign(_steps, filename, _user, _comment, _date, _op_mode): f = file(filename, 'w') steps = _steps start_step = steps[0] first_id_input_step = steps[1] second_id_input_step = steps[2] first_id_input_options = first_id_input_step.get('children')[2] first_general = first_id_input_step.get('children')[0] id_input_name1 = first_general.get('children')[0].get('myValue') id_input_comment1 = first_general.get('children')[1].get('myValue') second_id_input_options = second_id_input_step.get('children')[2] second_general = second_id_input_step.get('children')[0] id_input_name2 = second_general.get('children')[0].get('myValue') id_input_comment2 = second_general.get('children')[1].get('myValue') source1 = int(first_id_input_options.get('children')[0].get('myValue')) source2 = int(second_id_input_options.get('children')[0].get('myValue')) target1 = int(first_id_input_options.get('children')[1].get('myValue')) target2 = int(second_id_input_options.get('children')[1].get('myValue')) trigger1 = int(first_id_input_options.get('children')[7].get('children')[0].get('myValue')) trigger2 = int(second_id_input_options.get('children')[7].get('children')[0].get('myValue')) timeout1 = int(first_id_input_options.get('children')[6].get('children')[0].get('myValue')) thetxt = first_id_input_options.get('children')[6].get('children')[1].get('myValue') if isinstance(thetxt, unicode): thetxt = thetxt.encode('utf8') timeouttext1 = str(thetxt) timeout2 = int(second_id_input_options.get('children')[6].get('children')[0].get('myValue')) thetxt = second_id_input_options.get('children')[6].get('children')[1].get('myValue') if isinstance(thetxt, unicode): thetxt = thetxt.encode('utf8') timeouttext2 = str(thetxt) laserontime1 = float(first_id_input_options.get('children')[7].get('children')[1].get('myValue')) laserontime2 = float(second_id_input_options.get('children')[7].get('children')[1].get('myValue')) codes1 = "" for code in first_id_input_options.get('children')[7].get('children')[3].get('children'): if code.get('myValue') == "true": codes1 += 'codes.' + str(scancodes.get(code.get('myText'))) + ',' codes1 = codes1[:-1] codes2 = "" for code in second_id_input_options.get('children')[7].get('children')[3].get('children'): if code.get('myValue') == "true": codes2 += 'codes.' + str(scancodes.get(code.get('myText'))) + ',' codes2 = codes2[:-1] range1 = str(first_id_input_options.get('children')[2].get('myValue')) range1 = serializeScanRange(range1) range2 = str(second_id_input_options.get('children')[3].get('myValue')) range2 = serializeScanRange(range2) scanID1 = '$1' scanID2 = '$2' play_sound1 = (first_id_input_options.get('children')[7].get('children')[2].get('myValue')) play_sound2 = (second_id_input_options.get('children')[7].get('children')[2].get('myValue')) if play_sound1 == 'true': play_sound1 = 1 else: play_sound1 = 0 if play_sound2 == 'true': play_sound2 = 1 else: play_sound2 = 0 user = _user comment = _comment date = _date display_time = str(start_step.get('children')[0].get('children')[0].get('myValue')) thetxt= first_id_input_options.get('children')[4].get('myValue') if isinstance(thetxt, unicode): thetxt = thetxt.encode('utf8') display_text1 = str(thetxt) thetxt= second_id_input_options.get('children')[4].get('myValue') if isinstance(thetxt, unicode): thetxt = thetxt.encode('utf8') display_text2 = str(thetxt) idCodeAssign = IdCodeAssign() IdleCw = idCodeAssign.create(codes1, timeout1, range1, target1, trigger1, timeouttext1, display_text1, source1, play_sound1, laserontime1, id_input_name1, id_input_comment1, codes2, timeout2, range2, target2, trigger2, timeouttext2, display_text2, source2, play_sound2, laserontime2, id_input_name2, id_input_comment2, user, comment, date, display_time, _op_mode) fsio.write(filename, serializeArray(IdleCw)) def writeIdleCw(data, path, _session): if not isinstance(data, list): data = [data] active_column = 0 operating_mode = 0 column_steps = defaultdict(list) user = data[0].get('User') comment = data[0].get('Comment') date = data[0].get('Date') start_steps = [] #remove all subdirectories for root, dirs, f in os.walk(path): for d in dirs: shutil.rmtree(os.path.join(root, d), ignore_errors=True) for step in data[0].get('steps'): if 0 <= int(step.get('Column')) < 26: # Don't push Operation Mode step if step.get('Type') != 7: column_steps[int(step.get('Column'))].append(step) if step.get('Type') == 0: start_steps.append(step) else: active_column = int(step.get('children')[0].get('children')[0].get('myValue')) filename_op_modes = os.path.join(path, 'opmodes_declare.lua') declare_op_modes(start_steps, filename_op_modes, user, comment, date, active_column) for col, steps in column_steps.items(): steps.sort(key=lambda x: x.get('Row'), reverse=False) if steps[0].get('Name') == "Start": operating_mode = int(steps[0].get('children')[2].get('children')[0].get('myValue')) if 0 > operating_mode > 3: operating_mode = 0 root_dir = os.path.join(path, str(operating_mode)) if operating_mode > 0 else path if not os.path.exists(root_dir): os.makedirs(root_dir) filename_idle_cw = os.path.join(root_dir, 'idle_cw.lua') filename_init = os.path.join(root_dir, 'init.lua') filename_idle_en = os.path.join(root_dir, 'idle_en.lua') target = 0 target2 = 0 initJob(steps, filename_init, user, comment, date) idle_en(steps, filename_idle_en, user, comment, date, col) if len(steps) == 3: target = int(steps[1].get('children')[2].get('children')[1].get('myValue')) if len(steps) == 4: target2 = int(steps[2].get('children')[2].get('children')[1].get('myValue')) # Targets : [1, 'ID Code'], # [4, 'ID Input Assignment.'], # [5, 'ID Code / ID Input Assignment'], if len(steps) == 2: idle_default(filename_idle_cw, operating_mode) elif (len(steps) == 3) and target == 1: # ID Code if col == 0: idle_default(filename_idle_cw, operating_mode) idle_en_idcode(steps, filename_idle_en, user, comment, date) elif col > 0: idle_assign(steps, filename_idle_cw, user, comment, date, operating_mode, use_as_idcode=True) elif (len(steps) == 3) and (target == 4 or target == 5): # ID Input Assignment || ID Code / ID Input Assignment idle_assign(steps, filename_idle_cw, user, comment, date, operating_mode) elif (len(steps) == 4) and target2 == 4: # idcode + ID Input Assignment idcodeAssign(steps, filename_idle_cw, user, comment, date, operating_mode) notify_activity.configChanged(MASTER_PATH, Activity.MASTERJOB_CHANGED, session=_session)