package standby

import (
	"context"
	"fmt"
	"game-driver/internal/schema"
	"game-driver/pkg/relay"
	"go.uber.org/zap"
)

func Relay(item schema.WaitItemModel) func(c context.Context) error {
	return func(c context.Context) error {
		r, err := relay.New(item.Data)
		if err != nil {
			return fmt.Errorf("继电器初始化异常: %w", err)
		}
		defer r.Close()

		zap.S().Infoln("待机继电器供电")
		defer zap.S().Infoln("待机继电器断电")

		_ = r.On(0)
		<-c.Done()
		_ = r.Off(0)

		return nil
	}
}
