Files
SIG/Ashx/SIGGPSRCService.ashx

203 lines
6.8 KiB
Plaintext

<%@ WebHandler Language="C#" Class="SIGGPSRCService" %>
using System;
using System.Web;
using System.Web.SessionState;
using System.Data;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Extensiones;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Text;
public class SIGGPSRCService : IHttpHandler, IRequiresSessionState
{
public bool EsProduccion
{
get
{
if (System.Configuration.ConfigurationManager.AppSettings["EsProduccion"] == null)
return false;
else
return (System.Configuration.ConfigurationManager.AppSettings["EsProduccion"].ToString() == "1");
}
}
private Usuario ValidaUsuarioSistema(int CveEmpresa)
{
Usuario UsuarioActual = null;
ClsSisUsuarios ObjUsuarios = new ClsSisUsuarios();
if (ObjUsuarios.GetUsuarios(1727))
{
if (ObjUsuarios.DtResultados.Rows.Count > 0)
{
var Usuario = ObjUsuarios.DtResultados.Rows[0]["Usuario"].ToString();
var Contrasenia = ObjUsuarios.DtResultados.Rows[0]["Contraseña"].ToString();
UsuarioActual = ObjUsuarios.ValidaUsuario(Usuario, Contrasenia, ModExtensiones.IMEI, CveEmpresa);
UsuarioActual.CveEmpresa = CveEmpresa;
UsuarioActual.CveCliente = 183798;
CreaObjetoEmpresa(ref UsuarioActual);
CargaZonasHorarias(ref UsuarioActual);
}
}
ObjUsuarios.Dispose();
return UsuarioActual;
}
private void CreaObjetoEmpresa(ref Usuario UsuarioActual)
{
ClsSisEmpresas ObjEmpresa = new ClsSisEmpresas();
if (ObjEmpresa.GetEmpresas(UsuarioActual.CveEmpresa, "-1", "-1", "-1", "-1"))
{
if (ObjEmpresa.GetEmpresasFuncionalidades(UsuarioActual.CveEmpresa))
{
System.Data.DataTable dttmp = ObjEmpresa.DtResultados;
var EmpresasFuncionalidades = new HybridDictionary();
foreach (System.Data.DataRow Row in dttmp.Rows)
EmpresasFuncionalidades.Add(Convert.ToInt32(Row["CveFuncionalidad"]), Row["Valor"].ToString());
ObjEmpresa.Empresa.SetFuncionalidades(EmpresasFuncionalidades);
}
UsuarioActual.Empresa = ObjEmpresa.Empresa;
}
ObjEmpresa.Dispose();
}
private void CargaZonasHorarias(ref Usuario UsuarioActual)
{
ClsClientes ObjClientes = new ClsClientes();
try
{
if (ObjClientes.GetClientes(UsuarioActual.CveCliente, "-1", "-1", "-1", "-1", "-1", "-1", -1, -1, Estatus: 1))
{
if (ObjClientes.DtResultados.Rows.Count == 1)
UsuarioActual.UTCOffset = ObjClientes.DtResultados.Rows[0]["UTCOffset"].ToString().ToEntero();
}
}
catch (Exception ex)
{
ex.ToString().GuardaLog();
}
finally
{
ObjClientes.Dispose();
}
}
public bool IsReusable => false;
public void ProcessRequest(HttpContext context)
{
try
{
context.Response.ContentType = "text/plain";
bool retorno = false;
string Metodo = context.Request["Metodo"] == null ? "" : context.Request["Metodo"];
switch (Metodo)
{
case "EjecutaServicio":
ModExtensiones.GuardaLogAD("Servicio Ejecutado...........", "LogServicioConfiable");
IniciarTransmision(context);
retorno = true;
break;
default:
break;
}
if (retorno)
{
context.Response.Write("True");
}
else
{
context.Response.Write("False");
}
}
catch (Exception ex)
{
ex.GuardaLog(ex.Message);
}
}
private void IniciarTransmision(HttpContext context)
{
var UsuarioActual = ValidaUsuarioSistema(121);
var WS = new RCGPSService.RCServiceClient();
RCGPSService.UserTokenResult Token = WS.GetUserToken(UserService, PasswordService);
List<RCGPSService.Event> ListaEventos = new List<RCGPSService.Event>();
DataTable DtTrackings = null;
var ObjGPS = new ClsGPS();
try
{
if (ObjGPS.GetRCTracking(-1, -1, 1, -1, -1))
{
DtTrackings = ObjGPS.DtResultados.Copy();
}
if (DtTrackings != null)
{
foreach (DataRow RowTrack in DtTrackings.Rows)
{
DataTable dtTracking = null;
if (ObjGPS.GetRCTrackingDatos(RowTrack["CveRCTracking"].ToString().ToEntero()))
{
dtTracking = ObjGPS.DtResultados.Copy();
}
if (dtTracking != null)
{
foreach (DataRow Row in dtTracking.Rows)
{
ListaEventos.Add(new RCGPSService.Event()
{
code = "STT",
date = Row["Fecha"].ToString().ToDateTime(),
latitude = Row["Latitud"].ToString(),
longitude = Row["Longitud"].ToString(),
asset = Row["PlacasVehiculo"].ToString(),
speed = Row["Velocidad"].ToString(),
shipment = Row["Shipment"].ToString()
});
}
}
dtTracking.Dispose();
dtTracking = null;
}
}
}
catch (Exception ex)
{
this.OnError("Ocurrió un error al enviar los datos", ex);
}
ObjGPS.Dispose();
DtTrackings.Dispose();
DtTrackings = null;
if (ListaEventos.Count > 0)
{
var respuesta = WS.GPSAssetTracking(Token.token, ListaEventos.ToArray());
context.Response.Write(JsonConvert.SerializeObject(respuesta));
}
WS.Close();
//context.Response.Write(JsonConvert.SerializeObject(new { idJob = "1", exception = "" }));
}
private string UserService => EsProduccion ? "ws_avl_opsat" : "demo_cedis_corp";
private string PasswordService => EsProduccion ? "eiHv##889iWfz#&2" : "xxxxxxxx";
}