예제 #1
0
파일: Setup.go 프로젝트: ThingFu/hub
func loadRules(env api.Environment) {
	log.Println("[INFO] Dividing by Zero")
	rulesService := container.Instance().RulesManager()
	filepath.Walk(env.GetHome()+"/rules", func(path string, f os.FileInfo, err error) error {
		if !f.IsDir() {
			content, err := ioutil.ReadFile(path)
			if err != nil {
				panic(err)
			}

			var rule api.Rule
			err = json.Unmarshal(content, &rule)

			if err != nil {
				log.Printf("[ERROR] Parsing Rule %s\n", path)
				return nil
			}
			rule.Id = utils.RandomString(7)

			rule.Path = path[strings.LastIndex(path, "/")+1:]

			log.Printf("[INFO] Registering rule file: %s\n", rule.Name)

			rulesService.RegisterRule(rule)
		}
		return nil
	})
}
예제 #2
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

	//
}