<commit_before>import {NativeModules, Platform} from 'react-native';
import RCTDeviceEventEmitter from 'RCTDeviceEventEmitter';
import store from '../store/ConfigureStore';
import * as MessageActions from '../actions/MessageActions';
import React, { AsyncStorage } from 'react-native';

class MQTTClient{

    constructor(){
        this.mqttClient = NativeModules.RNMQTTClient;
    }

    async init(){
        let phoneNumber = await AsyncStorage.getItem("phoneNumber");
        if(!phoneNumber){
            phoneNumber = '3392247442';
        }
        if(phoneNumber){
            console.log("got phone number from async storage as "+ phoneNumber);
            this.connect(phoneNumber);
        }
        RCTDeviceEventEmitter.addListener('onMessageReceived', this.onMessageReceived);
        //RCTDeviceEventEmitter.addListener('onStatusChanged', this.onStatusChanged);
        RCTDeviceEventEmitter.addListener('onMQTTConnected', this.onConnectionInitialized.bind(this));
    }

    connect(phoneNumber){
        var connectionDetails = {
            host: 'broker.mqttdashboard.com',
            port: 1883,
            tls : false,
            clientId: phoneNumber
        }

        if(!this.mqttClient){
            return;
        }
        console.log('platform is '+Platform);
        //this.mqttClient.connect(connectionDetails);
        this.mqttClient.connect(connectionDetails, 'MQTTChatReceive', 1);
    }

    onConnectionInitialized(){
        console.log("connection initialized invoked");
        this.subscribeTo('MQTTChatReceive', 1);
    }

    publish(topicName, message, qosLevel = 0, retain = false){
        if(!topicName){
            throw("topic name is required to publish message");
        }
        if(!message || !message.text){
            throw("message is required to publish message");
        }
        this.mqttClient.publish(topicName, message.text, qosLevel, retain);
    }

    subscribeTo(topicName, qosLevel){
        this.mqttClient.subscribeTo(topicName, qosLevel);
    }

    onStatusChanged(newStatus){
        console.log("new status is "+newStatus);
    }

    onMessageReceived(message){
        store.dispatch(MessageActions.addMessage(message));
        console.log("received message in UI "+ message);
    }<commit_msg>added platform check sample<commit_after>import {NativeModules} from 'react-native';
import RCTDeviceEventEmitter from 'RCTDeviceEventEmitter';
import store from '../store/ConfigureStore';
import * as MessageActions from '../actions/MessageActions';
import React, { AsyncStorage } from 'react-native';
var {Platform} = React;

class MQTTClient{

    constructor(){
        this.mqttClient = NativeModules.RNMQTTClient;
    }

    async init(){
        let phoneNumber = await AsyncStorage.getItem("phoneNumber");
        if(!phoneNumber){
            phoneNumber = '3392247442';
        }
        if(phoneNumber){
            console.log("got phone number from async storage as "+ phoneNumber);
            this.connect(phoneNumber);
        }
        RCTDeviceEventEmitter.addListener('onMessageReceived', this.onMessageReceived);
        //RCTDeviceEventEmitter.addListener('onStatusChanged', this.onStatusChanged);
        RCTDeviceEventEmitter.addListener('onMQTTConnected', this.onConnectionInitialized.bind(this));
    }

    connect(phoneNumber){
        var connectionDetails = {
            host: 'broker.mqttdashboard.com',
            port: 1883,
            tls : false,
            clientId: phoneNumber
        }

        if(!this.mqttClient){
            return;
        }
        console.log('platform is '+Platform.OS);
        //this.mqttClient.connect(connectionDetails);
        this.mqttClient.connect(connectionDetails, 'MQTTChatReceive', 1);
    }

    onConnectionInitialized(){
        console.log("connection initialized invoked");
        this.subscribeTo('MQTTChatReceive', 1);
    }

    publish(topicName, message, qosLevel = 0, retain = false){
        if(!topicName){
            throw("topic name is required to publish message");
        }
        if(!message || !message.text){
            throw("message is required to publish message");
        }
        this.mqttClient.publish(topicName, message.text, qosLevel, retain);
    }

    subscribeTo(topicName, qosLevel){
        this.mqttClient.subscribeTo(topicName, qosLevel);
    }

    onStatusChanged(newStatus){
        console.log("new status is "+newStatus);
    }

    onMessageReceived(message){
        store.dispatch(MessageActions.addMessage(message));
        console.log("received message in UI "+ message);
    }
{'fraction_non_alphanumeric': 0.11190942738950577, 'fraction_numerical': 0.007402569126932288, 'mean_word_length': 2.7779605263157894, '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': '6408454', 'n_tokens_mistral': 1260, 'n_tokens_neox': 1218, 'n_words': 247}