package main

import (
	"context"
	"fmt"
	"game-driver/logger"
	"game-driver/pkg/card_reader"
	"go.uber.org/zap"
	"os"
	"os/signal"
	"syscall"
)

func main() {
	logger.DefaultLogger()
	defer logger.Sync()

	reader, err := card_reader.NewReader("/dev/ttyUSB0")
	if err != nil {
		zap.S().Panicln(err)
	}
	defer reader.Close()

	err = reader.Init()
	if err != nil {
		zap.S().Panicln(err)
	}

	ctx, cancel := context.WithCancel(context.Background())
	go reader.OnCardInfo(ctx, func(info *card_reader.CardInfo) {
		zap.S().Infow("Card info", "Type", fmt.Sprintf("%#x", info.Type), "ID", info.ID)
	})

	sig := make(chan os.Signal, 1)
	signal.Notify(sig, os.Interrupt, syscall.SIGTERM)

	<-sig
	zap.S().Infoln("接收到关闭命令 - 正在关闭程序")
	cancel()
	zap.S().Infoln("关闭完成")
}
