Skip to content

Instantly share code, notes, and snippets.

@cesjam7
Created August 17, 2020 17:35
Show Gist options
  • Save cesjam7/3f827c20c1d97eba254cd05479dd9291 to your computer and use it in GitHub Desktop.
Save cesjam7/3f827c20c1d97eba254cd05479dd9291 to your computer and use it in GitHub Desktop.
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { ActivatedRoute, Router } from '@angular/router';
import * as M from "materialize-css/dist/js/materialize";
@Injectable({
providedIn: 'root'
})
export class ResourcesService {
// route: string = 'http://localhost:8081';
route : string = 'http://159.65.47.181:8081';
// routePlanta: string = 'http://localhost:3001';
routePlanta: string = 'http://159.65.47.181:3001';
versionFE = '5';
versionModulo = 'Poultry';
internet : any = {
wifi : 'wifi',
wifi_icon : 'wifi',
status : 'Conexión establecida'
}
constructor(
private router: Router,
private http: HttpClient
) { }
updateLocalStorageFromOnline() {
this.http.get(this.route + '/galpones').subscribe(
(data: any) => {
console.log('LOCAL STORAGE GALPONES DONE!')
for (let i = 0; i < data.length; i++) {
data[i]['fecha_creacion'] = '2020-01-12T23:00:00.000Z'
}
localStorage.setItem('galpones', JSON.stringify(data));
}
);
this.http.get(this.route + '/lotes').subscribe(
(data: any) => {
console.log('LOCAL STORAGE LOTES DONE!')
// for (let i = 0; i < data.length; i++) {
// if (data[i]['FecinicioProd'] == '0000-00-00 00:00:00') {
// data[i]['FecinicioProd'] = '2020-01-12T23:00:00.000Z'
// }
// }
localStorage.setItem('lotes', JSON.stringify(data));
}
);
this.http.get(this.route + '/corrales').subscribe(
(data: any) => {
console.log('LOCAL STORAGE CORRALES DONE!')
localStorage.setItem('corrales', JSON.stringify(data));
}
);
this.http.get(this.route + '/semanas').subscribe(
(data: any) => {
for (let i = 0; i < data.length; i++) {
data[i]['online'] = 1
}
console.log('LOCAL STORAGE SEMANAS DONE!', data)
localStorage.setItem('semanas', JSON.stringify(data));
}
);
this.http.get(this.routePlanta + '/nacimiento/poultry').subscribe(
(data: any) => {
for (let i = 0; i < data.length; i++) {
data[i]['online'] = 1
}
console.log('LOCAL STORAGE NACIMIENTOS DONE!', data)
localStorage.setItem('nacimientos', JSON.stringify(data));
}
);
this.http.get(this.route + '/semanas/std/Granja').subscribe(
(data:any) => {
localStorage.setItem('stdGranja', JSON.stringify(data));
}
);
this.http.get(this.route + '/semanas/std/Planta').subscribe(
(data:any) => {
localStorage.setItem('stdPlanta', data);
}
)
}
updateOnlineFromLocalStorage() {
this.http.get(this.route + '/galpones').subscribe(
(data: any) => {
// SI HAY ACCESO AL BACKEND
// Todo esto va en ONLINE
var falta_json = localStorage.getItem('falta_subir')
if (falta_json == '' || falta_json == undefined) {
console.log('No hay data que falte subir')
} else {
var falta_subir = JSON.parse(falta_json)
var datos = {
dato: falta_subir
}
console.log('falta_subir', datos)
this.http.post(this.route + '/semanas/localstorage/', datos).subscribe(
(data: any) => {
// Se guardó el storage completo
for (let i = 0; i < falta_subir.length; i++) {
this.http.post(this.route + '/semanas/', falta_subir[i]).subscribe(
(data: any) => {
// VALIDAR QUE ESTA EN LA BASE DE DATOS DESDE EL BACKEND
// SI NO ESTA EN LA DB O SALE ERROR GUARDE LOS QUE FALLEN
// AGREGAR BARRA DE PROGRESO
M.toast({ html: 'Se sincronizó correctamente la semana ' + falta_subir[i]['semana'] })
console.log('SE SUBIÓ CORRECTAMENTE LA DATA QUE FALTABA DE PESOS', data)
},
error => console.log('NO SE PUDO SUBIR LA DATA QUE FALTABA DE PESOS', error)
)
// Despues de insertar todos borramos la data
if ((i + 1) == falta_subir.length) {
localStorage.removeItem('falta_subir');
this.router.navigate(['/galpones/']);
}
}
},
error => console.log('NO SE PUDO SUBIR LA DATA QUE FALTABA DE PESOS', error)
)
}
var falta_json_corral = localStorage.getItem('falta_subir_corral')
if (falta_json_corral == '' || falta_json_corral == undefined) {
console.log('No hay data que falte subir')
} else {
var falta_subir_corral = JSON.parse(falta_json_corral)
console.log('falta_subir_corral', falta_subir_corral)
for (let i = 0; i < falta_subir_corral.length; i++) {
this.http.post(this.route + '/corrales/', falta_subir_corral[i]).subscribe(
(data: any) => {
console.log('SE SUBIÓ CORRECTAMENTE LA DATA QUE FALTABA DE CORRAL', data)
M.toast({ html: 'Se sincronizó correctamente el corral ' + falta_subir_corral[i]['corral'] })
},
error => console.log('NO SE PUDO SUBIR LA DATA QUE FALTABA DE CORRAL', error)
)
// Despues de insertar todos borramos la data
if ((i + 1) == falta_subir_corral.length) {
localStorage.removeItem('falta_subir_corral');
this.router.navigate(['/galpones/']);
}
}
}
}, error => console.log('No hay acceso al backed para subir lo que falta')
)
}
updateSemanasFromOnline() {
this.http.get(this.route + '/semanas').subscribe(
(data: any) => {
for (let i = 0; i < data.length; i++) {
data[i]['online'] = 1
}
console.log('LOCAL STORAGE SEMANAS DONE!', data)
localStorage.setItem('semanas', JSON.stringify(data));
}
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment