From 481870cea9602d0a8e39637035e6469ca07df050 Mon Sep 17 00:00:00 2001 From: w1ndyb0y Date: Mon, 21 Dec 2020 16:18:08 +0800 Subject: [PATCH] check telegram string --- cmd/start.go | 8 +++++--- utils/telegram.go | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 3 deletions(-) create mode 100644 utils/telegram.go diff --git a/cmd/start.go b/cmd/start.go index 7b55f6e..a57af46 100644 --- a/cmd/start.go +++ b/cmd/start.go @@ -55,8 +55,7 @@ func init() { // startCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") } - -func start(cmd *cobra.Command, args []string){ +func start(cmd *cobra.Command, args []string) { // Go signal notification works by sending `os.Signal` // values on a channel. We'll create a channel to // receive these notifications (we'll also make one to @@ -95,7 +94,10 @@ func start(cmd *cobra.Command, args []string){ } buffer, count := utils.ReadPort() if count > 0 { - log.Print(string(buffer)) + if lograw { + log.Print(string(buffer)) + } + utils.Append(buffer) } } <-done diff --git a/utils/telegram.go b/utils/telegram.go new file mode 100644 index 0000000..e08db3b --- /dev/null +++ b/utils/telegram.go @@ -0,0 +1,38 @@ +package utils + +import ( + "log" + "regexp" + "strings" +) + +const ( + StartTag = "ZCZC" + EndTag = "NNNN" + Expression = "(?s)ZCZC.*NNNN" +) + +var buffer []byte + +func Append(data []byte) { + buffer = append(buffer, data...) + if buffer[len(buffer)-1] == '\r' { + buffer = append(buffer, '\n') + } + check() +} + +func check() { + teleString := string(buffer) + if strings.Index(teleString, EndTag) > 0 { + exp, _ := regexp.Compile(Expression) + telegram := exp.FindString(string(buffer)) + + if len(telegram) > 0 { + log.Println("telegram : ", telegram) + buffer = nil + //TODO save telegram + } + } + +}