ソースを参照

1. 后600套 获取服务器数据查看工具
2. 按照id 号 依次从服务器拉取数据, 并插入到excel 中
3. 根据设备数量和 网络质量, 拉取间隔时间不定, 可能会超过3分钟

fly 1 ヶ月 前
コミット
5900c36060
4 ファイル変更270 行追加0 行削除
  1. 18 0
      go.mod
  2. 36 0
      go.sum
  3. 64 0
      http.go
  4. 152 0
      main.go

+ 18 - 0
go.mod

@@ -0,0 +1,18 @@
+module git.familybaby.top/flight/NongTT
+
+go 1.21.10
+
+require github.com/sirupsen/logrus v1.9.3
+
+require (
+	github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect
+	github.com/richardlehane/mscfb v1.0.4 // indirect
+	github.com/richardlehane/msoleps v1.0.4 // indirect
+	github.com/xuri/efp v0.0.0-20240408161823-9ad904a10d6d // indirect
+	github.com/xuri/excelize/v2 v2.9.0 // indirect
+	github.com/xuri/nfp v0.0.0-20240318013403-ab9948c2c4a7 // indirect
+	golang.org/x/crypto v0.28.0 // indirect
+	golang.org/x/net v0.30.0 // indirect
+	golang.org/x/sys v0.26.0 // indirect
+	golang.org/x/text v0.19.0 // indirect
+)

+ 36 - 0
go.sum

@@ -0,0 +1,36 @@
+github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
+github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
+github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
+github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 h1:RWengNIwukTxcDr9M+97sNutRR1RKhG96O6jWumTTnw=
+github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826/go.mod h1:TaXosZuwdSHYgviHp1DAtfrULt5eUgsSMsZf+YrPgl8=
+github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
+github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
+github.com/richardlehane/mscfb v1.0.4 h1:WULscsljNPConisD5hR0+OyZjwK46Pfyr6mPu5ZawpM=
+github.com/richardlehane/mscfb v1.0.4/go.mod h1:YzVpcZg9czvAuhk9T+a3avCpcFPMUWm7gK3DypaEsUk=
+github.com/richardlehane/msoleps v1.0.1/go.mod h1:BWev5JBpU9Ko2WAgmZEuiz4/u3ZYTKbjLycmwiWUfWg=
+github.com/richardlehane/msoleps v1.0.4 h1:WuESlvhX3gH2IHcd8UqyCuFY5yiq/GR/yqaSM/9/g00=
+github.com/richardlehane/msoleps v1.0.4/go.mod h1:BWev5JBpU9Ko2WAgmZEuiz4/u3ZYTKbjLycmwiWUfWg=
+github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
+github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
+github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
+github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
+github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
+github.com/xuri/efp v0.0.0-20240408161823-9ad904a10d6d h1:llb0neMWDQe87IzJLS4Ci7psK/lVsjIS2otl+1WyRyY=
+github.com/xuri/efp v0.0.0-20240408161823-9ad904a10d6d/go.mod h1:ybY/Jr0T0GTCnYjKqmdwxyxn2BQf2RcQIIvex5QldPI=
+github.com/xuri/excelize/v2 v2.9.0 h1:1tgOaEq92IOEumR1/JfYS/eR0KHOCsRv/rYXXh6YJQE=
+github.com/xuri/excelize/v2 v2.9.0/go.mod h1:uqey4QBZ9gdMeWApPLdhm9x+9o2lq4iVmjiLfBS5hdE=
+github.com/xuri/nfp v0.0.0-20240318013403-ab9948c2c4a7 h1:hPVCafDV85blFTabnqKgNhDCkJX25eik94Si9cTER4A=
+github.com/xuri/nfp v0.0.0-20240318013403-ab9948c2c4a7/go.mod h1:WwHg+CVyzlv/TX9xqBFXEZAuxOPxn2k1GNHwG41IIUQ=
+golang.org/x/crypto v0.28.0 h1:GBDwsMXVQi34v5CCYUm2jkJvu4cbtru2U4TN2PSyQnw=
+golang.org/x/crypto v0.28.0/go.mod h1:rmgy+3RHxRZMyY0jjAJShp2zgEdOqj2AO7U0pYmeQ7U=
+golang.org/x/net v0.30.0 h1:AcW1SDZMkb8IpzCdQUaIq2sP4sZ4zw+55h6ynffypl4=
+golang.org/x/net v0.30.0/go.mod h1:2wGyMJ5iFasEhkwi13ChkO/t1ECNC4X4eBKkVFyYFlU=
+golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
+golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
+golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo=
+golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
+golang.org/x/text v0.19.0 h1:kTxAhCbGbxhK0IwgSKiMO5awPoDQ0RpfiVYBfK860YM=
+golang.org/x/text v0.19.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
+gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
+gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
+gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

