clarksons_watch.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package services
  2. import (
  3. "encoding/json"
  4. "eta/eta_data_analysis/models"
  5. "eta/eta_data_analysis/utils"
  6. "fmt"
  7. "github.com/patrickmn/go-cache"
  8. "github.com/tealeg/xlsx"
  9. "io/fs"
  10. "os"
  11. "path/filepath"
  12. "syscall"
  13. "time"
  14. )
  15. func clarksonsWatch() {
  16. fmt.Println("clarksonsWatch start")
  17. var err error
  18. defer func() {
  19. if err != nil {
  20. fmt.Println("clarksonsWatch Err:" + err.Error())
  21. }
  22. }()
  23. var cacheClient *cache.Cache
  24. if cacheClient == nil {
  25. cacheClient = cache.New(365*24*time.Hour, 365*24*time.Hour)
  26. }
  27. err = filepath.Walk(utils.ClarkSonsFilePath, func(path string, info fs.FileInfo, err error) error {
  28. if err != nil {
  29. return err
  30. }
  31. if !info.IsDir() {
  32. fileInfo, err := os.Stat(path)
  33. if err != nil {
  34. fmt.Println("os.Stat:", err.Error())
  35. }
  36. winFileAttr := fileInfo.Sys().(*syscall.Win32FileAttributeData)
  37. modifyTimeStr := utils.SecondToTime(winFileAttr.LastWriteTime.Nanoseconds() / 1e9).Format(utils.FormatDateTime)
  38. existModifyTime, ok := cacheClient.Get(path)
  39. if ok {
  40. existModifyTimeStr := existModifyTime.(string)
  41. if existModifyTimeStr != modifyTimeStr {
  42. err = Clarksons(path)
  43. }
  44. } else {
  45. err = Clarksons(path)
  46. }
  47. cacheClient.Delete(path)
  48. cacheClient.Set(path, modifyTimeStr, 24*time.Hour)
  49. }
  50. return nil
  51. })
  52. }
  53. func Clarksons(path string) (err error) {
  54. defer func() {
  55. if err != nil {
  56. fmt.Println("Clarksons Err:" + err.Error())
  57. utils.FileLog.Info(fmt.Sprintf("Clarksons, Err: %s", err))
  58. }
  59. }()
  60. //path = "/Users/xi/Desktop/SIN_Timeseries_20241129063320.xlsx"
  61. var xlFile *xlsx.File
  62. exist, err := PathExists(path)
  63. if err != nil {
  64. fmt.Println(err)
  65. return
  66. }
  67. if exist {
  68. xlFile, err = xlsx.OpenFile(path)
  69. if err != nil {
  70. fmt.Println("OpenFile err:", err)
  71. return
  72. }
  73. } else {
  74. fmt.Println("Not Exist")
  75. return
  76. }
  77. sheetDatas := make([]models.SheetData, 0)
  78. for i, sheet := range xlFile.Sheets {
  79. if i > 0 {
  80. break
  81. }
  82. data := sheet
  83. sheetData := models.SheetData{
  84. Name: data.Name,
  85. MaxRow: data.MaxRow,
  86. MaxCol: data.MaxCol,
  87. Hidden: data.Hidden,
  88. Selected: data.Selected,
  89. }
  90. rows := make([]models.Row, 0)
  91. for _, v := range data.Rows {
  92. cells := make([]models.Cell, 0)
  93. for _, cell := range v.Cells {
  94. cells = append(cells, models.Cell{
  95. Value: cell.String(),
  96. })
  97. }
  98. row := models.Row{
  99. Cells: cells,
  100. }
  101. rows = append(rows, row)
  102. }
  103. sheetData.Rows = rows
  104. sheetDatas = append(sheetDatas, sheetData)
  105. }
  106. params := make(map[string]interface{})
  107. params["SheetData"] = sheetDatas
  108. result, e := PostEdbLib(params, utils.LIB_ROUTE_CLARKSONS)
  109. if e != nil {
  110. b, _ := json.Marshal(params)
  111. fmt.Println(e)
  112. utils.FileLog.Info(fmt.Sprintf("PostEdbLib err: %s, params: %s", e.Error(), string(b)))
  113. return
  114. }
  115. resp := new(models.BaseEdbLibResponse)
  116. if e := json.Unmarshal(result, &resp); e != nil {
  117. utils.FileLog.Info(fmt.Sprintf("json.Unmarshal err: %s", e))
  118. return
  119. }
  120. if resp.Ret != 200 {
  121. utils.FileLog.Info(fmt.Sprintf("Msg: %s, ErrMsg: %s", resp.Msg, resp.ErrMsg))
  122. return
  123. }
  124. return
  125. }