예제 #1
0
func main() {
	insteon.PopulateAll()
	http.HandleFunc("/", foo)
	http.ListenAndServe(":9003", nil)
}
예제 #2
0
func foo(w http.ResponseWriter, r *http.Request) {
	var l RequestMaster
	var j ResponseMaster
	var k OutputSpeech
	body, _ := ioutil.ReadAll(r.Body)
	fmt.Println(string(body[:]))
	json.Unmarshal(body, &l)
	// For all responses
	j.Version = "1.0"
	k.Type = "PlainText"
	j.Response.ShouldEndSession = true
	userid := l.Session.User.UserId
	accessToken := l.Session.User.AccessToken
	insteon.Access_Token = accessToken
	//client_id := insteon.Client_Id
	if l.Request.Intent.Name != "" {
		fmt.Println("The name of the intent is " + l.Request.Intent.Name)
		fmt.Println(l.Request.Intent.Slots)
		for _, slot := range l.Request.Intent.Slots {
			fmt.Println(slot.Name)
			fmt.Println(slot.Value)
		}
	}
	switch l.Request.Intent.Name {
	case "Lighting", "Activate", "Deactivate":
		fmt.Println(Accounts)
		fmt.Println("PreIF")
		fmt.Println("Gonna make fuzzy")
		MakeFuzzy(userid)
		if accessToken != "" {
			fmt.Println("Access Token Present")
			if time.Since(Accounts[userid].Time) > 1*time.Hour {
				fmt.Println("Repopulating")
				insteon.PopulateAll()
				AccDevs[userid] = insteon.DevList
				AccScenes[userid] = insteon.SceneList
				AccRooms[userid] = insteon.RoomList
				var tmpacc Acc_Info
				tmpacc.Amazon = userid
				tmpacc.Time = time.Now()
				Accounts[userid] = tmpacc
				MakeFuzzy(userid)
			}

			insteon.DevList = AccDevs[userid]
			insteon.SceneList = AccScenes[userid]
			insteon.RoomList = AccRooms[userid]
			fmt.Println(insteon.Access_Token)
		} else {
			fmt.Println("I'm going into the ELSE")
		}
		switch l.Request.Intent.Name {
		case "Lighting":
			var direction string
			var item string
			var n insteon.Command
			for _, slot := range l.Request.Intent.Slots {
				if slot.Name == "Direction" {
					direction = slot.Value
					continue
				}
				if slot.Name == "Device" {
					item = slot.Value
				}
			}
			if direction == "" {
				direction = "on"
			}
			n.Command = direction
			item_type, id, location := insteon.SearchString(item)
			if item_type == "not_found" {
				s, _ := gofuzzy.Soundex(item)
				for _, x := range FuzzNames[userid] {
					if x.Fuzz == s {
						item_type = x.Kind
						location = x.Place
						switch item_type {
						case "device":
							id = AccDevs[userid][location].DeviceID
						case "scene":
							id = AccScenes[userid][location].SceneID
						case "room":
							id = AccRooms[userid][location].RoomID
						}
						fmt.Println("Found with fuzzy")
						break
					}
				}
			}
			fmt.Println(item_type + " ID = " + strconv.Itoa(id) + " Location " + strconv.Itoa(location))
			switch item_type {
			case "device":
				n.Device_Id = id
				n.Level = insteon.DevList[location].DimLevel / 254 * 100
				k.Text = "Turning " + insteon.DevList[location].DeviceName + " " + direction
			case "scene":
				n.Scene_Id = id
				k.Text = "Turning " + insteon.SceneList[location].SceneName + " " + direction
			case "room":
				fmt.Println("Rooms are a pain")
				k.Text = "Rooms are a pain, do it yourself"
			case "not_found":
				k.Text = "The following device, scene, or room was not found: " + item
			}
			go insteon.RunCommand(n) //Forking since i don't error check
		case "Activate":
			fmt.Println(insteon.Access_Token + " is the accesss token")
			var n insteon.Command
			n.Command = "on"
			scene_name := l.Request.Intent.Slots["Scene"].Value
			ret_type, scene_id, _ := insteon.SearchString(scene_name)
			if ret_type == "scene" {
				n.Scene_Id = scene_id
				go insteon.RunCommand(n) // Forking it because I am not error checking anyway
				k.Text = "Turning the scene " + scene_name + " on"
			} else {
				k.Text = "Failed to find scene " + scene_name
			}
		case "Deactivate":
			fmt.Println(insteon.Access_Token + " is the accesss token")
			var n insteon.Command
			n.Command = "off"
			scene_name := l.Request.Intent.Slots["Scene"].Value
			ret_type, scene_id, _ := insteon.SearchString(scene_name)
			if ret_type == "scene" {
				n.Scene_Id = scene_id
				go insteon.RunCommand(n) // Forking it because I am not error checking anyway
				k.Text = "Turning the scene " + scene_name + " on"
			} else {
				k.Text = "Failed to find scene " + scene_name
			}
		}
	}
	j.Response.OutputSpeech = &k
	b, _ := json.Marshal(j)
	fmt.Println(string(b[:]))
	w.Header().Set("Content-Type", "application/json")
	w.Header().Set("charset", "UTF-8")
	w.Write(b)
}
예제 #3
0
func foo(w http.ResponseWriter, r *http.Request) {
	var l RequestMaster
	var j ResponseMaster
	var k OutputSpeech
	body, _ := ioutil.ReadAll(r.Body)
	fmt.Println(string(body[:]))
	json.Unmarshal(body, &l)
	// For all responses
	j.Version = "1.0"
	k.Type = "PlainText"
	j.Response.ShouldEndSession = true
	userid := l.Session.User.UserId
	client_id := insteon.Client_Id
	if l.Request.Intent.Name != "" {
		fmt.Println("The name of the intent is " + l.Request.Intent.Name)
		fmt.Println(l.Request.Intent.Slots)
		for _, slot := range l.Request.Intent.Slots {
			fmt.Println(slot.Name)
			fmt.Println(slot.Value)
		}
	}
	switch l.Request.Intent.Name {
	case "Register":
		var m Card
		long_url := "http://connect.insteon.com/api/v2/oauth2/auth?client_id=" + client_id + "&state=" + userid + "&response_type=code&redirect_uri=http://veryoblivio.us:9001/"
		short_url := ShortenURL(long_url)
		m.Type = "Simple"
		m.Title = "Register your Insteon Connection"
		m.Content = short_url
		j.Response.Card = &m
		k.Text = "Please follow the link on the card to sign into your insteon account"
		break
	case "Lighting", "Activate", "Deactivate":
		fmt.Println(Accounts)
		fmt.Println("PreIF")
		if val, ok := Accounts[userid]; ok {
			fmt.Println("I'm going into the IF")
			insteon.Access_Token = val.Access_Token
			insteon.DevList = AccDevs[userid]
			insteon.SceneList = AccScenes[userid]
			insteon.RoomList = AccRooms[userid]
			fmt.Println("Post assignment")
			fmt.Println(insteon.Access_Token)
		} else {
			fmt.Println("I'm going into the ELSE")
			if val2, er2 := GetAccInfo(userid); er2 { //er2 is actually success if false - like an error
				fmt.Println("I'm going into the ELSEELSE")
				var m Card
				client_id := insteon.Client_Id
				long_url := "http://connect.insteon.com/api/v2/oauth2/auth?client_id=" + client_id + "&state=" + userid + "&response_type=code&redirect_uri=http://veryoblivio.us:9001/"
				short_url := ShortenURL(long_url)
				m.Type = "Simple"
				m.Title = "Register your Insteon Connection"
				m.Content = short_url
				j.Response.Card = &m
				k.Text = "No linked account found. Please follow the link on the card to sign into your insteon account"
			} else {
				fmt.Println("I'm going into the ELSEIF")
				insteon.Access_Token = val2.Access_Token
				insteon.PopulateAll()
				AccDevs[userid] = insteon.DevList
				AccScenes[userid] = insteon.SceneList
				AccRooms[userid] = insteon.RoomList
				Accounts[userid] = val2
				fmt.Println("Post assignment")
				fmt.Println(insteon.Access_Token)
				fmt.Println(Accounts)
			}
		}
		switch l.Request.Intent.Name {
		case "Lighting":
			var direction string
			var item string
			var n insteon.Command
			for _, slot := range l.Request.Intent.Slots {
				if slot.Name == "Direction" {
					direction = slot.Value
					continue
				}
				if slot.Name == "Device" {
					item = slot.Value
				}
			}
			n.Command = direction
			item_type, id, location := insteon.SearchString(item)
			fmt.Println(item_type + " ID = " + strconv.Itoa(id) + " Location " + strconv.Itoa(location))
			switch item_type {
			case "device":
				n.Device_Id = id
				n.Level = insteon.DevList[location].DimLevel / 254 * 100
				k.Text = "Turning " + item + " " + direction
			case "scene":
				n.Scene_Id = id
				k.Text = "Turning " + item + " " + direction
			case "room":
				fmt.Println("Rooms are a pain")
				k.Text = "Rooms are a pain, do it yourself"
			case "not_found":
				k.Text = "The following device, scene, or room was not found: " + item
			}
			go insteon.RunCommand(n) //Forking since i don't error check
		case "Activate":
			fmt.Println(insteon.Access_Token + " is the accesss token")
			var n insteon.Command
			n.Command = "on"
			scene_name := l.Request.Intent.Slots["Scene"].Value
			ret_type, scene_id, _ := insteon.SearchString(scene_name)
			if ret_type == "scene" {
				n.Scene_Id = scene_id
				go insteon.RunCommand(n) // Forking it because I am not error checking anyway
				k.Text = "Turning the scene " + scene_name + " on"
			} else {
				k.Text = "Failed to find scene " + scene_name
			}
		case "Deactivate":
			fmt.Println(insteon.Access_Token + " is the accesss token")
			var n insteon.Command
			n.Command = "off"
			scene_name := l.Request.Intent.Slots["Scene"].Value
			ret_type, scene_id, _ := insteon.SearchString(scene_name)
			if ret_type == "scene" {
				n.Scene_Id = scene_id
				go insteon.RunCommand(n) // Forking it because I am not error checking anyway
				k.Text = "Turning the scene " + scene_name + " on"
			} else {
				k.Text = "Failed to find scene " + scene_name
			}
		}
	}
	j.Response.OutputSpeech = &k
	b, _ := json.Marshal(j)
	fmt.Println(string(b[:]))
	w.Header().Set("Content-Type", "application/json")
	w.Header().Set("charset", "UTF-8")
	w.Write(b)
}