-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcn_read.py
More file actions
69 lines (58 loc) · 1.9 KB
/
Copy pathcn_read.py
File metadata and controls
69 lines (58 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
"""
中国交通联合卡读取 — 独立子进程入口。
在子进程里完成 PC/SC 读卡 + 站名/坐标解码, 输出一行 JSON, 然后退出。
这样主进程永不持有 PC/SC 上下文, 不会和日本 FeliCa(SFCV) 抢读卡器。
输出: 优先写到 --out 指定的文件(UTF-8); 没有则打印到 stdout。
(打包成无控制台 exe 时 sys.stdout 可能为 None, 所以默认用文件传递结果。)
"""
import json
import sys
def _ser_time(t):
return t.isoformat() if t else None
def _emit(obj):
text = json.dumps(obj, ensure_ascii=True, default=str) # 纯 ASCII, 跨编码绝不乱码
out_path = None
if "--out" in sys.argv:
i = sys.argv.index("--out")
if i + 1 < len(sys.argv):
out_path = sys.argv[i + 1]
if out_path:
try:
with open(out_path, "w", encoding="utf-8") as f:
f.write(text)
return
except OSError:
pass
try:
sys.stdout.reconfigure(encoding="utf-8")
except Exception:
pass
try:
print(text)
except Exception:
pass
def main():
try:
import pboc_reader
raw = pboc_reader.read_card(wait_sec=12)
except Exception as e:
_emit({"ok": False, "error": str(e)})
return
rides = []
try:
import cn_trips, cn_lines, cn_coords
line_db = cn_lines.load()
coord_db = cn_coords.CoordDB()
rides = cn_trips.resolve_rides(raw.get("trips", []), line_db, coord_db)
except Exception:
rides = []
info = raw.get("info", {}) or {}
_emit({
"ok": True,
"card_id": info.get("card_no", ""),
"balance_yuan": raw.get("balance_yuan"),
"txns": [{**t, "time": _ser_time(t.get("time"))} for t in raw.get("txns", [])],
"rides": [{**r, "time": _ser_time(r.get("time"))} for r in rides],
})
if __name__ == "__main__":
main()