104 lines
2.7 KiB
Go
104 lines
2.7 KiB
Go
/*
|
|
Copyright © 2020 NAME HERE <EMAIL ADDRESS>
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
package cmd
|
|
|
|
import (
|
|
"os"
|
|
"os/signal"
|
|
"syscall"
|
|
"fmt"
|
|
"it2000.com.cn/tele-recv/utils"
|
|
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
// startCmd represents the start command
|
|
var startCmd = &cobra.Command{
|
|
Use: "start",
|
|
Short: "start telegram receive service",
|
|
Long: `open serial port
|
|
start a tcp server for processing`,
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
start()
|
|
},
|
|
}
|
|
|
|
func init() {
|
|
rootCmd.AddCommand(startCmd)
|
|
|
|
// Here you will define your flags and configuration settings.
|
|
|
|
// Cobra supports Persistent Flags which will work for this command
|
|
// and all subcommands, e.g.:
|
|
// startCmd.PersistentFlags().String("foo", "", "A help for foo")
|
|
|
|
// Cobra supports local flags which will only run when this command
|
|
// is called directly, e.g.:
|
|
// startCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
|
}
|
|
|
|
func start() {
|
|
// 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
|
|
// notify us when the program can exit).
|
|
sigs := make(chan os.Signal, 1)
|
|
done := make(chan bool, 1)
|
|
|
|
// `signal.Notify` registers the given channel to
|
|
// receive notifications of the specified signals.
|
|
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
|
|
utils.ServerRunning = true
|
|
|
|
// This goroutine executes a blocking receive for
|
|
// signals. When it gets one it'll print it out
|
|
// and then notify the program that it can finish.
|
|
go func() {
|
|
sig := <-sigs
|
|
l.Info(sig)
|
|
utils.ServerRunning = false
|
|
utils.StopSocketServer()
|
|
done <- true
|
|
}()
|
|
|
|
// The program will wait here until it gets the
|
|
// expected signal (as indicated by the goroutine
|
|
// above sending a value on `done`) and then exit.
|
|
l.Info("awaiting signal")
|
|
_ = utils.InitDb(dbFile, dbInit)
|
|
go utils.Listen(socketAddress)
|
|
for utils.ServerRunning {
|
|
if !utils.IsPortOpen() {
|
|
l.Info("try to open port")
|
|
err := utils.OpenPort(device, baudrate, bytesize, stopbits)
|
|
if err != nil {
|
|
l.Fatal("error in open serial port ", err)
|
|
}
|
|
l.Info("starting read")
|
|
}
|
|
buffer, count := utils.ReadPort()
|
|
if count > 0 {
|
|
if lograw {
|
|
fmt.Print(string(buffer))
|
|
}
|
|
utils.Append(buffer)
|
|
}
|
|
}
|
|
<-done
|
|
l.Info("exiting")
|
|
|
|
}
|