func_test.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. SetOptions(Options{GtServerIp: "120.55.44.4:8900", FetchMultiItem: false})
  34. m := make(MultiEnvItem, 2)
  35. m["C.M.LT_CIP@out"] = &EnvItem{
  36. ProjectId: 92,
  37. Item: "C.M.LT_CIP@out",
  38. }
  39. m["C.M.RO1_DB@time_CS_display"] = &EnvItem{
  40. ProjectId: 92,
  41. Item: "C.M.RO1_DB@time_CS_display",
  42. }
  43. m["C.M.LT_QSC@out"] = &EnvItem{
  44. ProjectId: 92,
  45. Item: "C.M.LT_QSC@out",
  46. }
  47. for {
  48. ovmap := m.FindPrevString()
  49. err := m.FillCurrentValue()
  50. t.Log(err)
  51. println("ovmap: ", ToString(ovmap))
  52. println("nvmap: ", ToString(m.FindString()))
  53. time.Sleep(1 * time.Second)
  54. }
  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. }
  118. func TestEnvItem_WaitEqValue(t *testing.T) {
  119. SetOptions(Options{GtServerIp: "47.96.12.136:8788", Cache: cache, AdjustValue: false, PlcItemSecret: "237c92d2-8795-1094-11ef-00e2e48fce4a"})
  120. e := &EnvItem{
  121. ProjectId: 92,
  122. Item: "C.M.UF4_DB@cycle_sp",
  123. }
  124. t.Log(e.GetItemInt64Val())
  125. err := e.SetValue(fmt.Sprintf("%d", e.Value), "72")
  126. if err != nil {
  127. t.Log(err)
  128. } else {
  129. c := e.WaitValue("72", 0, 3*time.Second, 20)
  130. t.Log(<-c)
  131. }
  132. }
  133. func ToString(data interface{}) string {
  134. bs, _ := json.Marshal(data)
  135. return string(bs)
  136. }