Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- using uPLibrary.Networking.M2Mqtt;
- using System;
- using System.Runtime.InteropServices;
- using System.Text;
- using uPLibrary.Networking.M2Mqtt.Messages;
- using System.IO;
- using UnityEditor;
- using System.Timers;
- public class box : MonoBehaviour
- {
- private static System.Timers.Timer aTimer;
- public int MQTTexist = 1;
- public int rcvdID;
- private int n = 0;
- public float triggerertek;
- DateTime triggerido;
- //Timer timer = new Timer(3000);
- // DateTime a = new DateTime.Now;
- //DateTime b = new DateTime(2010, 05, 12, 13, 45, 00);
- public float q1;
- public float q2;
- public float q3;
- public float q4;
- public float acc1;
- public float acc2;
- public float acc3;
- public string brokerHostname = "192.168.43.1"; // !!!!!!!!!!!!!!!!
- public int brokerPort = 1883;
- public string userName = null;
- public string password = null;
- public string topic = "#";
- /* public GUIText scoreText;
- public int score;
- */
- public MqttClient4Unity client;
- void Start() // Use this for initialization
- {
- if (MQTTexist == 0)
- {
- return;
- }
- Connect(); //connect to mqtt broker
- client.Subscribe("sensors/q"); // subscribe to mqtt topics !!!!!!!!!!!!!
- triggerido = DateTime.Now;
- // aTimer.Interval = 3000;
- aTimer = new System.Timers.Timer(1);
- }
- void Update() // Update is called once per frame
- {
- if (MQTTexist == 0)
- {
- return;
- }
- {
- while (client.Count() > 0)
- {
- string s = client.Receive();
- Deconstruct(s);
- if (rcvdID == 23501) // ID of ball sensor 2 !!!!!!!!!!!!!!!!!!
- {
- transform.rotation = new Quaternion(-q3, q4, q2, -q1);
- }
- string path = "Assets\\test.txt";
- StreamWriter writer = new StreamWriter(path, true);
- writer.WriteLine(s);
- writer.WriteLine(DateTime.Now);
- writer.WriteLine(DateTime.Now.ToString("hh:mm:ss:ffffff"));
- writer.WriteLine(ScoretextScript.scorecounter);
- writer.WriteLine(aTimer.Interval);
- writer.WriteLine(aTimer.Enabled);
- writer.Flush();
- writer.Close();
- triggereles();
- //countdown();
- }
- }
- }
- private static void SetTimer()
- {
- // Create a timer with a two second interval.
- aTimer = new System.Timers.Timer(2000);
- // Hook up the Elapsed event for the timer.
- aTimer.Start();
- aTimer.AutoReset = false;
- /* aTimer.Enabled = true;
- if (aTimer == null)
- {
- aTimer.Enabled = false;
- }*/
- }
- public void Deconstruct(string input)
- {
- char[] delimiterchars = { '"', ',', ':', ' ', '{', '}', '\n' };
- string[] output = input.Split(delimiterchars);
- // locations: yaw -> 7, pitch -> 13, roll -> 19
- rcvdID = Convert.ToUInt16(output[6]);
- q1 = (float)Convert.ToDouble(output[12]);
- q2 = (float)Convert.ToDouble(output[18]);
- q3 = (float)Convert.ToDouble(output[24]);
- q4 = (float)Convert.ToDouble(output[30]);
- acc1 = (float)Convert.ToDouble(output[36]);
- acc2 = (float)Convert.ToDouble(output[42]);
- acc3 = (float)Convert.ToDouble(output[48]);
- }
- public void Connect()
- {
- try
- {
- client = new MqttClient4Unity(brokerHostname, brokerPort, false, null);
- string clientId = "UnityClient_pc" + UnityEngine.Random.Range(1, 10000); /*Guid.NewGuid().ToString();*/
- client.Connect(clientId, userName, password);
- }
- catch (Exception ex)
- {
- Debug.Log("failed to connect" + ex);
- }
- }
- public void Publish(string _topic, string msg)
- {
- client.Publish(
- _topic, Encoding.UTF8.GetBytes(msg),
- MqttMsgBase.QOS_LEVEL_AT_MOST_ONCE, false);
- }
- void triggereles()
- {
- if (q1 > 14000 && (aTimer.Enabled == false))
- {
- //n++;
- // if (n == 80)
- // {
- ScoretextScript.scorecounter = ScoretextScript.scorecounter + 1;
- SetTimer();
- // n = 0;
- //}
- }
- if (q1 < -2000 && (aTimer.Enabled==false))
- {
- // n++;
- // if (n == 40)
- //{
- ScoretextScript.scorecounter = ScoretextScript.scorecounter + 1;
- SetTimer();
- // n = 0;
- //}
- }
- }
- /*void countdown()
- {
- timer--;
- }
- */
- /* void UpdateText()
- {
- scoreText.text = "Score:" + score;
- }*/
- /* void necsinalj_semmit()
- {
- if(DateTime.Now
- }*/
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement