Advertisement
Guest User

Untitled

a guest
Dec 14th, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.54 KB | None | 0 0
  1. import { Injectable } from '@angular/core';
  2. import { Http, Response, Headers, RequestOptions, ResponseOptions, URLSearchParams } from '@angular/http';
  3. import { Backend, IOT_URL } from '../../../data/backend';
  4. import { Observable } from 'rxjs/Observable';
  5. import { Plant } from './plants';
  6. import { User } from '../users/users';
  7. import { Country } from '../country/country';
  8. import { System } from '../systems/systems';
  9. import { Alarm } from '../alarm/alarm';
  10. import { Sensor } from '../sensors/sensors';
  11. import { Gateway } from '../gateway/gateway';
  12. import { SystemsService } from '../systems/systems-service';
  13. const invalidAddressErr = "invalid address";
  14. import 'rxjs/Rx';
  15.  
  16. declare interface PlantDashboard {
  17. plant: Plant,
  18. systems: Array<{ name: string, alarms: Array<Alarm>, sensors?: Array<Sensor> }>
  19. }
  20.  
  21. @Injectable()
  22. export class PlantsService extends Backend {
  23. valid: boolean = false;
  24. errorMessage: string;
  25. newPlant: Plant;
  26.  
  27. constructor(public http: Http) {
  28. super(http, IOT_URL);
  29. }
  30. getPlantsByTenant(tenantId: string): Observable<Array<Plant>> {
  31. return this.getBackend('/customer/plant/list/by-tenant/' + tenantId)
  32. .map(response => response.plants)
  33. }
  34.  
  35. getPlants(): Observable<Array<Plant>> {
  36. return this.getBackend('/customer/plant/list/all')
  37. .map(response => response.plants)
  38. }
  39. getPlantManager(id: string): Observable<Array<User>> {
  40. return this.getBackend('/customer/tenant/' + id + '/list/managers')
  41. .map(response => response.managers)
  42. }
  43. addPlant(plantInfo: any, parentId: string): Observable<any> {
  44. let managerId = plantInfo.plantManagerID;
  45. let countryId = plantInfo.countryPlant.id;
  46. let name = plantInfo.name;
  47. let description = plantInfo.description;
  48. let address = plantInfo.address;
  49. let street = plantInfo.address;
  50. let state = plantInfo.state;
  51. let city = plantInfo.city;
  52. let zipCode = plantInfo.zipCode;
  53. let longitude = Number(plantInfo.longitude);
  54. let latitude = Number(plantInfo.latitude);
  55. console.log('parentId od service:' + parentId);
  56. console.log('country name' + plantInfo.countryPlant.name);
  57. let body = {
  58. managerId,
  59. parentId,
  60. name,
  61. description,
  62. countryId,
  63. address,
  64. street,
  65. city,
  66. zipCode,
  67. longitude,
  68. latitude
  69. };
  70. if (!longitude || !latitude) {
  71. console.log("nema lat i long")
  72. let googleAddress = address.split(' ').join('+');
  73. let googleCity = city.split(' ').join('+');
  74. let googleCountry = plantInfo.countryPlantID.name.idsplit(' ').join('+');
  75. let googleState = state.split(' ').join('+');
  76. let params = new URLSearchParams();
  77. let googleUrlParams = googleAddress + '+' + googleCity + '+' + googleState + '+' + zipCode + '+' + googleCountry;
  78. params.set('address', googleUrlParams);
  79. return this.http.get('https://maps.googleapis.com/maps/api/geocode/json', {
  80. search: params
  81. }).flatMap((response: Response) => {
  82. if (!response.json().results[0]) {
  83. console.log("invalid address");
  84. return Observable.throw(invalidAddressErr);
  85. }
  86. body.longitude = response.json().results[0].geometry.location.lng;
  87. body.latitude = response.json().results[0].geometry.location.lat;
  88.  
  89. return this.postPlant(body);
  90. });
  91. } else {
  92. return this.postPlant(body);
  93. }
  94. }
  95.  
  96. updatePlant(plantInfo: any, parentId: string, plantId: string): Observable<Plant> {
  97. let name = plantInfo.name;
  98. let description = plantInfo.description;
  99. let address = plantInfo.address;
  100. let street = plantInfo.address;
  101. let state = plantInfo.state;
  102. let city = plantInfo.city;
  103. let zipCode = plantInfo.zipCode;
  104. let longitude = Number(plantInfo.longitude);
  105. let latitude = Number(plantInfo.latitude);
  106. let managerId = plantInfo.plantManagerID;
  107. let countryId = plantInfo.countryPlant.id;
  108. let countryName = plantInfo.countryPlant.name;
  109. let body = {
  110. managerId,
  111. parentId,
  112. countryId,
  113. name,
  114. description,
  115. address,
  116. street,
  117. city,
  118. zipCode,
  119. longitude,
  120. latitude
  121. };
  122.  
  123. if (!longitude || !latitude) {
  124. let googleAddress = address.split(' ').join('+');
  125. let googleCity = city.split(' ').join('+');
  126. let googleCountry = countryName.split(' ').join('+');
  127. let googleState = state.split(' ').join('+');
  128. let params = new URLSearchParams();
  129. let googleUrlParams = googleAddress + '+' + googleCity + '+' + googleState + '+' + zipCode + '+' + googleCountry;
  130. params.set('address', googleUrlParams);
  131. return this.http.get('https://maps.googleapis.com/maps/api/geocode/json', {
  132. search: params
  133. }).flatMap((response: Response) => {
  134. if (!response.json().results[0]) {
  135. return Observable.throw(invalidAddressErr);
  136. }
  137. body.longitude = response.json().results[0].geometry.location.lng;
  138. body.latitude = response.json().results[0].geometry.location.lat;
  139. return this.updatePlantReady(body, plantId);
  140. });
  141. } else {
  142. return this.updatePlantReady(body, plantId);
  143. }
  144. }
  145. listPlantsForMap(lowerLatitude: number, lowerLongitude: number, higherLatitude: number, higherLongitude: number): Observable<Array<Plant>> {
  146. let body = {
  147. lowerLongitude,
  148. lowerLatitude,
  149. higherLongitude,
  150. higherLatitude,
  151. };
  152. return this.postBackend('/customer/plant/list/by-coordinates/v1', body)
  153. .map(response => response.plants)
  154. }
  155. postPlant(body: any) {
  156. return this.postBackend('/customer/plant/create', body)
  157. .map(response => response.plant)
  158. }
  159. updatePlantReady(body: any, pId: string) {
  160. return this.postBackend('/customer/plant/' + pId + '/update', body)
  161. .map(response => response.plant)
  162. }
  163.  
  164. getPlantDetails(plantId: string): Observable<Plant> {
  165. return this.getBackend('/customer/plant/' + plantId + '/get')
  166. .map(response => response.plant)
  167. }
  168.  
  169. getPlantDashboard(plantId: string): Observable<PlantDashboard> {
  170. return this.getBackend('/device/list/by-plant/' + plantId + '/by-system')
  171. .map(response => response)
  172. }
  173.  
  174. getGatewaysForPlant(plantId: string): Observable<Array<Gateway>> {
  175. return this.getBackend('/communication/plant/' + plantId + '/gateways')
  176. .map(response => response.gateways)
  177. }
  178.  
  179. getGatewaysForAccount(acountName: string, username: string, pass: string): Observable<any> {
  180. let companyName = acountName;
  181. let userName = username;
  182. let password = pass;
  183. let body = {
  184. companyName,
  185. userName,
  186. password,
  187. };
  188. console.log(body);
  189. return this.postBackend('/communication/list/gateways', body)
  190. .map(response => response.response)
  191. }
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement