예제 #1
0
// main loop
func mainLoop(bus *dbus.Conn, service devicehive.Service, config conf.Conf) {
	// getting server info
	info, err := service.GetServerInfo(waitTimeout)
	if err != nil {
		log.Warnf("Cannot get service info (error: %s)", err)
		return
	}

	// registering device
	device := devicehive.NewDevice(config.DeviceID, config.DeviceName,
		devicehive.NewDeviceClass("go-gateway-class", "0.1"))
	device.Key = config.DeviceKey
	if len(config.NetworkName) != 0 || len(config.NetworkKey) != 0 {
		device.Network = devicehive.NewNetwork(config.NetworkName, config.NetworkKey)
		device.Network.Description = config.NetworkDesc
	}
	err = service.RegisterDevice(device, waitTimeout)
	if err != nil {
		log.Warnf("Cannot register device (error: %s)", err)
		return
	}

	// start polling commands
	listener, err := service.SubscribeCommands(device, info.Timestamp, waitTimeout)
	if err != nil {
		log.Warnf("Cannot subscribe commands (error: %s)")
		return
	}

	wrapper := DBusWrapper{service: service, device: device}
	exportDBusObject(bus, &wrapper)

	for {
		select {
		case cmd := <-listener.C:
			params := ""
			if cmd.Parameters != nil {
				buf, err := json.Marshal(cmd.Parameters)
				if err != nil {
					log.Warnf("Cannot generate JSON from parameters of command %+v (error: %s)", cmd, err)
					continue
				}
				params = string(buf)
			}
			log.Infof("COMMAND %s -> %s(%v)", config.URL, cmd.Name, params)
			bus.Emit(ComDevicehiveCloudPath, ComDevicehiveCloudIface+".CommandReceived", cmd.Id, cmd.Name, params)
		}

		//time.Sleep(5 * time.Second)
	}
}
예제 #2
0
func wsImplementation(bus *dbus.Conn, config conf.Conf) {

	var conn *ws.Conn
	for {
		info, err := rest.GetApiInfo(config.URL)
		if err == nil {
			say.Verbosef("API info: %+v", info)
			c := ws.New(info.WebSocketServerUrl, config.DeviceID, config.SendNotificationQueueCapacity, func(m map[string]interface{}) {

				p := m["parameters"]
				params := ""

				if p != nil {
					b, err := json.Marshal(p)
					if err != nil {

						say.Fatalf("Could not generete JSON from command %+v\nWith error %s", m, err.Error())
					}

					params = string(b)
				}

				say.Verbosef("COMMAND %s -> %s(%v)", info.WebSocketServerUrl, m["command"].(string), params)

				bus.Emit("/com/devicehive/cloud",
					"com.devicehive.cloud.CommandReceived",
					uint32(m["id"].(float64)),
					m["command"].(string),
					params)
			})
			conn = &c

			if err == nil {
				break
			}
		}
		say.Infof("API info error: %s", err.Error())
		time.Sleep(5 * time.Second)
	}

	w := NewDbusObjectWrapper(conn)
	go conn.Run(config.AccessKey, func() {
		conn.RegisterDevice(config.DeviceID, config.DeviceName, config.DeviceKey,
			config.NetworkName, config.NetworkKey, config.NetworkDesc)
		conn.Authenticate(config.DeviceID, config.DeviceKey)
		conn.SubscribeCommands()
	})

	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 {}

}
예제 #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 {}
}