Ejemplo n.º 1
0
        // Save  type, label, and descritpion to SensorCloud
        private void  SaveState(string label, string description, string type)
        {
            var payload = new MemoryStream();
            var xdr     = new XdrWriter(payload);

            const int VERSION = 1;

            xdr.WriteInt(VERSION);
            xdr.WriteString(type);
            xdr.WriteString(label);
            xdr.WriteString(description);


            var request = _requests.url("/sensors/" + this.Name + "/")
                          .Param("version", "1")
                          .Accept("application/xdr")
                          .ContentType("application/xdr")
                          .Data(payload.ToArray())
                          .Post();

            if (request.ResponseCode != HttpStatusCode.Created)
            {
                throw SensorCloudException.GenerateSensorCloudException(request, "Save Sensor state failed.");
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Add time-series data to the channel.
        /// </summary>
        /// <param name="sensorName">sensor containing the channel to add data to.</param>
        /// <param name="channelName">channel to add data to.</param>
        /// <param name="sampleRate">sample-rate for points in data</param>
        /// <param name="data">a list of points to add to the channel.
        /// <remarks>The points should be ordered and match the sample-rate provided.</remarks>
        /// </param>
        public void AddTimeSeriesData(SampleRate sampleRate, IEnumerable <Point> data)
        {
            var payload = new MemoryStream();
            var xdr     = new XdrWriter(payload);

            const int VERSION = 1;

            xdr.WriteInt(VERSION);
            xdr.WriteInt(sampleRate.Type.ToXdr());
            xdr.WriteInt(sampleRate.Rate);

            //Writing an array in XDR.  an array is always prefixed by the array length
            xdr.WriteInt(data.Count());
            foreach (Point p in data)
            {
                xdr.WriteUnsingedHyper(p.UnixTimestamp);
                xdr.WriteFloat(p.Value);
            }

            string url     = "/sensors/" + _sensorName + "/channels/" + this.Name + "/streams/timeseries/data/";
            var    request = _requests.url(url)
                             .Param("version", "1")
                             .ContentType("application/xdr")
                             .Data(payload.ToArray())
                             .Put();

            // check the response code for success
            if (request.ResponseCode != HttpStatusCode.Created)
            {
                throw SensorCloudException.GenerateSensorCloudException(request, "AddTimeSeriesData failed.");
            }
        }
Ejemplo n.º 3
0
        public void Authenticate()
        {
            //determine protocol from the auth server
            String PROTOCOL = AuthServer.StartsWith("http://") ? "http://" : "https://";

            String url = AuthServer + "/SensorCloud/devices/" + DeviceId + "/authenticate/";

            var request = _requests.url(url)
                          .Param("version", "1")
                          .Param("key", DeviceKey)
                          .Accept("application/xdr")
                          .Get();

            // check the response code for success
            if (request.ResponseCode != HttpStatusCode.OK)
            {
                throw new AuthenticationException(request.Text);
            }


            // Extract the authentication token and server from the response
            var ms  = new MemoryStream(request.Raw);
            var xdr = new XdrReader(ms);

            AuthToken = xdr.ReadString(1000);              //a token is generally about 60 chars.  Limit the read to at most 1000 chars as a precation so in their is a protocol error we don't try to allocate lots of memory
            ApiServer = PROTOCOL + xdr.ReadString(255);    //max length for a FQDN is 255 chars
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Determine if a sensor exists for this Device.
        /// </summary>
        /// <param name="sensorName"></param>
        /// <returns>True if the sensor exists.</returns>
        public bool HasSensor(String sensorName)
        {
            var request = _requests.url("/sensors/" + sensorName + "/")
                          .Param("version", "1")
                          .Accept("application/xdr")
                          .Get();

            // check the response code for success
            switch (request.ResponseCode)
            {
            case HttpStatusCode.OK: return(true);

            case HttpStatusCode.NotFound: return(false);

            default: throw SensorCloudException.GenerateSensorCloudException(request, "HasSensor failed");
            }
        }
Ejemplo n.º 5
0
        private IList <Point> DownloadData(ulong startTime, ulong endTime)
        {
            //url: /sensors/<sensor_name>/channels/<channel_name>/streams/timeseries/data/
            // params:
            //    starttime (required)
            //    endtime  (required)
            //    showSampleRateBoundary (oiptional)
            //    samplerate (oiptional)

            string url     = "/sensors/" + _sensorName + "/channels/" + this._channelName + "/streams/timeseries/data/";
            var    request = _requests.url(url)
                             .Param("version", "1")
                             .Param("starttime", startTime)
                             .Param("endtime", endTime)
                             .Accept("application/xdr")
                             .Get();

            // check the response code for success
            if (request.ResponseCode == HttpStatusCode.NotFound)
            {
                //404 is an empty list
                return(new List <Point>());
            }
            else if (request.ResponseCode != HttpStatusCode.OK)
            {
                //all other errors are exceptions
                throw SensorCloudException.GenerateSensorCloudException(request, "AddTimeSeriesData failed.");
            }

            var xdrReader = new XdrReader(request.Raw);

            var datapoints = new List <Point>();

            ulong timestamp;
            float value;

            try
            {
                // timeseries/data always returns a relativly small chunk of data less than 50,000 points so we can proccess it all at once.  We won't be given an infinite stream
                //however a futre enhancemnts could be to stat proccessing this stream as soon as we have any bytes avialable to the user, so they could be
                //iterating over the data while it is still being downloaded.
                while (true)
                {
                    timestamp = xdrReader.ReadUnsingedHyper();
                    value     = xdrReader.ReadFloat();

                    datapoints.Add(new Point(timestamp, value));
                }
            }
            catch (EndOfStreamException)
            {}

            return(datapoints);
        }