-
Notifications
You must be signed in to change notification settings - Fork 0
/
Controller.go
108 lines (98 loc) · 2.7 KB
/
Controller.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
package main
import (
"encoding/json"
"fmt"
"github.com/labstack/echo"
"net/http"
"os"
"time"
)
// 创建打印任务的RESTFul控制器
// 方法:POST
// Body:JSON->{HTML:··}
func CreateMission(c echo.Context) (err error) {
// 将body中的HTML文本解析到对象中
uRequest := new(CreateItem)
if err = c.Bind(uRequest); err != nil {
return
}
fmt.Println("origin HTML TEXT:", uRequest.HTML)
// 翻译HTML文本
res := Translation(uRequest.HTML)
// 创建打印元对象
var item PrintItem
// 遍历HTML翻译文本并加入到打印任务元中
for _, val := range res {
for _, v := range ToPrintData(val) {
item.LineNum += 1
item.Line = append(item.Line, v)
}
}
fmt.Println("打印行数:", item.LineNum)
fmt.Println("打印Item:")
for _, val := range item.Line {
fmt.Println(val)
}
// 打印对象
m := mission{
progress: 0,
item: &item,
}
// 检查串口连接
if conn == false {
fmt.Println("系统无法找到打印机!请确认打印机连接状态后重新启动客户端!")
return json.NewEncoder(c.Response()).Encode(struct {
Code string `json:"code"`
ResponseMessage string `json:"message"`
ResponsePayload interface{} `json:"payload,omitempty"`
}{
Code: "NotFound",
ResponseMessage: "创建新打印任务失败!系统找不到打印机设备,请重新确认打印机连接状态后重启客户端!",
})
}
// 异步启动打印任务
go m.On()
// 创建标准HTTP Response
c.Response().WriteHeader(http.StatusOK)
return json.NewEncoder(c.Response()).Encode(struct {
Code string `json:"code"`
ResponseMessage string `json:"message"`
ResponsePayload interface{} `json:"payload,omitempty"`
}{
Code: "OK",
ResponseMessage: "创建新打印任务成功!",
})
}
func PingController(c echo.Context) (err error) {
c.Response().WriteHeader(http.StatusOK)
printConn := "Stop"
if conn == true {
printConn = "OK"
}
return json.NewEncoder(c.Response()).Encode(struct {
Code string `json:"code"`
ResponseMessage string `json:"message"`
PrintConnected string `json:"printConnected"`
Version string `json:"version"`
}{
Code: "OK",
ResponseMessage: "OK",
PrintConnected: printConn,
Version: Version,
})
}
func ExitSystemController(c echo.Context) (err error) {
go func() {
time.Sleep(time.Second * 3)
os.Exit(0)
}()
c.Response().WriteHeader(http.StatusOK)
return json.NewEncoder(c.Response()).Encode(struct {
Code string `json:"code"`
ResponseMessage string `json:"message"`
PrintConnected string `json:"printConnected"`
}{
Code: "OK",
ResponseMessage: "退出成功!",
})
}