Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using uPLibrary.Networking.M2Mqtt;
- using System;
- using System.IO;
- using System.Runtime.InteropServices;
- using System.Text;
- using uPLibrary.Networking.M2Mqtt.Messages;
- using System.Collections.Generic;
- public class mqtt : MonoBehaviour
- {
- //MQTT
- private MqttClient4Unity client;
- public string brokerHostname = "192.168.1.243";
- public int brokerPort = 1883;
- public string userName = null;
- public string password = null;
- public string topic = "#";
- //Data
- public UInt64 actualmin; //Contains the current min
- public struct datas //We store the incoming datas in structs and read them back from the logfiles to datas struct type queue
- {
- public int rcvdID;
- public float q1, q2, q3, q4;
- public UInt64 time;
- }
- public readonly string path = @""; //Log file directory path
- public datas actualdata; //Struct for the incoming data
- public Queue<datas> results = new Queue<datas>(); //We store the results in a Queue
- public uint dispersion; //Dispersion in ms
- // Use this for initialization
- void Start()
- {
- dispersion = 5;
- if (brokerHostname != null && userName != null && password != null)
- {
- try
- {
- Connect();
- client.Subscribe(topic);
- }
- catch (Exception e)
- {
- Debug.Log( e.Message);
- }
- }
- }
- // Update is called once per frame
- void Update()
- {
- while (client.Count() > 0)
- {
- string s = client.Receive();
- Deconstruct(s); // Get id,q1,q2,q3,q4,time from the MQTT message
- actualmin = actualdata.time / 60000;
- WriteToFile(path, actualmin.ToString());
- }
- }
- //MQTT connection
- public void Connect()
- {
- client = new MqttClient4Unity(brokerHostname, brokerPort, false, null);
- string clientId = Guid.NewGuid().ToString();
- client.Connect(clientId, userName, password);
- }
- //Get data from message string
- void Deconstruct(string input)
- {
- char[] delimiterchars = { '"', ',', ':', ' ', '{', '}', '\n' };
- string[] output = input.Split(delimiterchars);
- actualdata.rcvdID = Convert.ToInt16(output[6]);
- actualdata.q1 = (float)Convert.ToDouble(output[12]);
- actualdata.q2 = (float)Convert.ToDouble(output[18]);
- actualdata.q3 = (float)Convert.ToDouble(output[24]);
- actualdata.q4 = (float)Convert.ToDouble(output[30]);
- actualdata.time = (UInt64)Convert.ToUInt64(output[35]);
- }
- //Create a new file or append the existing text file
- void WriteToFile(string path, string filename)
- {
- try
- {
- using (StreamWriter sw = File.AppendText(path + filename + ".txt"))
- {
- sw.WriteLine(actualdata.time + ";" + actualdata.rcvdID.ToString() + ";" + actualdata.q1.ToString() + ";" + actualdata.q2.ToString() + ";" + actualdata.q3.ToString() + ";" + actualdata.q4.ToString());
- sw.Close();
- }
- }
- catch (Exception e)
- {
- Debug.Log("The file could not be write:");
- Debug.Log(e.Message);
- }
- }
- //Finds the appropriate logfile and look up datas with fitting timestamps
- void ReadFromFile(UInt64 targettime, uint dispersion)
- {
- datas hit;
- string line;
- string filename = (targettime / 60000).ToString();
- try
- { using (StreamReader sr = new StreamReader(path + filename + ".txt"))
- {
- while ((line = sr.ReadLine()) != null)
- {
- string[] tokens = line.Split(';');
- hit.time = (UInt64)Convert.ToUInt64(tokens[0]);
- if (hit.time >= (targettime - dispersion) && hit.time <= (targettime + dispersion))
- {
- hit.rcvdID = Convert.ToInt16(tokens[1]);
- hit.q1 = (float)Convert.ToDouble(tokens[2]);
- hit.q2 = (float)Convert.ToDouble(tokens[3]);
- hit.q3 = (float)Convert.ToDouble(tokens[4]);
- hit.q4 = (float)Convert.ToDouble(tokens[5]);
- results.Enqueue(hit);
- }
- }
- }
- }
- catch (Exception e)
- {
- Debug.Log("The file could not be read:");
- Debug.Log(e.Message);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement