Esempio n. 1
0
// Strings converts all fields into string array to print.
func (m MapItem) Strings(n int) []string {
	did := "(none)"
	if m.EndnodeAddress != nil {
		did = m.EndnodeAddress.IDString()
	}
	cid := "(none)"
	if m.CloudAddress != nil {
		cid = m.CloudAddress.IDString()
	}
	return []string{strconv.Itoa(n), did, cid, util.DataToString(m.States), util.DataToString(m.ThingProperties)}
}
Esempio n. 2
0
func (mn *mqttConverter) OnPublish(m *mqttserver.Message) error {
	t := mqttTopicRx.FindStringSubmatch(m.Topic)
	if len(t) != 5 {
		mn.p.log.Printf("WARN: ignored message topic:%s payload:%s",
			m.Topic, util.DataToString(m.Body))
		return nil
	}
	if t[1] != mn.id.serverLocation || t[2] != mn.id.appID {
		mn.p.log.Printf("WARN: ignored message topic:%s payload:%s",
			m.Topic, util.DataToString(m.Body))
	}

	enAddr := common.EndnodeAddress{
		ServerLocation: t[1],
		AppID:          t[2],
		VendorThingID:  t[3],
	}
	data, err := mn.sanitize(m.Body)
	if err != nil {
		mn.p.log.Printf("WARN: broken JSON topic:%s payload:%s",
			m.Topic, util.DataToString(m.Body))
		return nil
	}
	switch t[4] {
	case "states":
		err := mn.mapper.UpdateStatus(enAddr, data)
		if err != nil {
			mn.p.log.Println("WARN: failed to update states:", err)
		}
	case "commandResults":
		err := mn.mapper.RespondToCommand(enAddr, data)
		if err != nil {
			mn.p.log.Println("WARN: failed to respond command:", err)
		}
	case "connect":
		mn.endnodes[enAddr.VendorThingID] = enAddr
		err := mn.mapper.UpdateThingProperties(enAddr, data)
		if err != nil {
			mn.p.log.Println("WARN: failed to update thing properties:", err)
		}
		err = mn.mapper.ReportEndnodeStatus(enAddr, true)
		if err != nil {
			mn.p.log.Println("WARN: failed to report online:", err)
		}
	case "disconnect":
		mn.disconnectEndnode(enAddr.VendorThingID)
	default:
		mn.p.log.Printf("WARN: unknown operation: %s\n", t[4])
	}
	return nil
}
Esempio n. 3
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
}
Esempio n. 4
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)
}