forked from johnlauer/serial-port-json-server
-
Notifications
You must be signed in to change notification settings - Fork 101
/
bufferflow_timed.go
126 lines (100 loc) · 2.45 KB
/
bufferflow_timed.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package main
import (
"encoding/json"
"log"
"time"
)
type BufferflowTimed struct {
Name string
Port string
Output chan []byte
Input chan string
ticker *time.Ticker
IsOpen bool
bufferedOutput string
}
/*
var (
bufferedOutput string
)
*/
func (b *BufferflowTimed) Init() {
log.Println("Initting timed buffer flow (output once every 16ms)")
b.bufferedOutput = ""
b.IsOpen = true
go func() {
for data := range b.Input {
b.bufferedOutput = b.bufferedOutput + data
}
}()
go func() {
b.ticker = time.NewTicker(16 * time.Millisecond)
for _ = range b.ticker.C {
if b.bufferedOutput != "" {
m := SpPortMessage{b.Port, b.bufferedOutput}
buf, _ := json.Marshal(m)
b.Output <- []byte(buf)
//log.Println(buf)
b.bufferedOutput = ""
}
}
}()
}
func (b *BufferflowTimed) BlockUntilReady(cmd string, id string) (bool, bool, string) {
//log.Printf("BlockUntilReady() start\n")
return true, false, ""
}
func (b *BufferflowTimed) OnIncomingData(data string) {
b.Input <- data
}
// Clean out b.sem so it can truly block
func (b *BufferflowTimed) ClearOutSemaphore() {
}
func (b *BufferflowTimed) BreakApartCommands(cmd string) []string {
return []string{cmd}
}
func (b *BufferflowTimed) Pause() {
return
}
func (b *BufferflowTimed) Unpause() {
return
}
func (b *BufferflowTimed) SeeIfSpecificCommandsShouldSkipBuffer(cmd string) bool {
return false
}
func (b *BufferflowTimed) SeeIfSpecificCommandsShouldPauseBuffer(cmd string) bool {
return false
}
func (b *BufferflowTimed) SeeIfSpecificCommandsShouldUnpauseBuffer(cmd string) bool {
return false
}
func (b *BufferflowTimed) SeeIfSpecificCommandsShouldWipeBuffer(cmd string) bool {
return false
}
func (b *BufferflowTimed) SeeIfSpecificCommandsReturnNoResponse(cmd string) bool {
return false
}
func (b *BufferflowTimed) ReleaseLock() {
}
func (b *BufferflowTimed) IsBufferGloballySendingBackIncomingData() bool {
return true
}
func (b *BufferflowTimed) Close() {
if b.IsOpen == false {
// we are being asked a 2nd time to close when we already have
// that will cause a panic
log.Println("We got called a 2nd time to close, but already closed")
return
}
b.IsOpen = false
b.ticker.Stop()
close(b.Input)
}
func (b *BufferflowTimed) GetManualPaused() bool {
return false
}
func (b *BufferflowTimed) RewriteSerialData(cmd string, id string) string {
return ""
}
func (b *BufferflowTimed) SetManualPaused(isPaused bool) {
}