-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
84 lines (64 loc) · 1.87 KB
/
main.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package main
import (
"bytes"
"errors"
"fmt"
"image"
_ "image/jpeg"
"io/ioutil"
"os"
"time"
"github.com/edgexfoundry/app-functions-sdk-go/v2/pkg"
"github.com/edgexfoundry/app-functions-sdk-go/v2/pkg/interfaces"
"github.com/edgexfoundry/app-functions-sdk-go/v2/pkg/transforms"
"github.com/edgexfoundry/go-mod-core-contracts/v2/dtos"
)
func main() {
exitCode := 0
defer func() { os.Exit(exitCode) }()
service, ok := pkg.NewAppService("CborToJpegImageViewer")
if !ok {
exitCode = -1
return
}
lc := service.LoggingClient()
resourceNames, err := service.GetAppSettingStrings("ResourceNames")
if err != nil {
lc.Errorf("GetAppSettingStrings returned error: %v", err.Error())
exitCode = -1
return
}
if err := service.SetFunctionsPipeline(
transforms.NewFilterFor(resourceNames).FilterByResourceName,
processImages,
); err != nil {
lc.Errorf("SetFunctionsPipeline returned error: %v ", err.Error())
exitCode = -1
return
}
err = service.MakeItRun()
if err != nil {
lc.Errorf("MakeItRun returned error: %v", err.Error())
exitCode = -1
return
}
}
func processImages(edgexcontext interfaces.AppFunctionContext, params interface{}) (bool, interface{}) {
if params != nil {
event, ok := params.(dtos.Event)
if !ok {
return false, errors.New("processImages didn't receive models.Event")
}
for _, reading := range event.Readings {
imageData, imageType, err := image.Decode(bytes.NewReader(reading.BinaryValue))
if err != nil {
return false, errors.New("unable to decode image: " + err.Error())
}
ioutil.WriteFile("image.jpeg", reading.BinaryValue, 0644)
fmt.Print(time.Now().Format("2006-01-02 15:04:05"))
fmt.Printf(": EdgeX device-camera image received from: %s, ReadingName: %s, Type: %s, Size: %s\n",
reading.DeviceName, reading.ResourceName, imageType, imageData.Bounds().Size().String())
}
}
return false, nil
}