|
| 1 | +package picooc |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "errors" |
| 6 | + "net/http" |
| 7 | + "strconv" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/AlexxIT/SmartScaleConnect/pkg/core" |
| 11 | +) |
| 12 | + |
| 13 | +type Client struct { |
| 14 | + client *http.Client |
| 15 | + |
| 16 | + deviceID string |
| 17 | + userID string |
| 18 | + |
| 19 | + roleIDs map[string]string |
| 20 | +} |
| 21 | + |
| 22 | +func NewClient() *Client { |
| 23 | + return &Client{ |
| 24 | + client: &http.Client{Timeout: time.Minute}, |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +func (c *Client) GetAllWeights() ([]*core.Weight, error) { |
| 29 | + return c.GetFilterWeights("") |
| 30 | +} |
| 31 | + |
| 32 | +func (c *Client) GetFilterWeights(name string) ([]*core.Weight, error) { |
| 33 | + roleID, ok := c.roleIDs[name] |
| 34 | + if !ok { |
| 35 | + return nil, errors.New("picooc: unknown user: " + name) |
| 36 | + } |
| 37 | + |
| 38 | + var weights []*core.Weight |
| 39 | + |
| 40 | + params := c.values("bodyIndexList") |
| 41 | + //params.Set("orderType", "-1") |
| 42 | + params.Set("pageSize", "1000") |
| 43 | + params.Set("time", params.Get("timestamp")) |
| 44 | + params.Set("userId", c.userID) |
| 45 | + params.Set("roleId", roleID) |
| 46 | + |
| 47 | + for { |
| 48 | + res, err := c.client.Get(api + "bodyIndex/bodyIndexList?" + params.Encode()) |
| 49 | + if err != nil { |
| 50 | + return nil, err |
| 51 | + } |
| 52 | + defer res.Body.Close() |
| 53 | + |
| 54 | + var res1 struct { |
| 55 | + //Code int `json:"code"` |
| 56 | + //Msg string `json:"msg"` |
| 57 | + //Method string `json:"method"` |
| 58 | + //Result struct { |
| 59 | + // Code int `json:"code"` |
| 60 | + // Message string `json:"message"` |
| 61 | + //} `json:"result"` |
| 62 | + Resp struct { |
| 63 | + Records []struct { |
| 64 | + BodyTime int64 `json:"bodyTime"` |
| 65 | + DataType int `json:"dataType"` |
| 66 | + BodyIndexId int `json:"body_index_id"` |
| 67 | + RoleId int `json:"role_id"` |
| 68 | + BodyFat float32 `json:"body_fat"` |
| 69 | + Weight float32 `json:"weight"` |
| 70 | + BMI float32 `json:"bmi"` |
| 71 | + VisceralFatLevel int `json:"visceral_fat_level"` |
| 72 | + MuscleRace float32 `json:"muscle_race"` |
| 73 | + BodyAge int `json:"body_age"` |
| 74 | + BoneMass float32 `json:"bone_mass"` |
| 75 | + BasicMetabolism int `json:"basic_metabolism"` |
| 76 | + WaterRace float32 `json:"water_race"` |
| 77 | + SkeletalMuscle float32 `json:"skeletal_muscle"` |
| 78 | + LocalTime int `json:"local_time"` |
| 79 | + SubcutaneousFat float32 `json:"subcutaneous_fat"` |
| 80 | + ServerTime int `json:"server_time"` |
| 81 | + ServerId int `json:"server_id"` |
| 82 | + IsDel int `json:"is_del"` |
| 83 | + Abnormal struct { |
| 84 | + Weight int `json:"weight"` |
| 85 | + Time int `json:"time"` |
| 86 | + AbnormalFlag int `json:"abnormal_flag"` |
| 87 | + BodyFat int `json:"body_fat"` |
| 88 | + } `json:"abnormal"` |
| 89 | + AbnormalFlag int `json:"abnormal_flag"` |
| 90 | + ElectricResistance int `json:"electric_resistance"` |
| 91 | + IsManuallyAdd int `json:"is_manually_add"` |
| 92 | + IsFirstDay int `json:"is_first_day"` |
| 93 | + LandmarkIcons []string `json:"landmarkIcons"` |
| 94 | + MAC string `json:"mac"` |
| 95 | + AnchorWeight int `json:"anchor_weight"` |
| 96 | + AnchorBata int `json:"anchor_bata"` |
| 97 | + CorrectionValueR int `json:"correction_value_r"` |
| 98 | + BodyFatReferenceValue float32 `json:"body_fat_reference_value"` |
| 99 | + LabelMarker int `json:"label_marker"` |
| 100 | + DataSources int `json:"data_sources"` |
| 101 | + } `json:"records"` |
| 102 | + LastTime int `json:"lastTime"` |
| 103 | + Continue bool `json:"continue"` |
| 104 | + } `json:"resp"` |
| 105 | + } |
| 106 | + |
| 107 | + if err = json.NewDecoder(res.Body).Decode(&res1); err != nil { |
| 108 | + return nil, err |
| 109 | + } |
| 110 | + |
| 111 | + for _, v1 := range res1.Resp.Records { |
| 112 | + if v1.AbnormalFlag != 0 || v1.IsDel != 0 { |
| 113 | + continue |
| 114 | + } |
| 115 | + |
| 116 | + w := &core.Weight{ |
| 117 | + Date: time.Unix(v1.BodyTime, 0), |
| 118 | + Weight: v1.Weight, |
| 119 | + |
| 120 | + BMI: v1.BMI, |
| 121 | + BodyFat: v1.BodyFat, |
| 122 | + BodyWater: v1.WaterRace, |
| 123 | + BoneMass: v1.BoneMass, |
| 124 | + |
| 125 | + MetabolicAge: v1.BodyAge, // 0 |
| 126 | + VisceralFat: v1.VisceralFatLevel, |
| 127 | + |
| 128 | + BasalMetabolism: v1.BasicMetabolism, |
| 129 | + SkeletalMuscleMass: v1.SkeletalMuscle, // 0 |
| 130 | + |
| 131 | + User: name, |
| 132 | + Source: v1.MAC, |
| 133 | + } |
| 134 | + weights = append(weights, w) |
| 135 | + } |
| 136 | + |
| 137 | + if !res1.Resp.Continue { |
| 138 | + break |
| 139 | + } |
| 140 | + |
| 141 | + params.Set("time", strconv.Itoa(res1.Resp.LastTime)) |
| 142 | + } |
| 143 | + |
| 144 | + return weights, nil |
| 145 | +} |
0 commit comments