hap
hap copied to clipboard
Connection issues when using Chinese characters
package main
import (
"fmt"
"github.com/brutella/hap"
"github.com/brutella/hap/accessory"
"golang.org/x/text/secure/precis"
"golang.org/x/text/transform"
"golang.org/x/text/unicode/norm"
"strings"
"unicode"
"context"
"log"
"os"
"os/signal"
"syscall"
)
// RemoveAccentsFromString removes accent characters from string
// From https://stackoverflow.com/a/40405242/424814
func removeAccentsFromString(v string) string {
var loosecompare = precis.NewIdentifier(
precis.AdditionalMapping(func() transform.Transformer {
return transform.Chain(norm.NFD, transform.RemoveFunc(func(r rune) bool {
return unicode.Is(unicode.Mn, r)
}))
}),
precis.Norm(norm.NFC), // This is the default; be explicit though.
)
p, _ := loosecompare.String(v)
return p
}
func main() {
fmt.Println(removeAccentsFromString("智能开关"))
// Create the switch accessory.
a := accessory.NewSwitch(accessory.Info{
Name: removeAccentsFromString(strings.Replace("智能开关", " ", "", -1)),
SerialNumber: "974588889",
})
// Store the data in the "./db" directory.
fs := hap.NewFsStore("./db")
// Create the hap server.
server, err := hap.NewServer(fs, a.A)
if err != nil {
// stop if an error happens
log.Panic(err)
}
server.Pin = "88889999"
// Setup a listener for interrupts and SIGTERM signals
// to stop the server.
c := make(chan os.Signal)
signal.Notify(c, os.Interrupt)
signal.Notify(c, syscall.SIGTERM)
ctx, cancel := context.WithCancel(context.Background())
go func() {
<-c
// Stop delivering signals.
signal.Stop(c)
// Cancel the context to stop the server.
cancel()
}()
// Run the server.
server.ListenAndServe(ctx)
}
When attempting to connect in Chinese, there is an issue of inability to establish a connection. During pairing, a prompt indicating loss of connection is received. However, when using a non-Chinese language, the connection proceeds successfully, yet the device shows no response.