func_test.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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", PlcItemSecret: "237c92d2-8795-1094-11ef-00e2e48fce4a"})
  11. e := EnvItem{
  12. ProjectId: 92,
  13. Item: "C5.M5.RO1_SJSCXS",
  14. }
  15. gk := EnvItem{
  16. ProjectId: 92,
  17. Item: "C5.M5.RO1_SJJSKG",
  18. }
  19. gk.SetValue("1", "0")
  20. v, ht, err := gk.getCurrentValue()
  21. t.Log(v, ht, err)
  22. //
  23. //t.Log(e.SetValue("0", "1"))
  24. //b := <-e.WaitValue("0", 0, 3*time.Second, 0)
  25. //t.Log(b)
  26. v, ht, err = e.getCurrentValue()
  27. t.Log(v, ht, err)
  28. }
  29. func TestGetVirtualPlcItems(t *testing.T) {
  30. SetOptions(Options{GtServerIp: "47.96.12.136:8788/"})
  31. e := VirtualPlcItem{
  32. ProjectId: 92,
  33. PageSize: 999,
  34. }
  35. v, err := e.GetVirtualPlcItems()
  36. if v.Code == 200 {
  37. for _, line := range v.Data.List {
  38. fmt.Println(line.ItemName, line.Expression, line.IsVirtual, line.Frequency)
  39. }
  40. }
  41. t.Log(v, err)
  42. }
  43. func TestMultiEnvItem_FillCurrentValue(t *testing.T) {
  44. SetOptions(Options{GtServerIp: "120.55.44.4:8900", FetchMultiItem: false})
  45. m := make(MultiEnvItem, 2)
  46. m["C.M.LT_CIP@out"] = &EnvItem{
  47. ProjectId: 92,
  48. Item: "C.M.LT_CIP@out",
  49. }
  50. m["C.M.RO1_DB@time_CS_display"] = &EnvItem{
  51. ProjectId: 92,
  52. Item: "C.M.RO1_DB@time_CS_display",
  53. }
  54. m["C.M.LT_QSC@out"] = &EnvItem{
  55. ProjectId: 92,
  56. Item: "C.M.LT_QSC@out",
  57. }
  58. for {
  59. ovmap := m.FindPrevString()
  60. err := m.FillCurrentValue()
  61. t.Log(err)
  62. println("ovmap: ", ToString(ovmap))
  63. println("nvmap: ", ToString(m.FindString()))
  64. time.Sleep(1 * time.Second)
  65. }
  66. }
  67. func TestMultiEnvItem_FillCurrentValue2(t *testing.T) {
  68. cache := redis.NewClient(&redis.Options{Addr: "47.96.12.136:6379", Password: "", MaxRetries: 5})
  69. SetOptions(Options{GtServerIp: "47.96.12.136:8788", FetchMultiItem: false, Cache: cache})
  70. 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}}`
  71. var multiEnvItems MultiEnvItem
  72. _ = json.Unmarshal([]byte(js), &multiEnvItems)
  73. t.Log(multiEnvItems)
  74. _ = multiEnvItems.FillCurrentValue()
  75. for {
  76. //_ = multiEnvItems.FillCurrentValue()
  77. multiEnvItems.ClearValues()
  78. v := multiEnvItems.GetItemFloat64Value("feed_flow")
  79. t.Logf("item: %s, %f", "feed_flow", v)
  80. //for s, item := range multiEnvItems {
  81. // if item != nil {
  82. // t.Logf("item: %s value: %f htime: %v", s, item.GetItemFloat64Val(), item.GetItemHtime())
  83. // } else {
  84. // t.Logf("item: %s fetch failure", s)
  85. // }
  86. //
  87. //}
  88. time.Sleep(2 * time.Second)
  89. //ms := multiEnvItems.FindString()
  90. //t.Logf("%+v", multiEnvItems)
  91. //t.Logf("step: %d", multiEnvItems.GetItemInt64Value("step"))
  92. //early := checkEarly(multiEnvItems.GetItemFloat64Value("Display_Time"), multiEnvItems.GetItemFloat64Value("Filter_Time_Set"), multiEnvItems.GetItemStringValue("Step"))
  93. //advanced := checkAdvanced(multiEnvItems.GetItemFloat64Value("Display_Time"), multiEnvItems.GetItemFloat64Value("Filter_Time_Set"), multiEnvItems.GetItemStringValue("Step"))
  94. //
  95. //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"))
  96. //t.Log(err)
  97. //for s, item := range multiEnvItems {
  98. // t.Logf("item: %s value: %s htime: %s", s, item.Value, item.Htime)
  99. //}
  100. }
  101. }
  102. func TestEnvItem_Adjust(t *testing.T) {
  103. cache := redis.NewClient(&redis.Options{Addr: "47.96.12.136:6379", Password: "", MaxRetries: 5})
  104. SetOptions(Options{GtServerIp: "47.96.12.136:8788", Cache: cache, AdjustValue: true})
  105. e := EnvItem{
  106. ProjectId: 92,
  107. Item: "C.M.LT_QSC@out",
  108. }
  109. //v, ht, err := e.getCurrentValue()
  110. //e.IncreAdjust(3600 * time.Second)
  111. //e.IncreAdjust(3600 * time.Second)
  112. e.IncreAdjust(3600 * time.Second)
  113. //
  114. //e.SetAdjust("65325", 3*time.Second)
  115. adjust, err := e.GetAdjustInt64Val()
  116. //t.Log(v, ht, err)
  117. t.Log(adjust, err)
  118. }
  119. func TestEnvItem_GetValueForAdjust(t *testing.T) {
  120. cache := redis.NewClient(&redis.Options{Addr: "47.96.12.136:6379", Password: "", MaxRetries: 5})
  121. SetOptions(Options{GtServerIp: "47.96.12.136:8788", Cache: cache, AdjustValue: false})
  122. e := EnvItem{
  123. ProjectId: 92,
  124. Item: "C.M.UF1_DB@word_control",
  125. }
  126. v := e.GetItemInt64Val()
  127. t.Log(v)
  128. }
  129. func TestEnvItem_WaitEqValue(t *testing.T) {
  130. SetOptions(Options{GtServerIp: "47.96.12.136:8788", Cache: cache, AdjustValue: false, PlcItemSecret: "237c92d2-8795-1094-11ef-00e2e48fce4a"})
  131. e := &EnvItem{
  132. ProjectId: 92,
  133. Item: "C.M.UF4_DB@cycle_sp",
  134. }
  135. t.Log(e.GetItemInt64Val())
  136. err := e.SetValue(fmt.Sprintf("%d", e.Value), "72")
  137. if err != nil {
  138. t.Log(err)
  139. } else {
  140. c := e.WaitValue("72", 0, 3*time.Second, 20)
  141. t.Log(<-c)
  142. }
  143. }
  144. func ToString(data interface{}) string {
  145. bs, _ := json.Marshal(data)
  146. return string(bs)
  147. }
  148. func TestEnvItem_WaitNotValue(t *testing.T) {
  149. SetOptions(Options{GtServerIp: "47.96.12.136:8788", PlcItemSecret: "237c92d2-8795-1094-11ef-00e2e48fce4a"})
  150. e := &EnvItem{
  151. ProjectId: 92,
  152. Item: "C.M.RO1_DB@control_word",
  153. }
  154. t.Log(e.GetItemInt64Val())
  155. err := e.SetValue(fmt.Sprintf("%d", e.Value), "72")
  156. if err != nil {
  157. t.Log(err)
  158. } else {
  159. c := e.WaitNotValue([]string{"24"}, 3*time.Second, 20)
  160. t.Log(<-c)
  161. }
  162. }