Пример #1
0
// Rception POST with NGSI update from Orion
// It computes distance from image and updates:
// - apiweb via POST
// - Orion via POST UpdateContext with mean distance field and distance image
func (uc UpdateController) OnUpdateContext(c *gin.Context) {
	var status int
	var details string
	// This method receive JSON POST into NGSI format that contains update data to be processed..
	cr := gongsi.NotifyContextRequest{}

	//////////////////////////////////////////////////////////////////////////
	// Bind JSON into interval structure NotifyContextRequest
	if err := c.Bind(&cr); err != nil {
		log.Println("Error in request:", err)
		status = http.StatusBadRequest
		details = "JSON parsing error"
	} else {
		// Depending on ID send channel updates....
		Id, _ := gongsiutil.GetId(&cr)
		_ = Id
		//		battery, _ := gongsiutil.FindAttribute(&cr, string("battery_c"))
		//		temperature, _ := gongsiutil.FindAttribute(&cr, string("temperature_c"))
		//		capacity, _ := gongsiutil.FindAttribute(&cr, string("available_percentage"))
		distance, _ := gongsiutil.FindAttribute(&cr, string("mean_distance"))
		d, _ := conversions.Float64ForValue(distance)

		data := models.SiloData{Distance: d, Temperature: 35, Humidity: 40}
		h.unicast <- DirectMessage{Id, data}
	}

	//////////////////////////////////////////////////////////////////////////
	// Work with data sent
	response := gongsi.NotifyContextResponse{}
	sc := gongsi.StatusCode{}
	sc.Code = status
	sc.Details = details
	response.ResponseCode = sc

	// Marshal provided interface into XML structure
	uj, _ := json.Marshal(response)

	// Write content-type, statuscode, payload
	c.JSON(http.StatusCreated, uj)
}
Пример #2
0
//----------------------------------------------------------------------------
// Websocket
//----------------------------------------------------------------------------
func serveWs(w http.ResponseWriter, r *http.Request) {
	/*	if r.Method != "GET" {
		http.Error(w, "Method not allowed", 405)
		return
	}*/
	ws, err := upgrader.Upgrade(w, r, nil)
	if err != nil {
		if _, ok := err.(websocket.HandshakeError); !ok {
			log.Println(err)
		} else {
		}
		return
	}

	currentStatus := models.WebsocketData{
		Timestamp: time.Now().Unix() * 1000,
		Analog: models.AnalogData{
			Percentage:    0,
			Capacity:      0,
			WeightCurrent: 0,
			VolumeCurrent: 0,
		},
	}

	status_str := ""
	if b, err := json.Marshal(currentStatus); err == nil {
		status_str = string(b)
	}

	// Params POST:
	//   as: Altura Silo
	//   ds: Diametro Silo
	//   cs: Cono Silo
	//   d: Densidad material
	//   od : Offset distance. Distáncia desde el sensor al contenido(lleno)
	//   device: Numero de serie dispositivo
	// Sample:
	//   as=578&ds=340&cs=204&od=68&d=680.0&device=UBKD334F21E-3C23-11E5-8494-C3AD4A89321E
	as, _ := conversions.IntForValue(r.FormValue("as"))
	ds, _ := conversions.IntForValue(r.FormValue("ds"))
	cs, _ := conversions.IntForValue(r.FormValue("cs"))
	od, _ := conversions.IntForValue(r.FormValue("od"))
	dens, _ := conversions.Float64ForValue(r.FormValue("d"))

	fmt.Println(r.FormValue("as"), as, ds, cs, od, dens)
	q := models.SiloMeasureSimpleRequest{
		SiloHeightCm:         as,
		SiloDiameterCm:       ds,
		SiloHeightConeCm:     cs,
		SiloOffsetDistanceCm: od,
		ContentDensityKgm3:   dens,
	}

	c := &client{
		send:   make(chan models.SiloData, maxMessageSize),
		status: status_str,
		ws:     ws,
		sn:     r.FormValue("device"),
		config: q,
	}

	h.register <- c
	go c.writer(r.FormValue("device"))
	c.reader()
}