//
        public JsonResult TipoAlertaPost(int id_tipoalerta, int id_empresa, string descricao, string unidade_medida, int ativo, int tipo)
        {
            string ret  = string.Empty;
            string erro = string.Empty;

            //
            try
            {
                ColetorTipoAlerta cta = new ColetorTipoAlerta();
                //
                if (id_tipoalerta > 0)
                {
                    cta = db.ColetorTipoAlerta.Where(a => a.Id == id_tipoalerta && a.Id_Empresa == id_empresa).FirstOrDefault();
                    //
                    if (cta != null)
                    {
                        cta.Descricao       = descricao;
                        cta.UnidadeMedida   = unidade_medida;
                        cta.Ativo           = ativo == 1 ? true : false;
                        cta.Tipo            = tipo;
                        db.Entry(cta).State = EntityState.Modified;
                        db.SaveChanges();
                        ret = "ok";
                    }
                    else
                    {
                        ret = "Tipo Alerta não encontrado: TipoAlerta (" + id_tipoalerta + ") - Empresa (" + id_empresa + ")";
                    }
                }
                else
                {
                    cta.Id_Empresa    = id_empresa;
                    cta.Descricao     = descricao;
                    cta.UnidadeMedida = unidade_medida;
                    cta.Ativo         = ativo == 1 ? true : false;
                    cta.Tipo          = tipo;
                    db.ColetorTipoAlerta.Add(cta);
                    db.SaveChanges();
                    db.Entry(cta).Reload();
                    ret = "ok";
                }
            }
            catch (Exception exc)
            {
                erro = exc.Message;
                ret  = "nok";
            }
            //
            return(Json(new { ret, results = 0, success = true, erro }, JsonRequestBehavior.AllowGet));
        }
        //
        public JsonResult ExcluiTipoAlerta(int idtipoalerta, int idempresa)
        {
            string ret  = string.Empty;
            string erro = string.Empty;

            //
            try
            {
                ColetorAlerta ca = db.ColetorAlerta.Where(x => x.Id_TipoAlerta == idtipoalerta && x.Id_Empresa == idempresa).FirstOrDefault();
                if (ca != null)
                {
                    ret = "coletoralerta";
                }
                else
                {
                    ColetorTipoAlerta ctp = db.ColetorTipoAlerta.Where(x => x.Id == idtipoalerta && x.Id_Empresa == idempresa).FirstOrDefault();
                    if (ctp != null)
                    {
                        db.Entry(ctp).State = EntityState.Deleted;
                        db.SaveChanges();
                        ret = "ok";
                    }
                    else
                    {
                        ret = "nok";
                    }
                }
            }
            catch (Exception exc)
            {
                ret  = "erro";
                erro = exc.Message;
            }
            //
            return(Json(new { ret, results = 0, success = true, erro }, JsonRequestBehavior.AllowGet));
        }
        //
        public JsonResult SalvaColetorAlerta(int idcoletoralerta, int idempresa, int idcoletor, int idtipoalerta, int idregra, string valor, string email, int ativo)
        {
            string            ret                 = string.Empty;
            string            erro                = string.Empty;
            string            tipoRegraAlerta     = string.Empty;
            string            stipoalerta         = string.Empty;
            string            stipoalerta_unidade = string.Empty;
            ColetorTipoAlerta oColetorTipoAlerta  = new ColetorTipoAlerta();

            //
            try
            {
                oColetorTipoAlerta = db.ColetorTipoAlerta.Where(x => x.Id_Empresa == idempresa && x.Id == idtipoalerta).FirstOrDefault();
                stipoalerta        = oColetorTipoAlerta.Descricao;
                //
                if (oColetorTipoAlerta.Tipo != null)
                {
                    switch (oColetorTipoAlerta.Tipo.Value)
                    {
                    case 1:
                        stipoalerta = "Temperatura ";
                        break;

                    case 2:
                        stipoalerta = "Pressão ";
                        break;

                    case 3:
                        stipoalerta = "Produção ";
                        break;
                    }
                }
                else
                {
                    oColetorTipoAlerta.Descricao = "N/A";
                }
                //
                stipoalerta_unidade = oColetorTipoAlerta.UnidadeMedida;
                //
                switch (idregra)
                {
                case 1:
                    tipoRegraAlerta = "Maior que ";
                    break;

                case 2:
                    tipoRegraAlerta = "Menor que ";
                    break;

                case 3:
                    tipoRegraAlerta = "Igual a ";
                    break;

                case 4:
                    tipoRegraAlerta = "Maior ou Igual a ";
                    break;

                case 5:
                    tipoRegraAlerta = "Manor ou Igual a ";
                    break;
                }
                //
                if (idcoletoralerta > 0)
                {
                    ColetorAlerta ca = db.ColetorAlerta.Where(x => x.Id == idcoletoralerta && x.Id_Empresa == idempresa && x.Id_Coletor == idcoletor).FirstOrDefault();
                    //
                    if (ca != null)
                    {
                        ca.Id_TipoAlerta = idtipoalerta;
                        ca.Email         = email;
                        ca.Valor         = valor;
                        ca.Ativo         = ativo;
                        ca.Regra         = idregra;
                        ca.Descricao     = stipoalerta + " (" + stipoalerta_unidade + ") " + tipoRegraAlerta + valor;
                        //
                        db.Entry(ca).State = EntityState.Modified;
                        db.SaveChanges();
                    }
                    else
                    {
                        ret = "Não foi possível encontrar o alerta.";
                    }
                }
                else
                {
                    ColetorAlerta ca = new ColetorAlerta();
                    //
                    ca.Id_Empresa    = idempresa;
                    ca.Id_Coletor    = idcoletor;
                    ca.Id_TipoAlerta = idtipoalerta;
                    ca.Email         = email;
                    ca.Valor         = valor;
                    ca.Ativo         = ativo;
                    ca.Regra         = idregra;
                    ca.Descricao     = stipoalerta + " (" + stipoalerta_unidade + ") " + tipoRegraAlerta + " a " + valor;
                    //
                    db.ColetorAlerta.Add(ca);
                    db.SaveChanges();
                }
                //
                ret = "ok";
            }
            catch (Exception exc)
            {
                erro = exc.Message;
                ret  = "nok";
            }
            //
            return(Json(new { ret, erro, success = true }, JsonRequestBehavior.AllowGet));
        }