package service import ( "bytes" "container/list" "encoding/json" "errors" "fmt" "strings" "time" ) var ( HeartBeatTime time.Time PrintWithRobot bool Color CardColor ) var ( ErrorHeartBeat error = errors.New("heart beat time exceded") ) type LabelPrinter uint8 const ( LabelPrinter01 LabelPrinter = iota << 2 LabelPrinter02 LabelPrinter03 LabelPrinter04 LabelPrinter05 ) type CardColor uint8 const ( CardColorBlueYellow CardColor = iota << 2 CardColorRed CardColorYellow CardColorGreen CardColorWhite CardColorBlue CardColorOrange CardColorBlack CardColorNone ) type IService interface { } type service struct { queue *list.List } func (s *service) HeartBeatHandler() error { if time.Since(HeartBeatTime) > time.Second*30 { return ErrorHeartBeat } HeartBeatTime = time.Now() return nil } func (s *service) CardColor(product, po, co, grade string) error { // grades calidadNormal := false calidadEspecial := false // set grade if strings.Contains(grade, "275") { calidadNormal = true } else { calidadEspecial = true } // none A product pendientes := co == fmt.Sprintf("5%s", po) chatarra := co == fmt.Sprintf("6%s", po) segundas := co == fmt.Sprintf("7%s", po) cortas := co == fmt.Sprintf("8%s", po) // pendientes if pendientes { return s.SetColorCard(CardColorOrange) } // chatarra if chatarra { return s.SetColorCard(CardColorRed) } // segundas or cortas if segundas || cortas { return s.SetColorCard(CardColorWhite) } // british british := strings.HasPrefix(product, "UB") || strings.HasPrefix(product, "UC") || strings.HasPrefix(product, "PFC") // american american := strings.HasPrefix(product, "W") // if calidadEspecial { return s.SetColorCard(CardColorBlueYellow) } else if (strings.HasPrefix(product, "HEA") || strings.HasPrefix(product, "IPEA")) && calidadNormal { return s.SetColorCard(CardColorYellow) } else if (strings.HasPrefix(product, "HEA") || strings.HasPrefix(product, "IPEA")) && calidadEspecial { return s.SetColorCard(CardColorBlue) } else if british && calidadNormal { return s.SetColorCard(CardColorBlack) } else if british && calidadEspecial { noneColorLabel = true } else if strings.HasPrefix(product, "W") { noneColorLabel = true } return nil } // SetColorCard set color card func (s *service) SetColorCard(color CardColor) error { // check color card if selectedCard, ok := CardColorMap[color]; !ok { return errors.New("invalid color") } else { selectedCard.Value = true } // data buffer data := bytes.NewBuffer([]byte{}) // tags tags := []Tag{} // add tags for _, v := range CardColorMap { tag := Tag{ Topic: v.Topic, Value: v.Value, } tags = append(tags, tag) } // encode color cards if err := json.NewEncoder(data).Encode(&tags); err != nil { return err } // publish color cards if err := s.broker.Publish("vnode/test", data.Bytes()); err != nil { return err } // init color cards s.CardColorInit() // set color Color = color return nil } // CardColorInit init color card func (s *service) CardColorInit() { for _, v := range CardColorMap { v.Value = false } } func (s *service) Enqueue(data any) { s.queue.PushBack(data) } func (s *service) Dequeue() any { if s.queue.Len() == 0 { return nil } return s.queue.Remove(s.queue.Front()) } func (s *service) Peek() any { if s.queue.Len() == 0 { return nil } return s.queue.Front().Value } func (s *service) QueueLen() int { return s.queue.Len() } func (s *service) PrintHandler(lp LabelPrinter) error { fmt.Println(LabelPrinter(lp)) return nil } func (s *service) ResetFifoHandler() error { s.queue.Init() return nil } func (s *service) PrintWithRobotHandler(v bool) { PrintWithRobot = v } func (s *service) PackagePiecesHandler(pieces int64) { fmt.Println(pieces) } func (s *service) RobotEventDataHandler(data []byte) error { rb := vnode.MqttMessage{} if err := json.NewDecoder(bytes.NewBuffer(data)).Decode(&rb); err != nil { return err } for _, msg := range rb { switch msg.Topic { case "/robot12/read/print_with_robot": if v, ok := msg.Value.(bool); ok { if v { s.PrintWithRobotHandler(v) } } case "/robot12/read/reset_fifo": // if true if v, ok := msg.Value.(bool); ok && v { if v { if err := s.ResetFifoHandler(); err != nil { return err } } } case "/robot12/read/heart_beat": // if true if v, ok := msg.Value.(bool); ok && v { if v { if err := s.HeartBeatHandler(); err != nil { return err } } } case "/robot12/read/print_on_paper_01": // if true if v, ok := msg.Value.(bool); ok && v { if v { if err := s.PrintHandler(LabelPrinter01); err != nil { return err } } } case "/robot12/read/print_on_paper_02": // if true if v, ok := msg.Value.(bool); ok && v { if v { if err := s.PrintHandler(LabelPrinter02); err != nil { return err } } } case "/robot12/read/print_on_adh_01": // if true if v, ok := msg.Value.(bool); ok && v { if v { if err := s.PrintHandler(LabelPrinter03); err != nil { return err } } } case "/robot12/read/print_on_adh_02": // if true if v, ok := msg.Value.(bool); ok && v { if v { if err := s.PrintHandler(LabelPrinter04); err != nil { return err } } } case "/robot12/read/package_no_pieces": if v, ok := msg.Value.(int64); ok { s.PackagePiecesHandler(v) } } } return nil } func init() { HeartBeatTime = time.Now() PrintWithRobot = false } func NewService(broker *vnode.Broker) IService { queue := list.New() return &service{ broker: broker, queue: queue, } }