예제 #1
0
        public ThingfaceClient(string deviceId, string secretKey, string host = "personal.thingface.io", int port = 8883, bool enableSsl = true)
        {
#if (NETMF44 || NETMF43)
            if (StringExt.IsNullOrWhiteSpace(deviceId))
#else
            if (string.IsNullOrWhiteSpace(deviceId))
#endif
            {
                throw new ArgumentNullException(nameof(deviceId));
            }

#if (NETMF44 || NETMF43)
            if (StringExt.IsNullOrWhiteSpace(secretKey))
#else
            if (string.IsNullOrWhiteSpace(secretKey))
#endif
            {
                throw new ArgumentNullException(nameof(secretKey));
            }

#if (NETMF44 || NETMF43)
            if (StringExt.IsNullOrWhiteSpace(host))
#else
            if (string.IsNullOrWhiteSpace(host))
#endif
            {
                throw new ArgumentNullException(nameof(host));
            }
            if (port <= 0 || port > 65536)
            {
                throw new ArgumentOutOfRangeException(nameof(port));
            }
            _deviceId  = deviceId;
            _secretKey = secretKey;
            _host      = host;
            _port      = port;
            _enableSsl = enableSsl;

            Initialize();
        }
예제 #2
0
        public void SendSensorValue <T>(string sensorId, T sensorValue)
#endif
        {
#if (NETMF43 || NETMF44)
            if (StringExt.IsNullOrWhiteSpace(sensorId))
#else
            if (string.IsNullOrWhiteSpace(sensorId))
#endif
            {
                throw new ArgumentNullException(nameof(sensorId));
            }

            if (sensorValue == null)
            {
                throw new ArgumentNullException(nameof(sensorValue));
            }

            if (!(sensorValue is double) &&
                !(sensorValue is int) &&
                !(sensorValue is long))
            {
                throw new ArgumentOutOfRangeException(nameof(sensorValue));
            }

            var topic = "sensor_data/" + _deviceId + "/" + sensorId;

#if (NETMF44 || NETMF43)
            var jsonString = "{\"v\":";
            jsonString += sensorValue;
            jsonString += "}";
            var message = Encoding.UTF8.GetBytes(jsonString);
#else
            var sensorValuePayload = new SensorPayload(sensorValue);
            var jsonString         = JsonConvert.SerializeObject(sensorValuePayload);
            var message            = Encoding.UTF8.GetBytes(jsonString);
#endif

            _client.Publish(topic, message, 0, false);
        }
예제 #3
0
        public void OffCommand(string senderId = null)
        {
            if (!_client.IsConnected)
            {
                throw new Exception("Client is disconnected.");
            }

#if (NETMF43 || NETMF44)
            if (!StringExt.IsNullOrWhiteSpace(senderId) && senderId.Length > 25)
#else
            if (!string.IsNullOrWhiteSpace(senderId) && senderId.Length > 25)
#endif
            {
                throw new ArgumentOutOfRangeException(nameof(senderId));
            }

            var topicFilter = BuildCommandTopicFilter(senderId);
            _client.Unsubscribe(new[] { topicFilter });

#if !(NETMF44 || NETMF43)
            _commandHandler = null;
#endif
        }