Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { Injectable } from '@angular/core';
- import { Http, Response, Headers, RequestOptions, ResponseOptions, URLSearchParams } from '@angular/http';
- import { Backend, IOT_URL } from '../../../data/backend';
- import { Observable } from 'rxjs/Observable';
- import { Plant } from './plants';
- import { User } from '../users/users';
- import { Country } from '../country/country';
- import { System } from '../systems/systems';
- import { Alarm } from '../alarm/alarm';
- import { Sensor } from '../sensors/sensors';
- import { Gateway } from '../gateway/gateway';
- import { SystemsService } from '../systems/systems-service';
- const invalidAddressErr = "invalid address";
- import 'rxjs/Rx';
- declare interface PlantDashboard {
- plant: Plant,
- systems: Array<{ name: string, alarms: Array<Alarm>, sensors?: Array<Sensor> }>
- }
- @Injectable()
- export class PlantsService extends Backend {
- valid: boolean = false;
- errorMessage: string;
- newPlant: Plant;
- constructor(public http: Http) {
- super(http, IOT_URL);
- }
- getPlantsByTenant(tenantId: string): Observable<Array<Plant>> {
- return this.getBackend('/customer/plant/list/by-tenant/' + tenantId)
- .map(response => response.plants)
- }
- getPlants(): Observable<Array<Plant>> {
- return this.getBackend('/customer/plant/list/all')
- .map(response => response.plants)
- }
- getPlantManager(id: string): Observable<Array<User>> {
- return this.getBackend('/customer/tenant/' + id + '/list/managers')
- .map(response => response.managers)
- }
- addPlant(plantInfo: any, parentId: string): Observable<any> {
- let managerId = plantInfo.plantManagerID;
- let countryId = plantInfo.countryPlant.id;
- let name = plantInfo.name;
- let description = plantInfo.description;
- let address = plantInfo.address;
- let street = plantInfo.address;
- let state = plantInfo.state;
- let city = plantInfo.city;
- let zipCode = plantInfo.zipCode;
- let longitude = Number(plantInfo.longitude);
- let latitude = Number(plantInfo.latitude);
- console.log('parentId od service:' + parentId);
- console.log('country name' + plantInfo.countryPlant.name);
- let body = {
- managerId,
- parentId,
- name,
- description,
- countryId,
- address,
- street,
- city,
- zipCode,
- longitude,
- latitude
- };
- if (!longitude || !latitude) {
- console.log("nema lat i long")
- let googleAddress = address.split(' ').join('+');
- let googleCity = city.split(' ').join('+');
- let googleCountry = plantInfo.countryPlantID.name.idsplit(' ').join('+');
- let googleState = state.split(' ').join('+');
- let params = new URLSearchParams();
- let googleUrlParams = googleAddress + '+' + googleCity + '+' + googleState + '+' + zipCode + '+' + googleCountry;
- params.set('address', googleUrlParams);
- return this.http.get('https://maps.googleapis.com/maps/api/geocode/json', {
- search: params
- }).flatMap((response: Response) => {
- if (!response.json().results[0]) {
- console.log("invalid address");
- return Observable.throw(invalidAddressErr);
- }
- body.longitude = response.json().results[0].geometry.location.lng;
- body.latitude = response.json().results[0].geometry.location.lat;
- return this.postPlant(body);
- });
- } else {
- return this.postPlant(body);
- }
- }
- updatePlant(plantInfo: any, parentId: string, plantId: string): Observable<Plant> {
- let name = plantInfo.name;
- let description = plantInfo.description;
- let address = plantInfo.address;
- let street = plantInfo.address;
- let state = plantInfo.state;
- let city = plantInfo.city;
- let zipCode = plantInfo.zipCode;
- let longitude = Number(plantInfo.longitude);
- let latitude = Number(plantInfo.latitude);
- let managerId = plantInfo.plantManagerID;
- let countryId = plantInfo.countryPlant.id;
- let countryName = plantInfo.countryPlant.name;
- let body = {
- managerId,
- parentId,
- countryId,
- name,
- description,
- address,
- street,
- city,
- zipCode,
- longitude,
- latitude
- };
- if (!longitude || !latitude) {
- let googleAddress = address.split(' ').join('+');
- let googleCity = city.split(' ').join('+');
- let googleCountry = countryName.split(' ').join('+');
- let googleState = state.split(' ').join('+');
- let params = new URLSearchParams();
- let googleUrlParams = googleAddress + '+' + googleCity + '+' + googleState + '+' + zipCode + '+' + googleCountry;
- params.set('address', googleUrlParams);
- return this.http.get('https://maps.googleapis.com/maps/api/geocode/json', {
- search: params
- }).flatMap((response: Response) => {
- if (!response.json().results[0]) {
- return Observable.throw(invalidAddressErr);
- }
- body.longitude = response.json().results[0].geometry.location.lng;
- body.latitude = response.json().results[0].geometry.location.lat;
- return this.updatePlantReady(body, plantId);
- });
- } else {
- return this.updatePlantReady(body, plantId);
- }
- }
- listPlantsForMap(lowerLatitude: number, lowerLongitude: number, higherLatitude: number, higherLongitude: number): Observable<Array<Plant>> {
- let body = {
- lowerLongitude,
- lowerLatitude,
- higherLongitude,
- higherLatitude,
- };
- return this.postBackend('/customer/plant/list/by-coordinates/v1', body)
- .map(response => response.plants)
- }
- postPlant(body: any) {
- return this.postBackend('/customer/plant/create', body)
- .map(response => response.plant)
- }
- updatePlantReady(body: any, pId: string) {
- return this.postBackend('/customer/plant/' + pId + '/update', body)
- .map(response => response.plant)
- }
- getPlantDetails(plantId: string): Observable<Plant> {
- return this.getBackend('/customer/plant/' + plantId + '/get')
- .map(response => response.plant)
- }
- getPlantDashboard(plantId: string): Observable<PlantDashboard> {
- return this.getBackend('/device/list/by-plant/' + plantId + '/by-system')
- .map(response => response)
- }
- getGatewaysForPlant(plantId: string): Observable<Array<Gateway>> {
- return this.getBackend('/communication/plant/' + plantId + '/gateways')
- .map(response => response.gateways)
- }
- getGatewaysForAccount(acountName: string, username: string, pass: string): Observable<any> {
- let companyName = acountName;
- let userName = username;
- let password = pass;
- let body = {
- companyName,
- userName,
- password,
- };
- console.log(body);
- return this.postBackend('/communication/list/gateways', body)
- .map(response => response.response)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement