Exemple #1
0
// BeepDevice triggers a device beep
func BeepDevice(w http.ResponseWriter, req *http.Request) {
	setHeaders(w)
	var sc model.ServiceCommand
	if err := json.NewDecoder(req.Body).Decode(&sc); err != nil {
		writeError(w, model.CommandMalformed)
		return
	}
	if !checkServiceCommand(sc) {

		w.WriteHeader(http.StatusForbidden)
		json.NewEncoder(w).Encode(sc)
		return
	}

	cs, err := authenticate(w, sc.Creds)
	if err != nil {
		return
	}

	d, err := getDevices(w, cs)
	if err != nil {
		return
	}

	dv, err := d.GetDeviceByName(sc.Name)
	if err != nil {
		writeError(w, fmt.Sprintf(model.NoDeviceName, err))
		return
	}

	if sc.Message == "" {
		sc.Message = model.DefaultMessage
	}
	r := model.ServiceResponse{false, fmt.Sprintf(model.PlayingSound, dv.Name, sc.Message)}
	w.WriteHeader(http.StatusOK)
	if err := json.NewEncoder(w).Encode(r); err != nil {
		panic(err)
	}
	commands.PlaySound(cs, dv, sc.Message)
	return
}
Exemple #2
0
// main can either accept every arg as a flag, or you can step through in
// an interactive manner.
func main() {
	// console flags
	uf := flag.String("user", "", model.FlagAppleID)
	pf := flag.String("pass", "", model.FlagApplePass)
	nf := flag.String("name", "", model.FlagDeviceName)
	mf := flag.String("msg", model.DefaultMessage, model.FlagDeviceMessage)
	sf := flag.Bool("service", false, model.FlagRunService)
	portf := flag.Int("port", 9443, model.FlagServicePort)
	certf := flag.String("key", "", model.FlagServiceKey)
	keyf := flag.String("cert", "", model.FlagServiceCert)

	var un, pw, dn, msg string
	flag.Parse()
	ufVal := *uf
	pfVal := *pf
	nfVal := *nf
	mfVal := *mf
	sfVal := *sf
	portVal := *portf
	keyVal := *keyf
	certVal := *certf

	// check for service mode
	if sfVal {
		service.StartService(portVal, certVal, keyVal)
		return
	}

	// print welcome!
	console.PrintWelcomeBanner()

	// defaults
	if ufVal == "" {
		un = console.CollectUsername()
	} else {
		un = strings.TrimSpace(ufVal)
	}
	if pfVal == "" {
		pw = console.CollectPassword()
	} else {
		pw = strings.TrimSpace(pfVal)
	}
	if nfVal == "" {
		dn = "" // device name
	} else {
		dn = strings.TrimSpace(nfVal)
	}
	msg = strings.TrimSpace(mfVal)

	var cr = model.Creds{AppleID: un, Password: pw}
	var cs model.CloudService

	cs, err := commands.Authenticate(cr)
	if err != nil {
		console.PrintAuthFailed(err)
		return
	}

	d, err := commands.RefreshDeviceList(&cs)
	if err != nil {
		fmt.Printf("\n"+model.DeviceRefreshFailed, err)
		return
	}

	var dID int
	var dv *model.Device
	if nfVal == "" {
		console.PrintDevices(&d)
		dID = console.CollectDeviceSelection(len(d.Devices))
		dv, err = d.GetDeviceByIndex(dID - 1)
		if err != nil {
			console.PrintNoDeviceFound(string(dID))
			return
		}
	} else {
		dv, err = d.GetDeviceByName(dn)
		if err != nil {
			console.PrintNoDeviceFound(dn)
			return
		}
	}
	console.PrintPlayingSound(dv.Name, msg)
	commands.PlaySound(&cs, dv, msg)
}