func_test.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. PageSize: 999,
  23. }
  24. v, err := e.GetVirtualPlcItems()
  25. if v.Code == 200 {
  26. for _, line := range v.Data.List {
  27. fmt.Println(line.ItemName, line.Expression, line.IsVirtual, line.Frequency)
  28. }
  29. }
  30. t.Log(v, err)
  31. }
  32. func TestMultiEnvItem_FillCurrentValue(t *testing.T) {
  33. cache := redis.NewClient(&redis.Options{Addr: "47.96.12.136:6379", Password: "", MaxRetries: 5})
  34. SetOptions(Options{GtServerIp: "47.96.12.136:8788", FetchMultiItem: false, Cache: cache, AdjustValue: true})
  35. m := make(MultiEnvItem, 2)
  36. m["C.M.LT_CIP@out"] = &EnvItem{
  37. ProjectId: 92,
  38. Item: "C.M.LT_CIP@out",
  39. }
  40. m["C.M.RO1_DB@time_CS_display"] = &EnvItem{
  41. ProjectId: 92,
  42. Item: "C.M.RO1_DB@time_CS_display",
  43. }
  44. m["C.M.LT_QSC@out"] = &EnvItem{
  45. ProjectId: 92,
  46. Item: "C.M.LT_QSC@out",
  47. }
  48. err := m.FillCurrentValue()
  49. t.Log(err)
  50. for s, item := range m {
  51. t.Logf("item: %s value: %s htime: %s", s, item.Value, item.Htime)
  52. }
  53. m["C.M.LT_QSC@out"].ClearAdjust()
  54. }
  55. func TestMultiEnvItem_FillCurrentValue2(t *testing.T) {
  56. cache := redis.NewClient(&redis.Options{Addr: "47.96.12.136:6379", Password: "", MaxRetries: 5})
  57. SetOptions(Options{GtServerIp: "47.96.12.136:8788", FetchMultiItem: false, Cache: cache})
  58. 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}}`
  59. var multiEnvItems MultiEnvItem
  60. _ = json.Unmarshal([]byte(js), &multiEnvItems)
  61. t.Log(multiEnvItems)
  62. _ = multiEnvItems.FillCurrentValue()
  63. for {
  64. //_ = multiEnvItems.FillCurrentValue()
  65. multiEnvItems.ClearValues()
  66. v := multiEnvItems.GetItemFloat64Value("feed_flow")
  67. t.Logf("item: %s, %f", "feed_flow", v)
  68. //for s, item := range multiEnvItems {
  69. // if item != nil {
  70. // t.Logf("item: %s value: %f htime: %v", s, item.GetItemFloat64Val(), item.GetItemHtime())
  71. // } else {
  72. // t.Logf("item: %s fetch failure", s)
  73. // }
  74. //
  75. //}
  76. time.Sleep(2 * time.Second)
  77. //ms := multiEnvItems.FindString()
  78. //t.Logf("%+v", multiEnvItems)
  79. //t.Logf("step: %d", multiEnvItems.GetItemInt64Value("step"))
  80. //early := checkEarly(multiEnvItems.GetItemFloat64Value("Display_Time"), multiEnvItems.GetItemFloat64Value("Filter_Time_Set"), multiEnvItems.GetItemStringValue("Step"))
  81. //advanced := checkAdvanced(multiEnvItems.GetItemFloat64Value("Display_Time"), multiEnvItems.GetItemFloat64Value("Filter_Time_Set"), multiEnvItems.GetItemStringValue("Step"))
  82. //
  83. //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"))
  84. //t.Log(err)
  85. //for s, item := range multiEnvItems {
  86. // t.Logf("item: %s value: %s htime: %s", s, item.Value, item.Htime)
  87. //}
  88. }
  89. }
  90. func TestEnvItem_Adjust(t *testing.T) {
  91. cache := redis.NewClient(&redis.Options{Addr: "47.96.12.136:6379", Password: "", MaxRetries: 5})
  92. SetOptions(Options{GtServerIp: "47.96.12.136:8788", Cache: cache, AdjustValue: true})
  93. e := EnvItem{
  94. ProjectId: 92,
  95. Item: "C.M.LT_QSC@out",
  96. }
  97. //v, ht, err := e.getCurrentValue()
  98. //e.IncreAdjust(3600 * time.Second)
  99. //e.IncreAdjust(3600 * time.Second)
  100. e.IncreAdjust(3600 * time.Second)
  101. //
  102. //e.SetAdjust("65325", 3*time.Second)
  103. adjust, err := e.GetAdjustInt64Val()
  104. //t.Log(v, ht, err)
  105. t.Log(adjust, err)
  106. }
  107. func TestEnvItem_GetValueForAdjust(t *testing.T) {
  108. cache := redis.NewClient(&redis.Options{Addr: "47.96.12.136:6379", Password: "", MaxRetries: 5})
  109. SetOptions(Options{GtServerIp: "47.96.12.136:8788", Cache: cache, AdjustValue: false})
  110. e := EnvItem{
  111. ProjectId: 92,
  112. Item: "C.M.UF1_DB@word_control",
  113. }
  114. v := e.GetItemInt64Val()
  115. t.Log(v)
  116. }