forked from c3re/can2mqtt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconverter.go
45 lines (40 loc) · 1.32 KB
/
converter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package main
import (
"fmt"
"github.com/jaster-prj/can2mqtt/convertmode"
)
type ConverterFactory struct {
converterMap map[string]ConvertMode
}
func NewConverterFactory() *ConverterFactory {
converterMap := map[string]ConvertMode{}
// initialize all convertModes
converterMap[convertmode.None{}.String()] = convertmode.None{}
converterMap[convertmode.SixteenBool2Ascii{}.String()] = convertmode.SixteenBool2Ascii{}
converterMap[convertmode.PixelBin2Ascii{}.String()] = convertmode.PixelBin2Ascii{}
converterMap[convertmode.ByteColor2ColorCode{}.String()] = convertmode.ByteColor2ColorCode{}
converterMap[convertmode.MyMode{}.String()] = convertmode.MyMode{}
// Dynamically create int and uint convertmodes
for _, bits := range []uint{8, 16, 32, 64} {
for _, instances := range []uint{1, 2, 4, 8} {
if bits*instances <= 64 {
// int
cmi, _ := convertmode.NewInt2Ascii(instances, bits)
converterMap[cmi.String()] = cmi
// uint
cmu, _ := convertmode.NewUint2Ascii(instances, bits)
converterMap[cmu.String()] = cmu
}
}
}
return &ConverterFactory{
converterMap: converterMap,
}
}
func (c *ConverterFactory) GetConverter(converter string) (ConvertMode, error) {
conv, ok := c.converterMap[converter]
if !ok {
return nil, fmt.Errorf("converter not initialized: %s", converter)
}
return conv, nil
}