Пример #1
0
func TestIniNewSerialDevices(t *testing.T) {
	assert := assert.New(t)

	conf, err := inidef.LoadConfig("testing_conf.ini")
	brokerList, err := broker.NewBrokers(conf, make(chan message.Message))
	assert.Nil(err)
	deviceList, _, err := device.NewDevices(conf, brokerList)
	assert.Nil(err)
	assert.Equal(3, len(deviceList))
}
Пример #2
0
// StartByFileWithChannel starts Gateway with command Channel
func StartByFileWithChannel(conf inidef.Config, commandChannel chan string) error {
	gw, err := gateway.NewGateway(conf)
	if err != nil {
		log.Fatalf("gateway create error, %v", err)
	}
	brokerList, err := broker.NewBrokers(conf, gw.BrokerChan)
	if err != nil {
		log.Fatalf("broker(s) create error, %v", err)
	}
	deviceList, deviceChannels, err := device.NewDevices(conf, brokerList)
	if err != nil {
		log.Fatalf("device create error, %v", err)
	}

	gw.Devices = deviceList
	gw.Brokers = brokerList
	gw.CmdChan = commandChannel
	gw.DeviceChannels = deviceChannels

	status, err := device.NewStatus(conf)
	if err != nil {
		log.Warnf("status create error, %v", err)
		// run whenever status created
	} else {
		gw.Devices = append(gw.Devices, status)
	}

	// add to brokers subscribed
	for _, device := range gw.Devices {
		err := device.AddSubscribe()
		if err != nil {
			log.Errorf("device subscribe error, %v", err)
			continue
		}
	}

	// Start brokers and devices
	for _, b := range gw.Brokers {
		err := b.MQTTClientSetup(gw.Name)
		if err != nil {
			log.Errorf("MQTTClientSetup failed, %v", err)
			continue
		}
	}
	for _, device := range gw.Devices {
		err := device.Start(gw.MsgChan)
		if err != nil {
			log.Errorf("device start error, %v", err)
			continue
		}
	}

	// start gateway
	return gw.Start()
}
Пример #3
0
// generalIniRetainSerialDeviceTest checks retain function with serial device
func generalIniRetainSerialDeviceTest(test iniRetainTestCase, t *testing.T) {
	assert := assert.New(t)

	conf, err := inidef.LoadConfigByte([]byte(test.iniStr))
	assert.Nil(err)

	brokers, err := broker.NewBrokers(conf, make(chan message.Message))
	assert.Nil(err)

	devices, _, err := device.NewDevices(conf, brokers)
	assert.Nil(err)
	assert.Equal(1, len(devices))
}