Exemple #1
0
func moduleInstStartHandler(w http.ResponseWriter, req *http.Request) {

	w.Header().Set("Content-Type", "application/json; charset=UTF-8")

	vars := mux.Vars(req)
	id := vars["id"]
	if id == "" {
		w.Write(getJSONError("The URL should include the module instance ID", nil))
		log.Printf("The URL should include the module instance ID\n")
		return
	}

	idInt, idIntErr := strconv.ParseInt(id, 10, 32)

	if idIntErr != nil {
		w.Write(getJSONError("Error parsing id", idIntErr))
		log.Printf("Error parsing id ERR: %v\n", idIntErr)
		return
	}

	idInt32 := int32(idInt)

	modInst, modInstErr := module.GetModuleInstance(idInt32)
	if modInstErr != nil {
		w.Write(getJSONError("Unknown module id", modInstErr))
		log.Printf("Unknown module id ERR: %v\n", modInstErr)
		return
	}

	jsonBuf, jsonBufErr := json.MarshalIndent(modInst, "", "  ")
	if jsonBufErr != nil {
		w.Write(getJSONError("Error marshalling object to json", jsonBufErr))
		log.Printf("Error marshalling object to json ERR: %v\n", jsonBufErr)
		return
	}

	if modInst.IsStarted {
		w.Write(jsonBuf)
		return
	}

	startErr := module.StartModule(idInt32)

	if startErr != nil {
		w.Write(getJSONError("Error starting moudle", startErr))
		log.Printf("Error starting module ERR: %v\n", startErr)
		return
	}

	jsonBufErr = nil
	jsonBuf, jsonBufErr = json.MarshalIndent(modInst, "", "  ")
	if jsonBufErr != nil {
		w.Write(getJSONError("Error marshalling object to json", jsonBufErr))
		log.Printf("Error marshalling object to json ERR: %v\n", jsonBufErr)
		return
	}

	w.Write(jsonBuf)
}
Exemple #2
0
func moduleInstStartHandler(w http.ResponseWriter, req *http.Request) {

	w.Header().Set("Content-Type", "application/json; charset=UTF-8")

	vars := mux.Vars(req)
	name := vars["name"]
	if name == "" {
		w.Write(getJSONError("The URL should include the module instance name", nil))
		log.Printf("The URL should include the module instance name\n")
		return
	}

	modInst, modInstErr := module.GetModuleInstance(name)
	if modInstErr != nil {
		w.Write(getJSONError("Unknown module name", modInstErr))
		log.Printf("Unknown module name ERR: %v\n", modInstErr)
		return
	}

	jsonBuf, jsonBufErr := json.MarshalIndent(modInst, "", "  ")
	if jsonBufErr != nil {
		w.Write(getJSONError("Error marshalling object to json", jsonBufErr))
		log.Printf("Error marshalling object to json ERR: %v\n", jsonBufErr)
		return
	}

	if modInst.IsStarted {
		w.Write(jsonBuf)
		return
	}

	startErr := module.StartModule(name)

	if startErr != nil {
		w.Write(getJSONError("Error starting moudle", startErr))
		log.Printf("Error starting module ERR: %v\n", startErr)
		return
	}

	jsonBufErr = nil
	jsonBuf, jsonBufErr = json.MarshalIndent(modInst, "", "  ")
	if jsonBufErr != nil {
		w.Write(getJSONError("Error marshalling object to json", jsonBufErr))
		log.Printf("Error marshalling object to json ERR: %v\n", jsonBufErr)
		return
	}

	w.Write(jsonBuf)
}
Exemple #3
0
func moduleInstPostHandler(w http.ResponseWriter, req *http.Request) {
	typeID := req.FormValue("typeId")
	address := req.FormValue("address")
	cfgJSON := req.FormValue("config")
	start := req.FormValue("start")

	log.WithFields(log.Fields{
		"typeId":  typeID,
		"address": address,
		"start":   start,
		"config":  cfgJSON,
	}).Debug("received module instance post request")

	cfg, cfgErr := config.FromJSON(cfgJSON)
	if cfgErr != nil {
		w.Write(getJSONError("Error unmarshalling config json", nil))
		log.Printf("Error unmarshalling config json\n")
		return
	}

	modConfig := &module.Config{
		Address: address,
		Config:  cfg,
	}

	w.Header().Set("Content-Type", "application/json; charset=UTF-8")

	if typeID == "" || address == "" {
		w.Write(getJSONError("Fields typeId and address are required", nil))
		log.Printf("Fields typeId and address are required\n")
		return
	}

	typeIDInt, typeIDIntErr := strconv.ParseInt(typeID, 10, 32)
	if typeIDIntErr != nil {
		w.Write(getJSONError("Error parsing typeId", typeIDIntErr))
		log.Printf("Error parsing typeId ERR: %v\n", typeIDIntErr)
		return
	}

	typeIDInt32 := int32(typeIDInt)

	modInst, initErr := module.InitializeModule(typeIDInt32, modConfig)
	if initErr != nil {
		w.WriteHeader(http.StatusBadRequest)
		log.Printf("Error initializing module ERR: %v\n", initErr)
		return
	}

	jsonBuf, jsonBufErr := json.MarshalIndent(modInst, "", "  ")
	if jsonBufErr != nil {
		w.Write(getJSONError("Error marshalling object to json", jsonBufErr))
		log.Printf("Error marshalling object to json ERR: %v\n", jsonBufErr)
		return
	}

	startBool, startBoolErr := strconv.ParseBool(start)
	if startBoolErr != nil {
		startBool = false
	}

	if startBool {
		startErr := module.StartModule(modInst.ID)
		if startErr != nil {
			w.Write(getJSONError("Error starting module", startErr))
			log.Printf("Error starting module ERR: %v\n", startErr)
			return
		}

		jsonBufErr = nil
		jsonBuf, jsonBufErr = json.MarshalIndent(modInst, "", "  ")
		if jsonBufErr != nil {
			w.Write(getJSONError("Error marshalling object to json", jsonBufErr))
			log.Printf("Error marshalling object to json ERR: %v\n", jsonBufErr)
			return
		}
		w.Write(jsonBuf)
	} else {
		w.Write(jsonBuf)
	}
}