From c357c533e31e7634d5ad09a3fd9aeacdb74286a3 Mon Sep 17 00:00:00 2001 From: Tianyu Liu Date: Mon, 16 Sep 2024 16:55:29 +0200 Subject: [PATCH] Some working --- .gitignore | 6 +- .vscode/launch.json | 10 +++ src/LICENSE | 0 src/cmd/root.go | 41 +++++++++ src/cmd/serve.go | 119 +++++++++++++++++++++++++ src/components/pooRecorder.go | 156 +++++++++++++++++++++++++++++++++ src/go.mod | 28 +++++- src/go.sum | 69 ++++++++++++++- src/main.go | 11 +++ src/util/notion/notion.go | 81 ++++++++--------- src/util/notion/notion_test.go | 27 ------ 11 files changed, 474 insertions(+), 74 deletions(-) create mode 100644 src/LICENSE create mode 100644 src/cmd/root.go create mode 100644 src/cmd/serve.go create mode 100644 src/components/pooRecorder.go create mode 100644 src/main.go delete mode 100644 src/util/notion/notion_test.go diff --git a/.gitignore b/.gitignore index 5cc7ee4..a4b6d63 100644 --- a/.gitignore +++ b/.gitignore @@ -27,6 +27,8 @@ go.work.sum temp_data/ # py file for branch switching -*venv +.venv __pycache__/ -.pytest_cache/ \ No newline at end of file +.pytest_cache/ +config.yaml +bin/ \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json index 8591c83..c6a1460 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -20,6 +20,16 @@ "args": [ "reverse" ] + }, + { + "name": "Launch Home Automation", + "type": "go", + "request": "launch", + "mode": "auto", + "program": "${workspaceFolder}/src/main.go", + "args": [ + "serve" + ] } ] } \ No newline at end of file diff --git a/src/LICENSE b/src/LICENSE new file mode 100644 index 0000000..e69de29 diff --git a/src/cmd/root.go b/src/cmd/root.go new file mode 100644 index 0000000..14f8d34 --- /dev/null +++ b/src/cmd/root.go @@ -0,0 +1,41 @@ +/* +Copyright © 2024 Tianyu Liu +*/ +package cmd + +import ( + "os" + + "github.com/spf13/cobra" +) + +// rootCmd represents the base command when called without any subcommands +var rootCmd = &cobra.Command{ + Use: "home-automation-backend", + Short: "This is the entry point of the home automation backend", + Long: `Home automation backend is a RESTful API server that provides +automation features for may devices.`, + // Uncomment the following line if your bare application + // has an action associated with it: + // Run: func(cmd *cobra.Command, args []string) { }, +} + +// Execute adds all child commands to the root command and sets flags appropriately. +// This is called by main.main(). It only needs to happen once to the rootCmd. +func Execute() { + err := rootCmd.Execute() + if err != nil { + os.Exit(1) + } +} + +func init() { + // Here you will define your flags and configuration settings. + // Cobra supports persistent flags, which, if defined here, + // will be global for your application. + + // rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.home-automation-backend.yaml)") + + // Cobra also supports local flags, which will only run + // when this action is called directly. +} diff --git a/src/cmd/serve.go b/src/cmd/serve.go new file mode 100644 index 0000000..2b2845a --- /dev/null +++ b/src/cmd/serve.go @@ -0,0 +1,119 @@ +/* +Copyright © 2024 Tianyu Liu +*/ +package cmd + +import ( + "context" + "fmt" + "log/slog" + "net/http" + "os" + "os/signal" + "syscall" + "time" + + "github.com/gorilla/mux" + "github.com/spf13/cobra" + "github.com/spf13/viper" + pooRecorder "github.com/t-liu93/home-automation-backend/components" + "github.com/t-liu93/home-automation-backend/util/notion" +) + +var port string + +// serveCmd represents the serve command +var serveCmd = &cobra.Command{ + Use: "serve", + Short: "Server automation backend", + Run: serve, +} + +func initUtil() { + // init notion + if viper.InConfig("notion.token") { + notion.Init(viper.GetString("notion.token")) + } else { + slog.Error("Notion token not found in config file, exiting..") + os.Exit(1) + } +} + +func initComponent() { + // init pooRecorder + pooRecorder.Init() +} + +func serve(cmd *cobra.Command, args []string) { + slog.Info("Starting server...") + viper.SetConfigName("config") // name of config file (without extension) + viper.SetConfigType("yaml") + viper.AddConfigPath("$HOME/.config/home-automation") + viper.AddConfigPath(".") // optionally look for config in the working directory + err := viper.ReadInConfig() + if err != nil { + slog.Error(fmt.Sprintf("Cannot read config file, %s, exiting..", err)) + os.Exit(1) + } + viper.WatchConfig() + if viper.InConfig("port") { + port = viper.GetString("port") + } else { + slog.Error("Port not found in config file, exiting..") + os.Exit(1) + } + + initUtil() + initComponent() + + // routing + router := mux.NewRouter() + router.HandleFunc("/status", func(w http.ResponseWriter, r *http.Request) { + w.Write([]byte("OK")) + }).Methods("GET") + router.HandleFunc("/poo/record", pooRecorder.HandleRecordPoo).Methods("POST") + + srv := &http.Server{ + Addr: ":" + port, + Handler: router, + } + + stop := make(chan os.Signal, 1) + signal.Notify(stop, syscall.SIGINT, syscall.SIGTERM) + + go func() { + if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed { + slog.Error(fmt.Sprintf("ListenAndServe error: %v", err)) + os.Exit(1) + } + }() + + slog.Info(fmt.Sprintln("Server started on port", port)) + + <-stop + + slog.Info(fmt.Sprintln("Shutting down the server...")) + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) + defer cancel() + + if err := srv.Shutdown(ctx); err != nil { + slog.Error(fmt.Sprintf("Server Shutdown Failed:%+v", err)) + os.Exit(1) + } + slog.Info(fmt.Sprintln("Server gracefully stopped")) +} + +func init() { + rootCmd.AddCommand(serveCmd) + + // Here you will define your flags and configuration settings. + + // Cobra supports Persistent Flags which will work for this command + // and all subcommands, e.g.: + // serveCmd.PersistentFlags().String("foo", "", "A help for foo") + + // Cobra supports local flags which will only run when this command + // is called directly, e.g.: + // serveCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") + serveCmd.Flags().StringVarP(&port, "port", "p", "18881", "Port to listen on") +} diff --git a/src/components/pooRecorder.go b/src/components/pooRecorder.go new file mode 100644 index 0000000..28aac87 --- /dev/null +++ b/src/components/pooRecorder.go @@ -0,0 +1,156 @@ +package pooRecorder + +import ( + "bytes" + "database/sql" + "encoding/json" + "fmt" + "net/http" + "os" + "time" + + "github.com/spf13/viper" + "github.com/t-liu93/home-automation-backend/util/notion" + "golang.org/x/exp/slog" +) + +type recordDetail struct { + Status string `json:"status"` + Latitude string `json:"latitude"` + Longitude string `json:"longitude"` +} + +type pooStatusHttpSensorAttributes struct { + LastPoo string `json:"last_poo"` +} + +type pooStatusHttpSensor struct { + EntityId string `json:"entity_id"` + State string `json:"state"` + Attributes pooStatusHttpSensorAttributes `json:"attributes"` +} + +var ( + db *sql.DB +) + +func publishPooStatus(pooStatus pooStatusHttpSensor) { + if viper.InConfig("pooRecorder.homeassistantIp") && + viper.InConfig("pooRecorder.homeassistantPort") && + viper.InConfig("pooRecorder.homeassistantToken") { + homeAssistantIp := viper.GetString("pooRecorder.homeassistantIp") + homeAssistantPort := viper.GetString("pooRecorder.homeassistantPort") + url := fmt.Sprintf("http://%s:%s/api/states/%s", homeAssistantIp, homeAssistantPort, pooStatus.EntityId) + payload, err := json.Marshal(pooStatus) + if err != nil { + slog.Warn(fmt.Sprintln("HandleRecordPoo Error marshalling poo status", err)) + return + } + req, err := http.NewRequest("POST", url, bytes.NewBuffer(payload)) + if err != nil { + slog.Warn(fmt.Sprintln("HandleRecordPoo Error creating request", err)) + return + } + req.Header.Set("Content-Type", "application/json") + req.Header.Set("Authorization", "Bearer "+viper.GetString("pooRecorder.homeassistantToken")) + client := &http.Client{ + Timeout: time.Second * 1, + } + resp, err := client.Do(req) + if err != nil { + slog.Warn(fmt.Sprintln("HandleRecordPoo Error sending request", err)) + } + if resp.StatusCode != http.StatusOK { + slog.Warn(fmt.Sprintln("HandleRecordPoo Unexpected response status", resp.StatusCode)) + } + defer resp.Body.Close() + } else { + slog.Warn("HandleRecordPoo Home Assistant IP, port, or token not found in config file") + } +} + +func storeStatus(record recordDetail, timestamp time.Time) { + tableId := viper.GetString("pooRecorder.tableId") + recordDate := timestamp.Format("2006-01-02") + recordTime := timestamp.Format("15:04") + slog.Info(fmt.Sprintln("Recording poo", record.Status, "at", record.Latitude, record.Longitude)) + go func() { + header, err := notion.GetTableRows(tableId, 1, "") + if err != nil { + slog.Warn(fmt.Sprintln("HandleRecordPoo Failed to get table header", err)) + return + } + if len(header) == 0 { + slog.Warn("HandleRecordPoo Table header not found") + return + } + headerId := header[0].GetID() + err = notion.WriteTableRow([]string{recordDate, recordTime, record.Status, record.Latitude + "," + record.Longitude}, tableId, headerId.String()) + if err != nil { + slog.Warn(fmt.Sprintln("HandleRecordPoo Failed to write table row", err)) + } + }() + +} + +func migrateDb() { + +} + +func initDb() { + if !viper.InConfig("pooRecorder.dbPath") { + slog.Info("HandleRecordPoo dbPath not found in config file, using default: pooRecorder.db") + viper.SetDefault("pooRecorder.dbPath", "pooRecorder.db") + } + + dbPath := viper.GetString("pooRecorder.dbPath") + err := error(nil) + db, err = sql.Open("sqlite3", dbPath) + if err != nil { + slog.Error(fmt.Sprintln("PooRecorderInit Error opening database", err)) + os.Exit(1) + } + err = db.Ping() + if err != nil { + slog.Error(fmt.Sprintln("PooRecorderInit Error pinging database", err)) + os.Exit(1) + } + _, err = db.Exec(`CREATE TABLE IF NOT EXISTS poo_records ( + timestamp TEXT PRIMARY KEY, + status TEXT, + latitude TEXT, + longitude TEXT)`) + +} + +func Init() { + initDb() +} + +func HandleRecordPoo(w http.ResponseWriter, r *http.Request) { + var record recordDetail + if !viper.InConfig("pooRecorder.tableId") { + slog.Warn("HandleRecordPoo Table ID not found in config file") + http.Error(w, "Table ID not found in config file", http.StatusInternalServerError) + return + } + decorder := json.NewDecoder(r.Body) + decorder.DisallowUnknownFields() + err := decorder.Decode(&record) + if err != nil { + slog.Warn(fmt.Sprintln("HandleRecordPoo Error decoding request body", err)) + http.Error(w, err.Error(), http.StatusBadRequest) + return + } + now := time.Now() + storeStatus(record, now) + timeAttribute := now.Format("Mon | 2006-01-02 | 15:04") + pooStatus := pooStatusHttpSensor{ + EntityId: "sensor.test_poo_sensor", + State: record.Status, + Attributes: pooStatusHttpSensorAttributes{ + LastPoo: timeAttribute, + }, + } + publishPooStatus(pooStatus) +} diff --git a/src/go.mod b/src/go.mod index e67889a..3f0b255 100644 --- a/src/go.mod +++ b/src/go.mod @@ -2,13 +2,33 @@ module github.com/t-liu93/home-automation-backend go 1.23.0 -require github.com/jomei/notionapi v1.13.2 +require ( + github.com/gorilla/mux v1.8.1 + github.com/jomei/notionapi v1.13.2 + github.com/spf13/cobra v1.8.1 + github.com/spf13/viper v1.19.0 + golang.org/x/term v0.24.0 +) require ( + github.com/fsnotify/fsnotify v1.7.0 // indirect + github.com/hashicorp/hcl v1.0.0 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect - github.com/spf13/cobra v1.8.1 // indirect + github.com/magiconair/properties v1.8.7 // indirect + github.com/mitchellh/mapstructure v1.5.0 // indirect + github.com/pelletier/go-toml/v2 v2.2.2 // indirect + github.com/sagikazarmark/locafero v0.4.0 // indirect + github.com/sagikazarmark/slog-shim v0.1.0 // indirect + github.com/sourcegraph/conc v0.3.0 // indirect + github.com/spf13/afero v1.11.0 // indirect + github.com/spf13/cast v1.6.0 // indirect github.com/spf13/pflag v1.0.5 // indirect - golang.org/x/crypto v0.27.0 // indirect + github.com/subosito/gotenv v1.6.0 // indirect + go.uber.org/atomic v1.9.0 // indirect + go.uber.org/multierr v1.9.0 // indirect + golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect golang.org/x/sys v0.25.0 // indirect - golang.org/x/term v0.24.0 // indirect + golang.org/x/text v0.14.0 // indirect + gopkg.in/ini.v1 v1.67.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/src/go.sum b/src/go.sum index 53cf857..1a7a9f2 100644 --- a/src/go.sum +++ b/src/go.sum @@ -1,18 +1,83 @@ github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM= +github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8= +github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0= +github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA= +github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM= +github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= +github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY= +github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ= +github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= +github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= github.com/jomei/notionapi v1.13.2 h1:YpHKNpkoTMlUfWTlVIodOmQDgRKjfwmtSNVa6/6yC9E= github.com/jomei/notionapi v1.13.2/go.mod h1:BqzP6JBddpBnXvMSIxiR5dCoCjKngmz5QNl1ONDlDoM= +github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= +github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= +github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= +github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY= +github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0= +github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY= +github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= +github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM= +github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U= +github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8= +github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/sagikazarmark/locafero v0.4.0 h1:HApY1R9zGo4DBgr7dqsTH/JJxLTTsOt7u6keLGt6kNQ= +github.com/sagikazarmark/locafero v0.4.0/go.mod h1:Pe1W6UlPYUk/+wc/6KFhbORCfqzgYEpgQ3O5fPuL3H4= +github.com/sagikazarmark/slog-shim v0.1.0 h1:diDBnUNK9N/354PgrxMywXnAwEr1QZcOr6gto+ugjYE= +github.com/sagikazarmark/slog-shim v0.1.0/go.mod h1:SrcSrq8aKtyuqEI1uvTDTK1arOWRIczQRv+GVI1AkeQ= +github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo= +github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0= +github.com/spf13/afero v1.11.0 h1:WJQKhtpdm3v2IzqG8VMqrr6Rf3UYpEF239Jy9wNepM8= +github.com/spf13/afero v1.11.0/go.mod h1:GH9Y3pIexgf1MTIWtNGyogA5MwRIDXGUr+hbWNoBjkY= +github.com/spf13/cast v1.6.0 h1:GEiTHELF+vaR5dhz3VqZfFSzZjYbgeKDpBxQVS4GYJ0= +github.com/spf13/cast v1.6.0/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo= github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM= github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y= github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= -golang.org/x/crypto v0.27.0 h1:GXm2NjJrPaiv/h1tb2UH8QfgC/hOf/+z0p6PT8o1w7A= -golang.org/x/crypto v0.27.0/go.mod h1:1Xngt8kV6Dvbssa53Ziq6Eqn0HqbZi5Z6R0ZpwQzt70= +github.com/spf13/viper v1.19.0 h1:RWq5SEjt8o25SROyN3z2OrDB9l7RPd3lwTWU8EcEdcI= +github.com/spf13/viper v1.19.0/go.mod h1:GQUN9bilAbhU/jgc1bKs99f/suXKeUMct8Adx5+Ntkg= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8= +github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU= +go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE= +go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= +go.uber.org/multierr v1.9.0 h1:7fIwc/ZtS0q++VgcfqFDxSBZVv/Xo49/SYnDFupUwlI= +go.uber.org/multierr v1.9.0/go.mod h1:X2jQV1h+kxSjClGpnseKVIxpmcjrj7MNnI0bnlfKTVQ= +golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g= +golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k= golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34= golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.24.0 h1:Mh5cbb+Zk2hqqXNO7S1iTjEphVL+jb8ZWaqh/g+JWkM= golang.org/x/term v0.24.0/go.mod h1:lOBK/LVxemqiMij05LGJ0tzNr8xlmwBRJ81PX6wVLH8= +golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= +golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= +gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA= +gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/src/main.go b/src/main.go new file mode 100644 index 0000000..a87c15d --- /dev/null +++ b/src/main.go @@ -0,0 +1,11 @@ +/* +Copyright © 2024 Tianyu Liu + +*/ +package main + +import "github.com/t-liu93/home-automation-backend/cmd" + +func main() { + cmd.Execute() +} diff --git a/src/util/notion/notion.go b/src/util/notion/notion.go index a4b617f..c917e83 100644 --- a/src/util/notion/notion.go +++ b/src/util/notion/notion.go @@ -2,15 +2,14 @@ package notion import ( "context" + "errors" "github.com/jomei/notionapi" ) var client *notionapi.Client -var authToken string func Init(token string) { - authToken = token client = notionapi.NewClient(notionapi.Token(token)) } @@ -18,45 +17,49 @@ func GetClient() *notionapi.Client { return client } -func AGetBlock(blockId string) chan struct { - Block notionapi.Block - Error error -} { - retval := make(chan struct { - Block notionapi.Block - Error error +func GetTableRows(tableId string, numberOfRows int, startFromId string) ([]notionapi.Block, error) { + if client == nil { + return nil, errors.New("notion client not initialized") + } + block, err := client.Block.GetChildren(context.Background(), notionapi.BlockID(tableId), ¬ionapi.Pagination{ + StartCursor: notionapi.Cursor(startFromId), + PageSize: numberOfRows, }) - go func() { - block, err := client.Block.Get(context.Background(), notionapi.BlockID(blockId)) - retval <- struct { - Block notionapi.Block - Error error - }{block, err} - }() - return retval + if err != nil { + return nil, err + } + return block.Results, nil } -func AGetBlockChildren(blockId string, startCursor string, pageSize int) chan struct { - Children []notionapi.Block - NextCursor string - HasMore bool - Error error -} { - retval := make(chan struct { - Children []notionapi.Block - NextCursor string - HasMore bool - Error error +func WriteTableRow(content []string, tableId string, after string) error { + if client == nil { + return errors.New("notion client not initialized") + } + rich := [][]notionapi.RichText{} + for _, c := range content { + rich = append(rich, []notionapi.RichText{ + { + Type: "text", + Text: ¬ionapi.Text{ + Content: c, + }, + }, + }) + } + tableRow := notionapi.TableRowBlock{ + BasicBlock: notionapi.BasicBlock{ + Object: "block", + Type: "table_row", + }, + TableRow: notionapi.TableRow{ + Cells: rich, + }, + } + + _, err := client.Block.AppendChildren(context.Background(), notionapi.BlockID(tableId), ¬ionapi.AppendBlockChildrenRequest{ + After: notionapi.BlockID(after), + Children: []notionapi.Block{tableRow}, }) - pagination := notionapi.Pagination{StartCursor: notionapi.Cursor(startCursor), PageSize: pageSize} - go func() { - children, err := client.Block.GetChildren(context.Background(), notionapi.BlockID(blockId), &pagination) - retval <- struct { - Children []notionapi.Block - NextCursor string - HasMore bool - Error error - }{children.Results, children.NextCursor, children.HasMore, err} - }() - return retval + + return err } diff --git a/src/util/notion/notion_test.go b/src/util/notion/notion_test.go deleted file mode 100644 index 0b57442..0000000 --- a/src/util/notion/notion_test.go +++ /dev/null @@ -1,27 +0,0 @@ -package notion_test - -import ( - "testing" - - "github.com/t-liu93/home-automation-backend/util/notion" -) - -func TestGetBlockBasic(t *testing.T) { - notion.Init("") - block := <-notion.AGetBlock("") - actObj := block.Block.GetObject() - expObj := "block" - if string(actObj) != expObj { - t.Errorf("Expected %s, but got %s", expObj, actObj) - } -} - -func TestGetBlockChildrenBasic(t *testing.T) { - notion.Init("") - blockChildren := <-notion.AGetBlockChildren("", "", 100) - actLen := len(blockChildren.Children) - expLen := 100 - if actLen != expLen { - t.Errorf("Expected %d, but got %d", expLen, actLen) - } -}