//
        public JsonResult ExcluiColetorAlertaPost(int idalerta, int idcoletor, int idempresa)
        {
            string ret  = string.Empty;
            string erro = string.Empty;

            try
            {
                ColetorAlerta oColetorAlerta = db.ColetorAlerta.Where(x => x.Id == idalerta && x.Id_Coletor == idcoletor && x.Id_Empresa == idempresa).FirstOrDefault();
                //
                if (oColetorAlerta != null)
                {
                    string        sql           = "DELETE ColetorAlertaLog WHERE Id_Coletor = " + idcoletor + " AND Id_ColetorAlerta = " + oColetorAlerta.Id;
                    SQLController sqlcontroller = new SQLController();
                    sqlcontroller.ExecutaSQLNonQuery(sql, out erro);
                    //
                    db.Entry(oColetorAlerta).State = EntityState.Deleted;
                    db.SaveChanges();
                    ret = "ok";
                }
                else
                {
                    ret = "nao_encontrada";
                }
            }
            catch (Exception exc)
            {
                ret  = "erro";
                erro = exc.Message;
            }

            return(Json(new { ret, results = 0, erro, success = true }, 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));
        }