Skip to content

Commit

Permalink
add query version api
Browse files Browse the repository at this point in the history
  • Loading branch information
rwby-ovo committed Sep 8, 2022
1 parent bf2cdff commit 59984a6
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
27 changes: 26 additions & 1 deletion api.go
Original file line number Diff line number Diff line change
Expand Up @@ -876,7 +876,7 @@ func apiStart(br *broker) {
})
})

// 设备 action=bind|unbind|reboot devid=设备id
// 设备 action=bind|unbind|reboot|version devid=设备id
r.GET("/hi/device/:action/:devid", func(c *gin.Context) {
action := c.Param("action")
devid := c.Param("devid")
Expand Down Expand Up @@ -959,6 +959,31 @@ func apiStart(br *broker) {
})
}
return
} else if action == "version" {
content, err := ioutil.ReadAll(c.Request.Body)
if err != nil {
c.Status(http.StatusBadRequest)
return
}
name := jsoniter.Get(content, "name").ToString()
call_url := jsoniter.Get(content, "call_url").ToString()
onlyid := devidGetOnlyid(br, devid)
if len(onlyid) == 0 {
c.JSON(http.StatusOK, gin.H{
"ret": 0,
"msg": "设备不在线",
"data": nil,
})
} else {
c.JSON(http.StatusOK, gin.H{
"ret": 1,
"msg": "success",
"data": gin.H{
"token": hiVersionDevice(br, devid, name, call_url),
},
})
}
return
}

c.JSON(http.StatusOK, gin.H{
Expand Down
19 changes: 19 additions & 0 deletions hi.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,25 @@ func hiRebootDevice(br *broker, devid string) string {
return hiExecBefore(br, db, devid, "#!/bin/sh\nreboot", "")
}

// 版本信息
func hiVersionDevice(br *broker, devid string, name string, call_url string) string {
if len(br.cfg.HiApiUrl) == 0 {
log.Info().Msgf("api url is empty")
return ""
}
db, err := hi.InstanceDB(br.cfg.DB)
if err != nil {
return ""
}
var cmds string
if name == "firmware" {
cmds = "[ -e '/etc/glversion' ] && {\ncat /etc/glversion ; exit 0\n}\ncat /etc/openwrt_release|grep DISTRIB_RELEASE |awk -F'=' '{print $2}'"
} else {
cmds = fmt.Sprintf("opkg info %s |grep 'Version' |awk '{print $2=$2}'", name)
}
return hiExecBefore(br, db, devid, fmt.Sprintf("#!/bin/sh\n%s", cmds), call_url)
}

// 固件升级
func hiDeviceFirmwareUpgrade(br *broker, devid string, path string, callback string) string {
if len(br.cfg.HiApiUrl) == 0 {
Expand Down

0 comments on commit 59984a6

Please sign in to comment.