46 lines
1.8 KiB
Python
46 lines
1.8 KiB
Python
import os, csv
|
|
from typing import Dict
|
|
from config import PARTDB_BASE, PARTDB_TOKEN, COM_PORT, BAUD_RATE, SCAN_APPEND_CSV
|
|
from apis.partdb_api import PartDB
|
|
from parsers.digikey_mh10 import parse_digikey
|
|
from devices.scanner_serial import scan_loop
|
|
|
|
def _row_for_csv(fields: Dict[str,str]) -> Dict[str,str]:
|
|
return {
|
|
"DigiKeyPart": fields.get("DigiKeyPart",""),
|
|
"MfrPart": fields.get("MfrPart",""),
|
|
"CustomerPart": fields.get("CustomerPart",""),
|
|
"InternalLot1": fields.get("InternalLot1",""),
|
|
"InternalLot2": fields.get("InternalLot2",""),
|
|
"DateCodeRaw": fields.get("DateCodeRaw",""),
|
|
"TraceID": fields.get("TraceID",""),
|
|
"PackageSerial": fields.get("PackageSerial",""),
|
|
"PickTicket": fields.get("PickTicket",""),
|
|
}
|
|
|
|
def run_scan_import():
|
|
pdb = PartDB(PARTDB_BASE, PARTDB_TOKEN)
|
|
|
|
def on_scan(flat: str):
|
|
fields = parse_digikey(flat)
|
|
print("\nScan:", flat)
|
|
print("→", fields)
|
|
|
|
# Create (very minimal) part record using MPN/DKPN
|
|
name = fields.get("MfrPart") or fields.get("DigiKeyPart") or "Unknown"
|
|
mpn = fields.get("MfrPart") or ""
|
|
# You can enrich with ensure_category/manufacturer/footprint if you want here
|
|
|
|
# Optional: CSV log
|
|
if SCAN_APPEND_CSV:
|
|
newfile = not os.path.exists(SCAN_APPEND_CSV)
|
|
with open(SCAN_APPEND_CSV, "a", newline="", encoding="utf-8") as f:
|
|
w = csv.DictWriter(f, fieldnames=list(_row_for_csv(fields).keys()))
|
|
if newfile: w.writeheader()
|
|
w.writerow(_row_for_csv(fields))
|
|
|
|
# If you want to insert a stock movement instead of creating parts,
|
|
# call your Part-DB stock API here.
|
|
|
|
scan_loop(COM_PORT, BAUD_RATE, on_scan)
|