示例#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
func NewNoticeFromCB(ngsi []byte, service string) (n map[string]interface{}, err error) {
	if mylog.Debugging {
		mylog.Debugf("enter NewNoticeFromCB(%s,%d)\n", ngsi, service)
		defer func() { mylog.Debugf("exit NewNotifFromCB (%+v,%v)\n", n, err) }()
	}

	n = make(map[string]interface{})
	n["noticeId"] = uuid.New()
	n["received"] = time.Now()

	var ncr NotifyContextRequest
	err = json.Unmarshal(ngsi, &ncr)
	if err != nil {
		return nil, err
	}
	mylog.Debugf("in NewNoticeFromCB NotifyContextRequest: %+v\n", ncr)

	n["id"] = ncr.ContextResponses[0].ContextElement.Id
	n["type"] = ncr.ContextResponses[0].ContextElement.Type
	n["isPattern"] = ncr.ContextResponses[0].ContextElement.IsPattern
	n["service"] = service

	//Transform name-value-type
	for _, attr := range ncr.ContextResponses[0].ContextElement.Attributes {
		n[attr.Name] = attr.Value
		n[attr.Name+"__type"] = attr.Type
	}
	n = util.FlattenMap("", n)
	n2 := make(map[string]interface{}, len(n))
	for k, v := range n {
		n2[util.ChangeDot(k)] = v
	}
	return n2, nil
}