package main

import (
	"cicv-data-closedloop/pjisuv_msgs"
	"github.com/bluenviron/goroslib/v2"
	"log"
	"os"
	"os/signal"
	"time"
)

func main() {
	// create a node and connect to the master
	n, err := goroslib.NewNode(goroslib.NodeConf{
		Name:          "goroslib_pub_fault_info",
		MasterAddress: "127.0.0.1:11311",
	})
	if err != nil {
		panic(err)
	}
	defer n.Close()

	// create a publisher
	pub, err := goroslib.NewPublisher(goroslib.PublisherConf{
		Node:  n,
		Topic: "/fault_info",
		Msg:   &pjisuv_msgs.FaultVec{},
	})
	if err != nil {
		panic(err)
	}
	defer pub.Close()

	r := n.TimeRate(1 * time.Second)

	c := make(chan os.Signal, 1)
	signal.Notify(c, os.Interrupt)

	for {
		select {
		// publish a message every second
		case <-r.SleepChan():
			msg := &pjisuv_msgs.FaultVec{
				InfoVec: []pjisuv_msgs.FaultInfo{
					{ErrorCode: 12345}, // 发送故障码12345
				},
			}
			log.Printf("Outgoing: %+v\n", msg)
			pub.Write(msg)

		// handle CTRL-C
		case <-c:
			return
		}
	}
}