Пример #1
0
func UpdateLocation(rw http.ResponseWriter, req *http.Request, p httprouter.Params) {
	var idata Response

	session := connectToDb()

	c := session.DB("vinaysh").C("admin")

	body, err := ioutil.ReadAll(req.Body)
	if err != nil {
		log.Fatal("Read Error" + err.Error())
	}

	if err := req.Body.Close(); err != nil {
		log.Fatal("Body Close Error" + err.Error())
	}

	fmt.Println("Input JSON data", string(body))

	if err := json.Unmarshal(body, &idata); err != nil {
		panic(err)
		fmt.Println("Unmarshal failed")
	}

	fmt.Println("Unmarshaled string is", idata.Address, idata.City, idata.State, idata.Zip)

	query := idata.Address + " " + idata.City + " " + idata.State + " " + idata.Zip
	fmt.Println("Query is", query)
	lat1, lng1, err := geocoder.Geocode(query)
	if err != nil {
		panic("THERE WAS SOME ERROR!!")
	}

	fmt.Println("Lat Long Coordinates")

	idata.Coordinate.Lat = lat1
	idata.Coordinate.Lng = lng1

	id, _ := strconv.Atoi(p.ByName("id"))

	if err := c.Update(bson.M{"id": id}, bson.M{"id": id, "name": idata.Name, "address": idata.Address, "city": idata.City, "state": idata.State, "zip": idata.Zip, "coordinate": idata.Coordinate}); err != nil {
		rw.WriteHeader(404)
		return
	}

	rData := Response{id, idata.Name, idata.Address, idata.City, idata.State, idata.Zip, idata.Coordinate}
	fmt.Println("Response is", rData)

	resp, err := json.Marshal(rData)
	if err != nil {
		panic(err)
	}

	fmt.Println("Marshaled Json Response is", string(resp))

	rw.Header().Set("Content-Type", "application/json")
	rw.Write(resp)

}
Пример #2
0
func CreateLocation(rw http.ResponseWriter, req *http.Request, p httprouter.Params) {
	var idata Response

	session := connectToDb()

	c := session.DB("vinaysh").C("admin")

	body, err := ioutil.ReadAll(req.Body)
	if err != nil {
		log.Fatal("Read Error" + err.Error())
	}

	if err := req.Body.Close(); err != nil {
		log.Fatal("Body Close Error" + err.Error())
	}

	fmt.Println("Input JSON data", string(body))

	if err := json.Unmarshal(body, &idata); err != nil {
		panic(err)
		fmt.Println("Unmarshal failed")
	}

	fmt.Println("Unmarshaled string is", idata.Name, idata.Address, idata.City, idata.State, idata.Zip)

	randnum := rand.Int()
	fmt.Println("Random number generated is", randnum)

	query := idata.Address + " " + idata.City + " " + idata.State + " " + idata.Zip
	fmt.Println("Query is", query)
	lat1, lng1, err := geocoder.Geocode(query)
	if err != nil {
		panic("THERE WAS SOME ERROR!!")
	}

	fmt.Println("Lat Long Coordinates")

	idata.Coordinate.Lat = lat1
	idata.Coordinate.Lng = lng1

	fmt.Println("Latitude is", idata.Coordinate.Lat)
	fmt.Println("Longitude is", idata.Coordinate.Lng)

	lol := Coordinates{idata.Coordinate.Lat, idata.Coordinate.Lng}
	resp1, err := json.Marshal(lol)
	if err != nil {
		panic(err)
	}
	fmt.Println("Marshaled lol is", string(resp1))

	if err := c.Insert(&Response{randnum, idata.Name, idata.Address, idata.City, idata.State, idata.Zip, idata.Coordinate}); err != nil {
		rw.WriteHeader(404)
		return
	}

	rData := Response{randnum, idata.Name, idata.Address, idata.City, idata.State, idata.Zip, idata.Coordinate}
	fmt.Println("Response is", rData)

	resp, err := json.Marshal(rData)
	if err != nil {
		panic(err)
	}

	fmt.Println("Marshaled Json Response is", string(resp))

	rw.Header().Set("Content-Type", "application/json")
	rw.Write(resp)

}
func (w *WeatherController) Get_information(rw http.ResponseWriter, req *http.Request) {
	// my_ip := get_my_ip()
	// query := my_ip
	city_name := "Mysore"
	lat, lng, err := geocoder.Geocode(city_name)
	if err != nil {
		panic(err)
	}
	latitude := strconv.FormatFloat(lat, 'f', 6, 64)
	longitude := strconv.FormatFloat(lng, 'f', 6, 64)

	keybytes, err := ioutil.ReadFile("api_key.txt")
	if err != nil {
		log.Fatal(err)
	}
	key := string(keybytes)
	key = strings.TrimSpace(key)

	f, err := forecast.Get(key, latitude, longitude, "now", forecast.CA)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("Temperature: %v*C\n", f.Currently.Temperature)

	daily_weather := []models.DailyWeather{}
	weather := models.DailyWeather{}
	for i := 0; i <= 6; i++ {
		weather = models.DailyWeather{f.Daily.Data[i].WindSpeed, f.Daily.Data[i].Icon, f.Daily.Data[i].Humidity * 100, f.Daily.Data[i].TemperatureMin, f.Daily.Data[i].TemperatureMax}
		daily_weather = append(daily_weather, weather)
		fmt.Printf("Windspeed: %v km/h \n", f.Daily.Data[i].WindSpeed)
		fmt.Printf("Climate: %v\n", f.Daily.Data[i].Icon)
		fmt.Printf("Humidity: %v%% \n", f.Daily.Data[i].Humidity*100)
		fmt.Printf("Minimum Temperature: %v*C \n", f.Daily.Data[i].TemperatureMin)
		fmt.Printf("Maximum Temperature: %v*C \n", f.Daily.Data[i].TemperatureMax)
	}

	float_lat, err := strconv.ParseFloat(latitude, 32)
	if err != nil {
		log.Fatal(err)
	}
	float_long, err := strconv.ParseFloat(longitude, 32)
	if err != nil {
		log.Fatal(err)
	}
	b, err := json.Marshal(models.GeoLocation{
		WeatherForWeek: models.DaysOfWeek{daily_weather[0], daily_weather[1], daily_weather[2], daily_weather[3], daily_weather[4], daily_weather[5], daily_weather[6]},
		Latitude:       float_lat,
		Longitude:      float_long,
		Temperature:    f.Currently.Temperature,
		Humidity:       f.Currently.Humidity,
		Windspeed:      f.Currently.WindSpeed,
		Climate:        f.Daily.Icon,
		City:           city_name,
		Success:        "True",
		Message:        "Windspeed updated.",
	})
	if err != nil {
		log.Fatal(err)
	}
	rw.Header().Set("Content-Type", "application/json")
	rw.Write(b)
}