<commit_before>import time
from json import loads, decoder
from ssl import PROTOCOL_TLSv1_2
import paho.mqtt.client as mqtt
from myDevices.utils.logger import debug, error, exception, info, logJson, warn

# Topics
DATA_TOPIC = 'data/json'
COMMAND_TOPIC = 'cmd'
COMMAND_JSON_TOPIC = 'cmd.json'
COMMAND_RESPONSE_TOPIC = 'response'

# Data Channels
SYS_HARDWARE_MAKE = 'sys:hw:make'
SYS_HARDWARE_MODEL = 'sys:hw:model'
SYS_OS_NAME = 'sys:os:name'
SYS_OS_VERSION = 'sys:os:version'
SYS_NET = 'sys:net'
SYS_STORAGE = 'sys:storage'
SYS_RAM = 'sys:ram'
SYS_CPU = 'sys:cpu'
SYS_I2C = 'sys:i2c'
SYS_SPI = 'sys:spi'
SYS_UART = 'sys:uart'
SYS_ONEWIRE = 'sys:1wire'
SYS_DEVICETREE = 'sys:devicetree'
SYS_GPIO = 'sys:gpio'
SYS_POWER_RESET = 'sys:pwr:reset'
SYS_POWER_HALT = 'sys:pwr:halt'
AGENT_VERSION = 'agent:version'
AGENT_DEVICES = 'agent:devices'
AGENT_MANAGE = 'agent:manage'
DEV_SENSOR = 'dev'

# Channel Suffixes
IP = 'ip'
SPEEDTEST = 'speedtest'
SSID = 'ssid'
USAGE = 'usage'
CAPACITY = 'capacity'
LOAD = 'load'
TEMPERATURE = 'temp'
VALUE = 'value'
FUNCTION = 'function'


class DataChannel:
    @staticmethod
    def add(data_list, prefix, channel=None, suffix=None, value=None, type=None, unit=None, name=None):
        """Create data channel dict and append it to a list"""
        data_channel = prefix
        if channel:
            data_channel += ':' + str(channel)
        if suffix:
            data_channel += ';' + str(suffix)
        data = {}
        data['channel'] = data_channel
        data['value'] = value
        if type:
            data['type'] = type
        if unit:
            data['unit'] = unit
        if name:
            data['name'] = name
        data_list.append(data)
        

class CayenneMQTTClient:
    """Cayenne MQTT Client class.
    
    This is the main client class for connecting to Cayenne and sending and recFUeiving data.
    
    Standard usage:
    * Set on_message callback, if you are receiving data.
    * Connect to Cayenne using the begin() function.
    * Call loop() at intervals (or loop_forever() once) to perform message processing.
    * Send data to Cayenne using write functions: virtualWrite(), celsiusWrite(), etc.
    * Receive and process data from Cayenne in the on_message callback.

    The on_message callback can be used by creating a function and assigning it to CayenneMQTTClient.on_message member.
    The callback function should have the following signature: on_message(topic, message)
    If it exists this callback is used as the default message handler.
    """<commit_msg>Check if values are set to None to prevent issues with 0 values.<commit_after>import time
from json import loads, decoder
from ssl import PROTOCOL_TLSv1_2
import paho.mqtt.client as mqtt
from myDevices.utils.logger import debug, error, exception, info, logJson, warn

# Topics
DATA_TOPIC = 'data/json'
COMMAND_TOPIC = 'cmd'
COMMAND_JSON_TOPIC = 'cmd.json'
COMMAND_RESPONSE_TOPIC = 'response'

# Data Channels
SYS_HARDWARE_MAKE = 'sys:hw:make'
SYS_HARDWARE_MODEL = 'sys:hw:model'
SYS_OS_NAME = 'sys:os:name'
SYS_OS_VERSION = 'sys:os:version'
SYS_NET = 'sys:net'
SYS_STORAGE = 'sys:storage'
SYS_RAM = 'sys:ram'
SYS_CPU = 'sys:cpu'
SYS_I2C = 'sys:i2c'
SYS_SPI = 'sys:spi'
SYS_UART = 'sys:uart'
SYS_ONEWIRE = 'sys:1wire'
SYS_DEVICETREE = 'sys:devicetree'
SYS_GPIO = 'sys:gpio'
SYS_POWER_RESET = 'sys:pwr:reset'
SYS_POWER_HALT = 'sys:pwr:halt'
AGENT_VERSION = 'agent:version'
AGENT_DEVICES = 'agent:devices'
AGENT_MANAGE = 'agent:manage'
DEV_SENSOR = 'dev'

# Channel Suffixes
IP = 'ip'
SPEEDTEST = 'speedtest'
SSID = 'ssid'
USAGE = 'usage'
CAPACITY = 'capacity'
LOAD = 'load'
TEMPERATURE = 'temp'
VALUE = 'value'
FUNCTION = 'function'


class DataChannel:
    @staticmethod
    def add(data_list, prefix, channel=None, suffix=None, value=None, type=None, unit=None, name=None):
        """Create data channel dict and append it to a list"""
        data_channel = prefix
        if channel is not None:
            data_channel += ':' + str(channel)
        if suffix is not None:
            data_channel += ';' + str(suffix)
        data = {}
        data['channel'] = data_channel
        data['value'] = value
        if type is not None:
            data['type'] = type
        if unit is not None:
            data['unit'] = unit
        if name is not None:
            data['name'] = name
        data_list.append(data)
        

class CayenneMQTTClient:
    """Cayenne MQTT Client class.
    
    This is the main client class for connecting to Cayenne and sending and recFUeiving data.
    
    Standard usage:
    * Set on_message callback, if you are receiving data.
    * Connect to Cayenne using the begin() function.
    * Call loop() at intervals (or loop_forever() once) to perform message processing.
    * Send data to Cayenne using write functions: virtualWrite(), celsiusWrite(), etc.
    * Receive and process data from Cayenne in the on_message callback.

    The on_message callback can be used by creating a function and assigning it to CayenneMQTTClient.on_message member.
    The callback function should have the following signature: on_message(topic, message)
    If it exists this callback is used as the default message handler.
    """
{'fraction_non_alphanumeric': 0.10067114093959731, 'fraction_numerical': 0.002109300095877277, 'mean_word_length': 3.5875109938434475, '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': '5261830', 'n_tokens_mistral': 1731, 'n_tokens_neox': 1707, 'n_words': 531}