Пример #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 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)
		//		}
	}
}