-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.go
154 lines (133 loc) · 3.13 KB
/
model.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package main
import (
"errors"
"fmt"
"os"
//"time"
"github.com/mitchellh/go-homedir"
"go.bug.st/serial"
)
// this struct stores the configuration details that will be passed to the upload/download backend
type SerialConfig struct {
Port string
Settings serial.Mode
}
// The model of MVC, stores internal state
type Model struct {
Config SerialConfig
Port serial.Port
UploadFilepath string
UploadFileBuffer string
DownloadFilepath string
DownloadFileBuffer []byte
DownloadBufLen int
}
// sets the upload filepath
func (self *Model) SetUploadFilepath(path string) error {
filepath, err := homedir.Expand(path)
if err != nil {
return err
}
self.UploadFilepath = filepath
return nil
}
func (self *Model) SetDownloadFilepath(path string) error {
filepath, err := homedir.Expand(path)
if err != nil {
return err
}
self.DownloadFilepath = filepath
return nil
}
func (self *Model) WriteDownloadFile() error {
err := os.WriteFile(self.DownloadFilepath, self.DownloadFileBuffer[0:self.DownloadBufLen], os.ModePerm)
if err != nil {
return err
}
//os.Chmod()
return nil
}
// reads the value of the upload filepath
func (self *Model) ReadUploadFile() error {
bytes, err := os.ReadFile(self.UploadFilepath)
if err != nil {
return err
}
self.UploadFileBuffer = MsdosEncode(string(bytes))//string(bytes)
return nil
}
// initalizes with proper defaults
func DefaultModel() Model {
return Model{
Config: SerialConfig{
Port: "",
Settings: serial.Mode{
BaudRate: 4800,
//BaudRate: 230400,
Parity: serial.EvenParity,
DataBits: 8,
StopBits: serial.OneStopBit,
},
},
UploadFilepath: "",
DownloadFilepath: "",
UploadFileBuffer: "",
DownloadFileBuffer: make([]byte, 100000),
DownloadBufLen: 0,
Port: nil,
}
}
// uploads the file in the model to the port in the model
func (m *Model) Upload() (int, error) {
// m.Port, err := serial.Open(m.Config.Port, &m.Config.Settings)
if m.Port == nil {
return 0, errors.New("no open port")
}
err := m.ReadUploadFile()
if err != nil {
return 0, err
}
return m.Port.Write([]byte(m.UploadFileBuffer))
}
func (m *Model) Listen(callback func()) {
go func() {
for {
if m.Port == nil {
//fmt.Println("port is nil")
//time.Sleep(1 * time.Second)
} else {
//t, _ := time.ParseDuration("500ms")
//_ = m.Port.SetReadTimeout(t)
//m.Port.Close()
//var err error
//m.Port, err = serial.Open(m.Config.Port, &m.Config.Settings)
//if err != nil {
//continue
//}
//fmt.Println(m.Config.Port)
n, err := m.Port.Read(m.DownloadFileBuffer)
//fmt.Print("read in lines: ")
//fmt.Println(n)
if err == nil && n > 0 {
m.DownloadBufLen += n
callback()
} else if err != nil {
fmt.Println(err)
}
}
}
}()
}
//func (m *Model) Save() (int, error) {
// // m.Port, err := serial.Open(m.Config.Port, &m.Config.Settings)
// if m.Port == nil {
// return 0, errors.New("no open port")
// }
//
// err := m.ReadUploadFile()
// if err != nil {
// return 0, err
// }
//
// return m.Port.Write([]byte(m.UploadFileBuffer))
//}