示例#1
0
// FindByEndnodeAddress returns an item which matches with endnode address.
func (db *DB) FindByEndnodeAddress(a common.EndnodeAddress) (common.MapItem, error) {
	n, ok := db.endnodeIndex[a.IDString()]
	if !ok {
		return common.MapItem{}, common.ErrUnknownEndnode
	}
	return db.items[n], nil
}
示例#2
0
// UpsertEndnode updates or inserts a end-node with data.
func (db *DB) UpsertEndnode(ea common.EndnodeAddress, data []byte) (common.MapItem, error) {
	db.lock.Lock()
	defer db.lock.Unlock()

	// update data for a known end-node.
	id := ea.IDString()
	n, ok := db.endnodeIndex[id]
	if ok {
		db.items[n].States = data
		db.dumpTable()
		return db.items[n], nil
	}

	// check capacity.
	n = len(db.items)
	if n >= cap(db.items) {
		return common.MapItem{}, common.ErrTooManyEndnode
	}

	// insert a new end-node and its data.
	db.items = append(db.items, common.MapItem{
		EndnodeAddress: &ea,
		States:         data,
	})
	db.endnodeIndex[id] = n
	db.dumpTable()
	return db.items[n], nil
}
示例#3
0
// RegisterEndnode registers cloud address for a end-node.
func (m *Mapper) RegisterEndnode(ea common.EndnodeAddress, ca common.CloudAddress) error {
	item, err := m.Store.UpdateCloud(ea, ca)
	if err != nil {
		return err
	}
	m.Log.Printf("mapped: endnodeID=%s cloudID=%s\n", ea.IDString(), ca.IDString())
	return m.Cloud.SendState(*item.CloudAddress, item.States)
}
示例#4
0
// FindByEndnodeAddress returns an item which matches with endnode address.
func (m *DB) FindByEndnodeAddress(ea common.EndnodeAddress) (common.MapItem, error) {
	var item common.MapItem
	err := m.db.View(func(tx *bolt.Tx) (err error) {
		item, err = m.getMapItem(tx, []byte(ea.IDString()))
		return err
	})
	if err != nil {
		return common.MapItem{}, err
	}
	return item, nil
}
示例#5
0
// UpdateThingProperties updates thing properties of endnode to locale DB.
func (m *Mapper) UpdateThingProperties(a common.EndnodeAddress, d []byte) error {
	item, err := m.Store.UpsertEndnodeProperties(a, d)
	if err != nil {
		return err
	}
	if item.CloudAddress == nil {
		m.Log.Printf("pended: endnodeID=%s ThingProperties=%s\n",
			a.IDString(), util.DataToString(d))
	}
	return nil
}
示例#6
0
// UpdateStatus updates status of endnode and send it to cloud if need.
func (m *Mapper) UpdateStatus(a common.EndnodeAddress, d []byte) error {
	item, err := m.Store.UpsertEndnode(a, d)
	if err != nil {
		return err
	}
	if item.CloudAddress == nil {
		m.Log.Printf("pended: endnodeID=%s data=%s\n",
			a.IDString(), util.DataToString(d))
		return nil
	}
	return m.Cloud.SendState(*item.CloudAddress, item.States)
}
示例#7
0
// UpdateCloud updates CloudAddress field of a end-node.
func (db *DB) UpdateCloud(ea common.EndnodeAddress, caddr common.CloudAddress) (common.MapItem, error) {
	db.lock.Lock()
	defer db.lock.Unlock()

	n, ok := db.endnodeIndex[ea.IDString()]
	if !ok {
		return common.MapItem{}, common.ErrUnknownEndnode
	}
	db.items[n].CloudAddress = &caddr
	db.cloudIndex[caddr.IDString()] = n
	db.dumpTable()
	return db.items[n], nil
}
示例#8
0
func testUpdateCloud(t *testing.T, s mapper.Store, ea common.EndnodeAddress, ca common.CloudAddress) common.MapItem {
	m, err := s.UpdateCloud(ea, ca)
	if err != nil {
		t.Fatal("UpdateCloud failed:", err)
	}
	if m.EndnodeAddress == nil || m.EndnodeAddress.IDString() != ea.IDString() {
		t.Fatalf("UpdateCloud modified EndnodeAddress: %#v", m.EndnodeAddress)
	}
	if m.CloudAddress == nil || m.CloudAddress.IDString() != ca.IDString() {
		t.Fatalf("UpdateCloud returned unexpected CloudAddress: %#v",
			m.CloudAddress)
	}
	return m
}
示例#9
0
func testUpsertEndnodeProperties(t *testing.T, s mapper.Store, ea common.EndnodeAddress, d []byte) common.MapItem {
	m, err := s.UpsertEndnodeProperties(ea, d)
	if err != nil {
		t.Fatal("UpsertEndnodeProperties failed:", err)
	}
	if m.EndnodeAddress == nil || m.EndnodeAddress.IDString() != ea.IDString() {
		t.Fatalf("UpsertEndnode returns unexpected MapItem: %#v",
			m.EndnodeAddress)
	}
	if !bytes.Equal(m.ThingProperties, d) {
		t.Fatalf("UpsertEndnode store wrong data: %#v (expecetd:%#v)",
			m.States, d)
	}
	return m
}
示例#10
0
// UpdateCloud updates CloudAddress field of a end-node.
func (m *DB) UpdateCloud(ea common.EndnodeAddress, ca common.CloudAddress) (common.MapItem, error) {
	var item common.MapItem
	err := m.db.Update(func(tx *bolt.Tx) (err error) {
		item, err = m.getMapItem(tx, []byte(ea.IDString()))
		if err != nil {
			return err
		}
		item.CloudAddress = &ca
		return m.putMapItem(tx, item)
	})
	if err != nil {
		return common.MapItem{}, err
	}
	return item, nil
}
示例#11
0
// UpdateEndnode update EndnodeAddress for existing endnode
func (db *DB) UpdateEndnode(newEA common.EndnodeAddress, caddr common.CloudAddress) (common.MapItem, error) {
	db.lock.Lock()
	defer db.lock.Unlock()

	// retrieve mapItem by cloudAddress
	item, err := db.FindByCloudAddress(caddr)
	if err != nil {
		return common.MapItem{}, err
	}
	n := db.cloudIndex[caddr.IDString()]
	// update endnodeIndex
	delete(db.endnodeIndex, item.EndnodeAddress.IDString())
	db.endnodeIndex[newEA.IDString()] = n

	// update mapitem
	db.items[n].EndnodeAddress = &newEA
	db.dumpTable()
	return db.items[n], nil
}
示例#12
0
// UpsertEndnodeProperties updates or inserts a end-node with thing properties.
func (m *DB) UpsertEndnodeProperties(ea common.EndnodeAddress, data []byte) (common.MapItem, error) {
	var item common.MapItem
	err := m.db.Update(func(tx *bolt.Tx) (err error) {
		item, err = m.getMapItem(tx, []byte(ea.IDString()))
		if err != nil {
			if err != common.ErrUnknownEndnode {
				return err
			}
			item = common.MapItem{
				EndnodeAddress: &ea,
			}
		}
		item.ThingProperties = data
		return m.putMapItem(tx, item)
	})
	if err != nil {
		return common.MapItem{}, err
	}
	return item, nil
}