Advertisement
Guest User

Untitled

a guest
May 13th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.18 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. using UnityEditor;
  12. using System.Timers;
  13.  
  14.  
  15.  
  16. public class box : MonoBehaviour
  17. {
  18.     private static System.Timers.Timer aTimer;
  19.     public int MQTTexist = 1;
  20.     public int rcvdID;
  21.     private int n = 0;
  22.     public float triggerertek;
  23.     DateTime triggerido;
  24.     //Timer timer = new Timer(3000);
  25.  
  26.  
  27.     // DateTime a = new DateTime.Now;
  28.     //DateTime b = new DateTime(2010, 05, 12, 13, 45, 00);
  29.  
  30.  
  31.     public float q1;
  32.     public float q2;
  33.     public float q3;
  34.     public float q4;
  35.  
  36.     public float acc1;
  37.     public float acc2;
  38.     public float acc3;
  39.  
  40.     public string brokerHostname = "192.168.43.1"; // !!!!!!!!!!!!!!!!
  41.     public int brokerPort = 1883;
  42.     public string userName = null;
  43.     public string password = null;
  44.     public string topic = "#";
  45.  
  46.  
  47.  
  48.     /* public GUIText scoreText;
  49.      public int score;
  50.      */
  51.  
  52.  
  53.     public MqttClient4Unity client;
  54.  
  55.     void Start() // Use this for initialization
  56.     {
  57.         if (MQTTexist == 0)
  58.         {
  59.             return;
  60.         }
  61.         Connect(); //connect to mqtt broker
  62.         client.Subscribe("sensors/q"); // subscribe to mqtt topics !!!!!!!!!!!!!
  63.         triggerido = DateTime.Now;
  64.         // aTimer.Interval = 3000;
  65.         aTimer = new System.Timers.Timer(1);
  66.  
  67.  
  68.  
  69.     }
  70.  
  71.     void Update() // Update is called once per frame
  72.     {
  73.         if (MQTTexist == 0)
  74.         {
  75.             return;
  76.         }
  77.         {
  78.             while (client.Count() > 0)
  79.             {
  80.                 string s = client.Receive();
  81.  
  82.                 Deconstruct(s);
  83.  
  84.                 if (rcvdID == 23501) // ID of ball sensor 2 !!!!!!!!!!!!!!!!!!
  85.                 {
  86.                     transform.rotation = new Quaternion(-q3, q4, q2, -q1);
  87.                 }
  88.  
  89.                 string path = "Assets\\test.txt";
  90.                 StreamWriter writer = new StreamWriter(path, true);
  91.                 writer.WriteLine(s);
  92.                 writer.WriteLine(DateTime.Now);
  93.                 writer.WriteLine(DateTime.Now.ToString("hh:mm:ss:ffffff"));
  94.                 writer.WriteLine(ScoretextScript.scorecounter);
  95.                 writer.WriteLine(aTimer.Interval);
  96.                 writer.WriteLine(aTimer.Enabled);
  97.                 writer.Flush();
  98.                 writer.Close();
  99.  
  100.                 triggereles();
  101.                 //countdown();
  102.  
  103.             }
  104.         }
  105.  
  106.     }
  107.  
  108.     private static void SetTimer()
  109.     {
  110.         // Create a timer with a two second interval.
  111.         aTimer = new System.Timers.Timer(2000);
  112.         // Hook up the Elapsed event for the timer.
  113.         aTimer.Start();
  114.         aTimer.AutoReset = false;
  115.       /*  aTimer.Enabled = true;
  116.         if (aTimer == null)
  117.         {
  118.             aTimer.Enabled = false;
  119.  
  120.         }*/
  121.     }
  122.  
  123.  
  124.  
  125.     public void Deconstruct(string input)
  126.     {
  127.         char[] delimiterchars = { '"', ',', ':', ' ', '{', '}', '\n' };
  128.         string[] output = input.Split(delimiterchars);
  129.         // locations: yaw -> 7, pitch -> 13, roll -> 19
  130.  
  131.         rcvdID = Convert.ToUInt16(output[6]);
  132.  
  133.         q1 = (float)Convert.ToDouble(output[12]);
  134.         q2 = (float)Convert.ToDouble(output[18]);
  135.         q3 = (float)Convert.ToDouble(output[24]);
  136.         q4 = (float)Convert.ToDouble(output[30]);
  137.  
  138.         acc1 = (float)Convert.ToDouble(output[36]);
  139.         acc2 = (float)Convert.ToDouble(output[42]);
  140.         acc3 = (float)Convert.ToDouble(output[48]);
  141.     }
  142.  
  143.     public void Connect()
  144.     {
  145.         try
  146.         {
  147.             client = new MqttClient4Unity(brokerHostname, brokerPort, false, null);
  148.             string clientId = "UnityClient_pc" + UnityEngine.Random.Range(1, 10000); /*Guid.NewGuid().ToString();*/
  149.             client.Connect(clientId, userName, password);
  150.         }
  151.         catch (Exception ex)
  152.         {
  153.             Debug.Log("failed to connect" + ex);
  154.         }
  155.     }
  156.  
  157.     public void Publish(string _topic, string msg)
  158.     {
  159.  
  160.         client.Publish(
  161.             _topic, Encoding.UTF8.GetBytes(msg),
  162.             MqttMsgBase.QOS_LEVEL_AT_MOST_ONCE, false);
  163.  
  164.     }
  165.  
  166.     void triggereles()
  167.     {
  168.  
  169.         if (q1 > 14000 && (aTimer.Enabled == false))
  170.         {
  171.  
  172.             //n++;
  173.  
  174.             // if (n == 80)
  175.             // {
  176.  
  177.             ScoretextScript.scorecounter = ScoretextScript.scorecounter + 1;
  178.             SetTimer();
  179.            
  180.             //   n = 0;
  181.  
  182.             //}
  183.  
  184.         }
  185.        
  186.        
  187.         if (q1 < -2000 && (aTimer.Enabled==false))
  188.         {
  189.  
  190.             //  n++;
  191.             // if (n == 40)
  192.             //{
  193.  
  194.             ScoretextScript.scorecounter = ScoretextScript.scorecounter + 1;
  195.             SetTimer();
  196.             //  n = 0;
  197.  
  198.             //}
  199.  
  200.         }
  201.  
  202.     }
  203.  
  204.     /*void countdown()
  205.     {
  206.         timer--;
  207.     }
  208. */
  209.  
  210.  
  211.  
  212.  
  213.  
  214.  
  215.     /* void UpdateText()
  216.       {
  217.           scoreText.text = "Score:" + score;
  218.       }*/
  219.  
  220.     /*   void necsinalj_semmit()
  221.        {
  222.            if(DateTime.Now
  223.  
  224.        }*/
  225. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement