Exemple #1
0
func InitES() (*es.ES, error) {
	host := os.Getenv("ES_HOST")
	if utils.IsEmpty(host) {
		host = "localhost"
	}

	port := os.Getenv("ES_PORT")
	if utils.IsEmpty(port) {
		port = "9200"
	}

	index := os.Getenv("ES_INDEX")
	if utils.IsEmpty(index) {
		index = es.DIndex
	}

	es, err := es.NewES(host, port)
	if err != nil {
		return nil, err
	}

	err = es.CreateIndex(index)
	if err != nil {
		return nil, err
	}

	return es, nil
}
Exemple #2
0
/**
 * Processing response
 * @param *net.UDPConn l - connection instance
 * @param dbClient - db instance
 * @param Response response - res data
 */
func ProcessResponse(l *net.UDPConn, dbClient db.DBClient, response Response) {
	// parse to CoAP struct
	rv := coap.Message{}
	err := rv.UnmarshalBinary(response.Data)
	if err == nil {
		if IsValidToken(rv.Token) {
			if rv.IsObservable() {
				// Send ACK
				if rv.IsConfirmable() {
					err := SendAck(l, response.FromAddr, rv.MessageID)
					if err != nil {
						log.Printf("Send ACK ERROR: %#v\n", err)
					}
				}
				// processing payload
				data := string(rv.Payload)
				if !utils.IsEmpty(data) {
					// Send to DB
					if dbClient != nil {
						dbClient.Processing(data)
					}
				}
			} else {
				RemoveUnObservable(&rv, response.FromAddr)
			}
		}
	}
}