示例#1
0
func (ac *controller) Post(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
	defer r.Body.Close()
	mylog.Debug("Action received")
	body, err := ioutil.ReadAll(r.Body)
	if err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}
	var object map[string]interface{}
	err = json.Unmarshal(body, &object)
	if err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}
	object = util.FlattenMap("", object)

	text, _ := json.MarshalIndent(object, "", " ")
	mylog.Debug("action text", string(text))
	var axn Action
	switch actionID := object["iotcepaction"].(type) {
	case string:
		axn = Find(actionID)
		if axn == nil {
			http.Error(w, fmt.Sprintf("unknown action %q ", actionID), http.StatusNotFound)
			return
		}
		axn.Do(object)
	}

	fmt.Fprintln(w, "Perfect for now")
}
示例#2
0
文件: sms.go 项目: robertocs/cepiot
func (a *SMSAction) Do(n notices.Notice) (err error) {
	if mylog.Debugging {
		defer func() { mylog.Debugf("exit SMSAction.Do  %+v", err) }()
	}

	text := util.ExpandFromMap(a.ActionData.Template, n)
	mylog.Debug("SMSAction text to send:", text)
	msg := fmt.Sprintf(`{"to":["tel:%s"], "message": %q}`, a.Parameters["to"], text)
	mylog.Debug("SMSAction msg to send:", msg)
	req, err := http.NewRequest("POST", config.SMSEndpoint(), strings.NewReader(msg))
	if err != nil {
		mylog.Alert("SMSAction.Do", err)
	}
	req.Header.Add("API_KEY", config.APIKey())
	req.Header.Add("API_SECRET", config.APISecret())
	req.Header.Add("Content-Type", "application/json")
	mylog.Debug("SMSAction.Do msg:", msg)
	resp, err := httpClient.Do(req)
	if err != nil {
		mylog.Alert("SMSAction.Do", err)
		return err
	}
	respDump, err := httputil.DumpResponse(resp, true)
	mylog.Debugf("SMSAction.Do server response: (%q,%v)\n", string(respDump), err)
	return err
}
示例#3
0
func (a *UpdateAction) Do(n notices.Notice) (err error) {

	// A litle (or very) dirty. Add "hidden" parameters as notification data
	// and remove them after executing template (better copy first level fields in a new map?)
	n["__attrName"] = a.ActionData.Parameters["name"]
	n["__attrValue"] = a.ActionData.Parameters["value"]
	n["__attrType"] = a.ActionData.Parameters["type"]
	text := util.ExpandFromMap(updateTemplateText, n)
	mylog.Debug("UpdateAction text to send:", text)
	delete(n, "__attrName")
	delete(n, "__attrValue")
	delete(n, "__attrType")

	req, err := http.NewRequest("POST", config.UpdateEndpoint(), strings.NewReader(text))
	if err != nil {
		mylog.Alert("UpdateAction.Do", err)
	}
	req.Header.Add("Content-Type", "application/json")
	resp, err := httpClient.Do(req)
	if err != nil {
		mylog.Alert("UpdateAction.Do", err)
		return err
	}
	respDump, err := httputil.DumpResponse(resp, true)
	mylog.Debugf("UpdateAction.Do server response: (%q,%v)\n", string(respDump), err)
	return err
}
示例#4
0
文件: rule.go 项目: robertocs/cepiot
func Delete(name string) (err error) {
	s := session.Clone()
	defer s.Close()
	c := session.DB("test").C("rules")
	info, err := c.RemoveAll(bson.M{"name": name})
	mylog.Debug(info, err)
	return err
}
示例#5
0
func (nc *controller) Post(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
	defer r.Body.Close()
	service := r.Header.Get("fiware-service")

	data, err := ioutil.ReadAll(r.Body)
	if err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}
	n, err := NewNoticeFromCB(data, service)
	if err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}
	mylog.Debug(n)
	text, err := json.Marshal(n)
	mylog.Debug(string(text))
	if err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}
	resp, err := http.Post(config.NoticeEndpoint(), "application/json", bytes.NewReader(text))
	mylog.Debug(resp, err)
}