Advertisement
Guest User

logread

a guest
Dec 6th, 2016
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.21 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. using uPLibrary.Networking.M2Mqtt;
  5. using System;
  6. using System.IO;
  7. using System.Runtime.InteropServices;
  8. using System.Text;
  9. using uPLibrary.Networking.M2Mqtt.Messages;
  10. using System.Collections.Generic;
  11.  
  12.  
  13. public class mqtt : MonoBehaviour
  14. {
  15.  
  16.     //MQTT
  17.     private MqttClient4Unity client;
  18.     public string brokerHostname = "192.168.1.243";
  19.     public int brokerPort = 1883;
  20.     public string userName = null;
  21.     public string password = null;
  22.     public string topic = "#";
  23.    
  24.  
  25.     //Data
  26.        
  27.     public UInt64 actualmin;                                    //Contains the current min
  28.  
  29.     public struct datas                                         //We store the incoming datas in structs and read them back from the logfiles to datas struct type queue
  30.     {
  31.         public int rcvdID;
  32.         public float q1, q2, q3, q4;
  33.         public UInt64 time;
  34.     }
  35.  
  36.     public readonly string path = @"";                          //Log file directory path
  37.  
  38.     public datas actualdata;                                    //Struct for the incoming data
  39.  
  40.     public Queue<datas> results = new Queue<datas>();           //We store the results in a Queue
  41.  
  42.     public uint dispersion;                                     //Dispersion in ms
  43.  
  44.     // Use this for initialization
  45.     void Start()
  46.     {
  47.  
  48.         dispersion = 5;
  49.        
  50.         if (brokerHostname != null && userName != null && password != null)
  51.         {
  52.             try
  53.             {
  54.                 Connect();
  55.                 client.Subscribe(topic);
  56.             }
  57.  
  58.             catch (Exception e)
  59.             {
  60.                 Debug.Log( e.Message);
  61.             }
  62.         }
  63.  
  64.  
  65.  
  66.      
  67.  
  68.  
  69.     }
  70.  
  71.     // Update is called once per frame
  72.     void Update()
  73.     {
  74.        
  75.         while (client.Count() > 0)
  76.         {
  77.            
  78.                 string s = client.Receive();
  79.                    
  80.                 Deconstruct(s);                                      // Get id,q1,q2,q3,q4,time from the MQTT message          
  81.                                
  82.                 actualmin = actualdata.time / 60000;
  83.                 WriteToFile(path, actualmin.ToString());
  84.  
  85.  
  86.         }
  87.        
  88.        
  89.     }
  90.  
  91.    
  92.     //MQTT connection
  93.     public void Connect()
  94.     {
  95.         client = new MqttClient4Unity(brokerHostname, brokerPort, false, null);
  96.         string clientId = Guid.NewGuid().ToString();
  97.         client.Connect(clientId, userName, password);
  98.     }
  99.  
  100.     //Get data from message string
  101.     void Deconstruct(string input)
  102.     {
  103.         char[] delimiterchars = { '"', ',', ':', ' ', '{', '}', '\n' };
  104.         string[] output = input.Split(delimiterchars);
  105.  
  106.         actualdata.rcvdID = Convert.ToInt16(output[6]);
  107.         actualdata.q1 = (float)Convert.ToDouble(output[12]);
  108.         actualdata.q2 = (float)Convert.ToDouble(output[18]);
  109.         actualdata.q3 = (float)Convert.ToDouble(output[24]);
  110.         actualdata.q4 = (float)Convert.ToDouble(output[30]);
  111.         actualdata.time = (UInt64)Convert.ToUInt64(output[35]);
  112.        
  113.     }
  114.  
  115.     //Create a new file or append the existing text file
  116.     void WriteToFile(string path, string filename)
  117.  
  118.     {
  119.         try
  120.         {
  121.             using (StreamWriter sw = File.AppendText(path + filename + ".txt"))
  122.             {
  123.                 sw.WriteLine(actualdata.time + ";" + actualdata.rcvdID.ToString() + ";" + actualdata.q1.ToString() + ";" + actualdata.q2.ToString() + ";" + actualdata.q3.ToString() + ";" + actualdata.q4.ToString());
  124.                 sw.Close();
  125.             }
  126.  
  127.         }
  128.         catch (Exception e)
  129.         {
  130.  
  131.             Debug.Log("The file could not be write:");
  132.             Debug.Log(e.Message);
  133.         }
  134.     }
  135.  
  136.     //Finds the appropriate logfile and look up datas with fitting timestamps
  137.     void ReadFromFile(UInt64 targettime, uint dispersion)
  138.     {
  139.  
  140.         datas hit;
  141.         string line;
  142.         string filename = (targettime / 60000).ToString();
  143.        
  144.        
  145.         try
  146.         { using (StreamReader sr = new StreamReader(path + filename + ".txt"))
  147.             {
  148.                while ((line = sr.ReadLine()) != null)
  149.                 {
  150.                     string[] tokens = line.Split(';');
  151.  
  152.                     hit.time = (UInt64)Convert.ToUInt64(tokens[0]);
  153.  
  154.                     if (hit.time >= (targettime - dispersion) && hit.time <= (targettime + dispersion))
  155.                     {
  156.                                                                        
  157.                         hit.rcvdID = Convert.ToInt16(tokens[1]);
  158.                         hit.q1 = (float)Convert.ToDouble(tokens[2]);
  159.                         hit.q2 = (float)Convert.ToDouble(tokens[3]);
  160.                         hit.q3 = (float)Convert.ToDouble(tokens[4]);
  161.                         hit.q4 = (float)Convert.ToDouble(tokens[5]);
  162.  
  163.                         results.Enqueue(hit);                      
  164.                     }
  165.                    
  166.  
  167.                 }
  168.  
  169.    
  170.             }
  171.         }
  172.         catch (Exception e)
  173.         {
  174.  
  175.             Debug.Log("The file could not be read:");
  176.             Debug.Log(e.Message);
  177.         }
  178.  
  179.            
  180.        
  181.  
  182.  
  183.     }
  184.  
  185.  
  186.        
  187.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement