def get_order(self, request): """ Apriso -> Redis, 设定工位工单 """ ## workstation, WI path, 3D model path, wip_order, operation, step result = None IP = request.args.get("IP", request.remote_addr) type_, wskey, addr, ip_, group, timestamp = parse_ip(IP) try: id_ = request.args.get("id", 1) callback = request.args.get("callback", None) ## now = time.strftime("%Y-%m-%d %H:%M:%S") result = self.redis.call_lua( method="lua_order_get", keys=[group], args=[id_, timestamp]) if callback == None: return result else: return "{}({});".format(callback, result) except Exception as ex: log.error(ex)
def get_andon_status(self, request): """ Redis -> """ IP = request.args.get("IP", request.remote_addr) id_ = request.args.get("id", 1) callback = request.args.get("JSONCALLBACK", "ng_jsonp_callback") type_, wskey, addr, ip_, group, timestamp = parse_ip(IP) ip_4 = 10 * (int(addr[3]) // 10) + 7 addrs = addr[:3] addrs.append(str(ip_4)) IP_Andon = ".".join(addrs) port = 8000 url = "http://{}:{}/andon/status?JSONCALLBACK={}&IP={}".format( IP_Andon, port, callback, IP) try: r = requests.get(url) return r.text except Exception as ex: log.error(ex) return "{}({});".format(callback, json.dumps({"error": repr(ex)}))
def get_part(self, request, frame): """part_current & part_next e.g. /api/part/get?IP=<ip>&callback=<callback> """ IP = request.args.get("IP", request.remote_addr) type_, wskey, addr, ip_, group, timestamp = parse_ip(IP) frame = frame.lower() id_ = request.args.get("id", 1) seq = request.args.get("seq", 1) callback = request.args.get("callback", None) data = [] error = "" try: # get part_curr rtn = self.redis.call_lua( method="lua_part_get", keys=[group], args=[IP, frame, seq, id_, timestamp], ) x = json.loads(rtn) I_PRODUCTNSP = x["part"] # get part_next if I_PRODUCTNSP == "": data = [] else: data = self.db.call_proc( "AP_3DXML.ProcTdXml_GetGenelogy", [ I_PRODUCTNSP, "part:<STRING>", "<STRING>", "<STRING>", "<STRING>", "<STRING>", "<STRING>", "<STRING>", "<STRING>", "<STRING>", "<STRING>", ], "array", ) except Exception as err: log.error(err) error = repr(err) result = json.dumps({ "jsonrpc": "2.0", "result": data, "x": x, "id": id_, "error": error }) if callback == None: return result else: return "{}({});".format(callback, result)
def get_data(self, request): """ work Instructions """ wi = {"wiurl": "", "tdurl": ""} IP = request.args.get("IP", request.remote_addr) type_, wskey, addr, ip_, group, timestamp = parse_ip(IP) data = self.db.call_proc("sp_test", ["string"], "array") return json.dumps({"value": data, "IP": IP, "id": 1})
def check_andon(self, request): """ Redis -> """ IP = request.args.get("IP", request.remote_addr) id_ = request.args.get("id", 1) # callback = request.args.get("JSONCALLBACK", "ng_jsonp_callback") type_, wskey, addr, ip_, group, timestamp = parse_ip(IP) result = self.redis.call_lua( method="lua_andon_get", keys=[group], args=[id_, timestamp]) return result
def set_measure(self, request): """""" IP = request.args.get("IP", request.remote_addr) val = request.args.get("val", None) type_, wskey, addr, ip_, group, timestamp = parse_ip(IP) ### print(json.loads(request.get_data(as_text=True))) ### return u"received: {}".format(request.get_data(as_text=True)) if val != None: self.db.call_proc("AP_3DXML.ProcTDXML_SetSerialRuler", ["SN", val], "array") return "OK"
def set_andon(self, request): """ Apriso -> Andon """ ### signals = set(['material', 'quality', 'equipment', 'start', 'beep']) IP = request.args.get("IP", request.remote_addr) id_ = request.args.get("id", 1) type_, wskey, addr, ip_, group, timestamp = parse_ip(IP) log.debug("{}:{}".format(addr, ip)) signal = request.args.get("signal") status = request.args.get("status") callback = request.args.get("JSONCALLBACK", "ng_jsonp_callback") ## 7 for andon ip_4 = 10 * (int(addr[3]) // 10) + 7 addrs = addr[:3] addrs.append(str(ip_4)) IP_Andon = ".".join(addrs) # IP_Andon = "172.31.27.57" port = 8000 url = "http://{}:{}/andon/change/status?title={}&status={}&JSONCALLBACK={}&IP={}".format( IP_Andon, port, signal, status, callback, IP) try: r = requests.get(url) log.debug(r.text) result = self.redis.call_lua( method="lua_andon_set", keys=[group], args=[signal, status, id_, timestamp], ) log.debug(result) return r.text except Exception as ex: log.error(ex) return "{}({});".format(callback, json.dumps({"error": repr(ex)}))
def get_wi(self, request): """work Instructions e.g. http://localhost:8000/api/wi/get?IP=<IP> """ IP = request.args.get("IP", request.remote_addr) seq = request.args.get("seq", 1) type_, wskey, addr, ip_, group, timestamp = parse_ip(IP) callback = request.args.get("callback", None) result = self.redis.call_lua( method="lua_wi_get", keys=[group], args=[seq, timestamp]) if callback == None: return result else: return "{}({});".format(callback, result)
def get_part_list(self, request): """ parts as build """ info = {"零件号": "", "零件名": "", "序列号": "", "装配人": "", "专配时间": ""} IP = request.args.get("IP", request.remote_addr) type_, wskey, addr, ip_, group, timestamp = parse_ip(IP) ### q =self.conf.get("ORACLE")["QUERY"] ### ## rpc in ora_sv ### data1 = self.db.query(q["operators"], ['a']) result = self.db.call_proc("sp_get_parts", ["order1", "<CURSOR>"], "array") return json.dumps({"jsonrpc": "2.0", "result": result, "id": 1})
def get_wi_part(self, request, step): """ step: curr, next """ info = {"零件号": "", "零件名": "", "序列号": "", "装配人": "", "专配时间": ""} IP = request.args.get("IP", request.remote_addr) type_, wskey, addr, ip_, group, timestamp = parse_ip(IP) part = self.redis.hget(group, "part") changed = self.redis.hget(group, "changed") if changed == "1": self.redis.hset(group, "changed", 0) return json.dumps({"step": step, "part": part, "changed": changed})
def set_order(self, request): """ Apriso -> Redis, 设定工位工单 """ ## workstation, WI path, 3D model path, wip_order, operation, step params = set(["ws", "path_wi", "path_3d", "wip_order", "op", "step"]) IP = request.args.get("IP", request.remote_addr) type_, wskey, addr, ip_, group, timestamp = parse_ip(IP) ws = request.args.get("ws", "no_ws") path_wi = request.args.get("path_wi", "") path_3d = request.args.get("path_3d", "no_path") wip_order = request.args.get("wip_order", "") order_type = request.args.get("order_type", "") operation = request.args.get("operation", "") step = request.args.get("step", "") callback = request.args.get("callback", None) ## now = time.strftime("%Y-%m-%d %H:%M:%S") result = self.redis.call_lua( method="lua_order_set", keys=[group], args=[ ws, path_wi, path_3d, wip_order, order_type, operation, step, timestamp, IP, ], ) if callback == None: return result else: return "{}({});".format(callback, result)
def query_get(self, request): """ datasource: influxDB """ IP = request.args.get("IP", request.remote_addr) type_, wskey, addr, ip_, group, timestamp = parse_ip(IP) db = request.args.get("db") q = request.args.get("q") epoch = request.args.get("epoch") log.debug(db) log.debug(q) log.debug(epoch) a1 = request.args.get("IP") # type = "table" d1 = [] d2 = [] for i in range(1, 5): d1.append([i * i, 1000 * time.time() - 1000 * i]) d2.append([10 + i * i, 1000 * time.time() - 1000 * i]) data = { "results": [{ "series": [{ "name": "node_st", "columns": ["time", "eqpt", "desc", "status"], "values": [ [1000 * time.time(), "HPU", "Running", 2], [1452858402099, "BEP", "Running", 2], [1452858402121, "BEP-M1", "Running", 2], [1452858402152, "BEP-M2", "Running", 2], [1452858402175, "AVL", "Off", 1], [1452858402199, "AVL-M1", "Off", 1], [1452858402220, "DOOR", "Running", 2], ], }] }] } return json.dumps(data)
def add_user(self, request): ## add user ic & id to Redis ## /api/user/add?ic=4253955683&id=ADMIN """ {"jsonrpc":"2.0", id=1, method="c1", params=[]} """ json_text = request.get_data(as_text=True) id_ = request.args.get("id", 1) ic = request.args.get("ic") userid = request.args.get("userid") IP = request.args.get("IP", request.remote_addr) type_, wskey, addr, ip_, group, timestamp = parse_ip(IP) result = self.redis.call_lua( method="lua_user_add", keys=[group], args=[ic, userid, timestamp]) return "OK"
def scan_user(self, request): """""" ## IC scanner -> ## /api/user/scan?ic=4253955683 IP = request.args.get("IP", request.remote_addr) type_, wskey, addr, ip_, group, timestamp = parse_ip(IP) ic = request.args.get("ic", 1) id_ = request.args.get("id", 1) employeeNo = self.redis.call_lua( method="lua_user_scan", keys=[group], args=[ic, id_, timestamp]) if employeeNo == "": return "user_not_found" else: ## submit order in db # result = self.db.call_proc("AP_3DXML.completeWipOrder", [employeeNo, '<NUMBER>']) # return json.dumps( {"jsonrpc": "2.0", "result": result, "id": id}) return employeeNo
def set_part(self, request): """ Apriso -> 设定零件 """ IP = request.args.get("IP", request.remote_addr) type_, wskey, addr, ip_, group, timestamp = parse_ip(IP) part = request.args.get("part") next = request.args.get("next") id_ = request.args.get("id", 1) callback = request.args.get("callback", None) result = self.redis.call_lua( method="lua_part_set", keys=[group], args=[part, next, id_, timestamp]) if callback == None: return "{}({});".format(callback, result) else: return result
def get_user_login(self, request): """""" ## Apriso -> callback = request.args.get("callback", None) id_ = request.args.get("id", 1) ### log.debug("{},{}".format(callback,id)) IP = request.args.get("IP", request.remote_addr) type_, wskey, addr, ip_, group, timestamp = parse_ip(IP) ### group = "ws:{}".format(wskey) payload = self.redis.call_lua( method="lua_user_get", keys=[group], args=[id_, timestamp]) if callback == None: return Response(payload, status=200, mimetype="application/json") else: return "{}({});".format(callback, payload)
def get_user_list(self, request): """ get operators for this(IP) workstation """ ##['pic_url', 'employeeno', 'name', 'status', 'zpq'] IP = request.args.get("IP", request.remote_addr) callback = request.args.get("callback", None) type_, wskey, addr, ip_, group, timestamp = parse_ip(IP) result = self.db.call_proc( "sp_ws_emp", ["wip_order", "order_type", "operation", "step", "<CURSOR>"], "array", ) empinfo = [] tmp = result[4] for item in tmp: employeeno = item["employeeno"] login_ts = self.redis.hget("login:"******"login_ts") if login_ts == None: item["login"] = False elif timestamp - float(login_ts) < 60 * 60 * 8 * 1000: item["login"] = True else: item["login"] = True empinfo.append(item) data = json.dumps({"jsonrpc": "2.0", "result": empinfo, "id": 1}) if callback == None: return data else: return "{}({});".format(callback, data)
def call_jsonrpc(self, request): """ jsonrpc """ """ --> {"jsonrpc": "2.0", "method": "subtract", "params": [42, 23], "id": 1} <-- {"jsonrpc": "2.0", "result": 19, "id": 1} --> {"jsonrpc": "2.0", "method": "foobar", "id": "1"} <-- {"jsonrpc": "2.0", "error": {"code": -32601, "message": "Method not found"}, "id": "1"} """ IP: str = request.args.get("IP", request.remote_addr) type_, wskey, addr, ip_, group, timestamp = parse_ip(IP) json_text: str = request.get_data(as_text=True) res = json.loads(json_text) method = res["method"] params = res["params"] id_ = res["id"] # call method(params) here, # redis lua ; pg function/stored procedure; oracle sp... mtype: list = method.split("_")[0] if mtype == "lua": """lua in redis""" try: result = self.redis.call_lua( method=method, keys=[group], args=[json.dumps(params), IP, id_, timestamp], ) data = { "jsonrpc": "2.0", "result": json.loads(result), "id": id_ } except Exception as ex: log.error(ex) raise (Exception(repr(ex))) elif mtype == "ora": """oracle""" data = ["ora"] elif mtype == "ht": """http""" data = [mtype] elif mtype == "pg": """postgresql""" data = [mtype] elif mtype == "py": """python""" data = [] else: data = {"jsonrpc": "2.0", "result": data, "id": id_} return json.dumps(data)