Ejemplo n.º 1
0
func (m *ThingModel) Create(thing *model.Thing, conn redis.Conn) error {
	m.syncing.Wait()
	//defer m.sync()

	if thing.ID == "" {
		thing.ID = uuid.NewRandom().String()
	}

	_, err := m.save(thing.ID, thing, conn)

	return err
}
Ejemplo n.º 2
0
func (m *ThingModel) onFetch(thing *model.Thing, syncing bool, conn redis.Conn) error {
	deviceID, err := m.GetDeviceIDForThing(thing.ID, conn)

	if err != nil && err != RecordNotFound {
		return fmt.Errorf("Failed to fetch device id for thing (id:%s) : %s", thing.ID, err)
	}

	if deviceID != nil {

		thing.DeviceID = deviceID

		if !syncing {
			device, err := m.DeviceModel.Fetch(*deviceID, conn)
			if err != nil {
				return fmt.Errorf("Failed to fetch nested device (id:%s) on thing %s : %s", *deviceID, thing.ID, err)
			}
			thing.Device = device
		}
	}

	return nil
}