func mainInfinityLoop() {
	c := conf.TestConf()

	control := rest.NewPollAsync()
	out := make(chan rest.DeviceCmdResource, 16)

	go rest.DeviceCmdPollAsync(c.URL, c.DeviceID, c.AccessKey, out, control)

	for {
		select {
		case item := <-out:
			say.Infof("item: %+v", item)
		}
	}
}
func mainInfinityLoopWithInterruption() {
	c := conf.TestConf()

	control := rest.NewPollAsync()
	out := make(chan rest.DeviceCmdResource, 16)

	go rest.DeviceCmdPollAsync(c.URL, c.DeviceID, c.AccessKey, out, control)

	for {
		select {
		case item := <-out:
			say.Infof("item: %+v", item)
		case <-time.After(15 * time.Second):
			say.Infof("start Stop()")
			control.Stop()
			say.Infof("finish Stop()")
			return
		}
	}
}
Esempio n. 3
0
func restImplementation(bus *dbus.Conn, config conf.Conf) {

	err := rest.DeviceRegisterEasy(config.URL, config.DeviceID, config.AccessKey,
		config.DeviceName, config.DeviceKey, config.NetworkName,
		config.NetworkKey, config.NetworkDesc)
	if err != nil {
		say.Infof("DeviceRegisterEasy error: %s", err.Error())
		return
	}

	go func() {
		nControl := rest.NewPollAsync()
		cControl := rest.NewPollAsync()
		nOut := make(chan rest.DeviceNotificationResource, 16)
		cOut := make(chan rest.DeviceCmdResource, 16)

		go rest.DeviceNotificationPollAsync(config.URL, config.DeviceID, config.AccessKey, nOut, nControl)
		go rest.DeviceCmdPollAsync(config.URL, config.DeviceID, config.AccessKey, cOut, cControl)

		for {
			select {
			case n := <-nOut:
				parameters := ""
				if n.Parameters != nil {
					b, err := json.Marshal(n.Parameters)
					if err != nil {
						say.Infof("Could not generate JSON from parameters of notification %+v\nWith error %s", n, err.Error())
						continue
					}

					parameters = string(b)
				}
				say.Verbosef("NOTIFICATION %s -> %s(%v)", config.URL, n.Notification, parameters)
				bus.Emit(restObjectPath, restCommandName, uint32(n.Id), n.Notification, parameters)
			case c := <-cOut:
				parameters := ""
				if c.Parameters != nil {
					b, err := json.Marshal(c.Parameters)
					if err != nil {
						say.Infof("Could not generate JSON from parameters of command %+v\nWith error %s", c, err.Error())
						continue
					}

					parameters = string(b)

				}
				say.Verbosef("COMMAND %s -> %s(%v)", config.URL, c.Command, parameters)
				bus.Emit(restObjectPath, restCommandName, uint32(c.Id), c.Command, parameters)
			}
		}
	}()

	w := NewDbusRestWrapper(config)
	bus.Export(w, "/com/devicehive/cloud", DBusConnName)

	// Introspectable
	n := &introspect.Node{
		Name: "/com/devicehive/cloud",
		Interfaces: []introspect.Interface{
			introspect.IntrospectData,
			prop.IntrospectData,
			{
				Name:    "com.devicehive.cloud",
				Methods: introspect.Methods(w),
			},
		},
	}

	bus.Export(introspect.NewIntrospectable(n), "/com/devicehive/cloud", "org.freedesktop.DBus.Introspectable")

	root := &introspect.Node{
		Children: []introspect.Node{
			{
				Name: "com/devicehive/cloud",
			},
		},
	}

	bus.Export(introspect.NewIntrospectable(root), "/", "org.freedesktop.DBus.Introspectable")

	select {}
}