|
|
@@ -0,0 +1,223 @@
|
|
|
+package main
|
|
|
+
|
|
|
+import (
|
|
|
+ "context"
|
|
|
+ "encoding/json"
|
|
|
+ "errors"
|
|
|
+ "fmt"
|
|
|
+ "net/http"
|
|
|
+ "os"
|
|
|
+ "strconv"
|
|
|
+ "time"
|
|
|
+
|
|
|
+ "github.com/kardianos/service"
|
|
|
+ "hnyfkj.com.cn/rtu/linux/baseapp"
|
|
|
+ netmgrd "hnyfkj.com.cn/rtu/linux/netmgrd"
|
|
|
+ "hnyfkj.com.cn/rtu/linux/utils/jsonrpc2"
|
|
|
+)
|
|
|
+
|
|
|
+var (
|
|
|
+ Version = "0.0.0.1"
|
|
|
+ BuildTime = ""
|
|
|
+)
|
|
|
+
|
|
|
+type program struct {
|
|
|
+ name string
|
|
|
+ core_server *jsonrpc2.RPCServer
|
|
|
+ rpc_methods jsonrpc2.MethodMap
|
|
|
+ http_server *http.Server
|
|
|
+}
|
|
|
+
|
|
|
+// Start should not block. Do the actual work async.
|
|
|
+func (p *program) Start(s service.Service) error {
|
|
|
+ baseapp.InitPath()
|
|
|
+ if err := EnsureDefaultConfig(); err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ baseapp.InitLogger("")
|
|
|
+ baseapp.Logger.Infof("App Version: %s, Build Time: %s", Version, BuildTime)
|
|
|
+
|
|
|
+ if !netmgrd.ModemInit() {
|
|
|
+ return errors.New("modem init failed")
|
|
|
+ }
|
|
|
+
|
|
|
+ netmgrd.SetTimeSyncEnabled(false) // 关闭时间同步, 由"timesyncd.service"完成
|
|
|
+ netmgrd.ModuleInit()
|
|
|
+
|
|
|
+ var err error
|
|
|
+ p.core_server, err = jsonrpc2.NewRPCServer(p.name, baseapp.Logger)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ p.rpc_methods = make(jsonrpc2.MethodMap)
|
|
|
+
|
|
|
+ //////////////////////////////////////////////////////////////////
|
|
|
+ p.rpc_methods["basic.getBuildVer"] = p.getBuildVer /// 获取软件版本
|
|
|
+ p.rpc_methods["basic.getLogLevel"] = p.getLogLevel /// 获取日志级别
|
|
|
+ p.rpc_methods["basic.setLogLevel"] = p.setLogLevel /// 设置日志级别
|
|
|
+ p.rpc_methods["basic.saveLogConf"] = p.saveLogConf /// 保存日志设置
|
|
|
+ //////////////////////////////////////////////////////////////////
|
|
|
+ p.rpc_methods["core.getModemInfo"] = p.getModemInfo // IMEI、ICCID
|
|
|
+ p.rpc_methods["core.getNetStatus"] = p.getNetStatus // 联网状态信息
|
|
|
+ //////////////////////////////////////////////////////////////////
|
|
|
+
|
|
|
+ err = p.core_server.RegisterMethods(p.rpc_methods)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ mux := http.NewServeMux()
|
|
|
+ mux.Handle("/rpc", p.core_server)
|
|
|
+
|
|
|
+ p.http_server = &http.Server{
|
|
|
+ Addr: "127.0.0.1:7000",
|
|
|
+ Handler: mux,
|
|
|
+ }
|
|
|
+
|
|
|
+ go p.run()
|
|
|
+
|
|
|
+ return nil
|
|
|
+}
|
|
|
+
|
|
|
+// Do work here
|
|
|
+func (p *program) run() {
|
|
|
+ err := p.http_server.ListenAndServe() // 同步阻塞
|
|
|
+ if err != nil && !errors.Is(err, http.ErrServerClosed) {
|
|
|
+ baseapp.Logger.Errorf("[%s] start http server error: %v", p.name, err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+// Stop should not block. Return with a few seconds.
|
|
|
+func (p *program) Stop(s service.Service) error {
|
|
|
+ if p.core_server != nil {
|
|
|
+ p.core_server.Stop()
|
|
|
+ }
|
|
|
+
|
|
|
+ if p.http_server != nil {
|
|
|
+ ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
|
+ defer cancel()
|
|
|
+
|
|
|
+ _ = p.http_server.Shutdown(ctx)
|
|
|
+ }
|
|
|
+
|
|
|
+ netmgrd.ModemExit()
|
|
|
+ baseapp.ExitLogger()
|
|
|
+ return nil
|
|
|
+}
|
|
|
+
|
|
|
+func (p *program) getBuildVer(ctx context.Context, req *jsonrpc2.Request) *jsonrpc2.Response {
|
|
|
+ result := map[string]string{
|
|
|
+ "version": Version,
|
|
|
+ "build_time": BuildTime,
|
|
|
+ }
|
|
|
+ return jsonrpc2.BuildResponse(req, result, nil)
|
|
|
+}
|
|
|
+
|
|
|
+func (p *program) getLogLevel(ctx context.Context, req *jsonrpc2.Request) *jsonrpc2.Response {
|
|
|
+ level, _ := baseapp.GetLogLevel()
|
|
|
+ result := map[string]string{
|
|
|
+ "log_level": level,
|
|
|
+ }
|
|
|
+ return jsonrpc2.BuildResponse(req, result, nil)
|
|
|
+}
|
|
|
+
|
|
|
+func (p *program) setLogLevel(ctx context.Context, req *jsonrpc2.Request) *jsonrpc2.Response {
|
|
|
+ var params map[string]string
|
|
|
+ if err := json.Unmarshal(req.Params, ¶ms); err != nil {
|
|
|
+ return jsonrpc2.BuildError(req, jsonrpc2.ErrInvalidParams, err.Error())
|
|
|
+ }
|
|
|
+
|
|
|
+ level, ok := params["log_level"]
|
|
|
+ if !ok || level == "" {
|
|
|
+ return jsonrpc2.BuildError(req, jsonrpc2.ErrInvalidParams, "missing or empty 'log_level' parameter")
|
|
|
+ }
|
|
|
+
|
|
|
+ err := baseapp.UpdateLogLevel(level)
|
|
|
+ if err != nil {
|
|
|
+ return jsonrpc2.BuildError(req, jsonrpc2.ErrInternal, err.Error())
|
|
|
+ }
|
|
|
+
|
|
|
+ return jsonrpc2.BuildResponse(req, "success", nil)
|
|
|
+}
|
|
|
+
|
|
|
+func (p *program) saveLogConf(ctx context.Context, req *jsonrpc2.Request) *jsonrpc2.Response {
|
|
|
+ err := baseapp.SaveLogConfig()
|
|
|
+ if err != nil {
|
|
|
+ return jsonrpc2.BuildError(req, jsonrpc2.ErrInternal, err.Error())
|
|
|
+ }
|
|
|
+
|
|
|
+ return jsonrpc2.BuildResponse(req, "success", nil)
|
|
|
+}
|
|
|
+
|
|
|
+func (p *program) getModemInfo(ctx context.Context, req *jsonrpc2.Request) *jsonrpc2.Response {
|
|
|
+ result := map[string]string{
|
|
|
+ "imei": netmgrd.GetIMEI(),
|
|
|
+ "iccid": netmgrd.GetSimICCID(),
|
|
|
+ "rssi": netmgrd.GetRSSI(),
|
|
|
+ }
|
|
|
+ return jsonrpc2.BuildResponse(req, result, nil)
|
|
|
+}
|
|
|
+
|
|
|
+func (p *program) getNetStatus(ctx context.Context, req *jsonrpc2.Request) *jsonrpc2.Response {
|
|
|
+ result := map[string]string{
|
|
|
+ "net_available": strconv.FormatBool(netmgrd.IsInetAvailable()),
|
|
|
+ "net_type": netmgrd.GetCurrentNetType().String(),
|
|
|
+ }
|
|
|
+ return jsonrpc2.BuildResponse(req, result, nil)
|
|
|
+}
|
|
|
+
|
|
|
+func main() {
|
|
|
+ if baseapp.IsArgsParam("-h") {
|
|
|
+ help()
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ if baseapp.IsArgsParam("-v") {
|
|
|
+ fmt.Printf("Version: %s, Build Time: %s\n", Version, BuildTime)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ svcConfig := &service.Config{
|
|
|
+ Name: "yfkj-networkd",
|
|
|
+ DisplayName: "yfkj-networkd",
|
|
|
+ Description: "Network connectivity management",
|
|
|
+ Option: map[string]any{
|
|
|
+ "Restart": "always", // 无条件重启
|
|
|
+ },
|
|
|
+ }
|
|
|
+
|
|
|
+ if baseapp.IsArgsParam("-n") {
|
|
|
+ fmt.Println(svcConfig.Name + ".service")
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ prg := &program{
|
|
|
+ name: svcConfig.Name,
|
|
|
+ }
|
|
|
+
|
|
|
+ svc, err := service.New(prg, svcConfig)
|
|
|
+ if err != nil {
|
|
|
+ fmt.Printf("%s.service creation failed: %v\n",
|
|
|
+ svcConfig.Name, err)
|
|
|
+ os.Exit(1)
|
|
|
+ }
|
|
|
+
|
|
|
+ if err := svc.Run(); err != nil {
|
|
|
+ fmt.Printf("%s.service run failed: %v\n",
|
|
|
+ svcConfig.Name, err)
|
|
|
+ os.Exit(1)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func help() {
|
|
|
+ h := `
|
|
|
+-h 显示帮助提示
|
|
|
+-n 显示服务名称
|
|
|
+-v 当前程序版本
|
|
|
+`
|
|
|
+
|
|
|
+ fmt.Println(h)
|
|
|
+}
|