def update_local(): postData = request.values local_ip = postData.get('ip_address') local_port = postData.get('port_addr') use_flag = postData.get('use_flag') if check_null(local_ip) and check_null(local_port): selModel = models.LocalServer selServer = selModel.query.first() if selServer: selServer.ip_addr = local_ip selServer.port_addr = local_port selServer.use_flag = use_flag else: newServer = selModel(ip_addr=local_ip, port_addr=local_port, use_flag=use_flag) db.session.add(newServer) db.session.commit() response = {'status': True} else: response = {'status': False, 'message': "Invalid request"} return json.dumps(response)
def add_control(): postData = request.values logicID = postData.get('logicID') controlID = postData.get('controlID') beforeID = postData.get('beforeID') if check_null(logicID) and check_null( controlID) and check_null(beforeID) > 0: controlModel = models.Control if int(beforeID) > 0: beforeControl = controlModel.query.filter_by(id=beforeID).first() beforeControl.logicid = '0' beforeControl.options = '' beforeControl.ind = '1000' for selid in controlID.split(','): selCondGroup = controlModel.query.filter_by(id=selid).first() if selCondGroup: selCondGroup.logicid = logicID db.session.commit() response = {'status': True} else: response = {'status': False, 'message': 'Invalid request'} return json.dumps(response)
def add_monitor(): postData = request.values inputName = postData.get('input_name') selID = postData.get('inputID') if check_null(inputName) and check_null(selID): if len(selID) > 1: newMonitor = models.Monitor.query.filter_by( monitor_id=selID).first() newMonitor.name = inputName else: letters = string.digits while True: monitor_id = ''.join(random.choice(letters) for i in range(3)) checkMonitor = models.Monitor.query.filter_by( monitor_id=monitor_id).first() if checkMonitor is None: newMonitor = models.Monitor(options="", back_img="", name=inputName, monitor_id=monitor_id) db.session.add(newMonitor) break db.session.commit() db.session.refresh(newMonitor) response = {'status': True, 'selid': newMonitor.monitor_id} else: response = {'status': False, 'message': 'Invalid request'} return json.dumps(response)
def recipe_val(): postData = request.form.to_dict() recipeDefID = postData.get('recipeDefID') varID = postData.get('var_id') nameID = postData.get('name_id') if check_null(recipeDefID) and check_null(varID) and check_null(nameID): selVal = models.RecipeValue.query.filter(models.RecipeValue.def_id == recipeDefID) \ .filter(models.RecipeValue.var_id == varID).filter(models.RecipeValue.name_id == nameID).first() postData.pop('var_id') postData.pop('name_id') postData.pop('recipeDefID') inputName = postData.get('inputName') postData.pop('inputName') if selVal: selVal.value = inputName selVal.options = json.dumps(postData) else: newVal = models.RecipeValue(def_id=recipeDefID, var_id=varID, name_id=nameID, options=json.dumps(postData), value=inputName, ind=0) db.session.add(newVal) db.session.commit() response = {'status': True} else: response = {'status': False, 'message': 'Invalid request'} return json.dumps(response)
def add_segment(): postData = request.form.to_dict() frame_id = postData.get('frame_id') selType = postData.get('type') selID = postData.get('selid') if check_null(frame_id) and check_null(selType) and check_null(selID): postData.pop('frame_id') postData.pop('type') selModel = models.FrameSegment if int(selID) > 0: selSeg = selModel.query.filter_by(id=selID).first() if selSeg: selSeg.options = json.dumps(postData) db.session.commit() response = {'status': True} else: response = {'status': False, 'message': "Segment not found"} else: newSeg = selModel(type=selType, frame_id=frame_id, options=json.dumps(postData)) db.session.add(newSeg) db.session.commit() response = {'status': True} else: response = {'status': False, 'message': "Invalid request"} return json.dumps(response)
def create_alarm(): postData = request.values alarmName = postData.get('alarm_name') selID = postData.get('selid') if check_null(alarmName) and check_null(selID): if int(selID) > 0: selAlarm = models.Alarm.query.filter_by(id=selID).first() selAlarm.name = alarmName selAlarm.type = postData.get('alarm_type') selAlarm.confirm = postData.get('alarm_confirm') selAlarm.category = postData.get('alarm_category') else: newAlarm = models.Alarm(name=alarmName, type=postData.get('alarm_type'), confirm=postData.get('alarm_confirm'), category=postData.get('alarm_category'), options="") db.session.add(newAlarm) db.session.commit() response = {'status': True} else: response = {'status': False, 'message': 'Invalid Alarm Data'} return json.dumps(response)
def add_condgroup(): postData = request.values controlID = postData.get('controlID') condgroupID = postData.get('condgroupID') beforeID = postData.get('beforeID') #if check_null(controlID) and check_null(condgroupID): # condgroupModel = models.ConditionGroup # if check_null(beforeID) and int(beforeID) > 0: # beforeCondGroup = condgroupModel.query.filter_by(id=beforeID).first() # beforeCondGroup.controlid = '0' # beforeCondGroup.options = '' # for selid in condgroupID.split(','): # selCondGroup = condgroupModel.query.filter_by(id=selid).first() # if selCondGroup: # selCondGroup.controlid = controlID # db.session.commit() # response = {'status': True} #else: # response = {'status': False, 'message': 'Invalid request'} if check_null(controlID) and check_null(condgroupID): condgroupModel = models.ConditionGroup for selid in condgroupID.split(','): selCondGroup = condgroupModel.query.filter_by(id=selid).first() if selCondGroup: selCondGroup.controlid = controlID db.session.commit() response = {'status': True} else: response = {'status': False, 'message': 'Invalid request'} return json.dumps(response)
def edit_control(): postData = request.values controlName = postData.get('inputName') selid = postData.get('controlID') if check_null(controlName) and check_null(selid): if int(selid) > 0: selControl = models.Control.query.filter_by(id=selid).first() selControl.name = controlName selControl.mode = postData.get('mode') selControl.use_flag = postData.get('use_flag') selControl.priority = postData.get('inputCnt') else: newControl = models.Control(options="", ind="1000", logicid="0", name=controlName, mode=postData.get('mode'), use_flag=postData.get('use_flag'), priority=postData.get('inputCnt')) db.session.add(newControl) db.session.commit() response = {'status': True} else: response = {'status': False, 'message': 'Invalid request'} return json.dumps(response)
def remove_modbus(): postData = request.values selIDs = postData.get('selRow') if check_null(selIDs): selIDS = selIDs.split(",") selType = postData.get('selType') for selid in selIDS: if check_null(selType): if selType == "channel": models.ModbusChannel.query.filter_by(id=selid).delete() elif selType == "custom": models.CustomChannel.query.filter_by(id=selid).delete() elif selType == "frame": models.CustomFrame.query.filter_by(id=selid).delete() elif selType == "remote": models.RemoteClient.query.filter_by(id=selid).delete() else: models.Modbus.query.filter_by(id=selid).delete() db.session.commit() response = {'status': True} else: response = {'status': False, 'message': "Invalid request"} return json.dumps(response)
def add_cond(): postData = request.form.to_dict() cond_group_id = postData.get('condGroupID') condition_ids = postData.get('condID') beforeID = postData.get('beforeID') if check_null(cond_group_id) and check_null(condition_ids) and check_null( beforeID): # if int(beforeID) > 0: # befCondition = models.Condition.query.filter_by(id=beforeID).first() # befCondition.condgroup = '0' # befCondition.condoptions = '' # befCondition.ind = '1000' for condition_id in condition_ids.split(','): selCondition = models.Condition.query.filter_by( id=condition_id).first() if selCondition: selCondition.condgroup = cond_group_id db.session.commit() response = {'status': True} else: response = {'status': False, 'message': 'Invalid request'} return json.dumps(response)
def edit_action_group(): postData = request.form.to_dict() actionGroupName = postData.get('inputName') selid = postData.get('act_group_id') if check_null(actionGroupName) and check_null(selid): if int(selid) > 0: selActGroup = models.ActionGroup.query.filter_by(id=selid).first() selActGroup.name = actionGroupName selActGroup.mode = postData.get('inputMode') selActGroup.cnt = postData.get('inputCnt') else: newActionGroup = models.ActionGroup( name=actionGroupName, controlid=postData.get('control_id'), options="", mode=postData.get('inputMode'), cnt=postData.get('inputCnt')) db.session.add(newActionGroup) db.session.commit() response = {'status': True} else: response = {'status': False, 'message': 'Invalid request'} return json.dumps(response)
def edit_cond_group(): postData = request.values condGroupName = postData.get('cond_group_name') selid = postData.get('cond_group_id') if check_null(condGroupName) and check_null('selid'): if int(selid) > 0: selCondGroup = models.ConditionGroup.query.filter_by( id=selid).first() selCondGroup.name = condGroupName selCondGroup.operator = postData.get('cond_group_operator') selCondGroup.reverse = postData.get('cond_group_reverse') else: newCondGroup = models.ConditionGroup( name=condGroupName, controlid=postData.get('control_id'), options="", operator=postData.get('cond_group_operator'), reverse=postData.get('cond_group_reverse')) db.session.add(newCondGroup) db.session.commit() response = {'status': True} else: response = {'status': False, 'message': 'Invalid request'} return json.dumps(response)
def edit_logic(): postData = request.form.to_dict() logicName = postData.get('name') selid = postData.get('selid') print("logicName, selid") print(selid) if check_null(logicName) and check_null(selid): if len(selid) > 1: newLogic = models.Logic.query.filter_by(logicid=selid).first() newLogic.name = logicName newLogic.mode = postData.get('mode') newLogic.use_flag = postData.get('use_flag') else: letters = string.digits while True: logicid = ''.join(random.choice(letters) for i in range(3)) checkLogic = models.Logic.query.filter_by( logicid=logicid).first() if checkLogic is None: newLogic = models.Logic(options='', name=logicName, mode=postData.get('mode'), use_flag=postData.get('use_flag'), logicid=logicid) db.session.add(newLogic) break db.session.commit() db.session.refresh(newLogic) response = {'status': True, 'selid': newLogic.logicid} else: response = {'status': False, 'message': 'Invalid request'} return json.dumps(response)
def write_variable(): postData = request.values variableAddr = postData.get('varAdd') variableType = postData.get('varType') writeValue = postData.get('writeValue') if check_null(writeValue) and check_null(variableAddr) and check_null( variableType): local_shm = LocalVar.SharedMem_LocalVar() response = local_shm.set_buff(variableAddr, variableType, writeValue) else: response = {'status': False, 'message': 'Invalid request'} return json.dumps(response)
def get_variable(): postData = request.form.to_dict() channel_id = postData.get('channel_id') selType = postData.get('type') if check_null(channel_id) and check_null(selType): response = {} isValid = True if selType == "master": selModbus = models.ModbusChannel.query.filter_by( id=channel_id).first() if selModbus: optionVal = json.loads(selModbus.options) response['channel_code'] = int( optionVal['channel_code'] ) if 'channel_code' in optionVal else 0 response['channel_readlen'] = int( optionVal['channel_readlen'] ) if 'channel_readlen' in optionVal else 0 response['channel_writelen'] = int( optionVal['channel_writelen'] ) if 'channel_writelen' in optionVal else 0 else: isValid = False else: channel_id = "slave-" + channel_id if isValid: varModel = models.ModbusVariable selOrder = postData.get('order') if 'order' in postData else '1' variables = varModel.query.filter(varModel.channel_id == channel_id).filter(varModel.order == selOrder) \ .order_by(cast(varModel.order, Integer)).all() response = { 'status': True, 'data_list': [{ 'order': variable.order, 'selid': variable.selid, 'seltype': variable.seltype, 'sellocstr': variable.sellocstr } for variable in variables] } else: response = {'status': False, 'message': 'Channel not found'} else: response = {'status': False, 'message': "Invalid request"} return json.dumps(response)
def chart_data(): postData = request.values selIDs = postData.get('selids') if check_null(selIDs): id_arr = selIDs.split(',') dataSet = models.CollectSet.query.filter_by(name='data_set').first() idArr = {} returnArr = {} collectModel = models.DataCollect if dataSet and len(id_arr) > 0: engine = create_engine( make_url('sqlite:///' + config.DB_SAVE_PATH + dataSet.path)) for id_val in id_arr: fetchSql = "SELECT var_value, createdAt FROM var_logs WHERE var_id = '{}' ORDER BY id DESC LIMIT 10".format( id_val) results = engine.execute(fetchSql) selRow = collectModel.query.filter_by(id=id_val).with_entities( collectModel.name).first() if selRow: idArr[selRow.name] = id_val returnArr[selRow.name] = [{ 'value': row.var_value, 'create': row.createdAt[:16] } for row in results] response = {'status': True, 'rows': returnArr, 'ids': idArr} else: response = {'status': False, 'message': 'Invalid request'} return json.dumps(response)
def set_save(): postData = request.values set_name = postData.get('set_name') if check_null(set_name): setModel = models.CollectSet selSet = setModel.query.filter_by(name=set_name).first() if selSet: selSet.path = postData.get('save_path') selSet.interval = postData.get('save_interval', '') selSet.interval_unit = postData.get('interval_unit', '') selSet.limit = postData.get('alarm_limit') selSet.limit_val = postData.get('record_cnt', '') selSet.limit_unit = postData.get('record_unit', '') else: selSet = setModel(name=set_name, path=postData.get('save_path'), interval=postData.get('save_interval', ''), interval_unit=postData.get('interval_unit', ''), limit=postData.get('alarm_limit'), limit_val=postData.get('record_cnt', ''), limit_unit=postData.get('record_unit', '')) db.session.add(selSet) db.session.commit() response = {'status': True} else: response = {'status': False, 'message': 'Invalid request'} return json.dumps(response)
def save_order(): postData = request.form.to_dict() selType = postData.get('selType') if check_null(selType): postData.pop('selType') selModel = None if selType == "action": selModel = models.Action elif selType == "condition": selModel = models.Condition elif selType == "control": selModel = models.Control elif selType == "monitor": selModel = models.MonitorElement if selModel: for key, val in postData.items(): selVal = selModel.query.filter_by(id=key).first() selVal.ind = val db.session.commit() response = {'status': True} else: response = {'status': False, 'message': 'Invalid request'} return json.dumps(response)
def remove_control(): postData = request.values selIDS = postData.get('selRow') if check_null(selIDS): selIDS = selIDS.split(",") for selid in selIDS: models.Control.query.filter_by(id=selid).delete() selActGroup = models.ActionGroup.query.filter_by( controlid=selid).first() if selActGroup: selActGroup.controlid = '0' selActGroup.options = '' selCondGroup = models.ConditionGroup.query.filter_by( controlid=selid).first() if selCondGroup: selCondGroup.controlid = '0' selCondGroup.options = '' db.session.commit() response = {'status': True} else: response = {'status': False, 'message': "Invalid request"} return json.dumps(response)
def recipe_detail(): recipeDefID = request.values.get('recipeDefID') if check_null(recipeDefID): varList = models.RecipeVar.query.filter_by(def_id=recipeDefID).all() var_list = [{ 'id': var.id, 'name': var.name, 'type': var.selid } for var in varList] recipeNames = models.RecipeName.query.filter_by( def_id=recipeDefID).all() name_list = [{'id': var.id, 'name': var.name} for var in recipeNames] recipeValues = models.RecipeValue.query.filter_by( def_id=recipeDefID).all() value_list = [{ 'id': var.id, 'var_id': var.var_id, 'value': var.value, 'options': var.options, 'name_id': var.name_id } for var in recipeValues] response = { 'status': True, 'var_list': var_list, 'name_list': name_list, 'value_list': value_list } else: response = {'status': False, 'message': 'Invalid request'} return json.dumps(response)
def add_client(): postData = request.values client_id = postData.get('selid') if check_null(client_id): if int(client_id) > 0: selClient = models.RemoteClient.query.filter_by( id=client_id).first() selClient.use_flag = postData.get('use_flag') selClient.name = postData.get('client_name') selClient.ip = postData.get('client_ip') selClient.port = postData.get('client_port') else: orderObj = models.RemoteClient.remote_id.desc() remoteInfo = models.RemoteClient.query.order_by(orderObj).first() newClient = models.RemoteClient(ip=postData.get('client_ip'), name=postData.get('client_name'), port=postData.get('client_port'), use_flag=postData.get('use_flag'), remote_id=remoteInfo.remote_id + 1) db.session.add(newClient) db.session.commit() response = {'status': True} else: response = {'status': False, 'message': "Invalid request"} return json.dumps(response)
def remove_recipe(): postData = request.values selIDs = postData.get('selRow') if check_null(selIDs): selType = postData.get('selType') selIDs = selIDs.split(',') if selType and len(selType) > 0: recipeDefID = postData.get('recipeDefID') if selType == "var": selModel = models.RecipeVar for selID in selIDs: models.RecipeValue.query.filter(models.RecipeValue.def_id == recipeDefID) \ .filter(models.RecipeValue.var_id == selID).delete() else: selModel = models.RecipeName for selID in selIDs: models.RecipeValue.query.filter(models.RecipeValue.def_id == recipeDefID) \ .filter(models.RecipeValue.name_id == selID).delete() else: selModel = models.RecipeDef for selID in selIDs: models.RecipeVar.query.filter_by(def_id=selID).delete() models.RecipeName.query.filter_by(def_id=selID).delete() models.RecipeValue.query.filter_by(def_id=selID).delete() for selID in selIDs: selModel.query.filter_by(id=selID).delete() db.session.commit() response = {'status': True} else: response = {'status': False, 'message': 'Invalid request'} return json.dumps(response)
def condition_list(): postData = request.values draw, start, length, columnIndex, columnName, sortOrder = datatable_head( postData) data_list = [] condModel = models.Condition conditions = condModel.query.outerjoin(models.ConditionGroup, models.ConditionGroup.id == condModel.condgroup) \ .with_entities(condModel.id, condModel.name, condModel.type, condModel.use_flag, condModel.options, condModel.condgroup) \ .add_columns(models.ConditionGroup.name.label('condgroup_name')) beforeID = postData.get('beforeID') if check_null(beforeID): conditions = conditions.filter( or_(condModel.condgroup == '0', condModel.id == beforeID)) totalCount = conditions.count() # if columnName == "use_flag": # sortObj = condModel.use_flag.asc() if sortOrder == "asc" else condModel.use_flag.desc() # elif columnName == "name": # sortObj = condModel.name.asc() if sortOrder == "asc" else condModel.name.desc() # elif columnName == "type": # sortObj = condModel.type.asc() if sortOrder == "asc" else condModel.type.desc() # else: # sortObj = condModel.id.asc() if sortOrder == "asc" else condModel.id.desc() sortObj = condModel.id.asc() conditions = conditions.order_by(sortObj).offset(start).limit(length).all() # validList = get_valid(LogicMemory.Const_Condition, start, length, const.uiSizeEval) for ind, condition in enumerate(conditions): result = { "id": condition[0], "name": condition[1], "type": condition[2], "use_flag": config.USE_FLAG_KR[int(condition[3])], "options": condition[4], "condgroup": condition[5], "condgroup_name": condition[6], "ind": start + ind + 1, # "valid": validList[ind], "valid": "TRUE" } settings = json.loads(result["options"]) result.pop("options") for key, val in settings.items(): result[key] = val result["desc"] = get_desc_str(result) result["type"] = config.VARIABLE_TYPE[result["type"]] for key, val in settings.items(): result.pop(key) data_list.append(result) return json.dumps(datatable_list(data_list, totalCount, draw))
def update_action(): postData = request.form.to_dict() selid = postData.get('selid') action_name = postData.get('action_name') action_order = postData.get('action_order') action_type = postData.get('action_type') if check_null(action_name) and check_null(action_order) and check_null( selid) and check_null(action_type): postData.pop('action_name', None) postData.pop('action_order', None) postData.pop('action_type', None) use_flag = postData.get('use_flag') postData.pop('use_flag', None) selid = int(selid) if selid > 0: selAction = models.Action.query.filter_by(id=selid).first() if selAction: selAction.name = action_name selAction.order = action_order selAction.use_flag = use_flag selAction.type = action_type selAction.options = json.dumps(postData) db.session.commit() response = {'status': True} else: response = {'message': 'Action not found', 'status': False} else: newAction = models.Action(name=action_name, order=action_order, use_flag=use_flag, type=action_type, actgroup="0", actoptions="", ind="1000", options=json.dumps(postData)) db.session.add(newAction) db.session.commit() response = {'status': True} else: response = {'status': False, 'message': 'Invalid request'} return json.dumps(response)
def new_def(): postData = request.values inputName = postData.get('inputName') defID = postData.get('recipeDefID') if check_null(inputName) and check_null(defID): if int(defID) > 0: selDef = models.RecipeDef.query.filter_by(id=defID).first() selDef.name = inputName else: newDef = models.RecipeDef(name=inputName) db.session.add(newDef) db.session.commit() response = {'status': True} else: response = {'status': False, 'message': 'Invalid request'} return json.dumps(response)
def update_ethercat(): postData = request.values ethercat_val = postData.get('ethercat_val') ethercat_type = postData.get('ethercat_type') if check_null(ethercat_type) and check_null(ethercat_val): selModel = models.Settings selRow = selModel.query.filter_by(name=ethercat_type).first() if selRow: selRow.value = ethercat_val else: newRow = selModel(name=ethercat_type, value=ethercat_val) db.session.add(newRow) db.session.commit() response = {'status': True} else: response = {'status': False, 'message': "Invalid request"} return json.dumps(response)
def add_slave(): postData = request.values selIDs = postData.get('selIDs') parentID = postData.get('parentID') if check_null(selIDs) and check_null(parentID): for selID in selIDs.split(','): selModel = models.EthercatDevice selEth = selModel.query.filter_by(device_id=selID).filter( selModel.parent_id == parentID).first() if not selEth: newEth = selModel(device_id=selID, parent_id=parentID) db.session.add(newEth) db.session.commit() response = {'status': True} else: response = {'status': False, 'message': "Invalid request"} return json.dumps(response)
def add_element(): postData = request.form.to_dict() elemName = postData.get('elem_name') selType = postData.get('seltype') monitorID = postData.get('monitorID') selID = postData.get('selid') if check_null(selType) and check_null(monitorID) and check_null( elemName) and check_null(selID): postData.pop('selid') postData.pop('seltype') postData.pop('monitorID') postData.pop('elem_name') if int(selID) > 0: selElement = models.MonitorElement.query.filter_by( id=selID).first() if selElement: selElement.name = elemName selElement.type = selType selElement.options = json.dumps(postData) db.session.commit() response = {'status': True} else: response = {'status': False, 'message': 'No element exists'} else: newElement = models.MonitorElement( ind='1000', type=selType, name=elemName, elemoptions= '{"bool_file": "0", "header_back": "#f8f9fc", "font_color": "black", "font_size": "13.6px", "font_family": "Nunito", "body_back": "white", "body_size": "16px", "body_color": "black", "body_family": "Nunito"}', sizeoptions='', monitorid=monitorID, options=json.dumps(postData)) db.session.add(newElement) db.session.commit() response = {'status': True} else: response = {'status': False, 'message': 'Invalid request'} return json.dumps(response)
def func_inout(): postData = request.values selID = postData.get('selid') if check_null(selID): in_list, out_list = in_out_list(selID) response = {'status': True, 'out_list': out_list, 'in_list': in_list} else: response = {'status': False, 'message': 'Invaid request'} return json.dumps(response)
def add_condition(): postData = request.form.to_dict() selid = postData.get("selid") conditionName = postData.get("condition_name") conditionType = postData.get("condition_type") if check_null(selid) and check_null(conditionName) and check_null( conditionType): postData.pop("selid") postData.pop("condition_name") postData.pop("condition_type") useFlag = postData.get("use_flag") postData.pop("use_flag") selid = int(selid) if selid > 0: selCondition = models.Condition.query.filter_by(id=selid).first() if selCondition: selCondition.name = conditionName selCondition.type = conditionType selCondition.use_flag = useFlag selCondition.options = json.dumps(postData) db.session.commit() response = {"status": True} else: response = {"status": False, "message": "No "} else: newCondition = models.Condition(name=conditionName, type=conditionType, use_flag=useFlag, condgroup="0", options=json.dumps(postData), condoptions="", ind="1000") db.session.add(newCondition) db.session.commit() response = {"status": True} else: response = {"status": False, "message": "Invalid Condition Data"} return json.dumps(response)