<commit_before>import paho.mqtt.client as mqtt
import ssl
import json
import time
from OmegaExpansion import onionI2C
from readTemp import SensorSHT31, SensorSHT25


class SensorPublisher(object):
    def __init__(self, sensors, order=None):
        self.sensors = sensors
        self.order = order
        if order is None:
            self.order = self.sensors.keys()
        else:
            self.order = order

        self.client = mqtt.Client()

        # userdata contains the last connection status
        self.user_data_set(None)

        self.client.on_connect = self.on_connect
        self.client.on_publish = self.on_publish
        self.client.on_message = self.on_message

    def connect(self, host, port, caFile, certFile, keyFile):
        self.client.tls_set(ca_certs=caFile, certfile=certFile, keyfile=keyFile, cert_reqs=ssl.CERT_REQUIRED, tls_version=ssl.PROTOCOL_TLSv1_2, ciphers=None)
        self.client.connect(host, port, keepalive=60)

    def disconnect(self):
        self.client.disconnect()

    def start(self, pollInterval):
        self.client.loop_start()

        while 1:
            try:
                startTime = time.time()
                print('----')
                for key in self.order:
                    try:
                        sensor = self.sensors[key]
                        cTemp, fTemp, humidity = sensor.read()
                        print('[{}] {} - {:5.2f} deg C, {:5.1f} deg F, {:4.1f} %RH'.format(time.ctime(), key.title(), cTemp, fTemp, humidity))
                        payload = self.buildPayload(cTemp, humidity)
                        fullTopic = '{}/{}'.format(args.topic, key)
                        result, mid = self.client.publish(fullTopic, payload, qos=1)
                    except IOError:
                        print('\n*** Failed to get data for {}, device {}!')
                measurementTime = time.time() - startTime
                time.sleep(max(pollInterval-measurementTime, 0))
            except KeyboardInterrupt:
                break

        self.client.loop_stop()

    def buildPayload(temp, humidity):
        data = {'timestamp': time.time(),
                'temperature': cTemp,
                'humidity': humidity
               }
        jsonString = json.dumps(data)
        return jsonString

    # The callback for when the client receives a CONNACK response from the server.
    @staticmethod
    def on_connect(client, userdata, flags, rc):
        # userdata contains the last connection status
        if rc != userdata:
            print("\n*** Connected with result code "+str(rc) + '\n')
            client.user_data_set(rc)<commit_msg>Monitor fix<commit_after>#!/usr/bin/env python

import paho.mqtt.client as mqtt
import ssl
import json
import time
from OmegaExpansion import onionI2C
from readTemp import SensorSHT31, SensorSHT25


class SensorPublisher(object):
    def __init__(self, sensors, order=None):
        self.sensors = sensors
        self.order = order
        if order is None:
            self.order = self.sensors.keys()
        else:
            self.order = order

        self.client = mqtt.Client()

        # userdata contains the last connection status
        self.client.user_data_set(None)

        self.client.on_connect = self.on_connect
        self.client.on_publish = self.on_publish
        self.client.on_message = self.on_message

    def connect(self, host, port, caFile, certFile, keyFile):
        self.client.tls_set(ca_certs=caFile, certfile=certFile, keyfile=keyFile, cert_reqs=ssl.CERT_REQUIRED, tls_version=ssl.PROTOCOL_TLSv1_2, ciphers=None)
        self.client.connect(host, port, keepalive=60)

    def disconnect(self):
        self.client.disconnect()

    def start(self, pollInterval):
        self.client.loop_start()

        while 1:
            try:
                startTime = time.time()
                print('----')
                for key in self.order:
                    try:
                        sensor = self.sensors[key]
                        cTemp, fTemp, humidity = sensor.read()
                        print('[{}] {} - {:5.2f} deg C, {:5.1f} deg F, {:4.1f} %RH'.format(time.ctime(), key.title(), cTemp, fTemp, humidity))
                        payload = self.buildPayload(cTemp, humidity)
                        fullTopic = '{}/{}'.format(args.topic, key)
                        result, mid = self.client.publish(fullTopic, payload, qos=1)
                    except IOError:
                        print('\n*** Failed to get data for {}, device {}!')
                measurementTime = time.time() - startTime
                time.sleep(max(pollInterval-measurementTime, 0))
            except KeyboardInterrupt:
                break

        self.client.loop_stop()

    def buildPayload(self, temp, humidity):
        data = {'timestamp': time.time(),
                'temperature': temp,
                'humidity': humidity
               }
        jsonString = json.dumps(data)
        return jsonString

    # The callback for when the client receives a CONNACK response from the server.
    @staticmethod
    def on_connect(client, userdata, flags, rc):
        # userdata contains the last connection status
        if rc != userdata:
            print("\n*** Connected with result code "+str(rc) + '\n')
            client.user_data_set(rc)
{'fraction_non_alphanumeric': 0.10517466841023725, 'fraction_numerical': 0.006725200821968989, 'mean_word_length': 1.9224890829694323, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1291196', 'n_tokens_mistral': 1576, 'n_tokens_neox': 1475, 'n_words': 373}