Skip to content

Commit

Permalink
新增battery指定udid、新增关机、修复设备不存在bug
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhouYixun committed Jul 18, 2022
1 parent 541c1f2 commit 5b419df
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 31 deletions.
70 changes: 45 additions & 25 deletions cmd/battery.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,41 +32,61 @@ var batteryCmd = &cobra.Command{
Short: "Show battery of your device.",
Long: "Show battery of your device.",
RunE: func(cmd *cobra.Command, args []string) error {
usbMuxClient, err := giDevice.NewUsbmux()
if err != nil {
return util.NewErrorPrint(util.ErrConnect, "usbMux", err)
}
list, err1 := usbMuxClient.Devices()
if err1 != nil {
return util.NewErrorPrint(util.ErrSendCommand, "listDevices", err1)
}
if len(list) != 0 {
var batteryList entity.BatteryList
for _, d := range list {
b := entity.Battery{}
bd, err := d.GetValue("com.apple.mobile.battery", "")
if err != nil {
continue
}
bi := entity.BatteryInter{}
mapstructure.Decode(bd, &bi)
b.SerialNumber = d.Properties().SerialNumber
b.Level = bi.BatteryCurrentCapacity
b.Temperature = 0
batteryList.BatteryInfo = append(batteryList.BatteryInfo, b)
if len(udid) != 0 {
device := util.GetDeviceByUdId(udid)
if device == nil {
os.Exit(0)
}
b := entity.Battery{}
bd, err := device.GetValue("com.apple.mobile.battery", "")
if err != nil {
return util.NewErrorPrint(util.ErrSendCommand, "get value", err)
}
data := util.ResultData(batteryList)
bi := entity.BatteryInter{}
mapstructure.Decode(bd, &bi)
b.SerialNumber = device.Properties().SerialNumber
b.Level = bi.BatteryCurrentCapacity
b.Temperature = 0
data := util.ResultData(b)
fmt.Println(util.Format(data, isFormat, isJson))
} else {
fmt.Println("no device connected")
os.Exit(0)
usbMuxClient, err := giDevice.NewUsbmux()
if err != nil {
return util.NewErrorPrint(util.ErrConnect, "usbMux", err)
}
list, err1 := usbMuxClient.Devices()
if err1 != nil {
return util.NewErrorPrint(util.ErrSendCommand, "listDevices", err1)
}
if len(list) != 0 {
var batteryList entity.BatteryList
for _, d := range list {
b := entity.Battery{}
bd, err := d.GetValue("com.apple.mobile.battery", "")
if err != nil {
continue
}
bi := entity.BatteryInter{}
mapstructure.Decode(bd, &bi)
b.SerialNumber = d.Properties().SerialNumber
b.Level = bi.BatteryCurrentCapacity
b.Temperature = 0
batteryList.BatteryInfo = append(batteryList.BatteryInfo, b)
}
data := util.ResultData(batteryList)
fmt.Println(util.Format(data, isFormat, isJson))
} else {
fmt.Println("no device connected")
os.Exit(0)
}
}
return nil
},
}

func init() {
rootCmd.AddCommand(batteryCmd)
batteryCmd.Flags().StringVarP(&udid, "udid", "u", "", "device's serialNumber")
batteryCmd.Flags().BoolVarP(&isJson, "json", "j", false, "convert to JSON string")
batteryCmd.Flags().BoolVarP(&isFormat, "format", "f", false, "convert to JSON string and format")
}
15 changes: 11 additions & 4 deletions cmd/reboot.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,19 @@ import (

var rebootCmd = &cobra.Command{
Use: "reboot",
Short: "Reboot device",
Long: "Reboot device",
Short: "Reboot or Shutdown device",
Long: "Reboot or Shutdown device",
RunE: func(cmd *cobra.Command, args []string) error {
device := util.GetDeviceByUdId(udid)
if device == nil {
os.Exit(0)
}
errReboot := device.Reboot()
var errReboot error
if isShutdown {
errReboot = device.Shutdown()
} else {
errReboot = device.Reboot()
}
if errReboot != nil {
fmt.Println("reboot failed")
os.Exit(0)
Expand All @@ -43,8 +48,10 @@ var rebootCmd = &cobra.Command{
},
}

var isShutdown bool

func init() {
rootCmd.AddCommand(rebootCmd)
rebootCmd.Flags().StringVarP(&udid, "udid", "u", "", "device's serialNumber")
rebootCmd.MarkFlagRequired("udid")
rebootCmd.Flags().BoolVarP(&isShutdown, "shutdown", "s", false, "shutdown your device")
}
2 changes: 1 addition & 1 deletion cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ var versionCmd = &cobra.Command{
Short: "Version code of sib",
Long: "Version code of sib",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("1.1.0")
fmt.Println("1.1.1")
},
}

Expand Down
16 changes: 16 additions & 0 deletions src/entity/batteryInfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,22 @@ type BatteryList struct {
BatteryInfo []Battery `json:"batteryList"`
}

func (battery Battery) ToString() string {
var s strings.Builder
s.WriteString(fmt.Sprintf("%s %d %d", battery.SerialNumber, battery.Level, battery.Temperature))
return s.String()
}

func (battery Battery) ToJson() string {
result, _ := json.Marshal(battery)
return string(result)
}

func (battery Battery) ToFormat() string {
result, _ := json.MarshalIndent(battery, "", "\t")
return string(result)
}

func (batteryList BatteryList) ToString() string {
var s strings.Builder
for i, e := range batteryList.BatteryInfo {
Expand Down
2 changes: 1 addition & 1 deletion src/util/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func GetDeviceByUdId(udId string) (device giDevice.Device) {
} else {
device = list[0]
}
if device.Properties().SerialNumber == "" {
if device == nil || device.Properties().SerialNumber == "" {
fmt.Println("device no found")
return nil
}
Expand Down

0 comments on commit 5b419df

Please sign in to comment.