lavva.exalushome
Version:
Library implementing communication and abstraction layers for ExalusHome system
115 lines • 6.4 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import { ResponseResult } from '../FieldChangeResult';
import { Coordinates, CoordinatesWithSourceInfo, SetGeolocationResponseCode } from './IGeolocationService';
import { DataFrame, Method, Status } from '../../DataFrame';
import { Api } from '../../Api';
import { ExalusConnectionService } from '../ExalusConnectionService';
import { DependencyContainer } from '../../DependencyContainer';
import { ControllerConfigurationService } from './ControllerConfigurationService';
import { WebApiCacheService } from '../WebApi/WebApiCacheService';
export class GeolocationService {
IsControllerGeolocationSetAsync() {
return __awaiter(this, void 0, void 0, function* () {
let req = new IsGeolocationSetRequest();
let response = yield Api.Get(ExalusConnectionService.ServiceName).SendAndWaitForResponseAsync(req, 15000, true);
switch (response === null || response === void 0 ? void 0 : response.Status) {
case Status.OK:
return response === null || response === void 0 ? void 0 : response.Data;
default:
return response === null || response === void 0 ? void 0 : response.Status;
}
});
}
GetCurrentGeolocationAsync() {
return __awaiter(this, void 0, void 0, function* () {
return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
window.navigator.geolocation.getCurrentPosition(position => {
resolve(position);
}, error => {
resolve(error);
});
}));
});
}
SetControllerGeolocationAsync(location) {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b, _c;
let req = new SetCoordinatesRequest();
req.Data.Latitude = location.Latitude;
req.Data.Longitude = location.Longitude;
(_a = DependencyContainer.Log) === null || _a === void 0 ? void 0 : _a.Warning(GeolocationService.ServiceName, `SetControllerGeolocationAsync() ${JSON.stringify(req)}`);
let response = yield Api.Get(ExalusConnectionService.ServiceName).SendAndWaitForResponseAsync(req, 15000, false);
(_b = DependencyContainer.Log) === null || _b === void 0 ? void 0 : _b.Warning(GeolocationService.ServiceName, `${JSON.stringify(response)}`);
(_c = DependencyContainer.Log) === null || _c === void 0 ? void 0 : _c.Error(GeolocationService.ServiceName, `EXIT CONFIGURATION MODE WILL BE FIRED! SetControllerGeolocationAsync()`);
Api.Get(ControllerConfigurationService.ServiceName).ExitConfigurationModeAsync();
switch (response === null || response === void 0 ? void 0 : response.Status) {
case Status.OK:
const cache = Api.Get(WebApiCacheService.ServiceName);
cache.Remove(new IsGeolocationSetRequest());
cache.Remove(new GetSetCoordinatesRequest());
return new ResponseResult(SetGeolocationResponseCode.OK, "");
case Status.NoPermissionToPerformThisOperation:
case Status.NoPermissionsToCallGivenResource:
return new ResponseResult(SetGeolocationResponseCode.NoPermissions, "");
case Status.UserIsNotLoggedIn:
return new ResponseResult(SetGeolocationResponseCode.UserIsNotLoggedIn, "");
default:
return new ResponseResult(SetGeolocationResponseCode.UnknownError, "");
}
});
}
GetControlllerGeolocationAsync() {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b;
let req = new GetSetCoordinatesRequest();
(_a = DependencyContainer.Log) === null || _a === void 0 ? void 0 : _a.Warning(GeolocationService.ServiceName, `GetControlllerGeolocationAsync(): ${JSON.stringify(req)}`);
let response = yield Api.Get(ExalusConnectionService.ServiceName).SendAndWaitForResponseAsync(req, 15000, true);
(_b = DependencyContainer.Log) === null || _b === void 0 ? void 0 : _b.Warning(GeolocationService.ServiceName, `${JSON.stringify(response)}`);
switch (response === null || response === void 0 ? void 0 : response.Status) {
case Status.OK:
let c = new CoordinatesWithSourceInfo();
c.IsNetworkBased = (response === null || response === void 0 ? void 0 : response.Data).IsNetworkBased;
c.Latitude = (response === null || response === void 0 ? void 0 : response.Data).Latitude;
c.Longitude = (response === null || response === void 0 ? void 0 : response.Data).Longitude;
return c;
default:
return response === null || response === void 0 ? void 0 : response.Status;
}
});
}
GetServiceName() {
return GeolocationService.ServiceName;
}
}
GeolocationService.ServiceName = "GeolocationService";
class IsGeolocationSetRequest extends DataFrame {
constructor() {
super();
this.Resource = "/geolocation/isset";
this.Method = Method.Get;
}
}
class GetSetCoordinatesRequest extends DataFrame {
constructor() {
super();
this.Resource = "/geolocation/get";
this.Method = Method.Get;
}
}
class SetCoordinatesRequest extends DataFrame {
constructor() {
super();
this.Data = new Coordinates();
this.Resource = "/geolocation/save";
this.Method = Method.Put;
}
}
//# sourceMappingURL=GeolocationService.js.map