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 System.Timers;
- public class box : MonoBehaviour
- {
- private static Timer aTimer = new System.Timers.Timer();
- public int MQTTexist = 1;
- public int rcvdID;
- private int n;
- public float triggerertek;
- public int triggerido;
- DateTime date1;
- DateTime date2;
- 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 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 = 2; //mp amennyi időnként triggereljen
- aTimer.Interval = 1000;
- aTimer.Elapsed += timerElapsed;
- date1 = DateTime.Now;
- }
- private void timerElapsed(System.Object source, System.Timers.ElapsedEventArgs e)
- {
- triggerido = triggerido-1;
- if (triggerido == 0)
- {
- aTimer.Stop();
- }
- }
- 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.Enabled);
- writer.Flush();
- writer.Close();
- triggereles();
- }
- }
- }
- 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))
- {
- date2 = DateTime.Now;
- ScoretextScript.scorecounter = ScoretextScript.scorecounter + 1;
- Forduloido.hossz = Math.Abs((date2 - date1).TotalSeconds);
- triggerido = 2;
- aTimer.Start();
- }
- if (q1 < -2000 && (aTimer.Enabled == false))
- {
- date2 = DateTime.Now;
- ScoretextScript.scorecounter = ScoretextScript.scorecounter + 1;
- Forduloido.hossz = Math.Abs((date2 - date1).TotalSeconds);
- triggerido = 2;
- aTimer.Start();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement