From b4f52495ea8c990bba30efcb3f36a6167cc85bab Mon Sep 17 00:00:00 2001
From: zhangzengfei <zhangzengfei@smartai.com>
Date: 星期五, 23 二月 2024 11:52:19 +0800
Subject: [PATCH] 添加设备所属机构的配置字段上报

---
 model/model.go     |    7 ++-
 config/config.go   |   19 +++++++--
 config/config.yaml |    7 ++-
 service/server.go  |   22 ++++++----
 main.go            |   33 +++++-----------
 README.md          |    7 +++
 6 files changed, 53 insertions(+), 42 deletions(-)

diff --git a/README.md b/README.md
index a31457b..acba119 100644
--- a/README.md
+++ b/README.md
@@ -2,3 +2,10 @@
 
 閲嶅簡椤圭洰鎺ㄩ�佸浘鐗�
 
+###鍔熻兘
+瀹氭椂璇诲彇鏈湴es鐨勬姤璀︽暟鎹紝鍚屾鍒颁簯绔�
+
+###閮ㄧ讲
+/opt/vasystem/procs/esSync-client
+
+/opt/vasystem/config/esSync.yaml
\ No newline at end of file
diff --git a/config/config.go b/config/config.go
index 1fc82e0..0f86c14 100644
--- a/config/config.go
+++ b/config/config.go
@@ -1,25 +1,34 @@
 package config
 
 import (
+	"github.com/fsnotify/fsnotify"
 	"github.com/spf13/viper"
 	"log"
 )
 
