Exemple #1
0
        /// <summary>
        /// Send a value and a context to Ubidots API and save it.
        /// </summary>
        /// <param name="Value">The value to be saved</param>
        /// <param name="Context">The context to be saved</param>
        public IEnumerator SaveValue(int Value, ServerBridge.contextData Context, System.Action <Value> values)
        {
            var outputMessage = "";

            ServerBridge.JsonData Data = new ServerBridge.JsonData()
            {
                value = Value, timestamp = GetTimestamp(), context = Context
            };
            //Dictionary<string, object> Data = new Dictionary<string, object>();
            //Data.Add("value", Value);
            //Data.Add("context", Context);
            //Data.Add("timestamp", GetTimestamp());

            string Json = JsonUtility.ToJson(Data);

            yield return(Extender.Instance.StartCoroutine(Bridge.Post("variables/" + GetId() + "/values", Json, result => outputMessage = result)));

            ServerBridge.JsonData RawValues = JsonUtility.FromJson <ServerBridge.JsonData>(outputMessage);

            Value Values = new Value(new ServerBridge.JsonData()
            {
                value = RawValues.value, timestamp = RawValues.timestamp, created_at = RawValues.created_at
            }, Api);

            values(Values);
        }
Exemple #2
0
        /// <summary>
        /// Get all the values of the variable.
        /// </summary>
        /// <returns>The list of all the values of the variable</returns>
        public IEnumerator GetValues(System.Action <Value[]> values, float size = 20)
        {
            var outputMessage = "";

            yield return(Extender.Instance.StartCoroutine(Bridge.Get("variables/" + GetId() + "/values/" + "?page=1&page_size=" + (size <= 0 ? 20 : size), result => outputMessage = result)));

            ServerBridge.JsonData RawValues = JsonUtility.FromJson <ServerBridge.JsonData>(outputMessage);

            Value[] Values = new Value[RawValues.results.Count];

            if (!string.IsNullOrEmpty(RawValues.detail) || !string.IsNullOrEmpty(RawValues.details))
            {
                values(null);
            }
            else
            {
                for (var i = 0; i < RawValues.results.Count; i++)
                {
                    Values[i] = new Value(new ServerBridge.JsonData()
                    {
                        value = RawValues.results[i].value, timestamp = RawValues.results[i].timestamp, created_at = RawValues.results[i].created_at
                    }, Api);
                }
            }

            values(Values);

            //Value[] Values = new Value[RawValues.results.Count];

            //for (var i = 0; i < RawValues.results.Count; i++)
            //{
            //    Values[i] = new Value(RawValues.results[i], Api);
            //}
        }
Exemple #3
0
        /// <summary>
        /// Send a bulk of values to  Ubidots API and save them
        /// </summary>
        /// <remarks>Values and Timestamps arrays must have the same length</remarks>
        /// <param name="Values">The values to be sent</param>
        /// <param name="Timestamps">The timestamps of the values to be sent</param>
        public IEnumerator SaveValues(double[] Values, long[] Timestamps, System.Action <Value[]> values)
        {
            var outputMessage = "";

            if (Values == null || Timestamps == null)
            {
                throw new ArgumentNullException();
            }
            else if (Values.Length != Timestamps.Length)
            {
                throw new IndexOutOfRangeException("Values and Timestamps must"
                                                   + " have the same values");
            }

            List <Dictionary <string, object> > ValuesList = new List <Dictionary <string, object> >();

            for (var i = 0; i < Values.Length; i++)
            {
                Dictionary <string, object> Data = new Dictionary <string, object>();
                Data.Add("value", Values[i]);
                Data.Add("timestamp", Timestamps[i]);
                ValuesList.Add(Data);
            }

            string Json = JsonUtility.ToJson(ValuesList);

            yield return(Extender.Instance.StartCoroutine(Bridge.Post("variables/" + GetId() + "/values", Json, result => outputMessage = result)));

            ServerBridge.JsonData RawValues = JsonUtility.FromJson <ServerBridge.JsonData>(outputMessage);


            Value[] tempValues = new Value[RawValues.results.Count];

            if (!string.IsNullOrEmpty(RawValues.detail) || !string.IsNullOrEmpty(RawValues.details))
            {
                values(null);
            }
            else
            {
                for (var i = 0; i < RawValues.results.Count; i++)
                {
                    tempValues[i] = new Value(new ServerBridge.JsonData()
                    {
                        value = RawValues.results[i].value, timestamp = RawValues.results[i].timestamp, created_at = RawValues.results[i].created_at
                    }, Api);
                }
            }

            values(tempValues);
        }
Exemple #4
0
        /// <summary>
        /// Gets a single DataSource in the user account
        /// </summary>
        /// <param name="Id">The DataSource Id</param>
        /// <returns>The DataSource wanted by the user</returns>
        public IEnumerator GetDataSource(string Id, System.Action <DataSource> dataSources)
        {
            var outputMessage = "";

            yield return(Extender.Instance.StartCoroutine(Bridge.Get("datasources/" + Id, result => outputMessage = result)));

            ServerBridge.JsonData RawValues = JsonUtility.FromJson <ServerBridge.JsonData>(outputMessage);

            if (!string.IsNullOrEmpty(RawValues.detail) || !string.IsNullOrEmpty(RawValues.details))
            {
                dataSources(null);
            }
            else
            {
                dataSources(new DataSource(RawValues, this));
            }
        }
Exemple #5
0
 public DataSource(ServerBridge.JsonData Raw, ApiClient Api) : base(Raw, Api)
 {
 }
 public ApiObject(ServerBridge.JsonData Raw, ApiClient Api)
 {
     this.Raw = Raw;
     this.Api = Api;
     Bridge   = Api.GetServerBridge();
 }
Exemple #7
0
 public Value(ServerBridge.JsonData Raw, ApiClient Api) : base(Raw, Api)
 {
 }