343 lines
7.0 KiB
Go
343 lines
7.0 KiB
Go
![]() |
package service
|
||
|
|
||
|
import (
|
||
|
"bytes"
|
||
|
"container/list"
|
||
|
"context"
|
||
|
"encoding/json"
|
||
|
"errors"
|
||
|
"fmt"
|
||
|
"strings"
|
||
|
"time"
|
||
|
|
||
|
"git.espin.casa/albert/cml04-printer-robot/internal/vnode"
|
||
|
"git.espin.casa/albert/cml04-printer-robot/pkg/api"
|
||
|
)
|
||
|
|
||
|
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 Tag struct {
|
||
|
Topic string `json:"t"`
|
||
|
Value any `json:"v"`
|
||
|
}
|
||
|
|
||
|
var (
|
||
|
CardColorMap = map[CardColor]*Tag{
|
||
|
CardColorBlueYellow: {
|
||
|
Topic: "/robot12/write/blue_yellow_card",
|
||
|
Value: false,
|
||
|
},
|
||
|
CardColorRed: {
|
||
|
Topic: "/robot12/write/red_card",
|
||
|
Value: false,
|
||
|
},
|
||
|
CardColorYellow: {
|
||
|
Topic: "/robot12/write/yellow_card",
|
||
|
Value: false,
|
||
|
},
|
||
|
CardColorGreen: {
|
||
|
Topic: "/robot12/write/green_card",
|
||
|
Value: false,
|
||
|
},
|
||
|
CardColorWhite: {
|
||
|
Topic: "/robot12/write/white_card",
|
||
|
Value: false,
|
||
|
},
|
||
|
CardColorBlue: {
|
||
|
Topic: "/robot12/write/blue_card",
|
||
|
Value: false,
|
||
|
},
|
||
|
CardColorOrange: {
|
||
|
Topic: "/robot12/write/orange_card",
|
||
|
Value: false,
|
||
|
},
|
||
|
CardColorBlack: {
|
||
|
Topic: "/robot12/write/black_card",
|
||
|
Value: false,
|
||
|
},
|
||
|
CardColorNone: {
|
||
|
Topic: "/robot12/write/none_card",
|
||
|
Value: false,
|
||
|
},
|
||
|
}
|
||
|
)
|
||
|
|
||
|
type IService interface {
|
||
|
PrintPackage(ctx context.Context, req *api.PrintPackageReq) (res *api.PrintPackageRes, err error)
|
||
|
RobotEventDataHandler(data []byte) error
|
||
|
}
|
||
|
|
||
|
type service struct {
|
||
|
broker *vnode.Broker
|
||
|
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()
|
||
|
}
|
||
|
|
||
|
// PrintPackage implements IService.
|
||
|
func (s *service) PrintPackage(ctx context.Context, req *api.PrintPackageReq) (res *api.PrintPackageRes, err error) {
|
||
|
s.Enqueue(req.PackageData)
|
||
|
return &api.PrintPackageRes{
|
||
|
Msg: "print package success",
|
||
|
TimeStamp: time.Now().UTC().Format(time.RFC3339),
|
||
|
}, nil
|
||
|
}
|
||
|
|
||
|
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,
|
||
|
}
|
||
|
}
|