93 lines
2.5 KiB
TypeScript
93 lines
2.5 KiB
TypeScript
import { Op } from "sequelize";
|
|
import { inject, injectable } from "tsyringe";
|
|
import { getLocationForWifi } from "../proxy/wigle";
|
|
import { WifiLocationRepository } from "../repositories/wifiLocationRepository";
|
|
import { WifiLocationHistoryService } from "./wifiLocationHistoryService";
|
|
import { StatusCodes } from "http-status-codes";
|
|
|
|
interface UpdateWifiLocationParams {
|
|
mac: string;
|
|
latitude: number;
|
|
longitude: number;
|
|
}
|
|
|
|
@injectable()
|
|
export class WifiLocationService {
|
|
constructor(
|
|
@inject(WifiLocationRepository) private repository: WifiLocationRepository,
|
|
@inject(WifiLocationHistoryService)
|
|
private wifiLocationHistory: WifiLocationHistoryService
|
|
) { }
|
|
|
|
public async getAllWifiLocations() {
|
|
return this.repository.findAll();
|
|
}
|
|
|
|
public async getAllWifiLocationsByAddresses(macAddresses: string[]) {
|
|
return this.repository.findAll({
|
|
where: { mac: { [Op.in]: macAddresses } },
|
|
});
|
|
}
|
|
|
|
public async getAllWifiLocationsByNotResolvable() {
|
|
return this.repository.findAll({
|
|
where: { location_not_resolvable: true },
|
|
});
|
|
}
|
|
|
|
public async getAllWifiLocationsByRequestLimitExceeded() {
|
|
return this.repository.findAll({
|
|
where: { request_limit_exceeded: true },
|
|
});
|
|
}
|
|
|
|
public async getWifiLocationByMac(mac: string) {
|
|
let wifiLocation = await this.repository.findById(mac);
|
|
|
|
if (wifiLocation) return wifiLocation;
|
|
|
|
const apiResponse = await getLocationForWifi(mac);
|
|
|
|
if (apiResponse == undefined) {
|
|
await this.repository.create({
|
|
mac,
|
|
});
|
|
return undefined;
|
|
}
|
|
|
|
if (apiResponse.response == undefined) {
|
|
const request_limit_exceeded = apiResponse.status_code === StatusCodes.TOO_MANY_REQUESTS;
|
|
await this.repository.create({
|
|
mac,
|
|
request_limit_exceeded,
|
|
});
|
|
return undefined;
|
|
}
|
|
|
|
if (apiResponse.response.totalResults == 0) {
|
|
await this.repository.create({ mac, location_not_resolvable: true });
|
|
return undefined;
|
|
}
|
|
|
|
wifiLocation = await this.repository.create({
|
|
mac,
|
|
latitude: apiResponse.response.results[0].trilat,
|
|
longitude: apiResponse.response.results[0].trilong,
|
|
});
|
|
|
|
await this.wifiLocationHistory.createWifiLocationHistory(
|
|
wifiLocation.dataValues
|
|
);
|
|
|
|
return wifiLocation;
|
|
}
|
|
|
|
public async updateWifiLocation(data: UpdateWifiLocationParams) {
|
|
return this.repository.update(data.mac, data);
|
|
}
|
|
|
|
public async deleteWifiLocation(id: string) {
|
|
return this.repository.delete(id);
|
|
}
|
|
}
|