package envitem import ( "encoding/json" "fmt" "github.com/go-redis/redis/v8" "testing" "time" ) func TestEnvItem_GetCurrentData(t *testing.T) { SetOptions(Options{GtServerIp: "120.55.44.4:8900", PlcItemSecret: "237c92d2-8795-1094-11ef-00e2e48fce4a"}) e := EnvItem{ ProjectId: 92, Item: "C5.M5.RO1_SJSCXS", } gk := EnvItem{ ProjectId: 92, Item: "C5.M5.RO1_SJJSKG", } gk.SetValue("1", "0") v, ht, err := gk.getCurrentValue() t.Log(v, ht, err) // //t.Log(e.SetValue("0", "1")) //b := <-e.WaitValue("0", 0, 3*time.Second, 0) //t.Log(b) v, ht, err = e.getCurrentValue() t.Log(v, ht, err) } func TestGetVirtualPlcItems(t *testing.T) { SetOptions(Options{GtServerIp: "47.96.12.136:8788/"}) e := VirtualPlcItem{ ProjectId: 92, PageSize: 999, } v, err := e.GetVirtualPlcItems() if v.Code == 200 { for _, line := range v.Data.List { fmt.Println(line.ItemName, line.Expression, line.IsVirtual, line.Frequency) } } t.Log(v, err) } func TestMultiEnvItem_FillCurrentValue(t *testing.T) { SetOptions(Options{GtServerIp: "120.55.44.4:8900", FetchMultiItem: false}) m := make(MultiEnvItem, 2) m["C.M.LT_CIP@out"] = &EnvItem{ ProjectId: 92, Item: "C.M.LT_CIP@out", } m["C.M.RO1_DB@time_CS_display"] = &EnvItem{ ProjectId: 92, Item: "C.M.RO1_DB@time_CS_display", } m["C.M.LT_QSC@out"] = &EnvItem{ ProjectId: 92, Item: "C.M.LT_QSC@out", } for { ovmap := m.FindPrevString() err := m.FillCurrentValue() t.Log(err) println("ovmap: ", ToString(ovmap)) println("nvmap: ", ToString(m.FindString())) time.Sleep(1 * time.Second) } } func TestMultiEnvItem_FillCurrentValue2(t *testing.T) { cache := redis.NewClient(&redis.Options{Addr: "47.96.12.136:6379", Password: "", MaxRetries: 5}) SetOptions(Options{GtServerIp: "47.96.12.136:8788", FetchMultiItem: false, Cache: cache}) 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}}` var multiEnvItems MultiEnvItem _ = json.Unmarshal([]byte(js), &multiEnvItems) t.Log(multiEnvItems) _ = multiEnvItems.FillCurrentValue() for { //_ = multiEnvItems.FillCurrentValue() multiEnvItems.ClearValues() v := multiEnvItems.GetItemFloat64Value("feed_flow") t.Logf("item: %s, %f", "feed_flow", v) //for s, item := range multiEnvItems { // if item != nil { // t.Logf("item: %s value: %f htime: %v", s, item.GetItemFloat64Val(), item.GetItemHtime()) // } else { // t.Logf("item: %s fetch failure", s) // } // //} time.Sleep(2 * time.Second) //ms := multiEnvItems.FindString() //t.Logf("%+v", multiEnvItems) //t.Logf("step: %d", multiEnvItems.GetItemInt64Value("step")) //early := checkEarly(multiEnvItems.GetItemFloat64Value("Display_Time"), multiEnvItems.GetItemFloat64Value("Filter_Time_Set"), multiEnvItems.GetItemStringValue("Step")) //advanced := checkAdvanced(multiEnvItems.GetItemFloat64Value("Display_Time"), multiEnvItems.GetItemFloat64Value("Filter_Time_Set"), multiEnvItems.GetItemStringValue("Step")) // //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")) //t.Log(err) //for s, item := range multiEnvItems { // t.Logf("item: %s value: %s htime: %s", s, item.Value, item.Htime) //} } } func TestEnvItem_Adjust(t *testing.T) { cache := redis.NewClient(&redis.Options{Addr: "47.96.12.136:6379", Password: "", MaxRetries: 5}) SetOptions(Options{GtServerIp: "47.96.12.136:8788", Cache: cache, AdjustValue: true}) e := EnvItem{ ProjectId: 92, Item: "C.M.LT_QSC@out", } //v, ht, err := e.getCurrentValue() //e.IncreAdjust(3600 * time.Second) //e.IncreAdjust(3600 * time.Second) e.IncreAdjust(3600 * time.Second) // //e.SetAdjust("65325", 3*time.Second) adjust, err := e.GetAdjustInt64Val() //t.Log(v, ht, err) t.Log(adjust, err) } func TestEnvItem_GetValueForAdjust(t *testing.T) { cache := redis.NewClient(&redis.Options{Addr: "47.96.12.136:6379", Password: "", MaxRetries: 5}) SetOptions(Options{GtServerIp: "47.96.12.136:8788", Cache: cache, AdjustValue: false}) e := EnvItem{ ProjectId: 92, Item: "C.M.UF1_DB@word_control", } v := e.GetItemInt64Val() t.Log(v) } func TestEnvItem_WaitEqValue(t *testing.T) { SetOptions(Options{GtServerIp: "47.96.12.136:8788", Cache: cache, AdjustValue: false, PlcItemSecret: "237c92d2-8795-1094-11ef-00e2e48fce4a"}) e := &EnvItem{ ProjectId: 92, Item: "C.M.UF4_DB@cycle_sp", } t.Log(e.GetItemInt64Val()) err := e.SetValue(fmt.Sprintf("%d", e.Value), "72") if err != nil { t.Log(err) } else { c := e.WaitValue("72", 0, 3*time.Second, 20) t.Log(<-c) } } func ToString(data interface{}) string { bs, _ := json.Marshal(data) return string(bs) } func TestEnvItem_WaitNotValue(t *testing.T) { SetOptions(Options{GtServerIp: "47.96.12.136:8788", PlcItemSecret: "237c92d2-8795-1094-11ef-00e2e48fce4a"}) e := &EnvItem{ ProjectId: 92, Item: "C.M.RO1_DB@control_word", } t.Log(e.GetItemInt64Val()) err := e.SetValue(fmt.Sprintf("%d", e.Value), "72") if err != nil { t.Log(err) } else { c := e.WaitNotValue([]string{"24"}, 3*time.Second, 20) t.Log(<-c) } }