Beispiel #1
0
func subscribeToDevices() {
	log.Info("Subscribing to devices...")
	for _, devAddr := range devices {
		topic := fmt.Sprintf("nodes/%s/packets", devAddr)
		if token := mqttClient.Subscribe(topic, 0, nil); token.Wait() && token.Error() != nil {
			log.WithField("topic", topic).WithError(token.Error()).Fatal("Could not subscribe")
		}
	}
}
Beispiel #2
0
func setupMQTT() {
	broker := fmt.Sprintf("tcp://%s:1883", viper.GetString("broker"))
	opts := MQTT.NewClientOptions().AddBroker(broker)

	clientID := fmt.Sprintf("ttntool-%s", util.RandString(15))
	opts.SetClientID(clientID)

	opts.SetKeepAlive(20)

	opts.SetOnConnectHandler(func(client *MQTT.Client) {
		log.Info("Connected to The Things Network")
		subscribeToDevices()
	})

	opts.SetDefaultPublishHandler(handleMessage)

	opts.SetConnectionLostHandler(func(client *MQTT.Client, err error) {
		log.WithError(err).Error("Connection Lost. Reconnecting...")
	})

	mqttClient = MQTT.NewClient(opts)
}
Beispiel #3
0
func main() {
	renameFlag := flag.String("r", "", "-r [newname]")
	pathFlag := flag.String("p", "", "-p [/some/path/]")
	dryRunFlag := flag.Bool("d", false, "-d")
	flag.Usage = func() {
		println(USAGE)
	}

	flag.Parse()
	args := flag.Args()

	log.Infof("Flags: rename(%s), path(%s), dry(%t)", *renameFlag, *pathFlag, *dryRunFlag)
	log.Infof("Args : %s", strings.Join(args, ", "))

	if len(args) < 1 {
		log.Fatal(USAGE)
	}

	project := strings.TrimSpace(args[0])
	if project == "" {
		log.Fatal(USAGE)
	}

	parts := strings.Split(project, "/")
	if len(parts) != 2 {
		log.Fatal(USAGE)
	}

	owner := strings.TrimSpace(parts[0])
	repo := strings.TrimSpace(parts[1])
	if owner == "" || repo == "" {
		log.Fatal(USAGE)
	}

	projectUrl := fmt.Sprintf("github.com/%s/%s", owner, repo)

	client := github.NewClient(nil)

	log.Infof("Grabbing from %s...", project)

	_, resp, err := client.Repositories.Get(owner, repo)
	if err != nil {
		if resp == nil {
			log.Fatalf("Error: Cannot reach %s", projectUrl)
		}

		if resp.StatusCode == 404 {
			log.Fatalf("Error: Project does not exist: %s", projectUrl)
		} else {
			log.Fatalf("Error: Unknown error (%d) reaching: %s", resp.StatusCode, projectUrl)
		}
	}

	release, resp, err := client.Repositories.GetLatestRelease(owner, repo)
	if err != nil {
		if resp.StatusCode == 404 {
			log.Infof("No official releases yet. Checking pre-release tags...")
			tags, _, err := client.Repositories.ListTags(owner, repo, nil)
			if err != nil || len(tags) < 1 {
				log.Fatalf(ERR_NO_RELEASES, err)
			}

			tag := tags[0]
			log.Infof("Latest tag: %s (%s)", *tag.Name, *tag.Commit.SHA)

			release, resp, err = client.Repositories.GetReleaseByTag(owner, repo, *tag.Name)
			if err != nil || len(tags) < 1 {
				log.Fatalf(ERR_NO_RELEASES, err)
			}
		}
	}

	assets := release.Assets
	var asset github.ReleaseAsset

	switch len(assets) {
	case 0:
		log.Fatal("Error: No assets to download")
		break
	case 1:
		asset = assets[0]
		break
	default:
		log.Info("Release Assets:")
		for i, a := range assets {
			log.Infof(" (%d) %s (%s)", i+1, *a.Name, *a.BrowserDownloadURL)
		}

		reader := bufio.NewReader(os.Stdin)
		fmt.Print("Which asset would you like to download? ")
		choice, _ := reader.ReadString('\n')
		choice = strings.TrimSpace(choice)
		choiceInt, err := strconv.Atoi(choice)
		if err != nil {
			log.Fatalf("Error: You must select an asset to download")
		}
		println("")
		asset = assets[choiceInt-1]

		break
	}

	if *dryRunFlag {
		log.Info("Dry-run completed")
	} else {
		downloadReleaseAsset(&asset, renameFlag, pathFlag)
	}
}
Beispiel #4
0
// Info level message.
func Info(msg string) {
	apexlog.Info(msg)
}