AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
JSONBuilder.cs
Go to the documentation of this file.
1 using System.Collections;
2 using System.Collections.Generic;
3 using UnityEngine;
4 using System.IO;
5 using System.Linq;
6 using UnityEngine.Networking;
7 using ARDesign.Widgets;
8 using ARDesign.Serialize;
9 using ARDesign.Influx;
10 
11 namespace ARDesign
12 {
16  public class JSONBuilder : MonoBehaviour
17  {
18  public static JSONBuilder instance = null;
19 
23  [SerializeField]
24  private GameObject widgetPrefab;
25 
29  [SerializeField]
30  private GameObject success;
34  [SerializeField]
35  private GameObject webError;
39  [SerializeField]
40  private GameObject serverCommunication;
41 
42  #region TEST_FIELDS
43  [SerializeField]
44  private bool test = false;
45  [SerializeField]
46  private GameObject testSceneObj; // something like this is needed ofc, but it should be initialized with Vuforia, not placed in the scene as it is here.
47  [SerializeField]
48  private string fileName = "test.json";
49  #endregion //TEST_FIELDS
50 
51  #region PRIVATE_MEMBER_VARIABLES
52  // List of all present widgets
53  private List<InfluxReader> widgets;
54  private List<InfluxReader> tempWidgets;
55 
56  private bool isRunning = false;
57  #endregion //PRIVATE_MEMBER_VARIABLES
58 
59  #region UNITY_MONOBEHAVIOUR_METHODS
60  // Use this for initialization
61  void Start()
62  {
63  if (instance == null)
64  {
65  instance = this;
66  }
67  else
68  {
69  Destroy(this.gameObject);
70  }
71  tempWidgets = new List<InfluxReader>();
72  widgets = new List<InfluxReader>();
73 
74 
75  if (test) Test();
76 
77  }
78  #endregion //UNITY_MONOBEHAVIOUR_METHODS
79 
80  #region PUBLIC_METHODS
81 
87  public void InitializeScene(string json, GameObject origin)
88  {
89  DBScene scene = Serialize.Utility.CreateFromJSON(json);
90  BuildScene(scene, origin);
91  StartCoroutine(SetUpWidgets());
92  Debug.Log("Finish init");
93  }
94 
100  public void AddWidget<T>(IList<T> toAdd) where T : InfluxReader
101  {
102  if (isRunning)
103  {
104  tempWidgets.AddRange(toAdd.Cast<InfluxReader>());
105  }
106  else
107  {
108  widgets.AddRange(toAdd.Cast<InfluxReader>());
109  }
110 
111  }
112 
118  public void AddWidget<T>(T toAdd) where T : InfluxReader
119  {
120  if (isRunning)
121  {
122  tempWidgets.Add(toAdd);
123  }
124  else
125  {
126  widgets.Add(toAdd);
127  }
128  }
129 
130 
136  public IEnumerator GetText(string id)
137  {
138  // create web request object with HTTP verb GET and URL
139  id = id.Substring(0, 24); // needs to be changed depending on id string length
140  UnityWebRequest www = Receiver.GetFromId(id);
141  Debug.Log("Created Web Request object for: " + www.url);
142 
143  www.downloadHandler = new DownloadHandlerBuffer();
144  serverCommunication.SetActive(true);
145 
146  yield return www.SendWebRequest();
147 
148  Debug.Log("Response Code: " + www.responseCode);
149  if (www.isNetworkError || www.isHttpError)
150  {
151  Debug.Log(www.error);
152  webError.SetActive(true);
153  serverCommunication.SetActive(false);
154  }
155  else
156  {
157  // retrieve results
158  string results = www.downloadHandler.text;
159  Debug.Log("Printing results...");
160  Debug.Log(results);
161  yield return www.downloadHandler.text;
162  success.SetActive(true);
163  serverCommunication.SetActive(false);
164  }
165  }
166 
167 
171  public void UpdateWidgets()
172  {
173  foreach (InfluxReader r in widgets)
174  {
175  if (r.toUpdate)
176  {
177  r.UpdateVals();
178  }
179  }
180  }
181  #endregion //PUBLIC_METHODS
182 
183  #region PRIVATE_METHODS
184  private void BuildScene(DBScene conf, GameObject sceneObj)
191  {
192 
193  InfluxSetup setup = sceneObj.AddComponent<InfluxSetup>();
194  setup.Setup(conf);
195  foreach (DBWidget wid in conf.Widgets)
196  {
197  CreateWidgets(wid, sceneObj);
198  }
199  }
200 
201 
208  private void CreateWidgets(DBWidget wid, GameObject sceneObj)
209  {
210  GameObject newWidget = Instantiate(widgetPrefab);
211  newWidget.transform.parent = sceneObj.transform;
212  newWidget.transform.localPosition = wid.Position;
213  InfluxReader r = newWidget.GetComponent<InfluxReader>();
214  r.SetDBVals(wid.Measure, wid.Building, wid.Room);
215  widgets.Add(r);
216  }
217 
218 
223  private IEnumerator SetUpWidgets()
224  {
225  //Stop thread when all widgets are build - maybe consider wrapping in a while true loop later?
226  while (!AllWidgetsSetup())
227  {
228  isRunning = true;
229  //Set up current widgets
230  foreach (InfluxReader r in widgets)
231  {
232  if (!r.isSetup)
233  {
234  yield return null;
235  r.SetVals();
236  }
237 
238  }
239  isRunning = false;
240  //Check if new widgets need to be added
241  if (tempWidgets.Count != 0)
242  {
243  widgets.AddRange(tempWidgets);
244  tempWidgets.Clear();
245  }
246  yield return null;
247  }
248 
249  }
250 
255  private bool AllWidgetsSetup()
256  {
257  foreach (InfluxReader r in widgets)
258  {
259  if (!r.isSetup)
260  return false;
261  }
262  return true;
263  }
264 
265 
269  private void Test()
270  {
271  string filePath = Path.Combine(Application.streamingAssetsPath, fileName);
272 
273  if (File.Exists(filePath))
274  {
275 
276  InitializeScene(File.ReadAllText(filePath), testSceneObj);
277  }
278  }
279  #endregion //PRIVATE_METHODS
280  }
281 }
282 
283 
284 
Singleton object for handling scene loading and widget creation
Definition: JSONBuilder.cs:16
void UpdateVals()
Update the values in the widget, by querying the database.
Definition: InfluxReader.cs:92
void InitializeScene(string json, GameObject origin)
Deserializes a scene configuration and builds it on the origin location
Definition: JSONBuilder.cs:87
void Setup(DBScene sceneToBuild)
Given a deserialized scene configuration, set all Influx server values from config ...
Definition: InfluxSetup.cs:27
void AddWidget< T >(IList< T > toAdd)
Adds widgets to list of active widgets
Definition: JSONBuilder.cs:100
IEnumerator GetText(string id)
Starts get request to fetch config for a given ID
Definition: JSONBuilder.cs:136
void SetDBVals(string m, string b, string r)
Sets the base database values - should be called before anything else!
Definition: InfluxReader.cs:78
static JSONBuilder instance
Definition: JSONBuilder.cs:18
Abstract class for querying Influx data to widgets. Includes implemented methods for building useful ...
Definition: InfluxReader.cs:50
void UpdateWidgets()
Updates all widgets that are set to update
Definition: JSONBuilder.cs:171
void SetVals()
Set the values in the widget, by querying the database. Heavy overhead, depending on data type ...
Object for storing scene configuration settings - should be parent to widgets
Definition: InfluxSetup.cs:11