|
|
@@ -59,14 +59,15 @@ func main() {
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
coupler = &MQTTCoupler{
|
|
|
- ctx: ctx,
|
|
|
- cancel: cancel,
|
|
|
- broker: CfgServers.MQTTSrv.Address,
|
|
|
- username: CfgServers.MQTTSrv.Username,
|
|
|
- password: CfgServers.MQTTSrv.Password,
|
|
|
- clientID: uuid.New().String(),
|
|
|
- imei: devIMEI,
|
|
|
- cwd: "/",
|
|
|
+ ctx: ctx,
|
|
|
+ cancel: cancel,
|
|
|
+ broker: CfgServers.MQTTSrv.Address,
|
|
|
+ username: CfgServers.MQTTSrv.Username,
|
|
|
+ password: CfgServers.MQTTSrv.Password,
|
|
|
+ clientID: uuid.New().String(),
|
|
|
+ imei: devIMEI,
|
|
|
+ cwd: "/",
|
|
|
+ interrupted: make(chan struct{}, 1),
|
|
|
}
|
|
|
|
|
|
id, err := machineid.ID()
|
|
|
@@ -96,7 +97,7 @@ func term(pingState *atomic.Bool) {
|
|
|
var executing atomic.Bool // 是否有正在执行中的命令
|
|
|
var interrupted atomic.Bool // 用户是否按键取消了命令
|
|
|
|
|
|
- interruptLoop(&executing, &interrupted) // Ctrl+C卍
|
|
|
+ interruptLoop(pingState, &executing, &interrupted) // Ctrl+C卍
|
|
|
|
|
|
printWelcome(pingState)
|
|
|
|
|
|
@@ -224,13 +225,17 @@ func help() {
|
|
|
fmt.Println(h)
|
|
|
}
|
|
|
|
|
|
-func interruptLoop(executing *atomic.Bool, interrupted *atomic.Bool) {
|
|
|
+func interruptLoop(pingState, executing, interrupted *atomic.Bool) {
|
|
|
sigCh := make(chan os.Signal, 1)
|
|
|
signal.Notify(sigCh, syscall.SIGINT)
|
|
|
go func() {
|
|
|
for range sigCh {
|
|
|
interrupted.Store(true)
|
|
|
- if executing.Load() {
|
|
|
+ if executing.Load() && pingState.Load() {
|
|
|
+ select {
|
|
|
+ case coupler.interrupted <- struct{}{}:
|
|
|
+ default:
|
|
|
+ }
|
|
|
_, _ = coupler.stop()
|
|
|
}
|
|
|
}
|