Пример #1
0
func addSensors(config *config, srv *webserver.Webserver) error {
	for name, sensorConfig := range config.Sensors {
		s, err := sensor.New(sensorConfig.ID)
		if err != nil {
			return err
		}
		srv.AddSensor(name, s)
	}
	return nil
}
Пример #2
0
	Describe("the root page", func() {
		It("resturns a 200", func() {
			resp := doGetRequest(server, "/")
			Expect(resp.Code).To(Equal(http.StatusOK))

		})
	})

	Describe("reading a sensor", func() {
		var (
			sensor *dummySensor
		)

		BeforeEach(func() {
			sensor = &dummySensor{}
			server.AddSensor("one", sensor)
		})

		It("returns the sensor data as JSON", func() {
			updateTime := time.Now().Add(-3 * time.Minute)
			sensor.SetTemperature(15643, updateTime)

			resp := doGetRequest(server, "/sensors/one")
			Expect(resp.Code).To(Equal(http.StatusOK))
			Expect(resp.Header().Get("Content-Type")).To(Equal("application/json"))

			data := decodeJsonResponse(resp)
			Expect(data["temperature"]).To(BeEquivalentTo(15643))

			updateTimeStr, _ := updateTime.MarshalText()
			Expect(data["updated_at"]).To(Equal(string(updateTimeStr)))