zhangzengfei
2024-02-23 b4f52495ea8c990bba30efcb3f36a6167cc85bab
添加设备所属机构的配置字段上报
6个文件已修改
95 ■■■■■ 已修改文件
README.md 7 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
config/config.go 19 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
config/config.yaml 7 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
main.go 33 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
model/model.go 7 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
service/server.go 22 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
README.md
@@ -2,3 +2,10 @@
重庆项目推送图片
###功能
定时读取本地es的报警数据,同步到云端
###部署
/opt/vasystem/procs/esSync-client
/opt/vasystem/config/esSync.yaml
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)
    })
}
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
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)
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,
}
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