// Usubscribe from data coming from the device specified by the deviceId
 public static bool UnsubscribeFromDeviceData(Device device)
 {
     return UnsubscribeFromDeviceData(device.DeviceId);
 }
Example #2
0
        public async void getMeasurements()
        {
            // Set the OAuth token = "YOUR OAUTH TOKEN HERE"
            Relayr.OauthToken = "8IBG3kct-JmrRy_cU5-IoLN708Z9_fAn";

            // Get a list of transmitters and connect to the MQTT broker with the first one in the list
            transmitters = await Relayr.GetTransmittersAsync();   // "CLIENT ID OF YOUR CHOICE"
            transmitter = Relayr.ConnectToBroker(transmitters[0], "4f585a21-f19b-44cc-8874-7afef539dcbb");
            userId = Relayr.UserId;
            userName = Relayr.Name;
            userEmail = Relayr.Email;
            displayUserInfo(userId, userName, userEmail);

            // Get a list of devices associated with the transmitter, 
            // Subscribe to data from the first device on the list
            devices = await transmitter.GetDevicesAsync();    // "{ADD SENSOR ID}"

            //Device[] listOfDevices;
            for (int i = 0; i < devices.Count; i++)
            {
                string tmpId = (devices[i]["id"]).ToString();
                string tmpName = (devices[i]["name"]).ToString();

                Device tmpDevice = new Device((devices[i]["id"]).ToString(), (devices[i]["name"]).ToString(), (devices[i]["firmwareVersion"]).ToString());
                devicesOfUser.Add(tmpDevice);
                //listOfDevices[i] = await transmitter.SubscribeToDeviceDataAsync((devices[i]["id"]).ToString());
                //listOfDevices[i].PublishedDataReceived += device_PublishedDataReceived;

                switch (tmpName)
                {
                    case "gyroscope":
                        Gyroscope(tmpId);
                        break;
                    case "light":
                        Light(tmpId);
                        break;
                    case "microphone":
                        Microphone(tmpId);
                        break;
                    case "thermometer":
                        Thermometer(tmpId);
                        break;


                    default:
                        //Console.WriteLine("Default case");
                        break;
                }
            }
            displayDevices(devicesOfUser);
        }
        // Does the meat of the work in subscribing to a device (as well as initalization and 
        // connecting if need be)
        public static async Task<Device> SubscribeToDeviceDataAsync(string deviceId, QualityOfService qualityOfService)
        {
            // Initialize, if required
            if (_client == null)
            {
                Initialize();
            }

            // Get the connection information for this device
            _arguments["deviceId"] = deviceId;
            HttpResponseMessage response = await HttpManager.Manager.PerformHttpOperation(ApiCall.CreateChannel, null, _arguments);
            dynamic connectionInfo = await HttpManager.Manager.ConvertResponseContentToObject(response);

            // Use that connection information to attempt to connect to the mqtt server, if necessary
            // Return null if a connection couldn't be created
            if (!_client.IsConnected)
            {
                string userId = (string)connectionInfo["credentials"]["user"];
                string password = (string)connectionInfo["credentials"]["password"];

                ConnectToBroker(userId, password);
                if (!_client.IsConnected)
                {
                    return null;
                }
            }

            // Get the quality of service byte
            byte serviceLevel = MqttMsgBase.QOS_LEVEL_AT_LEAST_ONCE;
            switch (qualityOfService)
            {
                case QualityOfService.AtMostOnce:
                    serviceLevel = MqttMsgBase.QOS_LEVEL_AT_MOST_ONCE;
                    break;
                case QualityOfService.ExactlyOnce:
                    serviceLevel = MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE;
                    break;
                case QualityOfService.GrantedFailure:
                    serviceLevel = MqttMsgBase.QOS_LEVEL_GRANTED_FAILURE;
                    break;
            }

            // Subscribe to data from the device
            string topic = (string)connectionInfo["credentials"]["topic"];
            _client.Subscribe(new string[] { topic }, new byte[] { serviceLevel });

            Device device = new Device(deviceId, topic, qualityOfService);
            _subscribedDevices.Add(topic, device);
            _deviceIdToTopic.Add(deviceId, topic);

            return device;
        }
Example #4
0
        // Subscribe to new data coming from the device specified by the deviceId, with the
        // specified MQTT quality of service. Return a Device isntance representing the device if
        // a device with that ID is registered to this transmitter. Otherwise, return null
        public async Task<Device> SubscribeToDeviceDataAsync(string deviceId, QualityOfService qualityOfService)
        {
            // Argument checking
            if(deviceId == null || deviceId.Equals(""))
            {
                throw new ArgumentException("Device ID cannot be null or empty");
            }

            // Determine whether the device is actually registered to the transmitter
            if (_devices == null)
            {
                await GetDevicesAsync();   
            }

            bool found = false;
            foreach(dynamic deviceObj in _devices) {
                if (((string)deviceObj["id"]).Equals(deviceId))
                {
                    found = true;
                    break;
                }
            }

            // return null if no such device is registered to a transmitter
            if (!found)
            {
                return null;
            }

            byte serviceLevel = MqttMsgBase.QOS_LEVEL_AT_LEAST_ONCE;
            switch (qualityOfService)
            {
                case QualityOfService.AtMostOnce:
                    serviceLevel = MqttMsgBase.QOS_LEVEL_AT_MOST_ONCE;
                    break;
                case QualityOfService.ExactlyOnce:
                    serviceLevel = MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE;
                    break;
                case QualityOfService.GrantedFailure:
                    serviceLevel = MqttMsgBase.QOS_LEVEL_GRANTED_FAILURE;
                    break;
            }

            // Subscribe to the MQTT topic for this device
            string topic = "/v1/" + deviceId + "/data";
            _client.Subscribe(new string[] { topic }, new byte[] { serviceLevel });

            // Create a device and add it to the list
            Device device = new Device(deviceId, topic, qualityOfService);
            _connectedDevices.Add(topic, device);

            // Return a reference to the device
            return device;
        }