// FindNonNilCloud returns items which have mapped to cloud. func (m *DB) FindNonNilCloud(serverLocation, appID string) ([]common.MapItem, error) { var onboardedItems []common.MapItem err := m.db.View(func(tx *bolt.Tx) (err error) { items := tx.Bucket([]byte(boltItemBucket)) onboardedItems = make([]common.MapItem, 0) err = items.ForEach(func(k, v []byte) error { enAdr, err1 := common.ParseEndnodeAddress(string(k)) if err1 != nil { return err1 } if enAdr.IsMatch(serverLocation, appID) { item, err1 := m.unmarshalMapItem(v) if err1 != nil { return err1 } if item.CloudAddress != nil { onboardedItems = append(onboardedItems, item) } } return nil }) return err }) if err != nil { return nil, err } return onboardedItems, nil }
// unmarshalMapItem deserializes into a common.MapItem (using JSON). func (m *DB) unmarshalMapItem(b []byte) (common.MapItem, error) { var v mapItemJSON err := json.Unmarshal(b, &v) if err != nil { return common.MapItem{}, err } item := common.MapItem{States: v.State, ThingProperties: v.ThingProperties} if v.EndnodeID != nil { item.EndnodeAddress, err = common.ParseEndnodeAddress(*v.EndnodeID) if err != nil { return common.MapItem{}, err } } if v.CloudID != nil { item.CloudAddress, err = common.ParseCloudAddress(*v.CloudID) if err != nil { return common.MapItem{}, err } } return item, nil }