Exemple #1
0
 def logout_expired_sessions(self):
     agentes_deslogueados = []
     agent_activity = AgentActivityAmiManager()
     conectado = False
     hora_actual = now()
     for agente_profile in AgenteProfile.objects.obtener_activos():
         session = None
         if agente_profile.user.last_session_key:
             try:
                 session = Session.objects.get(
                     session_key=agente_profile.user.last_session_key)
             except Session.DoesNotExist:
                 pass
         if session and session.expire_date < hora_actual:
             if not conectado:
                 agent_activity.connect_manager()
                 conectado = True
             agentes_deslogueados.append(str(agente_profile.id))
             agente_profile.force_logout()
             agent_activity.logout_agent(agente_profile,
                                         manage_connection=False)
     if conectado:
         agent_activity.disconnect_manager()
     if agentes_deslogueados:
         logger.info("Expired Sessions detected: " +
                     str(agentes_deslogueados))
class SupervisorActivityAmiManager(object):

    EXTENSIONES = [
        "AGENTLOGOUT", "AGENTUNPAUSE", "AGENTPAUSE", "CHANTAKECALL",
        "CHANSPYWISHPER", "CHANSPY", "CHANCONFER"
    ]

    def __init__(self, *args, **kwargs):
        self.manager = AMIManagerConnector()
        self.agent_activity = AgentActivityAmiManager()

    def _originate_call(self, originate_data):
        self.manager.connect()
        content = originate_data
        data_returned = self.manager._ami_manager('originate', content)
        self.manager.disconnect()
        return data_returned

    def obtener_agentes_activos(self):
        agentes_parseados = AgentesParsing()
        agentes_activos = []
        self.manager.connect()
        data_returned, error = self.manager._ami_manager(
            "command", "database show OML/AGENT")
        self.manager.disconnect()
        agentes_activos = agentes_parseados._parsear_datos_agentes(
            data_returned)
        return agentes_activos

    def ejecutar_accion_sobre_agente(self, supervisor, agente_id, exten):
        agente_profile = AgenteProfile.objects.get(id=agente_id)
        if exten not in self.EXTENSIONES:
            return _("La acción indicada no existe")
        channel = "PJSIP/{0}".format(supervisor.sip_extension)
        channel_vars = {
            'OMLAGENTID': str(agente_id),
        }
        originate_data = [channel, exten, 'oml-sup-actions', channel_vars]
        # Genero la llamada via originate por AMI
        if exten == "AGENTLOGOUT":
            agente_profile.force_logout()
            self.agent_activity.logout_agent(agente_profile,
                                             manage_connection=True)
        elif exten == "AGENTPAUSE":
            self.agent_activity.pause_agent(agente_profile,
                                            '00',
                                            manage_connection=True)
        elif exten == "AGENTUNPAUSE":
            self.agent_activity.unpause_agent(agente_profile,
                                              '00',
                                              manage_connection=True)
        else:
            self._originate_call(originate_data)

    def escribir_agentes_unavailable_astdb(self):
        """ Abre una conexion para setear a los agentes como UNAVAILABLE en ASTDB  """
        self.agent_activity.connect_manager()
        agentes_profiles = []
        # Nota: Accedo al manager del agent_activity para no crear otra conexion
        user_activity_list, error = self.agent_activity.manager._ami_manager(
            'command', 'queue show')
        for activity_line in user_activity_list.splitlines():
            if activity_line.find("Unavailable") != -1:
                fields_activity = activity_line.split()
                agente_id = fields_activity[1].split('_')[0]
                agente_profile = AgenteProfile.objects.get(id=agente_id)
                if agente_profile not in agentes_profiles:
                    agentes_profiles.append(agente_profile)
        if agentes_profiles:
            for agente_profile in agentes_profiles:
                self.agent_activity.set_agent_as_unavailable(
                    agente_profile, manage_connection=False)
        self.agent_activity.disconnect_manager()