Esempio n. 1
0
        /// <summary>Put (update) values in arduino record</summary>
        /// <param name="uri">e.g. /arduinoParameter</param>
        /// <param name="values">Models.ArduinoValues</param>
        /// <param name="recordID">arduino.id</param>
        public async Task <bool> PostArduinoValuesAsync(string uri, Models.ArduinoRecord values)
        {
            Models.nodeOffline = false;
            try
            {
                // CAPITALISATION - pay attention to case of properties!!
                string json = JsonConvert.SerializeObject(values);
                using (var client = new HttpClient()
                {
                    Timeout = new TimeSpan(0, 0, Constants.apiReqTimeoutSecs)
                })
                {
                    var response = await client.PostAsync(
                        uri,
                        new StringContent(json, Encoding.UTF8, "application/json"));

                    if (response.StatusCode == HttpStatusCode.OK)
                    {
                        return(true);
                    }
                }
            }
            catch (Exception ex)
            {
                if (ex.Message == "The operation was canceled." | ex.Message == "Connection refused")
                {
                    Models.nodeOffline = true;
                }
                Debug.WriteLine(string.Format("ERROR {0}", ex.Message));
            }
            return(false);
        }
Esempio n. 2
0
        /// <summary>Write the current state of a single control or all controls</summary>
        /// <param name="valueName">optional: If given, write a single control value</param>
        public static async Task <bool> WriteControlsState(string valueName = "")
        {
            bool allSucceeded = true;

            // write named value
            if (valueName != "")
            {
                Models.ArduinoRecord dataPoint = Models.dataPoints.Find(x => x.ValueName == valueName);
                if (dataPoint != null)
                {
                    bool r = await Services.WriteControlState(dataPoint);

                    if (!r)
                    {
                        allSucceeded = false;
                    }
                }
            }
            else
            // write all values
            {
                foreach (Models.ArduinoRecord dataPoint in Models.dataPoints)
                {
                    bool r = await Services.WriteControlState(dataPoint);

                    if (!r)
                    {
                        allSucceeded = false;
                    }
                }
            }

            return(allSucceeded);
        }
Esempio n. 3
0
        /// <summary>Write a single datapoint to the database. If dataPoint has no .Id, a new dataPoint is created.</summary>
        /// <param name="dataPoint">parameter to write</param>
        private static async Task <bool> WriteControlState(Models.ArduinoRecord dataPoint)
        {
            RestService _restService = new RestService();
            bool        response     = false;

            string requestUri = Constants.apiMarkGriffithsEndpoint;

            if (dataPoint.Id == 0)
            {
                // create new parameter
                requestUri += "/addArduino";

                Models.ArduinoRecord ardVal = new Models.ArduinoRecord
                {
                    UserID      = dataPoint.UserID,
                    ValueName   = dataPoint.ValueName,
                    ValueInt    = dataPoint.ValueInt,
                    ValueString = dataPoint.ValueString
                };

                response = false;
                try
                {
                    response = await _restService.PostArduinoValuesAsync(requestUri, ardVal);
                }
                catch (System.Exception ex)
                {
                    Debug.WriteLine("\tERROR {0}", ex.Message);
                }
            }
            else
            {
                // update parameter
                requestUri += "/updateArduino/" + dataPoint.Id;

                Models.ArduinoValues ardVal = new Models.ArduinoValues
                {
                    ValueInt    = dataPoint.ValueInt,
                    ValueString = dataPoint.ValueString
                };

                response = false;
                try
                {
                    response = await _restService.PutArduinoValuesAsync(requestUri, ardVal);
                }
                catch (System.Exception ex)
                {
                    Debug.WriteLine("\tERROR {0}", ex.Message);
                }
            }
            return(response);
        }