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
        }
		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
		}
示例#3
0
        /// <summary>
        /// Load info about this sensor from SensorCloud
        /// If this sensor doesn't exist then an exception will be thrown
        /// </summary>
        private void LoadFromSensorCloud()
        {
            if (_infoLoaded)
            {
                return;
            }

            //make a get request for channel attributes to determine if the channel exists
            var request = _requests.url("/sensors/" + this.Name + "/")
                          .Param("version", "1")
                          .Accept("application/xdr")
                          .Get();

            // check the response code for success
            if (request.ResponseCode == HttpStatusCode.OK)
            {
                var ms      = new MemoryStream(request.Raw);
                var xdr     = new XdrReader(ms);
                int version = xdr.ReadInt();
                if (version != 1)
                {
                    throw new SensorCloudException("Unsupported xdr version");
                }

                string type        = xdr.ReadString(1000);
                string label       = 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
                string descripiton = xdr.ReadString(1000);


                //Only if we get all the fields from xdr do we update this instance.  This prevents us from paritally updating the state of the instance.
                _label       = label;
                _description = descripiton;
                _sensorType  = type;
            }
            else
            {
                throw SensorCloudException.GenerateSensorCloudException(request, "Get Sensor Failed");
            }

            _infoLoaded = true;
        }
示例#4
0
		/// <summary>
		/// Get a list of existing Sensors for this device.
		/// </summary>
		public List<Sensor> GetSensors()
		{
			List<Sensor> outputList = new List<Sensor>();
			//make a get request for channel attributes to determine if the channel exists
			var request = _requests.url("/sensors/")
														.Param("version", "1")
														.Accept("application/xdr")
														.Get();

			// check the response code for success
			if (request.ResponseCode == HttpStatusCode.OK)
			{
				var ms = new MemoryStream(request.Raw);
				var xdr = new XdrReader(ms);
				//The first value is the version.  We expect this to be 1
				int version = xdr.ReadInt();
				if (version != 1)
				{
					throw new SensorCloudException("Unsupported xdr version");
				}

				//The next value is an integer telling us how many sensors to unpack
				//Each sensor has its info packed a certain way, so this allows us to know how many sensors we must unpack
				int sensorCount = xdr.ReadInt();
				//loop through sensors
				for(var i=0; i < sensorCount; i++)
				{
					string sensorName = xdr.ReadString(1000);
					//for example's sake, I'm going to push a Sensor Object onto our output array
					//This object only needs the Sensor name, and will fetch the Sensor info on it's own
					//However, to get all sensor names we must still parse all of the stream.  You can modify 
					//this function to meet your own needs, and return a different set of information 
					//tailored to what you need.
					outputList.Add(new Sensor(sensorName, _requests));

					string sensortype = xdr.ReadString(1000);
					string sensorlabel = 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
					string sensordescripiton = xdr.ReadString(1000);

					//the next value is the number of channels this sensor has
					int channelCount = xdr.ReadInt();
					//loop all channels
					for (var j=0; j < channelCount; j++)
					{
						string channelName = xdr.ReadString(1000);
						string channelLabel = xdr.ReadString(1000);
						string channelDescription = xdr.ReadString(1000);

						//This tells us how many datastreams the channel has
						//This should be one, as we only currently support TimeSeries, but could grow as more stream types are added
						int streamCount = xdr.ReadInt();
						//loop through streams
						for (var k=0; k < streamCount; k++)
						{
							//the first value in the stream is the type of stream.  We expect this to be TS_V1, meaning timeseries version 1
							string streamType = xdr.ReadString(1000);
							//the second value of a stream is the number of bytes that describes the stream.  
							//This is convenient as it allows us to just skip the stream if we are not interested in it.  
							//For this example's purpose, we are uninterested and will simply skip ahead in the xdr.  We do this by reading the number bytes, but doing anything
							//with the data
							int totalBytes = xdr.ReadInt();
							//just reading the bytes by previously parsed length advances the xdr reader forward in the data
							xdr.ReadBytes(totalBytes);
						}
					}
				}
				return outputList;
			}
			else
			{
				throw SensorCloudException.GenerateSensorCloudException(request, "Get Sensors Failed");
			}
		}
