Skip to content

Commit b3b097b

Browse files
committed
set device name
1 parent 4deabb5 commit b3b097b

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

api/http.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,9 @@ type pubkeyResp struct {
4747
}
4848

4949
type queryReq struct {
50-
DeviceID string `json:"deviceID" binding:"required"`
51-
Signature string `json:"signature,omitempty" binding:"required"`
50+
DeviceID string `json:"deviceID" binding:"required"`
51+
DeviceIMEI string `json:"deviceIMEI,omitempty"`
52+
Signature string `json:"signature,omitempty" binding:"required"`
5253
}
5354

5455
type queryResp struct {
@@ -141,6 +142,14 @@ func (s *httpServer) query(c *gin.Context) {
141142
return
142143
}
143144

145+
if req.DeviceIMEI != "" {
146+
if err := s.handleName(strings.ToLower(req.DeviceID), req.DeviceIMEI); err != nil {
147+
slog.Error("failed to update device name", "device_id", req.DeviceID)
148+
c.JSON(http.StatusBadRequest, newErrResp(errors.Wrap(err, "failed to update device name")))
149+
return
150+
}
151+
}
152+
144153
metrics.TrackDeviceCount(req.DeviceID)
145154
metrics.TrackRequestCount("get")
146155
now := time.Now()
@@ -495,6 +504,14 @@ func (s *httpServer) handle(id string, pkg *proto.BinPackage, data goproto.Messa
495504
return errors.Wrapf(err, "failed to handle %T", data)
496505
}
497506

507+
func (s *httpServer) handleName(id, name string) error {
508+
err := s.db.UpdateByID(id, map[string]any{
509+
"name": name,
510+
"updated_at": time.Now(),
511+
})
512+
return errors.Wrapf(err, "failed to update device name: %s", id)
513+
}
514+
498515
func (s *httpServer) handleConfig(id string, data *proto.SensorConfig) error {
499516
err := s.db.UpdateByID(id, map[string]any{
500517
"bulk_upload": int32(data.GetBulkUpload()),

0 commit comments

Comments
 (0)