+ 64 - 0
http.go

@@ -0,0 +1,64 @@
+package main
+
+import (
+	"encoding/json"
+	"io"
+	"net/http"
+	"time"
+)
+
+// Response 是顶层响应结构
+type Response struct {
+	Code string   `json:"code"`
+	Data []SDData `json:"data"`
+}
+
+// SDData 是数据项结构
+type SDData struct {
+	Key  string `json:"SD_Key"`
+	Addr string `json:"SD_Addr"`
+	Code string `json:"SD_Code"`
+	Name string `json:"SD_Name"`
+	Unit string `json:"SD_Unit"` // null值可以用空字符串处理
+	Data string `json:"SD_Data"`
+	Time string `json:"SD_Time"`
+}
+
+// FetchAndParseData 从URL获取数据并解析
+func FetchAndParseData(url string) (*Response, error) {
+	// 创建HTTP客户端,设置超时
+	client := &http.Client{
+		Timeout: 10 * time.Second,
+	}
+
+	// 发送GET请求
+	resp, err := client.Get(url)
+	if err != nil {
+		log.Errorf("HTTP请求失败: %v", err)
+		return nil, err
+	}
+	defer resp.Body.Close()
+
+	// 检查状态码
+	if resp.StatusCode != http.StatusOK {
+		log.Errorf("HTTP状态码错误: %d", resp.StatusCode)
+		return nil, err
+	}
+
+	// 读取响应体
+	body, err := io.ReadAll(resp.Body)
+	if err != nil {
+		log.Errorf("读取响应失败: %v", err)
+		return nil, err
+	}
+
+	// 解析JSON
+	var data Response
+	err = json.Unmarshal(body, &data)
+	if err != nil {
+		log.Errorf("JSON解析失败: %v", err)
+		return nil, err
+	}
+
+	return &data, nil
+}

+ 152 - 0
main.go

