vd_server.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/edgexfoundry/go-mod-core-contracts/clients"
  6. "github.com/edgexfoundry/go-mod-core-contracts/models"
  7. "log"
  8. "net/http"
  9. )
  10. const (
  11. testValueDesciptorDescription1 = "Temperature descriptor1"
  12. testValueDesciptorDescription2 = "Humidity descriptor2"
  13. )
  14. var testValueDescriptor1 = models.ValueDescriptor{Id: "Temperature", Created: 123, Modified: 123, Origin: 123, Name: "Temperature",
  15. Description: "test description", Min: -70, Max: 140, DefaultValue: 32, Formatting: "%d", Type:"STRING",
  16. Labels: []string{"temp", "room temp"}, UomLabel: "F", MediaType: clients.ContentTypeJSON, FloatEncoding: "eNotation"}
  17. var testValueDescriptor2 = models.ValueDescriptor{Id: "Humidity", Created: 123, Modified: 123, Origin: 123, Name: "Humidity",
  18. Description: "test description", Min: -70, Max: 140, DefaultValue: 32, Formatting: "%d", Type:"INT",
  19. Labels: []string{"humi", "room humidity"}, UomLabel: "F", MediaType: clients.ContentTypeJSON, FloatEncoding: "eNotation"}
  20. func main() {
  21. http.HandleFunc(clients.ApiValueDescriptorRoute, Hello)
  22. if e := http.ListenAndServe(":8080", nil); e != nil {
  23. log.Fatal(e)
  24. }
  25. }
  26. func Hello(w http.ResponseWriter, req *http.Request) {
  27. descriptor1 := testValueDescriptor1
  28. descriptor1.Description = testValueDesciptorDescription1
  29. descriptor2 := testValueDescriptor2
  30. descriptor2.Description = testValueDesciptorDescription2
  31. descriptors := []models.ValueDescriptor{descriptor1, descriptor2}
  32. data, err := json.Marshal(descriptors)
  33. if err != nil {
  34. fmt.Errorf("marshaling error: %s", err.Error())
  35. }
  36. if _, err := fmt.Fprintf(w, "%s", data); err != nil {
  37. log.Fatal(err)
  38. }
  39. //_, _ = w.Write(data)
  40. }