move to utils package

This commit is contained in:
w1ndyb0y
2020-12-21 15:38:33 +08:00
parent 51fa99e57b
commit 8a116818cf
10 changed files with 158 additions and 178 deletions
+2 -1
View File
@@ -1,2 +1,3 @@
tty*
telegram.db
telegram.db
.idea
-8
View File
@@ -1,8 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/tele-recv.iml" filepath="$PROJECT_DIR$/.idea/tele-recv.iml" />
</modules>
</component>
</project>
-8
View File
@@ -1,8 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
Generated
-6
View File
@@ -1,6 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
-82
View File
@@ -1,82 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ChangeListManager">
<list default="true" id="ab419b68-984b-4661-bb9a-26bcba1547b3" name="Default Changelist" comment="create test function">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
<option name="LAST_RESOLUTION" value="IGNORE" />
</component>
<component name="GOROOT" url="file:///usr/local/Cellar/go/1.15.5/libexec" />
<component name="Git.Settings">
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
</component>
<component name="GitToolBoxStore">
<option name="projectConfigVersion" value="2" />
</component>
<component name="ProblemsViewState">
<option name="selectedIndex" value="1" />
</component>
<component name="ProjectId" id="1lm7nLrR9364W3kc4Ropplb4iwV" />
<component name="ProjectLevelVcsManager" settingsEditedManually="true" />
<component name="ProjectViewState">
<option name="hideEmptyMiddlePackages" value="true" />
<option name="showLibraryContents" value="true" />
</component>
<component name="PropertiesComponent">
<property name="RunOnceActivity.OpenProjectViewOnStart" value="true" />
<property name="RunOnceActivity.ShowReadmeOnStart" value="true" />
<property name="WebServerToolWindowFactoryState" value="false" />
<property name="go.import.settings.migrated" value="true" />
<property name="go.sdk.automatically.set" value="true" />
<property name="go.tried.to.enable.integration.vgo.integrator" value="true" />
<property name="last_opened_file_path" value="$PROJECT_DIR$" />
<property name="node.js.detected.package.eslint" value="true" />
<property name="node.js.detected.package.tslint" value="true" />
<property name="node.js.path.for.package.eslint" value="project" />
<property name="node.js.path.for.package.tslint" value="project" />
<property name="node.js.selected.package.eslint" value="(autodetect)" />
<property name="node.js.selected.package.tslint" value="(autodetect)" />
<property name="settings.editor.selected.configurable" value="reference.settingsdialog.IDE.editor.colors.Console Font" />
</component>
<component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="application-level" UseSingleDictionary="true" transferred="true" />
<component name="TaskManager">
<task active="true" id="Default" summary="Default task">
<changelist id="ab419b68-984b-4661-bb9a-26bcba1547b3" name="Default Changelist" comment="create test function" />
<created>1608530373779</created>
<option name="number" value="Default" />
<option name="presentableId" value="Default" />
<updated>1608530373779</updated>
</task>
<servers />
</component>
<component name="TypeScriptGeneratedFilesManager">
<option name="version" value="3" />
</component>
<component name="Vcs.Log.Tabs.Properties">
<option name="TAB_STATES">
<map>
<entry key="MAIN">
<value>
<State />
</value>
</entry>
</map>
</option>
<option name="oldMeFiltersMigrated" value="true" />
</component>
<component name="VcsManagerConfiguration">
<MESSAGE value="create test function" />
<option name="LAST_COMMIT_MESSAGE" value="create test function" />
</component>
<component name="VgoProject">
<integration-enabled>true</integration-enabled>
<environment>
<map>
<entry key="GOPROXY" value="https://goproxy.io" />
</map>
</environment>
</component>
</project>
-55
View File
@@ -1,55 +0,0 @@
/*
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 (
"fmt"
"github.com/spf13/cobra"
)
// readCmd represents the read command
var readCmd = &cobra.Command{
Use: "read",
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("read called")
},
}
func init() {
rootCmd.AddCommand(readCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// readCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// readCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
func read() {
}
+104
View File
@@ -0,0 +1,104 @@
/*
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 (
"fmt"
"it2000.com.cn/tele-recv/utils"
"log"
"os"
"os/signal"
"syscall"
"github.com/spf13/cobra"
)
// startCmd represents the start command
var startCmd = &cobra.Command{
Use: "start",
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Run: start,
}
var run bool
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(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
// 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)
run = 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
fmt.Println()
fmt.Println(sig)
run = false
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.
fmt.Println("awaiting signal")
for run {
if !utils.IsPortOpen() {
log.Println("try to open port")
err := utils.OpenPort(device, baudrate, bytesize)
if err != nil {
log.Fatal("error in open serial port ", err)
}
log.Println("starting read")
}
buffer, count := utils.ReadPort()
if count > 0 {
log.Print(string(buffer))
}
}
<-done
fmt.Println("exiting")
}
+5 -15
View File
@@ -16,11 +16,9 @@ limitations under the License.
package cmd
import (
"fmt"
"github.com/spf13/cobra"
"github.com/tarm/serial"
"it2000.com.cn/tele-recv/database"
"os"
"it2000.com.cn/tele-recv/utils"
"log"
)
// testCmd represents the test command
@@ -34,18 +32,10 @@ Test load sqlite database and init table`,
}
func test(cmd *cobra.Command, args []string) {
fmt.Println("test called")
fmt.Println("device :", device)
c := &serial.Config{Name: device, Baud: baudrate, Size: bytesize}
s, err := serial.OpenPort(c)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println(c, "opened")
_ = s.Close()
log.Println("Testing environment")
_ = utils.OpenPort(device, baudrate, bytesize)
err = database.Init(dbFile, dbInit)
err := utils.InitDb(dbFile, dbInit)
if err != nil {
println("create database error ", err)
}
+45
View File
@@ -0,0 +1,45 @@
package utils
import (
"github.com/tarm/serial"
"io"
"log"
"time"
)
var(
device string
baudrate int
bytesize byte
port *serial.Port
portOpened bool
)
func OpenPort(device string, baudrate int, bytesize byte) error {
log.Println("opening port device:", device, ", baudrate=", baudrate, ", bytesize=",bytesize)
config := &serial.Config{Name: device, Baud: baudrate, Size: bytesize, ReadTimeout: time.Second * 1}
var err error
port, err = serial.OpenPort(config)
if err!=nil {
log.Fatalln("error in open serial port ", err)
return err
}
log.Println("opened port ", port)
portOpened = true
return nil
}
func ReadPort() ([]byte, int){
buffer := make([]byte, 256)
count,err := port.Read(buffer)
if err != nil && err != io.EOF {
log.Println("error in read port ", err)
//TODO: 3 time fail try to
}
return buffer, count
}
func IsPortOpen() bool {
return portOpened
}
+2 -3
View File
@@ -1,4 +1,4 @@
package database
package utils
import (
"database/sql"
@@ -16,7 +16,6 @@ const (
[tele_processed] int(1) NOT NULL DEFAULT 0,
[tele_text] TEXT NOT NULL
)
`
)
var (
@@ -42,7 +41,7 @@ func getDb(file string, init bool) (*sql.DB, error) {
return db, nil
}
func Init(file string, init bool) error {
func InitDb(file string, init bool) error {
var dbErr error
db, dbErr = getDb(file, init)
return dbErr