Exemplo n.º 1
0
func main() {
	configFile, config, err := conf.FromArgs()
	switch {
	case err != nil:
		log.Fatalf("Failed to read %q configuration (%s)", configFile, err)
	case configFile == "":
		log.Warnf("No configuration file provided!")
		log.Infof("Test configuration is used: %+v", config)
	default:
		log.Infof("Starting DeviceHive with %q configuration: %+v", configFile, config)
	}

	log.SetLevelByName(config.LoggingLevel)

	bus, err := dbus.SystemBus()
	if err != nil {
		log.Warnf("Cannot get system bus (error: %s)", err)
		log.Infof("Trying to use session bus for testing purposes...")
		if bus, err = dbus.SessionBus(); err != nil {
			log.Fatalf("Cannot get session bus (error: %s)", err)
			return
		}
	}

	reply, err := bus.RequestName(DBusConnName, dbus.NameFlagDoNotQueue)
	switch {
	case err != nil:
		log.Fatalf("Cannot request name %q (error: %s)", DBusConnName, err)
	case reply != dbus.RequestNameReplyPrimaryOwner:
		log.Fatalf("The name %q already taken", DBusConnName)
	}

	s, err := devicehive.NewService(config.URL, config.AccessKey)
	if err != nil {
		log.Fatalf("Failed to create DeviceHive service (error: %s)", err)
	}

	log.Infof("Starting %v", s)
	mainLoop(bus, s, config)
}
Exemplo n.º 2
0
func main() {
	log.SetLevel(log.TRACE)

	sig_ch := make(chan os.Signal, 1)
	signal.Notify(sig_ch, os.Interrupt)

	waitTimeout := 300 * time.Second
	deviceId := "go-test-device-id123"
	deviceKey := "go-test-device-key"

	url := "ws://localhost:8080" //"ws://playground.devicehive.com/api/websocket"
	key := "gQPScZHbvm7oNxKXKT3c32MUfQdezoJ5IS/C4pTyNjo="
	s, err := devicehive.NewService(url, key)
	if err != nil {
		log.Fatalf("Failed to create service (error: %s)", err)
	}
	log.Alwaysf("service created: %s", s)

	info, err := s.GetServerInfo(waitTimeout)
	if err != nil {
		log.Fatalf("Failed to get server info (error: %s)", err)
	}
	log.Alwaysf("server info: %s", info)

	if false {
		//		devices, err := s.GetDeviceList(0, 0, waitTimeout)
		//		if err != nil {
		//			log.Fatalf("Failed to get device list (error: %s)", err)
		//		}
		//		for _, d := range devices {
		//			log.Alwaysf("device %s", d)
		//		}
		//		log.Alwaysf("done %d", len(devices))
		//		return
	}

	device := devicehive.NewDevice(deviceId, "test-name",
		devicehive.NewDeviceClass("go-dev-class", "1.2.3"))
	device.DeviceClass.AddEquipment(devicehive.NewEquipment("n1", "c1", "t1"))
	device.DeviceClass.AddEquipment(devicehive.NewEquipment("n2", "c2", "t2"))
	//device.Network = devicehive.NewNetwork("dev-net", "net-key")
	device.Key = deviceKey

	//	err = s.Authenticate(device, waitTimeout)
	//	if err != nil {
	//		log.Fatalf("Failed to authenticate (error: %s)", err)
	//	}

	err = s.RegisterDevice(device, waitTimeout)
	if err != nil {
		log.Fatalf("Failed to register device (error: %s)", err)
	}

	//	*device, err = s.GetDevice(deviceId, deviceKey, waitTimeout)
	//	if err != nil {
	//		log.Fatalf("Failed to get device (error: %s)", err)
	//	}
	log.Alwaysf("device: %s", device)

	notification := devicehive.NewNotification("hello", 12345)
	err = s.InsertNotification(device, notification, waitTimeout)
	if err != nil {
		log.Fatalf("Failed to insert notification (error: %s)", err)
	}
	log.Alwaysf("notification: %s", notification)

	cmd_listener, err := s.SubscribeCommands(device, info.Timestamp, waitTimeout)
	if err != nil {
		log.Fatalf("failed to subscribe commands (error: %s)", err)
	}
	for {
		select {
		case command := <-cmd_listener.C:
			log.Alwaysf("command received: %s", command)
			command.Status = "Success"
			command.Result = "No"
			err = s.UpdateCommand(device, command, waitTimeout)
			if err != nil {
				log.Fatalf("failed to update command status (error: %s)", err)
			}
		case <-time.After(time.Second):
			log.Alwaysf(".")
		case <-sig_ch:
			log.Alwaysf("Exiting...")
			return
		}
	}
	return

	if false {
		//		command := devicehive.NewCommand("hello", 12345)
		//		err = s.InsertCommand(device, command, waitTimeout)
		//		if err != nil {
		//			log.Fatalf("Failed to insert command (error: %s)", err)
		//		}
		//		log.Alwaysf("command: %s", command)

		//		command.Status = "Done"
		//		command.Result = "No result"
		//		err = s.UpdateCommand(device, command, waitTimeout)
		//		if err != nil {
		//			log.Fatalf("Failed to update command (error: %s)", err)
		//		}
	}
}