Browse Source

增加查询设备是否在线接口

liuxiulin 2 years ago
parent
commit
08e95974b1
2 changed files with 27 additions and 0 deletions
  1. 26 0
      services/apiprovider/actions.go
  2. 1 0
      services/apiprovider/router.go

+ 26 - 0
services/apiprovider/actions.go

@@ -337,3 +337,29 @@ func CheckDeviceNetConfig(req *http.Request, r render.Render) {
 		Result: reply.Result,
 	})
 }
+
+func CheckDeviceIsOnline(req *http.Request, r render.Render) {
+
+	identifier := req.URL.Query().Get("device_code")
+	device := &models.Device{}
+	err := server.RPCCallByName(nil, rpcs.RegistryServerName, "Registry.FindDeviceByIdentifier", identifier, device)
+	if err != nil {
+		r.JSON(http.StatusOK, renderError(ErrDeviceNotFound, err))
+		return
+	}
+
+	onlineargs := rpcs.ArgsGetDeviceOnlineStatus{
+		Id: device.DeviceIdentifier,
+	}
+	onlinereply := rpcs.ReplyGetDeviceOnlineStatus{}
+
+	err = server.RPCCallByName(nil, rpcs.DeviceManagerName, "DeviceManager.GetDeviceOnlineStatus", onlineargs, &onlinereply)
+	if err != nil || onlinereply.ClientIP == "" {
+		r.JSON(http.StatusOK, Common{
+			Result: 2,
+		})
+	}
+	r.JSON(http.StatusOK, Common{
+		Result: 1,
+	})
+}

+ 1 - 0
services/apiprovider/router.go

@@ -63,6 +63,7 @@ func route(m *martini.ClassicMartini) {
 			AddRule)
 		r.Get("/devices/check_net_config", CheckDeviceNetConfig)
 
+		r.Get("/device/online", CheckDeviceIsOnline)
 	}, ValidateTokenMiddleware)
 
 	m.Post("/application/auth", AppAuth)