AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
InfluxReader.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections;
3 using System.Collections.Generic;
4 using UnityEngine;
5 using System.Linq;
6 using ARDesign.Serialize;
7 using ARDesign.Influx;
8 
9 namespace ARDesign
10 {
11  namespace Widgets
12  {
18  public abstract class InfluxType<T> : InfluxReader
19  {
20  protected T dataVals;
21 
22  protected abstract void CastWidget();
23 
24  #region UNITY_MONOBEHAVIOUR_METHODS
25  // Use this for initialization
26  void Awake()
27  {
28  widget = this.GetComponent<WidgetHandler>();
29  //Debug.Log("Start called on " + widget.name);
30  CastWidget();
31  }
32  #endregion //UNITY_MONOBEHAVIOUR_METHODS
33 
34  #region PUBLIC_METHODS
35  public T GetData()
40  {
41  return dataVals;
42  }
43  #endregion //PUBLIC_METHODS
44  }
45 
50  public abstract class InfluxReader : MonoBehaviour
51  {
52  //determines if widget needs to be updated or is static
53  public bool toUpdate = false;
54  [NonSerialized]
55  public bool isDataBuilt = false;
56  [NonSerialized]
57  public bool isSetup = false;
58 
59  #region PRIVATE_MEMBER_VARIABLES
60  protected string measure;
61  protected string building;
62  protected string room;
63  private InfluxSetup parent = null;
64 
65  private string urlToQuery;
66  private string urlToUpdate;
67 
68  protected WidgetHandler widget;
69  #endregion //PRIVATE_MEMBER_VARIABLES
70 
71  #region PUBLIC_METHODS
72  public void SetDBVals(string m, string b, string r)
79  {
80  parent = gameObject.GetComponentInParent<InfluxSetup>();
81  measure = m;
82  building = b;
83  room = r;
84 
85  urlToQuery = SetQueryUrl();
86  urlToUpdate = SetQueryUrl();
87  }
88 
92  public void UpdateVals()
93  {
94  StartCoroutine(RefreshCurVals());
95  }
96 
101  public void SetVals()
102  {
103  StartCoroutine(BuildValues());
104  isSetup = true;
105  }
106  #endregion //PUBLIC_METHODS
107 
108  #region ABSTRACT_METHODS
109  protected abstract string SetQueryUrl();
115 
121  protected abstract string SetUpdateUrl();
122 
127  protected abstract void ParseSetUpText(string webReturn);
128 
133  protected abstract void ParseUpdateText(string webReturn);
134 
135  #endregion //ABSTRACT_METHODS
136 
137  #region PRIVATE_METHODS
138  private IEnumerator RefreshCurVals()
143  {
144  WWW web = new WWW(urlToUpdate);
145  yield return web;
146 
147  if (web.error != null)
148  {
149  Debug.Log(web.error);
150  }
151  else
152  {
153  ParseUpdateText(web.text);
154  }
155 
156  }
157 
162  private IEnumerator BuildValues()
163  {
164  WWW web = new WWW(urlToQuery);
165  yield return web;
166  if (web.error != null)
167  {
168  Debug.Log(web.error);
169  }
170  else
171  {
172  ParseSetUpText(web.text);
173  }
174  }
175  #endregion //PRIVATE_METHODS
176 
177  #region QUERY_HELPER_FUNCTIONS
178 
184  protected string BuildUrl(string query)
185  {
186  return parent.BuildUrlWithQuery(query);
187  }
188 
194  public string BuildUrlWithLimit(int limit)
195  {
196  return BuildUrl("SELECT type,value FROM " + measure + " WHERE \"building\"=\'" + building + "\' AND \"room\"=\'" + room + "\' ORDER BY DESC LIMIT " + limit);
197 
198  }
199 
200 
205  public string BuildUrlWithoutLimit()
206  {
207 
208  return BuildUrl("SELECT type,value FROM " + measure + " WHERE \"building\"=\'" + building + "\' AND \"room\"=\'" + room + "\' ORDER BY DESC");
209 
210  }
211 
218  public string BuildUrlWithLimitSetType(string type, int limit)
219  {
220  return BuildUrl("SELECT value FROM " + measure + " WHERE \"building\"=\'" + building + "\' AND \"room\"=\'" + room + "\' AND \"type\"=\'" + type + "\' ORDER BY DESC LIMIT " + limit);
221 
222  }
223 
229  public string BuildUrlWithoutLimitSetType(string type)
230  {
231 
232  return BuildUrl("SELECT value FROM " + measure + " WHERE \"building\"=\'" + building + "\' AND \"room\"=\'" + room + "\' AND \"type\"=\'" + type + "\' ORDER BY DESC");
233 
234  }
235 
242  public string BuildUrlTagList(string keyName)
243  {
244  return BuildUrl("SHOW TAG VALUES FROM " + measure + " WITH KEY = \"" + keyName + "\"");
245  }
246  #endregion //QUERY_HELPER_FUNCTIONS
247  }
248  }
249 }
250 
string BuildUrlWithoutLimitSetType(string type)
Builds a https Influx query to return all values of a given type
string BuildUrlWithLimit(int limit)
Builds a https Influx query to return a fixed limit of values
void UpdateVals()
Update the values in the widget, by querying the database.
Definition: InfluxReader.cs:92
abstract void ParseUpdateText(string webReturn)
Call back function for updating the widget
T GetData()
Returns the data for the widget
Definition: InfluxReader.cs:39
This ensures type agnostic functions can be run on InfluxReaders - All widgets should inherit from TH...
Definition: InfluxReader.cs:18
string BuildUrlTagList(string keyName)
Builds a https Influx query to return all tags for a given key Use for building list of types in a me...
void SetDBVals(string m, string b, string r)
Sets the base database values - should be called before anything else!
Definition: InfluxReader.cs:78
abstract void ParseSetUpText(string webReturn)
Call back function for building the widget
abstract void CastWidget()
abstract string SetQueryUrl()
Sets the URL and query for setting up the widget Called automatically on widget creation ...
Abstract class for querying Influx data to widgets. Includes implemented methods for building useful ...
Definition: InfluxReader.cs:50
Refers to general data-agnostic widget functionality
string BuildUrlWithLimitSetType(string type, int limit)
Builds a https Influx query to return a fixed limit of values of a given type
abstract string SetUpdateUrl()
Sets the URL and query for updating up the widget Called automatically on widget creation ...
string BuildUrlWithoutLimit()
Builds a https Influx query to return all values
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
string BuildUrl(string query)
Builds a https Influx query for the given string
string BuildUrlWithQuery(string query)
Encodes a given plain text query into a InfluxDB https query
Definition: InfluxSetup.cs:53