func_test.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package envitem
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/go-redis/redis/v8"
  6. "testing"
  7. "time"
  8. )
  9. func TestEnvItem_GetCurrentData(t *testing.T) {
  10. SetOptions(Options{GtServerIp: "120.55.44.4:8900"})
  11. e := EnvItem{
  12. ProjectId: 92,
  13. Item: "C.M.UF1_DB@press_PV",
  14. }
  15. v, ht, err := e.getCurrentValue()
  16. t.Log(v, ht, err)
  17. }
  18. func TestGetVirtualPlcItems(t *testing.T) {
  19. SetOptions(Options{GtServerIp: "47.96.12.136:8788/"})
  20. e := VirtualPlcItem{
  21. ProjectId: 92,
  22. IsVirtual: 1,
  23. PageSize: 999,
  24. }
  25. v, err := e.GetVirtualPlcItems()
  26. if v.Code == 200 {
  27. for _, line := range v.Data.List {
  28. fmt.Println(line.ItemName, line.Expression)
  29. }
  30. }
  31. t.Log(v, err)
  32. }
  33. func TestMultiEnvItem_FillCurrentValue(t *testing.T) {
  34. cache := redis.NewClient(&redis.Options{Addr: "47.96.12.136:6379", Password: "", MaxRetries: 5})
  35. SetOptions(Options{GtServerIp: "47.96.12.136:8788", FetchMultiItem: false, Cache: cache, AdjustValue: true})
  36. m := make(MultiEnvItem, 2)
  37. m["C.M.LT_CIP@out"] = &EnvItem{
  38. ProjectId: 92,
  39. Item: "C.M.LT_CIP@out",
  40. }
  41. m["C.M.RO1_DB@time_CS_display"] = &EnvItem{
  42. ProjectId: 92,
  43. Item: "C.M.RO1_DB@time_CS_display",
  44. }
  45. m["C.M.LT_QSC@out"] = &EnvItem{
  46. ProjectId: 92,
  47. Item: "C.M.LT_QSC@out",
  48. }
  49. err := m.FillCurrentValue()
  50. t.Log(err)
  51. for s, item := range m {
  52. t.Logf("item: %s value: %s htime: %s", s, item.Value, item.Htime)
  53. }
  54. m["C.M.LT_QSC@out"].ClearAdjust()
  55. }
  56. func TestMultiEnvItem_FillCurrentValue2(t *testing.T) {
  57. cache := redis.NewClient(&redis.Options{Addr: "47.96.12.136:6379", Password: "", MaxRetries: 5})
  58. SetOptions(Options{GtServerIp: "47.96.12.136:8788", FetchMultiItem: false, Cache: cache})
  59. js := `{"tmp":{"project_id":92,"device_code":"UF-4001A","plc_device_id":"0","item":"C.M.UF1_DB@press_PV","default_val":0},"feed_flow":{"project_id":92,"device_code":"UF-4001A","plc_device_id":"0","item":"C.M.UF1_FT_JS@out","default_val":0},"feed_pressure":{"project_id":92,"device_code":"UF-4001A","plc_device_id":"0","item":"C.M.UF1_PT_JS@out","default_val":0},"product_pressure":{"project_id":92,"device_code":"UF-4001A","plc_device_id":"0","item":"C.M.UF1_PT_CS@out","default_val":0},"feed_wq_turbidity":{"project_id":92,"device_code":"AIT-94061","plc_device_id":"0","item":"C.M.UF_Tur_ZJS@out","default_val":0},"water_temperature":{"project_id":92,"device_code":"TIT-34061","plc_device_id":"0","item":"C.M.RO_TT_ZJS@out","default_val":0},"product_wq_ph":{"project_id":92,"device_code":"AIT-95065b","plc_device_id":"0","item":"C.M.UF_PH_ZCS@out","default_val":0},"step":{"project_id":92,"device_code":"HB","plc_device_id":"0","item":"C.M.UF1_DB@word_control","default_val":0}}`
  60. var multiEnvItems MultiEnvItem
  61. _ = json.Unmarshal([]byte(js), &multiEnvItems)
  62. t.Log(multiEnvItems)
  63. _ = multiEnvItems.FillCurrentValue()
  64. for {
  65. //_ = multiEnvItems.FillCurrentValue()
  66. multiEnvItems.ClearValues()
  67. v := multiEnvItems.GetItemFloat64Value("feed_flow")
  68. t.Logf("item: %s, %f", "feed_flow", v)
  69. //for s, item := range multiEnvItems {
  70. // if item != nil {
  71. // t.Logf("item: %s value: %f htime: %v", s, item.GetItemFloat64Val(), item.GetItemHtime())
  72. // } else {
  73. // t.Logf("item: %s fetch failure", s)
  74. // }
  75. //
  76. //}
  77. time.Sleep(2 * time.Second)
  78. //ms := multiEnvItems.FindString()
  79. //t.Logf("%+v", multiEnvItems)
  80. //t.Logf("step: %d", multiEnvItems.GetItemInt64Value("step"))
  81. //early := checkEarly(multiEnvItems.GetItemFloat64Value("Display_Time"), multiEnvItems.GetItemFloat64Value("Filter_Time_Set"), multiEnvItems.GetItemStringValue("Step"))
  82. //advanced := checkAdvanced(multiEnvItems.GetItemFloat64Value("Display_Time"), multiEnvItems.GetItemFloat64Value("Filter_Time_Set"), multiEnvItems.GetItemStringValue("Step"))
  83. //
  84. //t.Logf("early: %v, advanced: %v, step: %s filterNumber: %s Display_Time: %s Filter_Time_Set: %s", early, advanced, multiEnvItems.GetItemStringValue("Step"), multiEnvItems.GetItemStringValue("Filter_Number"), multiEnvItems.GetItemStringValue("Display_Time"), multiEnvItems.GetItemStringValue("Filter_Time_Set"))
  85. //t.Log(err)
  86. //for s, item := range multiEnvItems {
  87. // t.Logf("item: %s value: %s htime: %s", s, item.Value, item.Htime)
  88. //}
  89. }
  90. }
  91. func TestEnvItem_Adjust(t *testing.T) {
  92. cache := redis.NewClient(&redis.Options{Addr: "47.96.12.136:6379", Password: "", MaxRetries: 5})
  93. SetOptions(Options{GtServerIp: "47.96.12.136:8788", Cache: cache, AdjustValue: true})
  94. e := EnvItem{
  95. ProjectId: 92,
  96. Item: "C.M.LT_QSC@out",
  97. }
  98. //v, ht, err := e.getCurrentValue()
  99. //e.IncreAdjust(3600 * time.Second)
  100. //e.IncreAdjust(3600 * time.Second)
  101. e.IncreAdjust(3600 * time.Second)
  102. //
  103. //e.SetAdjust("65325", 3*time.Second)
  104. adjust, err := e.GetAdjustInt64Val()
  105. //t.Log(v, ht, err)
  106. t.Log(adjust, err)
  107. }
  108. func TestEnvItem_GetValueForAdjust(t *testing.T) {
  109. cache := redis.NewClient(&redis.Options{Addr: "47.96.12.136:6379", Password: "", MaxRetries: 5})
  110. SetOptions(Options{GtServerIp: "47.96.12.136:8788", Cache: cache, AdjustValue: false})
  111. e := EnvItem{
  112. ProjectId: 92,
  113. Item: "C.M.UF1_DB@word_control",
  114. }
  115. v := e.GetItemInt64Val()
  116. t.Log(v)
  117. }