Skip to content

Commit 6c1c6fa

Browse files
committed
add sendjsonraw command
usage: sendjsonraw { "P": "/dev/ttyACM0", "Data": [ { "D": "Zg==" } ] } D field contains base64 encoded data
1 parent 1130ea8 commit 6c1c6fa

File tree

3 files changed

+85
-9
lines changed

3 files changed

+85
-9
lines changed

hub.go

+4
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,10 @@ func checkCmd(m []byte) {
171171
// kill the running process (assumes singleton for now)
172172
go spHandlerProgramKill()
173173

174+
} else if strings.HasPrefix(sl, "sendjsonraw") {
175+
// will catch sendjsonraw
176+
go spWriteJsonRaw(s)
177+
174178
} else if strings.HasPrefix(sl, "sendjson") {
175179
// will catch sendjson
176180
go spWriteJson(s)

main.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -334,8 +334,8 @@ const homeTemplateHtml = `<!DOCTYPE html>
334334
return false;
335335
}
336336
socket.emit("command", msg.val());
337-
if (msg.val().indexOf("log off") != -1) {only_log = true;}
338-
if (msg.val().indexOf("log on") != -1) {only_log = false;}
337+
if (msg.val().indexOf("log off") != -1) {only_log = true}
338+
if (msg.val().indexOf("log on") != -1) {only_log = false}
339339
msg.val("");
340340
return false
341341
});

serial.go

+79-7
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,24 @@ type writeRequestJson struct {
2626
Data []writeRequestJsonData
2727
}
2828

29+
type writeRequestJsonRaw struct {
30+
p *serport
31+
P string
32+
Data []writeRequestJsonDataRaw
33+
}
34+
2935
type writeRequestJsonData struct {
3036
D string
3137
Id string
3238
Buf string
3339
}
3440

41+
type writeRequestJsonDataRaw struct {
42+
D []byte
43+
Id string
44+
Buf string
45+
}
46+
3547
type qReportJson struct {
3648
Cmd string
3749
QCnt int
@@ -73,7 +85,8 @@ type serialhub struct {
7385
unregister chan *serport
7486

7587
// regexp for json trimming
76-
reJsonTrim *regexp.Regexp
88+
reJsonTrim *regexp.Regexp
89+
reJsonRawTrim *regexp.Regexp
7790
}
7891

7992
type SpPortList struct {
@@ -103,12 +116,13 @@ var NetworkPorts SpPortList
103116

104117
var sh = serialhub{
105118
//write: make(chan *serport, chan []byte),
106-
write: make(chan writeRequest),
107-
writeJson: make(chan writeRequestJson),
108-
register: make(chan *serport),
109-
unregister: make(chan *serport),
110-
ports: make(map[*serport]bool),
111-
reJsonTrim: regexp.MustCompile("sendjson"),
119+
write: make(chan writeRequest),
120+
writeJson: make(chan writeRequestJson),
121+
register: make(chan *serport),
122+
unregister: make(chan *serport),
123+
ports: make(map[*serport]bool),
124+
reJsonTrim: regexp.MustCompile("sendjson"),
125+
reJsonRawTrim: regexp.MustCompile("sendjsonraw"),
112126
}
113127

114128
func (sh *serialhub) run() {
@@ -615,6 +629,64 @@ func spWriteJson(arg string) {
615629
sh.writeJson <- m
616630
}
617631

632+
func spWriteJsonRaw(arg string) {
633+
634+
log.Printf("spWriteJson. arg:%v\n", arg)
635+
636+
// remove sendjson string
637+
arg = sh.reJsonRawTrim.ReplaceAllString(arg, "")
638+
//log.Printf("string we're going to parse:%v\n", arg)
639+
640+
// this is a structured command now for sending in serial commands multiple at a time
641+
// with an ID so we can send back the ID when the command is done
642+
var m writeRequestJsonRaw
643+
/*
644+
m.P = "COM22"
645+
var data writeRequestJsonData
646+
data.Id = "234"
647+
str := "yeah yeah"
648+
data.D = str //[]byte(str) //[]byte(string("blah blah"))
649+
m.Data = append(m.Data, data)
650+
//m.Data = append(m.Data, data)
651+
bm, err2 := json.Marshal(m)
652+
if err2 == nil {
653+
log.Printf("Test json serialize:%v\n", string(bm))
654+
}
655+
*/
656+
657+
err := json.Unmarshal([]byte(arg), &m)
658+
659+
if err != nil {
660+
log.Printf("Problem decoding json. giving up. json:%v, err:%v\n", arg, err)
661+
spErr(fmt.Sprintf("Problem decoding json. giving up. json:%v, err:%v", arg, err))
662+
return
663+
}
664+
665+
// see if we have this port open
666+
portname := m.P
667+
myport, isFound := findPortByName(portname)
668+
669+
if !isFound {
670+
// we couldn't find the port, so send err
671+
spErr("We could not find the serial port " + portname + " that you were trying to write to.")
672+
return
673+
}
674+
675+
// we found our port
676+
m.p = myport
677+
678+
var mr writeRequestJson
679+
680+
mr.p = m.p
681+
mr.P = m.P
682+
var data writeRequestJsonData
683+
data.D = string(m.Data[0].D)
684+
mr.Data = append(mr.Data, data)
685+
686+
// send it to the writeJson channel
687+
sh.writeJson <- mr
688+
}
689+
618690
func spWrite(arg string) {
619691
// we will get a string of comXX asdf asdf asdf
620692
log.Println("Inside spWrite arg: " + arg)

0 commit comments

Comments
 (0)