class NotificationActionExempleTest: def __init__(self): self.configuration = TransactionConfiguration() self.configuration.loadEnvironment() self.message_dao = PikaDAO(self.configuration) self.message_dao.connecter() self.generateur_transaction = GenerateurTransaction(self.configuration, self.message_dao) def deconnecter(self): self.message_dao.deconnecter() def test1(self): # temps_lecture_ajuste = temps_lecture + datetime.timedelta(hours=4) domaine = TachesConstantes.TRANSACTION_ACTION_TACHE transaction_message = dict({ TachesConstantes.LIBELLE_ID_NOTIFICATION: '5c152457e094095d8c8d314e', TachesConstantes.LIBELLE_ACTION: TachesConstantes.ACTION_VUE, TachesConstantes.LIBELLE_DATE_ATTENTE_ACTION: 180 }) self.generateur_transaction.soumettre_transaction(transaction_message, domaine) print("Sent notification domaine %s: %s" % (domaine, transaction_message))
class TransactionMain: def __init__(self): self.configuration = TransactionConfiguration() self.configuration.loadEnvironment() self.transactionLirePika = PikaDAO(self.configuration) self.callbackImpl = CallbackNouvelleTransaction() def connecter(self): self.connexionMq = self.transactionLirePika.connecter() self.transactionLirePika.demarrer_lecture_nouvelles_transactions( self.callbackImpl.callbackAvecAck) def deconnecter(self): self.transactionLirePika.deconnecter() # Methode principale de traitement def run(self): print("Demarrage du traitement des transactions MQ -> MongoDB") print("MQ Host: %s, MQ Queue: %s" % (self.configuration.mq_host, self.configuration.queue_nouvelles_transactions)) self.connecter() print("Fin execution transactions MQ -> MongoDB")
class AggregationSenseursDocumentTest: def __init__(self): self.configuration = TransactionConfiguration() self.configuration.loadEnvironment() print("Connecter Pika") self.messageDao = PikaDAO(self.configuration) self.messageDao.connecter() print("Connection MongDB") self.documentDao = MongoDAO(self.configuration) self.documentDao.connecter() self.producteur = ProducteurDocumentSenseurPassif( self.messageDao, self.documentDao) def run(self): id_documents = self.producteur.trouver_id_documents_senseurs() print("Documents: %s" % str(id_documents))
class SenseursPassifsRequetesTest: def __init__(self): self.configuration = TransactionConfiguration() self.configuration.loadEnvironment() print("Connection MongDB") self.document_dao = MongoDAO(self.configuration) self.document_dao.connecter() print("Connecter Pika") self.message_dao = PikaDAO(self.configuration) self.message_dao.connecter() self._traitement = TraitementBacklogLecturesSenseursPassifs( self.message_dao, self.document_dao) def run_requete_plusrecentetransactionlecture_parsenseur(self): return self._traitement.run_requete_plusrecentetransactionlecture_parsenseur( ) def run_requete_genererdeclencheur_parsenseur(self, liste_senseurs): self._traitement.run_requete_genererdeclencheur_parsenseur( liste_senseurs)
class NotificationExempleTest: def __init__(self): self.configuration = TransactionConfiguration() self.configuration.loadEnvironment() self.message_dao = PikaDAO(self.configuration) self.message_dao.connecter() def deconnecter(self): self.message_dao.deconnecter() def test1(self): # temps_lecture_ajuste = temps_lecture + datetime.timedelta(hours=4) notification = dict({ '_mg-libelle': 'regle_simple', 'evenements': Constantes.EVENEMENT_NOTIFICATION, 'source': { '_collection': 'mgdomaines_appareils_SenseursPassifs', '_id': "5bef31be82cc2cb5ab0d57fe" }, 'regles': [{ "pasbonne_10": { "element": "temperature" } }], "date": int(datetime.datetime.utcnow().timestamp()), 'valeurs': { "element": "temperature", "valeur": 24.6 } }) self.message_dao.transmettre_notification( notification, TachesConstantes.AVERTISSEMENT) print("Sent notification: %s" % notification)
"5bee12bce09409b7881c0da4" } processus = "mgdomaines_appareils_SenseursPassifs:ProcessusTransactionSenseursPassifsLecture" demarreur.demarrer_processus(processus, message_test) return message_test # --- MAIN --- configuration = TransactionConfiguration() configuration.loadEnvironment() print("Connecter Pika") messageDao = PikaDAO(configuration) messageDao.connecter() print("Connection MongDB") documentDao = MongoDAO(configuration) documentDao.connecter() print("Envoyer message") demarreur = MGPProcessusDemarreur(messageDao, documentDao) # TEST enveloppe = message_test() # FIN TEST print("Sent: %s" % enveloppe)
# Script qui agit comme set-up pour RabbitMQ import time from millegrilles.dao.Configuration import TransactionConfiguration from millegrilles.dao.MessageDAO import PikaDAO configuration = TransactionConfiguration() configuration.loadEnvironment() messagedao = PikaDAO(configuration) messagedao.connecter() messagedao.configurer_rabbitmq() time.sleep(20) messagedao.deconnecter()
class ContexteRessourcesMilleGrilles: """ Classe helper qui permet d'initialiser et de passer les ressources (configuration, DAOs) """ def __init__(self, configuration: TransactionConfiguration = None, message_dao=None, additionals: list = None): """ Init classe. Fournir les ressources deja initialisee ou utiliser methode initialiser(). :param configuration: Optionnel, configuration MilleGrilles deja initialisee. :param message_dao: Optionnel, message_dao deja initialise. :param additionals: Fichiers de config additionels a combiner """ self._configuration = configuration self._message_dao = message_dao self._additionnals = additionals self._email_dao = None self._signateur_transactions = None self._verificateur_certificats = None self._verificateur_transactions = None self._generateur_transactions = None self.validation_workdir_tmp = None self.__logger = logging.getLogger(__name__ + '.' + self.__class__.__name__) def initialiser(self, init_message=True, connecter=True): """ Initialise/reinitialise le contexte et connecte les DAOs. :param init_message: Si True, initialise et connecte PikaDAO :param connecter: Si true, la connexion aux DAOs est ouverte immediatement """ if not self._configuration: self._configuration = TransactionConfiguration() self._configuration.loadEnvironment(additionals=self._additionnals) self._message_dao = None self.validation_workdir_tmp = tempfile.mkdtemp(prefix='millegrilles_pki_', dir=self._configuration.pki_workdir) if init_message: self._message_dao = PikaDAO(self._configuration) self._signateur_transactions = SignateurTransaction(self) self._signateur_transactions.initialiser() # Preparer les certificats, validateurs self._verificateur_transactions = VerificateurTransaction(self) self._verificateur_transactions.initialiser() self._verificateur_certificats = VerificateurCertificats(self) self._verificateur_certificats.initialiser() if connecter: self._message_dao.connecter() def fermer(self): try: shutil.rmtree(self.validation_workdir_tmp) except Exception as e: self.__logger.warning("Erreur suppression workdir pki tmp : %s", str(e)) try: self._message_dao.deconnecter() except: pass @property def configuration(self): return self._configuration @property def message_dao(self) -> PikaDAO: """ Retourne un message_dao. :return: Message dao. :raises: ValueError is le message dao n'a pas ete defini. """ # if self._message_dao is None: # raise ValueError("MessageDAO n'est pas initialise") return self._message_dao @message_dao.setter def message_dao(self, message_dao): self._message_dao = message_dao @property def generateur_transactions(self) -> GenerateurTransaction: if self._generateur_transactions is None: self._generateur_transactions = GenerateurTransaction(self) return self._generateur_transactions @property def signateur_transactions(self) -> SignateurTransaction: return self._signateur_transactions @property def verificateur_transaction(self) -> VerificateurTransaction: return self._verificateur_transactions @property def verificateur_certificats(self) -> VerificateurCertificats: return self._verificateur_certificats @property def idmg(self) -> str: return self._configuration.idmg
class TestCertificatsRabbitMQ: def __init__(self): self.configuration = TransactionConfiguration() self.connectionmq = None self.channel = None self.message_dao = None def configurer(self): self.configuration.loadEnvironment() self.message_dao = PikaDAO(self.configuration) def preparer_connexion(self): connection_parameters = { 'host': self.configuration.mq_host, 'port': self.configuration.mq_port, 'virtual_host': self.configuration.idmg, 'heartbeat': 300 } credentials = { 'username': self.configuration.mq_user, 'password': self.configuration.mq_password, 'erase_on_connect': True } connection_parameters['credentials'] = PlainCredentials(**credentials) if self.configuration.mq_ssl == 'on': # verify_mode = ssl.CERT_NONE # server_hostname = None ssl_options = { 'ssl_version': ssl.PROTOCOL_TLSv1_2, 'keyfile': '/usr/local/etc/millegrilles/certs/keys/think003.pivoine.mdugre.info.pem', 'certfile': '/usr/local/etc/millegrilles/certs/think003.pivoine.mdugre.info.cert.pem', 'ca_certs': '/usr/local/etc/millegrilles/certs/millegrilles.authority.pem', 'cert_reqs': ssl.CERT_REQUIRED } connection_parameters['ssl'] = True connection_parameters['ssl_options'] = ssl_options return connection_parameters def connecter_test(self): connection_parameters = pika.ConnectionParameters( **self.preparer_connexion()) self.connectionmq = pika.BlockingConnection(connection_parameters) def connecter_dao(self): self.connectionmq = self.message_dao.connecter() def verifier_connexion(self): self.channel = self.connectionmq.channel() self.channel.basic_qos(prefetch_count=1) def transmettre_message(self): pass def fermer(self): self.connectionmq.close()