Beispiel #1
0
// POST http://localhost:8181/api/thing
func (app *WebApplication) addThing(w http.ResponseWriter, req *http.Request) {
	body, err := ioutil.ReadAll(req.Body)
	if err != nil {
		log.Println(err)
	}
	var payload map[string]interface{}
	if err := json.Unmarshal(body, &payload); err != nil {
		panic(err)
	}

	// Create Thing Instance
	t := new(api.Thing)

	// Create ID if not assigned
	if payload["Id"] != nil {
		t.Id = payload["Id"].(string)
	} else {
		t.Id = utils.RandomString(7)
	}

	t.Name = payload["name"].(string)
	t.Description = payload["description"].(string)
	t.Group = "home"
	t.Type = payload["type"].(string)
	t.LogEvents = true
	t.Enabled = true

	// Handle Atrributes
	attrs := make(map[string]api.ThingAttributeValue, 0)
	for k, v := range payload {
		if strings.HasPrefix(k, "attrib_") {
			n := strings.Replace(k, "attrib_", "", -1)
			attr := api.NewThingAttributeValue(n, v)

			attrs[n] = attr
		}
	}
	t.Attributes = attrs

	app.thingManager.CreateThing(t)
	/*
		DatabaseId
		Descriptor  <Auto>
		Attributes  <attrib:name>

	*/

	// Check with Protocol Handler if
	// this instance already exists and return
	// error if exists

	//
}