Advertisement
Guest User

Untitled

a guest
May 14th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.64 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using uPLibrary.Networking.M2Mqtt;
  6. using System;
  7. using System.Runtime.InteropServices;
  8. using System.Text;
  9. using uPLibrary.Networking.M2Mqtt.Messages;
  10. using System.IO;
  11.  
  12. using System.Timers;
  13.  
  14.  
  15.  
  16. public class box : MonoBehaviour
  17. {
  18.     private static Timer aTimer = new System.Timers.Timer();
  19.     public int MQTTexist = 1;
  20.     public int rcvdID;
  21.     private int n;
  22.     public float triggerertek;
  23.     public int triggerido;
  24.    DateTime date1;
  25.     DateTime date2;
  26.  
  27.    
  28.  
  29.  
  30.     public float q1;
  31.     public float q2;
  32.     public float q3;
  33.     public float q4;
  34.  
  35.     public float acc1;
  36.     public float acc2;
  37.     public float acc3;
  38.  
  39.     public string brokerHostname = "192.168.43.1"; // !!!!!!!!!!!!!!!!
  40.     public int brokerPort = 1883;
  41.     public string userName = null;
  42.     public string password = null;
  43.     public string topic = "#";
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.     public MqttClient4Unity client;
  51.  
  52.     void Start() // Use this for initialization
  53.     {
  54.         if (MQTTexist == 0)
  55.         {
  56.             return;
  57.         }
  58.         Connect(); //connect to mqtt broker
  59.         client.Subscribe("sensors/q"); // subscribe to mqtt topics !!!!!!!!!!!!!
  60.         triggerido = 2; //mp amennyi idÅ‘nként triggereljen
  61.         aTimer.Interval = 1000;
  62.         aTimer.Elapsed += timerElapsed;
  63.         date1 = DateTime.Now;
  64.  
  65.     }
  66.  
  67.     private void timerElapsed(System.Object source, System.Timers.ElapsedEventArgs e)
  68.     {
  69.         triggerido = triggerido-1;
  70.         if (triggerido == 0)
  71.         {
  72.             aTimer.Stop();
  73.         }
  74.     }
  75.  
  76.     void Update() // Update is called once per frame
  77.     {
  78.         if (MQTTexist == 0)
  79.         {
  80.             return;
  81.         }
  82.         {
  83.             while (client.Count() > 0)
  84.             {
  85.                 string s = client.Receive();
  86.  
  87.                 Deconstruct(s);
  88.  
  89.                 if (rcvdID == 23501) // ID of ball sensor 2 !!!!!!!!!!!!!!!!!!
  90.                 {
  91.                     transform.rotation = new Quaternion(-q3, q4, q2, -q1);
  92.                 }
  93.  
  94.                 string path = "Assets\\test.txt";
  95.                 StreamWriter writer = new StreamWriter(path, true);
  96.                 writer.WriteLine(s);
  97.                 writer.WriteLine(DateTime.Now);
  98.                 writer.WriteLine(DateTime.Now.ToString("hh:mm:ss:ffffff"));
  99.                 writer.WriteLine(ScoretextScript.scorecounter);
  100.                
  101.  
  102.                 writer.WriteLine(aTimer.Enabled);
  103.                 writer.Flush();
  104.                 writer.Close();
  105.  
  106.                 triggereles();
  107.                
  108.             }
  109.         }
  110.  
  111.     }
  112.  
  113.     public void Deconstruct(string input)
  114.     {
  115.         char[] delimiterchars = { '"', ',', ':', ' ', '{', '}', '\n' };
  116.         string[] output = input.Split(delimiterchars);
  117.         // locations: yaw -> 7, pitch -> 13, roll -> 19
  118.  
  119.         rcvdID = Convert.ToUInt16(output[6]);
  120.  
  121.         q1 = (float)Convert.ToDouble(output[12]);
  122.         q2 = (float)Convert.ToDouble(output[18]);
  123.         q3 = (float)Convert.ToDouble(output[24]);
  124.         q4 = (float)Convert.ToDouble(output[30]);
  125.  
  126.         acc1 = (float)Convert.ToDouble(output[36]);
  127.         acc2 = (float)Convert.ToDouble(output[42]);
  128.         acc3 = (float)Convert.ToDouble(output[48]);
  129.     }
  130.  
  131.     public void Connect()
  132.     {
  133.         try
  134.         {
  135.             client = new MqttClient4Unity(brokerHostname, brokerPort, false, null);
  136.             string clientId = "UnityClient_pc" + UnityEngine.Random.Range(1, 10000); /*Guid.NewGuid().ToString();*/
  137.             client.Connect(clientId, userName, password);
  138.         }
  139.         catch (Exception ex)
  140.         {
  141.             Debug.Log("failed to connect" + ex);
  142.         }
  143.     }
  144.  
  145.     public void Publish(string _topic, string msg)
  146.     {
  147.  
  148.         client.Publish(
  149.             _topic, Encoding.UTF8.GetBytes(msg),
  150.             MqttMsgBase.QOS_LEVEL_AT_MOST_ONCE, false);
  151.  
  152.     }
  153.  
  154.     void triggereles()
  155.     {
  156.  
  157.         if (q1 > 14000 && (aTimer.Enabled == false))
  158.         {
  159.             date2 = DateTime.Now;
  160.             ScoretextScript.scorecounter = ScoretextScript.scorecounter + 1;
  161.             Forduloido.hossz = Math.Abs((date2 - date1).TotalSeconds);
  162.             triggerido = 2;
  163.             aTimer.Start();
  164.            
  165.         }
  166.  
  167.  
  168.         if (q1 < -2000 && (aTimer.Enabled == false))
  169.         {
  170.             date2 = DateTime.Now;
  171.             ScoretextScript.scorecounter = ScoretextScript.scorecounter + 1;
  172.             Forduloido.hossz = Math.Abs((date2 - date1).TotalSeconds);
  173.             triggerido = 2;
  174.             aTimer.Start();
  175.            
  176.      
  177.         }
  178.  
  179.     }
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement