forked from Elara6331/itd
		
	Re-add watch commands to itctl
This commit is contained in:
		| @@ -2,6 +2,8 @@ package main | ||||
|  | ||||
| import ( | ||||
| 	"os" | ||||
| 	"os/signal" | ||||
| 	"syscall" | ||||
|  | ||||
| 	"github.com/rs/zerolog" | ||||
| 	"github.com/rs/zerolog/log" | ||||
| @@ -174,6 +176,49 @@ func main() { | ||||
| 					}, | ||||
| 				}, | ||||
| 			}, | ||||
| 			{ | ||||
| 				Name:  "watch", | ||||
| 				Usage: "Watch a value for changes", | ||||
| 				Subcommands: []*cli.Command{ | ||||
| 					{ | ||||
| 						Flags: []cli.Flag{ | ||||
| 							&cli.BoolFlag{Name: "json"}, | ||||
| 							&cli.BoolFlag{Name: "shell"}, | ||||
| 						}, | ||||
| 						Name:   "heart", | ||||
| 						Usage:  "Watch heart rate value for changes", | ||||
| 						Action: watchHeart, | ||||
| 					}, | ||||
| 					{ | ||||
| 						Flags: []cli.Flag{ | ||||
| 							&cli.BoolFlag{Name: "json"}, | ||||
| 							&cli.BoolFlag{Name: "shell"}, | ||||
| 						}, | ||||
| 						Name:   "steps", | ||||
| 						Usage:  "Watch step count value for changes", | ||||
| 						Action: watchStepCount, | ||||
| 					}, | ||||
| 					{ | ||||
| 						Flags: []cli.Flag{ | ||||
| 							&cli.BoolFlag{Name: "json"}, | ||||
| 							&cli.BoolFlag{Name: "shell"}, | ||||
| 						}, | ||||
| 						Name:   "motion", | ||||
| 						Usage:  "Watch motion coordinates for changes", | ||||
| 						Action: watchMotion, | ||||
| 					}, | ||||
| 					{ | ||||
| 						Flags: []cli.Flag{ | ||||
| 							&cli.BoolFlag{Name: "json"}, | ||||
| 							&cli.BoolFlag{Name: "shell"}, | ||||
| 						}, | ||||
| 						Name:    "battery", | ||||
| 						Aliases: []string{"batt"}, | ||||
| 						Usage:   "Watch battery level value for changes", | ||||
| 						Action:  watchBattLevel, | ||||
| 					}, | ||||
| 				}, | ||||
| 			}, | ||||
| 		}, | ||||
| 		Before: func(c *cli.Context) error { | ||||
| 			newClient, err := api.New(c.String("socket-path")) | ||||
| @@ -196,3 +241,17 @@ func main() { | ||||
| 		log.Fatal().Err(err).Msg("Error while running app") | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func catchSignal(fn func()) { | ||||
| 	sigCh := make(chan os.Signal, 1) | ||||
| 	signal.Notify( | ||||
| 		sigCh, | ||||
| 		syscall.SIGINT, | ||||
| 		syscall.SIGTERM, | ||||
| 	) | ||||
| 	go func() { | ||||
| 		<-sigCh | ||||
| 		fn() | ||||
| 		os.Exit(0) | ||||
| 	}() | ||||
| } | ||||
|   | ||||
							
								
								
									
										104
									
								
								cmd/itctl/watch.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										104
									
								
								cmd/itctl/watch.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,104 @@ | ||||
| package main | ||||
|  | ||||
| import ( | ||||
| 	"encoding/json" | ||||
| 	"fmt" | ||||
| 	"os" | ||||
|  | ||||
| 	"github.com/urfave/cli/v2" | ||||
| ) | ||||
|  | ||||
| func watchHeart(c *cli.Context) error { | ||||
| 	heartCh, cancel, err := client.WatchHeartRate() | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	catchSignal(cancel) | ||||
|  | ||||
| 	for heartRate := range heartCh { | ||||
| 		if c.Bool("json") { | ||||
| 			json.NewEncoder(os.Stdout).Encode( | ||||
| 				map[string]uint8{"heartRate": heartRate}, | ||||
| 			) | ||||
| 		} else if c.Bool("shell") { | ||||
| 			fmt.Printf("HEART_RATE=%d\n", heartRate) | ||||
| 		} else { | ||||
| 			fmt.Println(heartRate, "BPM") | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| func watchBattLevel(c *cli.Context) error { | ||||
| 	battLevelCh, cancel, err := client.WatchBatteryLevel() | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	catchSignal(cancel) | ||||
|  | ||||
| 	for battLevel := range battLevelCh { | ||||
| 		if c.Bool("json") { | ||||
| 			json.NewEncoder(os.Stdout).Encode( | ||||
| 				map[string]uint8{"battLevel": battLevel}, | ||||
| 			) | ||||
| 		} else if c.Bool("shell") { | ||||
| 			fmt.Printf("BATTERY_LEVEL=%d\n", battLevel) | ||||
| 		} else { | ||||
| 			fmt.Printf("%d%%\n", battLevel) | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| func watchStepCount(c *cli.Context) error { | ||||
| 	stepCountCh, cancel, err := client.WatchStepCount() | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	catchSignal(cancel) | ||||
|  | ||||
| 	for stepCount := range stepCountCh { | ||||
| 		if c.Bool("json") { | ||||
| 			json.NewEncoder(os.Stdout).Encode( | ||||
| 				map[string]uint32{"stepCount": stepCount}, | ||||
| 			) | ||||
| 		} else if c.Bool("shell") { | ||||
| 			fmt.Printf("STEP_COUNT=%d\n", stepCount) | ||||
| 		} else { | ||||
| 			fmt.Println(stepCount, "Steps") | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| func watchMotion(c *cli.Context) error { | ||||
| 	motionCh, cancel, err := client.WatchMotion() | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	catchSignal(cancel) | ||||
|  | ||||
| 	for motionVals := range motionCh { | ||||
| 		if c.Bool("json") { | ||||
| 			json.NewEncoder(os.Stdout).Encode(motionVals) | ||||
| 		} else if c.Bool("shell") { | ||||
| 			fmt.Printf( | ||||
| 				"X=%d\nY=%d\nZ=%d\n", | ||||
| 				motionVals.X, | ||||
| 				motionVals.Y, | ||||
| 				motionVals.Z, | ||||
| 			) | ||||
| 		} else { | ||||
| 			fmt.Println(motionVals) | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	return nil | ||||
| } | ||||
		Reference in New Issue
	
	Block a user