-type servUrls struct {
+type common struct {
 	EsUrl     string `mapstructure: "esUrl"`
 	ServerUrl string `mapstructure: "serverUrl"`
+	OrgName   string `mapstructure: "orgName"`
 }
 
-var ServUrls = &servUrls{}
+var Options = &common{}
 
-func Init(env string) {
+func Init() {
 	var err error
 	viper.SetConfigType("yaml")
-	viper.SetConfigName(env)
+	viper.SetConfigName("esSync")
+	viper.AddConfigPath("../config")
 	viper.AddConfigPath("config")
+	viper.AddConfigPath("")
 	err = viper.ReadInConfig()
 	if err != nil {
 		log.Fatal("error on parsing configuration file", err)
 	}
-	viper.UnmarshalKey("servUrls", ServUrls)
+
+	viper.UnmarshalKey("common", Options)
+	viper.WatchConfig()
+	viper.OnConfigChange(func(in fsnotify.Event) {
+		viper.UnmarshalKey("common", Options)
+	})
 }
diff --git a/config/config.yaml b/config/config.yaml
index e398d27..162b78d 100644
--- a/config/config.yaml
+++ b/config/config.yaml
@@ -1,3 +1,4 @@
-servUrls:
-  esUrl: 192.168.20.223:9200
-  serverUrl: http://192.168.20.119:9696/api-a/device/alarm
+common:
+  esUrl: 192.168.10.10:9200
+  serverUrl: http://smartai.com:7012/api-a/device/alarm
+  orgName: Basic
diff --git a/main.go b/main.go
index d173e44..62e1db2 100644
--- a/main.go
+++ b/main.go
@@ -3,41 +3,28 @@
 import (
 	"data_msg_push_server/config"
 	"data_msg_push_server/service"
-	"flag"
+
 	"fmt"
 	"time"
 )
 
-var env = flag.String("config", "config", "read storage info")
-
 func init() {
-	config.Init(*env)
+	config.Init()
 }
 
 func main() {
-	fmt.Println("hello!!!")
 	done := make(chan bool)
 
 	go runEvery(done)
-	//result,err  := service.GetData()
-	//if err != nil {
-	//	fmt.Println(err)
-	//}
-	//if result != nil{
-	//
-	//}
 	<-done
 	fmt.Println("绋嬪簭宸查��鍑�")
-	//fmt.Println(res)
-	//fmt.Println(err)
-
 }
 
-func runEvery(done chan<- bool){
+func runEvery(done chan<- bool) {
 	ticker := time.NewTicker(10 * time.Second)
 	defer ticker.Stop()
 
-	for{
+	for {
 		select {
 		case <-ticker.C:
 			fmt.Println("寮�濮嬫墽琛屽嚱鏁般�傘�傘��")
@@ -48,10 +35,10 @@
 	}
 }
 
-func doServer()  {
+func doServer() {
 	total := 0
 	for {
-		t,err := service.GetTotal()
+		t, err := service.GetTotal()
 		if err != nil {
 			fmt.Println(err)
 		}
@@ -62,16 +49,16 @@
 		} else {
 			for {
 				connectStatus := service.ConnectControl()
-				fmt.Println("connectStatus: ",connectStatus)
+				fmt.Println("connectStatus: ", connectStatus)
 				if connectStatus == true {
-					url := config.ServUrls.ServerUrl
+					url := config.Options.ServerUrl
 					result, err1 := service.GetData()
 					if err1 != nil {
 						fmt.Println(err1)
 					}
 					id, errs2 := service.SendData(result, url)
-					fmt.Println("data id is: ",id)
-					if errs2 == nil{
+					fmt.Println("data id is: ", id)
+					if errs2 == nil {
 						//delStatus := service.DeleteData(id)
 						markStatus := service.MarkData(id)
 						fmt.Println(markStatus)
diff --git a/model/model.go b/model/model.go
index e4e4adf..58e175c 100644
--- a/model/model.go
+++ b/model/model.go
@@ -6,5 +6,8 @@
 	PicSmImages  [][]byte    `json:"picSmImages"`
 }
 
-var PushDataInfo = &pushDataInfo{SourceData: nil,
-	PicMaxImages: nil, PicSmImages: nil}
+var PushDataInfo = &pushDataInfo{
+	SourceData:   nil,
+	PicMaxImages: nil,
+	PicSmImages:  nil,
+}
diff --git a/service/server.go b/service/server.go
index 39f2f64..5f1eb69 100644
--- a/service/server.go
+++ b/service/server.go
@@ -13,7 +13,7 @@
 )
 
 func ConnectControl() bool {
-	url := config.ServUrls.ServerUrl
+	url := config.Options.ServerUrl
 	resp, err := http.Get(url)
 	if err != nil {
 		fmt.Println("杩炴帴澶辫触", err)
@@ -26,7 +26,7 @@
 }
 
 func GetTotal() (total int, err error) {
-	url := "http://" + config.ServUrls.EsUrl + "/ai_ocean/_search"
+	url := "http://" + config.Options.EsUrl + "/ai_ocean/_search"
 	queryDSL := `{
     "size": "1",
     "query": {
@@ -63,9 +63,9 @@
 }
 
 func GetData() (interface{}, error) {
-	//fmt.Println(config.ServUrls.EsUrl)
-	//fmt.Println(config.ServUrls.ServerUrl)
-	url := "http://" + config.ServUrls.EsUrl + "/ai_ocean/_search"
+	//fmt.Println(config.Options.EsUrl)
+	//fmt.Println(config.Options.ServerUrl)
+	url := "http://" + config.Options.EsUrl + "/ai_ocean/_search"
 	queryDSL := `{
     "size": "1",
     "query": {
@@ -101,7 +101,7 @@
 		return nil, nil
 	}
 	picMaxImages := make([][]byte, 0) // 瀛樺偍鍥剧墖鏁版嵁鐨勬暟缁�
-	if source[0]["picMaxUrl"].([]interface{}) != nil{
+	if source[0]["picMaxUrl"].([]interface{}) != nil {
 		//picMaxUrls := source[0]["picMaxUrl"].([]interface{})
 		if len(source[0]["picMaxUrl"].([]interface{})) > 0 {
 			for _, picMaxUrl := range source[0]["picMaxUrl"].([]interface{}) {
@@ -132,6 +132,10 @@
 			picSmImages = append(picSmImages, picSmImageData)
 		}
 	}
+
+	// 涓婃姤鏈烘瀯鍚嶇О
+	source[0]["orgName"] = config.Options.OrgName
+
 	model.PushDataInfo.SourceData = source[0]
 	model.PushDataInfo.PicMaxImages = picMaxImages
 	model.PushDataInfo.PicSmImages = picSmImages
@@ -172,7 +176,7 @@
 }
 
 func DeleteData(id string) bool {
-	url := "http://" + config.ServUrls.EsUrl + "/ai_ocean/_delete_by_query?refresh=true"
+	url := "http://" + config.Options.EsUrl + "/ai_ocean/_delete_by_query?refresh=true"
 	deleteDSL := `{
 					"query":{
 						"bool":{
@@ -199,7 +203,7 @@
 }
 
 func MarkData(id string) bool {
-	url := "http://" + config.ServUrls.EsUrl + "/ai_ocean/_update_by_query?refresh=true"
+	url := "http://" + config.Options.EsUrl + "/ai_ocean/_update_by_query?refresh=true"
 	markDSL := `{
     "script": {
         "source": "ctx._source.isDelete=true"
@@ -215,7 +219,7 @@
 		fmt.Println(err)
 		return false
 	}
-	total, err :=  util.SourceUpdated(buf)
+	total, err := util.SourceUpdated(buf)
 	if err != nil {
 		fmt.Println(err)
 		return false

--
Gitblit v1.8.0