|
|
@@ -27,6 +27,11 @@ import datetime
|
|
|
import copy
|
|
|
import hashlib
|
|
|
|
|
|
+import sys
|
|
|
+import getopt
|
|
|
+
|
|
|
+POPUFARE_API_VERSION = "2.0.0"
|
|
|
+
|
|
|
#conn = mysql.connector.connect(user='bus', password='bus', host='localhost', database='busdb', port=3306)
|
|
|
|
|
|
_USER = 'busapi'
|
|
|
@@ -68,8 +73,9 @@ USER_FIELDS = ["username", "comment", "first_name", "last_name", "phone",
|
|
|
GROUP_FIELDS = ["id", "group_id", "group_name"]
|
|
|
RULECLASS_FIELDS = ["id", "group_id", "group_name"]
|
|
|
|
|
|
-def Request(ctx):
|
|
|
- _conn = mysql.connector.connect(user=_USER, password=_PASSWORD, host=_HOST, database=_DATABASE, port=_PORT)
|
|
|
+def Request(ctx, db_info = { "user":_USER, "pass":_PASSWORD, "host":_HOST, "db":_DATABASE, "port":_PORT}):
|
|
|
+ #_conn = mysql.connector.connect(user=_USER, password=_PASSWORD, host=_HOST, database=_DATABASE, port=_PORT)
|
|
|
+ _conn = mysql.connector.connect(user=db_info["user"], password=db_info["pass"], host=db_info["host"], database=db_info["db"], port=db_info["port"])
|
|
|
|
|
|
res = {}
|
|
|
if "function" in ctx:
|
|
|
@@ -93,6 +99,11 @@ def Request(ctx):
|
|
|
res = AddCardBlock(_conn, ctx)
|
|
|
elif ctx["function"] == "Search":
|
|
|
res = Search(_conn, ctx)
|
|
|
+ elif ctx["function"] == "Pending":
|
|
|
+ res = Pending(_conn, ctx)
|
|
|
+ elif ctx["function"] == "ping":
|
|
|
+ res["result"] = "success"
|
|
|
+ res["data"] = "pong"
|
|
|
|
|
|
_conn.close()
|
|
|
|
|
|
@@ -545,6 +556,29 @@ def Group(db,ctx):
|
|
|
|
|
|
group_res["result"] = "success"
|
|
|
|
|
|
+ elif action == "add":
|
|
|
+
|
|
|
+ if "group_name" not in ctx:
|
|
|
+ group_res["result"] = "fail"
|
|
|
+ group_res["api_comment"] = "must provide group name to add"
|
|
|
+ return res
|
|
|
+
|
|
|
+ group_name = ctx["group_name"]
|
|
|
+
|
|
|
+ group_id = -1
|
|
|
+ cursor.execute("select max(group_id) from groups")
|
|
|
+ x = cursor.fetchone()
|
|
|
+ if x[0] is None:
|
|
|
+ group_id = 1
|
|
|
+ else:
|
|
|
+ group_id = int(x[0]) + 1
|
|
|
+
|
|
|
+ cursor.execute("insert into groups (group_id, group_name) values (%s, %s)", [group_id, group_name])
|
|
|
+
|
|
|
+ group_res["result"] = "success"
|
|
|
+ group_res["group_id"] = group_id
|
|
|
+ group_res["group_name"] = group_name
|
|
|
+
|
|
|
elif action == "getone":
|
|
|
|
|
|
group_res["group"] = []
|
|
|
@@ -1033,6 +1067,91 @@ def AddCardBlock(db, ctx):
|
|
|
|
|
|
return res
|
|
|
|
|
|
+## ____ _ _
|
|
|
+## | _ \ ___ _ __ __| (_)_ __ __ _
|
|
|
+## | |_) / _ \ '_ \ / _` | | '_ \ / _` |
|
|
|
+## | __/ __/ | | | (_| | | | | | (_| |
|
|
|
+## |_| \___|_| |_|\__,_|_|_| |_|\__, |
|
|
|
+## |___/
|
|
|
+
|
|
|
+def Pending(db, ctx):
|
|
|
+ res = {"result":"fail"}
|
|
|
+ cursor = db.cursor()
|
|
|
+
|
|
|
+ fields = ["org_card_order_queue_id", "userid", "logical_card_id", "created", "processed", "comment" ]
|
|
|
+
|
|
|
+ val = {}
|
|
|
+ for f in fields:
|
|
|
+ if f in ctx:
|
|
|
+ if (type(ctx[f]) == int) or (ctx[f] and ((type(ctx[f]) != int) and (len(ctx[f]) > 0))):
|
|
|
+ val[f] = ctx[f]
|
|
|
+
|
|
|
+ if (ctx["action"] == "getall"):
|
|
|
+ query = "select org_card_order_queue_id, userid, logical_card_id, "
|
|
|
+ query += " date_format(created, '%Y-%m-%d %H:%i:%S'), "
|
|
|
+ query += " date_format(processed,'%Y-%m-%d %H:%i:%S'), comment, pending "
|
|
|
+ query += " from org_card_order_queue "
|
|
|
+ query += " where pending = 1 "
|
|
|
+ query += " order by org_card_order_queue_id asc"
|
|
|
+
|
|
|
+ cursor.execute(query)
|
|
|
+ rows = cursor.fetchall()
|
|
|
+ res["data"] = []
|
|
|
+ for row in rows:
|
|
|
+
|
|
|
+ _c = Card(db, {"action":"get", "logical_card_id":row[2]})
|
|
|
+
|
|
|
+ mag_token = ""
|
|
|
+ if "mag_token" in _c: mag_token = _c["mag_token"]
|
|
|
+ rfid_token = ""
|
|
|
+ if "rfid_token" in _c: rfid_token = _c["rfid_token"]
|
|
|
+
|
|
|
+ _r = {"org_card_order_queue_id":row[0], "userid":row[1], "logical_card_id":row[2], "created": row[3],
|
|
|
+ "processed":row[4], "comment":row[5], "pending":row[6],
|
|
|
+ "mag_token":mag_token, "rfid_token":rfid_token}
|
|
|
+ res["data"].append(_r)
|
|
|
+ res["result"] = "success"
|
|
|
+
|
|
|
+ if (ctx["action"] == "get"):
|
|
|
+ query = "select org_card_order_queue_id, userid, logical_card_id, "
|
|
|
+ query += " date_format(created, '%Y-%m-%d %H:%i:%S'), "
|
|
|
+ query += " date_format(processed,'%Y-%m-%d %H:%i:%S'), comment, pending "
|
|
|
+ query += " from org_card_order_queue "
|
|
|
+ query += " where pending = 1 and org_card_order_queue_id = %s"
|
|
|
+
|
|
|
+ cursor.execute(query, [val["org_card_order_queue_id"]])
|
|
|
+ row = cursor.fetchone()
|
|
|
+ if row is None:
|
|
|
+ res["api_comment"] = "pending card not found"
|
|
|
+ return res
|
|
|
+
|
|
|
+ cols = ["org_card_order_queue_id", "userid", "logical_card_id", "created", "processed", "comment", "pending" ]
|
|
|
+ res["pending_card"] = {}
|
|
|
+ for idx,f in enumerate(cols):
|
|
|
+ res["pending_card"][f] = row[idx]
|
|
|
+
|
|
|
+ cid = res["pending_card"]["logical_card_id"]
|
|
|
+
|
|
|
+ _r = CardInfo(db, {"action":"get", "logical_card_id":cid})
|
|
|
+ if _r["result"] == "fail":
|
|
|
+ return _r
|
|
|
+
|
|
|
+ res["cardinfo"] = _r
|
|
|
+ res["result"] = "success"
|
|
|
+
|
|
|
+ elif (ctx["action"] == "add"):
|
|
|
+ query = "insert into org_card_order_queue (userid, logical_card_id, comment, pending, created) values (%s,%s,%s,1,now())"
|
|
|
+ cursor.execute(query, [val["userid"], val["logical_card_id"], val["comment"]])
|
|
|
+ res["org_card_order_queue_id"] = cursor.lastrowid
|
|
|
+ res["result"] = "success"
|
|
|
+
|
|
|
+ elif (ctx["action"] == "process"):
|
|
|
+ query = "update org_card_order_queue set pending = 0, processed = now() where org_card_order_queue_id = %s"
|
|
|
+ cursor.execute(query, [val["org_card_order_queue_id"]])
|
|
|
+ res["result"] = "success"
|
|
|
+
|
|
|
+ db.commit()
|
|
|
+ return res
|
|
|
|
|
|
## _
|
|
|
## _ __ ___ __ _(_)_ __
|
|
|
@@ -1041,7 +1160,7 @@ def AddCardBlock(db, ctx):
|
|
|
## |_| |_| |_|\__,_|_|_| |_|
|
|
|
##
|
|
|
|
|
|
-def main(db):
|
|
|
+def __test(db):
|
|
|
print("main")
|
|
|
|
|
|
print("---------")
|
|
|
@@ -1107,8 +1226,86 @@ def main(db):
|
|
|
res = Request({"function":"CardInfo", "action":"search", "logical_card_id":1})
|
|
|
print("request.card.search:", res)
|
|
|
|
|
|
+def show_version(ofp):
|
|
|
+ ofp.write("PopufareAPI version " + POPUFARE_API_VERSION + "\n")
|
|
|
+ ofp.flush()
|
|
|
+
|
|
|
+def show_help(ofp):
|
|
|
+ show_version(ofp)
|
|
|
+ ofp.write("\nusage:\n")
|
|
|
+ ofp.write("\n")
|
|
|
+ ofp.write(" PopufareAPI [-h] [-v] [-c config] [-u dbuser] [-p dbpass] [-H dbhost] [-D db] [-P dbport] <json_data>\n")
|
|
|
+ ofp.write("\n")
|
|
|
+ ofp.write(" -c config file\n")
|
|
|
+ ofp.write(" -u db user\n")
|
|
|
+ ofp.write(" -p db pass\n")
|
|
|
+ ofp.write(" -H db host\n")
|
|
|
+ ofp.write(" -D db name\n")
|
|
|
+ ofp.write(" -P db port\n")
|
|
|
+ ofp.write(" -h help (this screen)\n")
|
|
|
+ ofp.write(" -v show version\n")
|
|
|
+ ofp.write("\n")
|
|
|
+ ofp.write("example:\n")
|
|
|
+ ofp.write("\n")
|
|
|
+ ofp.write(" PopufareAPI -d '{\"function\":\"ping\"}\n")
|
|
|
+ ofp.write("\n")
|
|
|
+ ofp.flush()
|
|
|
+
|
|
|
if __name__ == "__main__":
|
|
|
- conn = mysql.connector.connect(user=_USER, password=_PASSWORD, host=_HOST, database=_DATABASE, port=_PORT)
|
|
|
- main(conn)
|
|
|
- conn.close()
|
|
|
+
|
|
|
+ api_opt = {
|
|
|
+ "db_info": {
|
|
|
+ "user":"busapi",
|
|
|
+ "pass":"bus",
|
|
|
+ "host":"localhost",
|
|
|
+ "db":"busdb",
|
|
|
+ "port":5506
|
|
|
+ },
|
|
|
+ "config":"",
|
|
|
+ "data":""
|
|
|
+ }
|
|
|
+
|
|
|
+ data = {}
|
|
|
+
|
|
|
+ opts,args = getopt.getopt(sys.argv[1:], "hd:u:p:H:D:P:c:")
|
|
|
+
|
|
|
+ for o,a in opts:
|
|
|
+ if o == "-h":
|
|
|
+ show_help(sys.stdout)
|
|
|
+ sys.exit(0)
|
|
|
+ elif o == "-c":
|
|
|
+ api_opt["config"] = a
|
|
|
+
|
|
|
+ elif o == "-u":
|
|
|
+ api_opt["db_info"]["user"] = a
|
|
|
+ elif o == "-p":
|
|
|
+ api_opt["db_info"]["pass"] = a
|
|
|
+ elif o == "-H":
|
|
|
+ api_opt["db_info"]["host"] = a
|
|
|
+ elif o == "-D":
|
|
|
+ api_opt["db_info"]["db"] = a
|
|
|
+ elif o == "-P":
|
|
|
+ api_opt["db_info"]["port"] = int(a)
|
|
|
+
|
|
|
+ #elif o == "-d":
|
|
|
+ # api_opt["data"] = json.loads(a)
|
|
|
+
|
|
|
+ else:
|
|
|
+ show_help(sys.stderr)
|
|
|
+ sys.exit(-1)
|
|
|
+
|
|
|
+ if len(args) < 1:
|
|
|
+ sys.stderr.write("no data request\n")
|
|
|
+ show_help(sys.stderr)
|
|
|
+ sys.exit(-1)
|
|
|
+
|
|
|
+ api_opt["data"] = json.loads(args[0])
|
|
|
+
|
|
|
+ res = Request(api_opt["data"], api_opt["db_info"])
|
|
|
+ print(json.dumps(res,indent=2))
|
|
|
+
|
|
|
+ #conn = mysql.connector.connect(user=_USER, password=_PASSWORD, host=_HOST, database=_DATABASE, port=_PORT)
|
|
|
+ #conn = mysql.connector.connect(user=api_opt["dbuser"], password=api_opt["dbpass"], host=api_opt["dbhost"], database=api_opt["db"], port=api_opt["dbport"])
|
|
|
+ #main(conn)
|
|
|
+ #conn.close()
|
|
|
|