Exemplo n.º 1
0
// FindGatewayByAppID returns CloudAddress for the gateway which matches with specified AppID
func (m *DB) FindGatewayByAppID(appID string) (common.CloudAddress, error) {
	var caddr *common.CloudAddress
	err := m.db.View(func(tx *bolt.Tx) (err error) {
		gateway := tx.Bucket([]byte(boltGatewayBucket))
		b := gateway.Get([]byte(appID))
		if b == nil {
			return common.ErrUnknownGateway
		}
		caddr, err = common.ParseCloudAddress(string(b))
		if err != nil {
			return err
		}
		return nil
	})
	if err != nil {
		return common.CloudAddress{}, err
	}
	return *caddr, nil
}
Exemplo n.º 2
0
// 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
}
Exemplo n.º 3
0
// InsertGateway inserts CloudAddress for the gateway
func (m *DB) InsertGateway(appID string, f common.OnboardGatewayFunc) (common.CloudAddress, error) {
	var pcaddr *common.CloudAddress
	// return stored common.CloudAddress if exists.
	err := m.db.View(func(tx *bolt.Tx) (err error) {
		gateway := tx.Bucket([]byte(boltGatewayBucket))
		b := gateway.Get([]byte(appID))
		if b != nil {
			p, err := common.ParseCloudAddress(string(b))
			if err != nil {
				return err
			}
			pcaddr = p
			return nil
		}
		return nil
	})
	if err != nil {
		return common.CloudAddress{}, err
	} else if pcaddr != nil {
		return *pcaddr, nil
	}
	// otherwise onboard to retrieve new CloudAddress.
	caddr, err := f(appID)
	if err != nil {
		return common.CloudAddress{}, err
	}
	err = m.db.Update(func(tx *bolt.Tx) error {
		gateway := tx.Bucket([]byte(boltGatewayBucket))
		err = gateway.Put([]byte(caddr.AppID), []byte(caddr.IDString()))
		if err != nil {
			return err
		}
		return nil
	})
	if err != nil {
		return common.CloudAddress{}, err
	}
	return caddr, nil
}