示例#1
0
func TestLocators(t *testing.T) {
	google, err := geolocate.New(geolocate.Google, googleAPIKey)
	if err != nil {
		t.Fatal(err)
	}
	mozilla, err := geolocate.New(geolocate.Mozilla, "test")
	if err != nil {
		t.Fatal(err)
	}
	yandex, err := geolocate.New(geolocate.Yandex, yandexAPIKey)
	if err != nil {
		t.Fatal(err)
	}
	mongodb, err := mongo.Connect("mongodb://localhost/watch")
	if err != nil {
		t.Fatal(err)
	}
	defer mongodb.Close()
	lbs, err := lbs.InitDB(mongodb)
	if err != nil {
		t.Fatal(err)
	}
	for _, s := range testData {
		req, err := ParseLBS("gsm", s)
		if err != nil {
			t.Fatal(err)
		}
		resp, err := google.Get(*req)
		if err != nil {
			fmt.Printf("%8s error: %v\n", "Google", err)
			t.Error(err)
		} else {
			fmt.Printf("%8s [%.7f,%.7f] %.2f\n", "Google", resp.Location.Lon, resp.Location.Lat, resp.Accuracy)
		}
		resp, err = yandex.Get(*req)
		if err != nil {
			fmt.Printf("%8s error: %v\n", "Yandex", err)
			t.Error(err)
		} else {
			fmt.Printf("%8s [%.7f,%.7f] %.2f\n", "Yandex", resp.Location.Lon, resp.Location.Lat, resp.Accuracy)
		}
		resp, err = mozilla.Get(*req)
		if err != nil {
			fmt.Printf("%8s error: %v\n", "Mozilla", err)
			t.Error(err)
		} else {
			fmt.Printf("%8s [%.7f,%.7f] %.2f\n", "Mozilla", resp.Location.Lon, resp.Location.Lat, resp.Accuracy)
		}
		resp, err = lbs.Get(*req)
		if err != nil {
			fmt.Printf("%8s error: %v\n", "Internal", err)
			t.Error(err)
		}
		fmt.Printf("%8s [%.7f,%.7f] %.2f\n", "Internal", resp.Location.Lon, resp.Location.Lat, resp.Accuracy)
		fmt.Println("-----------------------------------------")
	}
}
示例#2
0
文件: main.go 项目: jtprog/geotrack
func subscribe(mdb *mongo.DB, nc *nats.Conn) error {
	nce, err := nats.NewEncodedConn(nc, nats.JSON_ENCODER)
	if err != nil {
		return err
	}

	// nce.Subscribe("*", func(subj, reply string, data []byte) {
	// 	log.Printf("DEBUG: %q [%q]\n%s", subj, reply, string(data))
	// })

	lbs, err := lbs.InitDB(mdb)
	if err != nil {
		return err
	}
	if lbs.Records() == 0 {
		log.Warn("LBS DB is empty!")
	}
	lbsGoogle, err := geolocate.New(geolocate.Google, googleToken)
	if err != nil {
		return err
	}
	nce.Subscribe(serviceNameLBS, func(_, reply string, req geolocate.Request) {
		resp, err := lbsGoogle.Get(req)
		logger := log.WithFields(log.Fields{"request": req, "response": resp})
		if err != nil {
			logger.WithError(err).Error("LBS Google error")
		}
		if err := nce.Publish(reply, resp); err != nil {
			logger.WithError(err).Error("LBS Google response error")
		} else {
			logger.Debug("LBS")
		}
		_, err = lbs.Get(req) // оставил для сохранения в базу запроса.
		if err != nil {
			logger.WithError(err).Error("LBS Internal response error")
		}
	})

	ubloxCache, err := ublox.InitCache(mdb, ubloxToken)
	if err != nil {
		return err
	}
	profile := ublox.DefaultProfile
	nce.Subscribe(serviceNameUblox, func(_, reply string, point geo.Point) {
		data, err := ubloxCache.Get(point, profile)
		logger := log.WithFields(log.Fields{"request": point, "response length": len(data)})
		if err != nil {
			logger.WithError(err).Error("UBLOX error")
		}
		if err := nce.Publish(reply, data); err != nil {
			logger.WithError(err).Error("UBLOX response error")
		} else {
			logger.Debug("UBLOX")
		}
	})

	usersDB, err := users.InitDB(mdb)
	if err != nil {
		return err
	}
	// уникальный идентификатор группы пока для примера задан явно
	// groupID := users.SampleGroupID
	groupID := usersDB.GetSampleGroupID()
	nce.Subscribe(serviceNameIMEI, func(_, reply, data string) {
		group, err := usersDB.GetGroup(groupID)
		logger := log.WithFields(log.Fields{"request": data, "response": group})
		if err != nil {
			logger.WithError(err).Error("IMEI error")
		}
		if err := nce.Publish(reply, group); err != nil {
			logger.WithError(err).Error("IMEI response error")
		} else {
			logger.Debug("IMEI")
		}
	})

	tracksDB, err := tracks.InitDB(mdb)
	if err != nil {
		return err
	}
	nce.Subscribe(serviceNameTracks, func(tracks []tracks.TrackData) {
		logger := log.WithField("request", tracks)
		if err := tracksDB.Add(tracks...); err != nil {
			logger.WithError(err).Error("TRACKS error")
		} else {
			logger.Debug("TRACKS")
		}
	})

	sensorsDB, err := sensors.InitDB(mdb)
	if err != nil {
		return err
	}
	nce.Subscribe(serviceNameSensors, func(sensors []sensors.SensorData) {
		logger := log.WithField("request", sensors)
		if err := sensorsDB.Add(sensors...); err != nil {
			logger.WithError(err).Error("SENSORS error")
		} else {
			logger.Debug("SENSORS")
		}
	})

	var pairs pairing.Pairs
	nce.Subscribe(serviceNamePairing, func(_, reply, deviceID string) {
		key := pairs.Generate(deviceID)
		logger := log.WithFields(log.Fields{"request": deviceID, "response": key})
		if err := nce.Publish(reply, key); err != nil {
			logger.WithError(err).Error("PAIR error")
		} else {
			logger.Debug("PAIR")
		}
	})
	nce.Subscribe(serviceNamePairingKey, func(_, reply, key string) {
		newDeviceID := pairs.GetDeviceID(key)
		logger := log.WithFields(log.Fields{"request": key, "response": newDeviceID})
		if err := nce.Publish(reply, newDeviceID); err != nil {
			logger.WithError(err).Error("PAIR KEY error")
		} else {
			logger.Debug("PAIR KEY")
		}
	})

	return nil
}