Browse Source

add ability to start a fake temp-humidity sensor

Mark deVilliers 2 years ago
parent
commit
f65261c5c0
2 changed files with 25 additions and 9 deletions
  1. 23 8
      node/api/devices.go
  2. 2 1
      node/api/metadata_store.go

+ 23 - 8
node/api/devices.go

@@ -8,6 +8,7 @@ import (
8 8
 	"gogs.dyne.org/DECODE/decode-prototype-da/utils"
9 9
 	validator "gopkg.in/validator.v2"
10 10
 
11
+	randomdata "github.com/Pallinder/go-randomdata"
11 12
 	restful "github.com/emicklei/go-restful"
12 13
 	restfulspec "github.com/emicklei/go-restful-openapi"
13 14
 	uuid "github.com/satori/go.uuid"
@@ -32,13 +33,14 @@ func NewDeviceService(ctx context.Context, entitlementStore *EntitlementStore, m
32 33
 }
33 34
 
34 35
 type DeviceRequest struct {
35
-	Type        string `json:"type" description:"type of device" validate:"nonzero"`
36
-	Description string `json:"description" description:"description of device" validate:"nonzero"`
36
+	Type string `json:"type" description:"type of device" validate:"nonzero"`
37 37
 }
38 38
 
39 39
 type DeviceResponse struct {
40 40
 	DeviceRequest
41
-	UID string `json:"uid" description:"unique identifier for the device"`
41
+	UID         string `json:"uid" description:"unique identifier for the device"`
42
+	Name        string `json:"name" description:"unique name for the device" `
43
+	Description string `json:"description" description:"information about the device" `
42 44
 }
43 45
 
44 46
 func (e deviceResource) WebService() *restful.WebService {
@@ -100,19 +102,32 @@ func (e deviceResource) newDevice(request *restful.Request, response *restful.Re
100 102
 			Status: Accepted,
101 103
 		},
102 104
 	)
105
+	var description string
103 106
 
104
-	// add the metadata to the catalog
105
-	e.metaStore.Add(Metadata{Subject: subject.String(), Description: req.Description})
106
-
107
-	// start accepting from the sensor
108
-	if req.Type == "sine" {
107
+	switch req.Type {
108
+	case "fake-sine":
109
+		description = "fake device producing a sine curve"
109 110
 		sensor := sensors.NewSineCurveEmitterSensor(e.ctx, uid, e.out)
110 111
 		go sensor.Start()
112
+	case "fake-temp-humidity":
113
+		description = "fake device producing temperature and humidity values"
114
+		sensor := sensors.NewTemperatureHumiditySensor(e.ctx, uid, e.out)
115
+		go sensor.Start()
116
+
117
+	default:
118
+		response.WriteHeaderAndEntity(http.StatusBadRequest, ErrorResponse{Error: "unknown device type"})
119
+		return
111 120
 	}
112 121
 
122
+	name := randomdata.SillyName()
123
+	// add the metadata to the catalog
124
+	e.metaStore.Add(Metadata{Subject: subject.String(), Description: description, Name: name})
125
+
113 126
 	resp := DeviceResponse{
114 127
 		DeviceRequest: req,
115 128
 		UID:           uid,
129
+		Description:   description,
130
+		Name:          name,
116 131
 	}
117 132
 
118 133
 	// TODO : add lock

+ 2 - 1
node/api/metadata_store.go

@@ -8,7 +8,8 @@ import (
8 8
 
9 9
 type Metadata struct {
10 10
 	Description string `json:"description" description:"human readable description of the data"`
11
-	Subject     string `json:"subject" description:"unique name for the data"`
11
+	Subject     string `json:"subject" description:"description of the data"`
12
+	Name        string `json:"name" description:"name of the data"`
12 13
 }
13 14
 
14 15
 func NewMetadataStore() *MetadataStore {