-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
76 lines (64 loc) · 1.6 KB
/
main.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
package main
import (
"apicall/call"
"fmt"
"github.com/gin-gonic/gin"
"gopkg.in/ini.v1"
"net/http"
"os"
)
// PhoneChan 电话队列长度设置为2000
var PhoneChan chan string = make(chan string, 2000)
func main() {
//读取配置文件
cfg, err := ini.Load("config.ini")
if err != nil {
fmt.Printf("Fail to read file: %v", err)
os.Exit(1)
}
// 初始化串口
serialName := cfg.Section("").Key("serial").String() // /dev/ttyS1
serialBaud, _ := cfg.Section("").Key("baud").Int()
call.Init(serialName, serialBaud)
defer call.Modem_Port.Close()
//开启打电话go协程
go goCall()
//初始化http服务
httpPort := cfg.Section("").Key("http").String()
router := gin.Default()
router.GET("/call", handlerCall)
router.GET("/clean", handlerClean)
router.Run(":" + httpPort)
}
func goCall() {
for v := range PhoneChan {
call.Make_call(v)
//fmt.Printf("call=%s\n", v)
//time.Sleep(2 * time.Second)
}
}
func handlerClean(c *gin.Context) {
var phones string
var countPhones string
count := len(PhoneChan)
for len(PhoneChan) > 0 {
phones = <-PhoneChan
countPhones = countPhones + phones + ","
}
c.JSON(http.StatusOK, gin.H{"message": "clean=" + fmt.Sprintf("%d", count) + "|" + countPhones})
}
// 匹配的url格式: /call?phone=138138138138&key=iamops
func handlerCall(c *gin.Context) {
phone := c.Query("phone")
key := c.Query("key") // 是 c.Request.URL.Query().Get("lastname") 的简写
if key != "iamops" {
c.JSON(http.StatusForbidden, gin.H{
"message": "Failed",
})
} else {
c.JSON(http.StatusOK, gin.H{
"message": "OK," + phone + key,
})
PhoneChan <- phone
}
}