Skip to content
onem2m-mgr.go 36.5 KiB
Newer Older
Yann Garcia's avatar
Yann Garcia committed
	} else {
		log.Error("oneM2M_deserialize: CharacteristicName not found")
Yann Garcia's avatar
Yann Garcia committed
	}

	for i, m := range response {
		log.Debug("==> ", i, " value is ", m)
		// m is a map[string]interface.
		// loop over keys and values in the map.
		for k, v := range m {
			log.Debug(k, " value is ", v)
			log.Debug("oneM2M_deserialize: type(v): ", reflect.TypeOf(v))

			if k == "ri" {
				if item, ok := v.(string); ok {
					sensor.SensorIdentifier = item
				} else {
					log.Error("populateSensors: Failed to process ", k)
				}
			} else if k == "ty" {
				if item, ok := v.(string); ok {
					sensor.SensorType = item
				} else {
					log.Error("populateSensors: Failed to process ", k)
				}
Yann Garcia's avatar
Yann Garcia committed
			} else {
				if item, ok := v.(string); ok {
					sensorResp.SensorCharacteristicList = append(
						sensorResp.SensorCharacteristicList,
						SensorCharacteristic{
							CharacteristicName:  k,
							CharacteristicValue: string(item),
						})
				} else if item, ok := v.(float64); ok {
					sensorResp.SensorCharacteristicList = append(
						sensorResp.SensorCharacteristicList,
						SensorCharacteristic{
							CharacteristicName:  k,
							CharacteristicValue: strconv.FormatFloat(item, 'f', -1, 64),
						})
				} else if item, ok := v.(int64); ok {
					sensorResp.SensorCharacteristicList = append(
						sensorResp.SensorCharacteristicList,
						SensorCharacteristic{
							CharacteristicName:  k,
							CharacteristicValue: strconv.FormatInt(item, 10),
						})
				} else if item, ok := v.(bool); ok {
					sensorResp.SensorCharacteristicList = append(
						sensorResp.SensorCharacteristicList,
						SensorCharacteristic{
							CharacteristicName:  k,
							CharacteristicValue: strconv.FormatBool(item),
						})
				} else if item, ok := v.([]string); ok {
					sensorResp.SensorCharacteristicList = append(
						sensorResp.SensorCharacteristicList,
						SensorCharacteristic{
							CharacteristicName:  k,
							CharacteristicValue: strings.Join(item, ","),
						})
				} else if item, ok := v.([]int64); ok {
					log.Error("oneM2M_deserialize: Failed to convert list of int64 into string: ", item)
				} else if item, ok := v.([]interface{}); ok {
					log.Debug("populateSensors: Got []interface {} for ", k)
					log.Debug("populateSensors: ValueOf ", reflect.ValueOf(item))
					s := SensorCharacteristic{
						CharacteristicName: k,
					}
					var buf bytes.Buffer
					fmt.Fprintf(&buf, "%T", item)
					s.CharacteristicValue = buf.String()
					sensor.SensorCharacteristicList = append(sensor.SensorCharacteristicList, s)
				} else {
					log.Error("oneM2M_deserialize: Failed to process: ", k)
				}
Yann Garcia's avatar
Yann Garcia committed
			}
		} // End of 'for' loop

	} // End of 'for' loop

	return sensorResp, nil
Yann Garcia's avatar
Yann Garcia committed
}