Pārlūkot izejas kodu

迭代优化代码

niujiuru 1 mēnesi atpakaļ
vecāks
revīzija
ff69e3e492
3 mainītis faili ar 47 papildinājumiem un 4 dzēšanām
  1. 13 4
      networkd.service/main.go
  2. 22 0
      networkd.service/reademe.txt
  3. 12 0
      servicelib/util.go

+ 13 - 4
networkd.service/main.go

@@ -21,8 +21,9 @@ import (
 )
 
 var (
-	Version   = "0.0.0.1"
-	BuildTime = ""
+	Version    = "0.0.0.1"
+	BuildTime  = ""
+	ListenPort = "7000"
 )
 
 type program struct {
@@ -76,7 +77,7 @@ func (p *program) Start(s service.Service) error {
 	mux.Handle("/rpc", p.core_server)
 
 	p.http_server = &http.Server{
-		Addr:    "127.0.0.1:7000",
+		Addr:    "127.0.0.1:" + ListenPort,
 		Handler: mux,
 	}
 
@@ -202,7 +203,7 @@ func main() {
 	}
 
 	prg := &program{
-		name: svcConfig.Name,
+		name: "NetworkManager",
 	}
 
 	svc, err := service.New(prg, svcConfig)
@@ -220,6 +221,14 @@ func main() {
 		os.Exit(0)
 	}
 
+	if servicelib.IsPortInUse(ListenPort) { // 查询端口占用,避免运行多份实例
+		fmt.Printf(
+			"%s.service start failed: listen port %s already in use\n",
+			svcConfig.Name, ListenPort,
+		)
+		os.Exit(1)
+	}
+
 	if err := svc.Run(); err != nil {
 		fmt.Printf("%s.service run failed: %v\n",
 			svcConfig.Name, err)

+ 22 - 0
networkd.service/reademe.txt

@@ -0,0 +1,22 @@
+接口测试:
+
+// ping联通测试
+curl -s -X POST http://127.0.0.1:7000/rpc -H "Content-Type: application/json" -d '{"jsonrpc":"2.0","method":"ping","params":{},"id":1}'
+
+// 获取软件版本
+curl -s -X POST http://127.0.0.1:7000/rpc -H "Content-Type: application/json" -d '{"jsonrpc":"2.0","method":"basic.getBuildVer","params":{},"id":2}'
+
+// 获取日志级别
+curl -s -X POST http://127.0.0.1:7000/rpc -H "Content-Type: application/json" -d '{"jsonrpc":"2.0","method":"basic.getLogLevel","params":{},"id":3}'
+
+// 设置日志级别
+curl -s -X POST http://127.0.0.1:7000/rpc -H "Content-Type: application/json" -d '{"jsonrpc":"2.0","method":"basic.setLogLevel","params":{"log_level":"trace"},"id":4}'
+
+// 保存日志设置
+curl -s -X POST http://127.0.0.1:7000/rpc -H "Content-Type: application/json" -d '{"jsonrpc":"2.0","method":"basic.saveLogConf","params":{},"id":5}'
+
+// 获取4G模组的IMEI、ICCID、RSSI等信息
+curl -s -X POST http://127.0.0.1:7000/rpc -H "Content-Type: application/json" -d '{"jsonrpc":"2.0","method":"core.getModemInfo","params":{},"id":6}'
+
+// 获取联网状态信息
+curl -s -X POST http://127.0.0.1:7000/rpc -H "Content-Type: application/json" -d '{"jsonrpc":"2.0","method":"core.getNetStatus","params":{},"id":7}'

+ 12 - 0
servicelib/util.go

@@ -0,0 +1,12 @@
+package servicelib
+
+import "net"
+
+func IsPortInUse(port string) bool {
+	ln, err := net.Listen("tcp", ":"+port)
+	if err != nil {
+		return true
+	}
+	ln.Close()
+	return false
+}