示例#1
0
func ServerStats() (map[string]interface{}, error) {
	if len(srvPort) == 0 {
		return nil, nil
	}
	resp, err := http.Get("http://localhost:" + srvPort + "/rpc/stats?token=" + srvToken)
	if err != nil {
		return nil, err
	}

	defer resp.Body.Close()
	data, err := util.JsonDecode(resp.Body)
	if err != nil {
		return nil, err
	}
	return data, err
}
示例#2
0
func ValidateToken(token string) (pid string, err error) {
	url := "https://www.googleapis.com/oauth2/v1/tokeninfo?id_token=" + token
	response, err := http.Get(url)
	if err != nil {
		return
	}
	defer response.Body.Close()
	result, err := util.JsonDecode(response.Body)
	if err != nil {
		return
	}
	if e, ok := result["error"]; ok {
		err = errors.New(e.(string))
		return
	}
	pid = result["user_id"].(string)
	return
}
示例#3
0
func (mt *MapTile) ScanToken(token string) (err error) {
	defer func() {
		if r := recover(); r != nil {
			var ok bool
			err, ok = r.(error)
			if !ok {
				err = fmt.Errorf("Err: %v", r)
			}
		}
	}()

	xy := strings.Split(mt.MapPos, ",")
	if len(xy) != 2 {
		return errors.New("invalid_mappos:" + mt.MapPos)
	}

	log.Println("MapTile.Scan:", mt.MapPos)
	xInt, _ := strconv.Atoi(xy[0])
	yInt, _ := strconv.Atoi(xy[1])
	x, y := float64(xInt)+0.5, float64(yInt)+0.5
	lat, lon := geom.TilePos2LL(x, y)
	latStr := strconv.FormatFloat(lat, 'f', 6, 64)
	lonStr := strconv.FormatFloat(lon, 'f', 6, 64)

	apiKeysStr := GetConfigValue("MapsApiKeys", "")
	apiKeys := strings.Split(apiKeysStr, ",")
	if len(apiKeys) <= 0 || len(apiKeysStr) <= 0 {
		return errors.New("missing_api_keys")
	}

	apiKey := apiKeys[poi_apikey_index]
	getTypes := "establishment"
	getRadius := "134"

	url := "https://maps.googleapis.com/maps/api/place/search/json?location=" + latStr + "," + lonStr + "&radius=" + getRadius + "&sensor=true&key=" + apiKey + "&language=de&types=" + getTypes
	if len(token) > 0 {
		url += "&pagetoken=" + token
	}

	log.Println("POIScan:", url)
	response, err := http.Get(url)
	if err != nil {
		return err
	}
	defer response.Body.Close()
	result, err := util.JsonDecode(response.Body)
	if err != nil {
		return err
	}
	if status, ok := result["status"]; ok {
		statusStr := status.(string)
		if statusStr != "OK" && statusStr != "ZERO_RESULTS" {

			if statusStr == "OVER_QUERY_LIMIT" {
				if apiKey == apiKeys[poi_apikey_index] {
					poi_apikey_index = (poi_apikey_index + 1) % len(apiKeys)
					if mt.scanfail >= len(apiKeys) {
						return errors.New(statusStr)
					}
					mt.scanfail++
					mt.Scan()
					return nil
				}
			}

			err = errors.New(statusStr)
			log.Println("POIScan err:", result)
			return err
		}
	}
	res := result["results"].([]interface{})
	//log.Println(len(res))
	pois := make([]*POI, 0)

	for i := 0; i < len(res); i++ {
		data := res[i].(map[string]interface{})
		poi := &POI{}
		poi.ID = data["id"].(string)
		poi.Name = data["name"].(string)
		location := data["geometry"].(map[string]interface{})["location"].(map[string]interface{})
		poi.Addr = data["vicinity"].(string)
		poi.Lat = location["lat"].(float64)
		poi.Lon = location["lng"].(float64)
		poi.MapPos = LL2MapPos(poi.Lat, poi.Lon)
		if poi.MapPos != mt.MapPos {
			continue
		}
		//log.Println(i, poi.MapPos, poi.MapPos == mt.MapPos)
		types := data["types"].([]interface{})
		poi.Types = ""
		for i, value := range types {
			if i > 0 {
				poi.Types += ","
			}
			poi.Types += value.(string)
		}
		pois = append(pois, poi)

		tblPOI.Delete(poi)
		tblPOI.Create(poi)
	}
	//pid = result["user_id"].(string)
	//mt.Pois = pois
	DoRepeat(func() bool {
		mt.Scanned = POIScanVer
		mt.PoisVersion++
		return tblMapTile.WriteParam(mt, &db.RWParams{ReadOnWriteConflict: true})
	}, WRITE_REPEAT)

	//log.Println(">>>>>> PoisVersion:", mt.Version, mt.PoisVersion, mt)

	nextToken, _ := result["next_page_token"]
	if nextToken != nil {
		strToken, _ := nextToken.(string)
		if len(strToken) > 0 {
			go func() {
				time.Sleep(time.Millisecond * 2000)
				mt.ScanToken(strToken)
			}()
		}
	}

	return nil
}