예제 #1
0
파일: ini_test.go 프로젝트: chansuke/fuji
func TestIniNewGateway(t *testing.T) {
	assert := assert.New(t)

	conf, err := inidef.LoadConfig("testing_conf.ini")
	assert.Nil(err)
	gw, err := gateway.NewGateway(conf)
	assert.Nil(err)
	assert.Equal("ham", gw.Name)
}
예제 #2
0
파일: app.go 프로젝트: chansuke/fuji
// 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
파일: will_test.go 프로젝트: chansuke/fuji
func genericWillTestDriver(t *testing.T, iniStr string, expectedTopic string, expectedPayload []byte) (ok bool) {
	assert := assert.New(t)

	conf, err := inidef.LoadConfigByte([]byte(iniStr))
	assert.Nil(err)
	commandChannel := make(chan string)
	go fuji.StartByFileWithChannel(conf, commandChannel)

	gw, err := gateway.NewGateway(conf)
	assert.Nil(err)

	brokers, err := broker.NewBrokers(conf, gw.BrokerChan)
	assert.Nil(err)

	go func() {
		time.Sleep(1 * time.Second)

		subscriberChannel, err := setupWillSubscriber(gw, brokers[0])
		if err != inidef.Error("") {
			t.Error(err)
		}

		time.Sleep(1 * time.Second)

		// kill publisher
		brokers[0].FourceClose()
		fmt.Println("broker killed for getting will message")

		// check will message
		willMsg := <-subscriberChannel

		assert.Equal(expectedTopic, willMsg.Topic())
		assert.Equal(expectedPayload, willMsg.Payload())
		assert.Equal(byte(0), willMsg.Qos())
	}()
	time.Sleep(3 * time.Second)
	ok = true
	return ok
}
예제 #4
0
// TestConnectLocalPubSub tests
// 1. connect gateway to local broker
// 2. send data from dummy
// 3. check subscribe
func TestConnectLocalPubSub(t *testing.T) {
	assert := assert.New(t)

	// pub/sub test to broker on localhost
	// dummydevice is used as a source of published message
	// publised messages confirmed by subscriber

	// get config
	conf, err := inidef.LoadConfig("connectlocalpubsub.ini")
	assert.Nil(err)

	// get Gateway
	gw, err := gateway.NewGateway(conf)
	assert.Nil(err)

	// get Broker
	brokerList, err := broker.NewBrokers(conf, gw.BrokerChan)
	assert.Nil(err)

	// get DummyDevice
	dummyDevice, err := device.NewDummyDevice(conf.Sections[3], brokerList, device.NewDeviceChannel())
	assert.Nil(err)
	assert.NotNil(dummyDevice)

	// Setup MQTT pub/sub client to confirm published content.
	//
	subscriberChannel := make(chan [2]string)

	opts := MQTT.NewClientOptions()
	url := fmt.Sprintf("tcp://%s:%d", brokerList[0].Host, brokerList[0].Port)
	opts.AddBroker(url)
	opts.SetClientID(gw.Name)
	opts.SetCleanSession(false)
	opts.SetDefaultPublishHandler(func(client *MQTT.Client, msg MQTT.Message) {
		subscriberChannel <- [2]string{msg.Topic(), string(msg.Payload())}
	})

	client := MQTT.NewClient(opts)
	assert.Nil(err)
	if token := client.Connect(); token.Wait() && token.Error() != nil {
		assert.Nil(token.Error())
	}

	qos := 0
	topic := "#"
	client.Subscribe(topic, byte(qos), func(client *MQTT.Client, msg MQTT.Message) {
	})

	// TODO: should be write later
	/*
		channel := fuji.SetupMQTTChannel(client, gateway, brokerList[0])

		// Setup DummyDevice to publish test payload

		dummyDevice.Start(channel)

		// wait for 1 publication of dummy worker
		message := <-subscriberChannel
		assert.Equal("dummy", message)

		client.Disconnect(250)
	*/
}
예제 #5
0
// TestRetainSubscribePublishClose
// 1. connect gateway to local broker
// 2. send data with retaind flag from dummy device
// 3. disconnect
// 4. reconnect
// 5. subscirbe and receive data
func TestRetainSubscribePublishClose(t *testing.T) {
	assert := assert.New(t)
	iniStr := `
	[gateway]
	
	    name = testRetainafterclose
	
	[broker "local/1"]
	
	    host = localhost
	    port = 1883
	
	[device "dora/dummy"]
	
	    broker = local
	    qos = 0
	
	    interval = 10
	    payload = Hello retained world to subscriber after close.
	
	    type = EnOcean
	    retain = true
`
	commandChannel := make(chan string)
	conf, err := inidef.LoadConfigByte([]byte(iniStr))
	assert.Nil(err)
	go fuji.StartByFileWithChannel(conf, commandChannel)

	gw, err := gateway.NewGateway(conf)
	if err != nil {
		t.Error("Cannot make Gateway")
	}

	brokerList, err := broker.NewBrokers(conf, gw.BrokerChan)
	if err != nil {
		t.Error("Cannot make BrokerList")
	}

	dummyDevice, err := device.NewDummyDevice(conf.Sections[3], brokerList, gw.DeviceChan)
	if err != nil {
		t.Error("Cannot make DummyDeviceList")
	}

	go func() {
		time.Sleep(2 * time.Second)

		// kill publisher
		gw.Stop()

		time.Sleep(2 * time.Second)

		subscriberChannel, err := setupRetainSubscriber(gw, brokerList[0], &dummyDevice)
		if err != inidef.Error("") {
			t.Error(err)
		}
		// check Retained message
		retainedMessage := <-subscriberChannel
		retainedTopic := retainedMessage[0]
		retainedPayload := retainedMessage[1]

		expectedTopic := fmt.Sprintf("%s/%s/%s/%s", brokerList[0].TopicPrefix, gw.Name, dummyDevice.Name, dummyDevice.Type)
		expectedPayload := dummyDevice.Payload

		assert.Equal(expectedTopic, retainedTopic)
		assert.Equal(expectedPayload, retainedPayload)
	}()
	time.Sleep(5 * time.Second)
}