示例#5
0
		/// <summary>
		/// Load info about this sensor from SensorCloud
		/// If this sensor doesn't exist then an exception will be thrown
		/// </summary>
		private void LoadFromSensorCloud()
		{
			if(_infoLoaded) { return; }

			//make a get request for channel attributes to determine if the channel exists
			var request = _requests.url("/sensors/" + this.Name + "/")
								  .Param("version", "1")
								  .Accept("application/xdr")
								  .Get();

			// check the response code for success
			if(request.ResponseCode == HttpStatusCode.OK)
			{
				
				var ms = new MemoryStream(request.Raw);
				var xdr = new XdrReader(ms);
				int version = xdr.ReadInt();
				if (version != 1)
				{
					throw new SensorCloudException("Unsupported xdr version");
				}

				string type = xdr.ReadString(1000);
				string label = 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
				string descripiton = xdr.ReadString(1000);
				

				//Only if we get all the fields from xdr do we update this instance.  This prevents us from paritally updating the state of the instance.
				_label = label;
				_description = descripiton;
				_sensorType = type;
			}
			else
			{
				throw SensorCloudException.GenerateSensorCloudException(request, "Get Sensor Failed");
			}

			_infoLoaded = true;
		} 
示例#6
0
        /// <summary>
        /// Get a list of existing Sensors for this device.
        /// </summary>
        public List <Sensor> GetSensors()
        {
            List <Sensor> outputList = new List <Sensor>();
            //make a get request for channel attributes to determine if the channel exists
            var request = _requests.url("/sensors/")
                          .Param("version", "1")
                          .Accept("application/xdr")
                          .Get();

            // check the response code for success
            if (request.ResponseCode == HttpStatusCode.OK)
            {
                var ms  = new MemoryStream(request.Raw);
                var xdr = new XdrReader(ms);
                //The first value is the version.  We expect this to be 1
                int version = xdr.ReadInt();
                if (version != 1)
                {
                    throw new SensorCloudException("Unsupported xdr version");
                }

                //The next value is an integer telling us how many sensors to unpack
                //Each sensor has its info packed a certain way, so this allows us to know how many sensors we must unpack
                int sensorCount = xdr.ReadInt();
                //loop through sensors
                for (var i = 0; i < sensorCount; i++)
                {
                    string sensorName = xdr.ReadString(1000);
                    //for example's sake, I'm going to push a Sensor Object onto our output array
                    //This object only needs the Sensor name, and will fetch the Sensor info on it's own
                    //However, to get all sensor names we must still parse all of the stream.  You can modify
                    //this function to meet your own needs, and return a different set of information
                    //tailored to what you need.
                    outputList.Add(new Sensor(sensorName, _requests));

                    string sensortype        = xdr.ReadString(1000);
                    string sensorlabel       = 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
                    string sensordescripiton = xdr.ReadString(1000);

                    //the next value is the number of channels this sensor has
                    int channelCount = xdr.ReadInt();
                    //loop all channels
                    for (var j = 0; j < channelCount; j++)
                    {
                        string channelName        = xdr.ReadString(1000);
                        string channelLabel       = xdr.ReadString(1000);
                        string channelDescription = xdr.ReadString(1000);

                        //This tells us how many datastreams the channel has
                        //This should be one, as we only currently support TimeSeries, but could grow as more stream types are added
                        int streamCount = xdr.ReadInt();
                        //loop through streams
                        for (var k = 0; k < streamCount; k++)
                        {
                            //the first value in the stream is the type of stream.  We expect this to be TS_V1, meaning timeseries version 1
                            string streamType = xdr.ReadString(1000);
                            //the second value of a stream is the number of bytes that describes the stream.
                            //This is convenient as it allows us to just skip the stream if we are not interested in it.
                            //For this example's purpose, we are uninterested and will simply skip ahead in the xdr.  We do this by reading the number bytes, but doing anything
                            //with the data
                            int totalBytes = xdr.ReadInt();
                            //just reading the bytes by previously parsed length advances the xdr reader forward in the data
                            xdr.ReadBytes(totalBytes);
                        }
                    }
                }
                return(outputList);
            }
            else
            {
                throw SensorCloudException.GenerateSensorCloudException(request, "Get Sensors Failed");
            }
        }