11package yomo
22
33import (
4+ "fmt"
45 "io"
56 "log"
6- "os"
77 "time"
88
9+ y3 "github.com/yomorun/yomo-codec-golang"
10+ "github.com/yomorun/yomo-codec-golang/pkg/codes"
11+ "github.com/yomorun/yomo-codec-golang/pkg/codes/packetstructure"
12+
13+ "github.com/yomorun/yomo-codec-golang/pkg/packetutils"
14+
15+ "github.com/yomorun/yomo/configs"
16+
917 "github.com/yomorun/yomo/pkg/pprof"
1018
1119 "github.com/yomorun/yomo/pkg/plugin"
@@ -37,15 +45,28 @@ func RunStream(plugin plugin.YomoStreamPlugin, endpoint string) {
3745
3846// RunDev makes test plugin connect to a demo YoMo server
3947func RunDev (plugin plugin.YomoObjectPlugin , endpoint string ) {
48+ RunDevWith (plugin , endpoint , OutputPacketPrinter )
49+ }
4050
51+ type OutputFormatter int32
52+
53+ const (
54+ OutputHexString OutputFormatter = 0
55+ OutputPacketPrinter OutputFormatter = 1
56+ OutputEchoData OutputFormatter = 2
57+ OutputThermometerData OutputFormatter = 3
58+ )
59+
60+ // RunDev makes test plugin connect to a demo YoMo server. OutputFormatter: OutputHexString/OutputPacketPrinter/OutputEchoData
61+ func RunDevWith (plugin plugin.YomoObjectPlugin , endpoint string , formatter OutputFormatter ) {
4162 go func () {
4263 logger .Infof ("plugin service [%s] start... [%s]" , plugin .Name (), endpoint )
4364
4465 // activation service
4566 framework .NewServer (endpoint , plugin )
4667 }()
4768
48- yomoEchoClient , err := util .QuicClient ("161.189.140.133:11520" )
69+ yomoEchoClient , err := util .QuicClient (configs . DefaultEchoConf . EchoServerAddr )
4970 //yomoEchoClient, err := util.QuicClient("localhost:11520")
5071 if err != nil {
5172 panic (err )
@@ -57,7 +78,22 @@ func RunDev(plugin plugin.YomoObjectPlugin, endpoint string) {
5778 }
5879
5980 go io .Copy (yomoPluginClient , yomoEchoClient ) // nolint
60- go io .Copy (os .Stdout , yomoPluginClient ) // nolint
81+
82+ // select formatter
83+ var w io.Writer
84+ switch formatter {
85+ case OutputHexString :
86+ w = & hexStringFormatter {}
87+ case OutputPacketPrinter :
88+ w = & packetPrinterFormatter {}
89+ case OutputEchoData :
90+ w = & echoDataFormatter {}
91+ case OutputThermometerData :
92+ w = & thermometerDataFormatter {}
93+ default :
94+ w = & packetPrinterFormatter {}
95+ }
96+ go util .CopyTo (w , yomoPluginClient ) // nolint
6197
6298 for {
6399 time .Sleep (time .Second )
@@ -66,5 +102,71 @@ func RunDev(plugin plugin.YomoObjectPlugin, endpoint string) {
66102 log .Fatal (err )
67103 }
68104 }
105+ }
106+
107+ // hexStringFormatter
108+ type hexStringFormatter struct {
109+ io.Writer
110+ }
111+
112+ func (w * hexStringFormatter ) Write (b []byte ) (int , error ) {
113+ fmt .Printf ("%v:\t %s\n " , time .Now ().Format ("2006-01-02 15:04:05" ), packetutils .FormatBytes (b )) // debug:
114+ return 0 , nil
115+ }
116+
117+ // packetPrinterFormatter
118+ type packetPrinterFormatter struct {
119+ io.Writer
120+ }
121+
122+ func (w * packetPrinterFormatter ) Write (b []byte ) (int , error ) {
123+ res , _ , _ := y3 .DecodeNodePacket (b )
124+ fmt .Printf ("%v:\t " , time .Now ().Format ("2006-01-02 15:04:05" )) // debug:
125+ packetutils .PrintNodePacket (res )
126+ fmt .Println ()
127+ return 0 , nil
128+ }
129+
130+ // echoDataFormatter
131+ type echoDataFormatter struct {
132+ io.Writer
133+ }
134+
135+ func (w * echoDataFormatter ) Write (b []byte ) (int , error ) {
136+ var mold = echoData {}
137+ res , _ , _ := y3 .DecodeNodePacket (b )
138+ _ = packetstructure .Decode (res , & mold )
139+ fmt .Printf ("%v:\t %v\n " , time .Now ().Format ("2006-01-02 15:04:05" ), mold ) // debug:
140+ return 0 , nil
141+ }
142+
143+ type echoData struct {
144+ Id int32 `yomo:"0x10"`
145+ Name string `yomo:"0x11"`
146+ Test test `yomo:"0x20"`
147+ }
148+
149+ type test struct {
150+ Tag []string `yomo:"0x13"`
151+ }
152+
153+ // thermometerDataFormatter
154+ type thermometerDataFormatter struct {
155+ io.Writer
156+ }
157+
158+ func (w * thermometerDataFormatter ) Write (b []byte ) (int , error ) {
159+ var mold = []thermometerData {}
160+
161+ proto := codes .NewProtoCodec ("0x20" )
162+ proto .UnmarshalStruct (b , & mold )
163+ fmt .Printf ("%v:\t %v\n " , time .Now ().Format ("2006-01-02 15:04:05" ), mold ) // debug:
164+ return 0 , nil
165+ }
69166
167+ type thermometerData struct {
168+ Id string `yomo:"0x10"`
169+ Temperature float32 `yomo:"0x11"`
170+ Humidity float32 `yomo:"0x12"`
171+ Stored bool `yomo:"0x13"`
70172}
0 commit comments