etri-smartfarm-poc-controll.../model/cache.go

70 lines
1.3 KiB
Go
Raw Normal View History

2021-11-26 01:58:47 +00:00
package model
import (
2021-12-13 05:16:05 +00:00
"errors"
2021-11-26 01:58:47 +00:00
"etrismartfarmpoccontroller/constants"
"fmt"
"io/ioutil"
"net/http"
)
2021-12-13 05:16:05 +00:00
func (s *dbHandler) StatusCheck(did string, new map[string]interface{}) bool {
origin, ok := s.states[did]
if !ok {
fmt.Println(did)
fmt.Println("insert origin, before", s.states[did])
s.states[did] = new
// origin = map[string]interface{}{}
// s.states[did] = origin
// for k, v := range new {
// origin[k] = v
// }
fmt.Println("insert origin, after", s.states[did])
fmt.Println("insert origin, new", new)
return true
}
changed := false
for k, v := range new {
if v.(float64) != origin[k].(float64) {
fmt.Println("origin, new", v, origin[k])
origin[k] = v
changed = true
}
}
return changed
}
2021-11-26 01:58:47 +00:00
func (s *dbHandler) GetSID(sname string) (string, error) {
sid, ok := s.cache[sname]
if !ok {
req, err := http.NewRequest("GET",
fmt.Sprintf("http://%s/%s", constants.Config["serverAddr"], "services"),
2021-12-13 05:16:05 +00:00
nil,
2021-11-26 01:58:47 +00:00
)
2021-12-13 05:16:05 +00:00
req.Header.Set("sname", sname)
2021-11-26 01:58:47 +00:00
if err != nil {
return "", err
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return "", err
2021-12-13 05:16:05 +00:00
} else if resp.ContentLength == 0 {
return "", errors.New("not exist service")
2021-11-26 01:58:47 +00:00
}
2021-12-13 05:16:05 +00:00
b, err := ioutil.ReadAll(resp.Body)
2021-11-26 01:58:47 +00:00
if err != nil {
return "", err
}
sid = string(b)
}
return sid, nil
}