@@ -0,0 +1,152 @@
+package main
+
+import (
+	"errors"
+	"github.com/sirupsen/logrus"
+	"github.com/xuri/excelize/v2"
+	"strconv"
+	"time"
+)
+
+var (
+	log         = logrus.WithField("module", "main")
+	excelHandle *excelize.File
+	ids         []string
+	startId     = 25120001
+	endId       = 25120060
+)
+
+func init() {
+	logrus.SetReportCaller(true)
+	logrus.SetFormatter(&logrus.TextFormatter{
+		//以下设置只是为了使输出更美观
+		DisableColors:    false,
+		DisableTimestamp: false,
+		TimestampFormat:  "2006-01-02 15:04:05",
+	})
+	//logger := &lumberjack.Logger{
+	//	Filename:   "./pest.log",
+	//	MaxSize:    500,  // 日志文件大小,单位是 MB
+	//	MaxBackups: 3,    // 最大过期日志保留个数
+	//	MaxAge:     28,   // 保留过期文件最大时间,单位 天
+	//	LocalTime:  true, // 使用本地时间
+	//	Compress:   true, // 是否压缩日志,默认是不压缩。这里设置为true,压缩日志
+	//}
+	//logrus.SetOutput(logger) // logrus 设置日志的输出方式
+
+	for i := startId; i <= endId; i++ {
+		ids = append(ids, strconv.Itoa(i))
+	}
+}
+
+func addSheetHeader(id string, row string) error {
+	var tmp []interface{}
+
+	tmp = append(tmp, "id")
+	for i := 0; i < 21; i++ {
+		tmp = append(tmp, "通用数据-"+strconv.Itoa(i))
+	}
+	tmp = append(tmp, "更新时间")
+
+	if err := excelHandle.SetSheetRow(id, row, &tmp); err != nil {
+		log.Errorf("插入表格错误(%s):%v", id, err)
+		return err
+	}
+
+	return nil
+}
+
+func putDataToSheet(id string, row string, data []SDData) error {
+	var tmp []interface{}
+
+	if len(data) != 21 {
+		return errors.New("数据长度不对")
+	}
+
+	tmp = append(tmp, id)
+
+	for _, d := range data {
+		tmp = append(tmp, d.Data)
+	}
+
+	tmp = append(tmp, data[0].Time)
+
+	if err := excelHandle.SetSheetRow(id, row, &tmp); err != nil {
+		log.Errorf("插入表格错误(%s):%v", id, err)
+		return err
+	}
+
+	return nil
+}
+
+func main() {
+	var err error
+	log.Info("start")
+	//excelHandle, err = excelize.OpenFile("real.xlsx")
+	//if err != nil {
+	//	log.Errorf("打开Excel失败", err)
+	//}
+	log.Info("新建 excel 表格")
+	excelHandle = excelize.NewFile()
+	defer excelHandle.Close()
+
+	err = excelHandle.SaveAs(time.Now().Format("20060102150405") + ".xlsx")
+	if err != nil {
+		log.Errorf("保存文件失败%v", err)
+	}
+
+	//新建所有sheet
+	for _, id := range ids {
+		if _, err := excelHandle.NewSheet(id); err != nil {
+			log.Errorf("新建表格错误(%s):%v", id, err)
+			continue
+		}
+
+		if err := addSheetHeader(id, "A1"); err != nil {
+			log.Errorf("增加表头错误(%s):%v", id, err)
+		}
+	}
+
+	err = excelHandle.DeleteSheet("Sheet1")
+	if err != nil {
+		log.Error("删除默认sheet 失败", err)
+	}
+
+	//base := "http://dataupload.nongtt.com/devdata/GetSensorData?key=Nongtt0020251204"
+	base := "http://dataupload.nongtt.com/devdata/GetSensorData?key=Nongtt00"
+
+	col := 2
+	for true {
+		for _, id := range ids {
+			url := base + id
+			log.WithField("id", id).Info("获取数据")
+			data, err := FetchAndParseData(url)
+			if err != nil {
+				log.Errorf("获取数据失败%v", err)
+				continue
+			}
+			log.WithField("id", id).Info("获取完成, 准备插入数据")
+			//for i, datum := range data.Data {
+			//	var tmp []interface{}
+			//	tmp = append(tmp, datum.Key, datum.Addr, datum.Code, datum.Name, datum.Data, datum.Time)
+			//
+			//	if err := excelHandle.SetSheetRow(id, "A"+strconv.Itoa(i+1), &tmp); err != nil {
+			//		log.Errorf("插入表格错误(%s):%v", id, err)
+			//	}
+			//}
+			if err := putDataToSheet(id, "A"+strconv.Itoa(col), data.Data); err != nil {
+				log.Error("插入表格错误(%s):%v", id, err)
+				continue
+			}
+			log.WithField("id", id).Info("插入数据 完成")
+		}
+
+		if err := excelHandle.Save(); err != nil {
+			log.Error("保存文件失败%v", err)
+			continue
+		}
+		log.Info("保存 完成")
+		time.Sleep(time.Minute)
+		col++
+	}
+}