platform_api_test.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package test
  2. import (
  3. commonConfig "cicv-data-closedloop/aarch64/jili/common/config"
  4. "cicv-data-closedloop/common/config/c_log"
  5. "fmt"
  6. "testing"
  7. "time"
  8. )
  9. func init() {
  10. // 初始化日志配置
  11. c_log.InitLog(commonConfig.LogDir, commonConfig.ControlLogPrefix)
  12. // 初始化本地配置文件(第1处配置,在本地文件)
  13. commonConfig.InitLocalConfig()
  14. commonConfig.InitOssConfig()
  15. commonConfig.InitCloudConfig()
  16. }
  17. func TestGetConfig(t *testing.T) {
  18. fmt.Println(commonConfig.LocalConfig.EquipmentNo)
  19. PlatformConfig, err := commonConfig.GetPlatformTasks()
  20. fmt.Println("PlatformConfig", PlatformConfig)
  21. if err != nil {
  22. fmt.Println("获取配置status失败:", err)
  23. }
  24. }
  25. func TestControl(t *testing.T) {
  26. wait := false
  27. // 轮询任务接口判断是否有更新
  28. for {
  29. if wait { // 第一次就不等待一分钟了
  30. time.Sleep(time.Duration(60) * time.Second)
  31. }
  32. wait = true
  33. // 1. 获取当前设备的任务配置
  34. platformTasks, err := commonConfig.GetPlatformTasks()
  35. fmt.Println("platformTasks", platformTasks)
  36. commonConfig.PlatformTasks = platformTasks
  37. if err != nil {
  38. c_log.GlobalLogger.Error("获取平台任务失败:", err)
  39. continue
  40. }
  41. // 2. 判断当前时间是否有需要执行的任务
  42. for _, task := range platformTasks {
  43. flag := shouldExecuteTask(task.StartTime, task.EndTime)
  44. fmt.Println("flag", flag)
  45. }
  46. }
  47. }
  48. func shouldExecuteTask(startTimeStr, endTimeStr string) bool {
  49. // 定义时间格式(根据输入的时间字符串格式)
  50. timeFormat := "2006-01-02 15:04:05"
  51. // 解析 startTime 和 endTime
  52. startTime, err := time.ParseInLocation(timeFormat, startTimeStr, time.Local)
  53. fmt.Println("startTime", startTime)
  54. if err != nil {
  55. fmt.Println("Error parsing startTime:", err)
  56. return false
  57. }
  58. endTime, err := time.ParseInLocation(timeFormat, endTimeStr, time.Local)
  59. fmt.Println("endTime", endTime)
  60. if err != nil {
  61. fmt.Println("Error parsing endTime:", err)
  62. return false
  63. }
  64. // 获取当前时间
  65. currentTime := time.Now()
  66. fmt.Println("currentTime", currentTime)
  67. // 判断当前时间是否在 startTime 和 endTime 之间
  68. return !currentTime.Before(startTime) && !currentTime.After(endTime)
  69. }