/// <summary> /// Comparison /// </summary> /// <param name="that"></param> /// <returns></returns> public bool Equals(IoTHubRecord that) { if (that == null) { return(false); } if (!Etag.Equals(that.Etag)) { return(false); } return(Equals((INameRecord)that)); }
/// <summary> /// Lookup record by address /// </summary> /// <param name="address"></param> /// <param name="type"></param> /// <param name="ct"></param> /// <returns></returns> public async Task <IEnumerable <INameRecord> > LookupAsync(Reference address, NameRecordType type, CancellationToken ct) { if (address == null || address == Reference.Null) { throw new ArgumentException(nameof(address)); } var sql = new StringBuilder("SELECT * FROM devices WHERE "); if (address != Reference.All) { sql.Append("tags.address = '"); sql.Append(address.ToString().ToLower()); sql.Append("' AND "); } sql.Append(IoTHubRecord.CreateTypeQueryString(type)); return(await LookupAsync(sql.ToString(), ct).ConfigureAwait(false)); }
/// <summary> /// Lookup record by record type /// </summary> /// <param name="name"></param> /// <param name="type"></param> /// <param name="ct"></param> /// <returns></returns> public async Task <IEnumerable <INameRecord> > LookupAsync(string name, NameRecordType type, CancellationToken ct) { if (string.IsNullOrEmpty(name)) { throw new ArgumentException(nameof(name)); } name = name.ToLowerInvariant(); if (NameRecordType.Host == (type & NameRecordType.Host)) { // Use cache to look up a host record INameRecord record; if (_cache.TryGetValue(name, out record) && record.References.Any()) { return(new NameRecord(record).AsEnumerable()); } } var sql = new StringBuilder("SELECT * FROM devices WHERE "); sql.Append("(deviceId = '"); sql.Append(name); if (NameRecordType.Proxy == (type & NameRecordType.Proxy)) { sql.Append("' OR tags.name = '"); sql.Append(name); } Reference address; if (Reference.TryParse(name, out address)) { sql.Append("' OR tags.address = '"); sql.Append(address.ToString().ToLower()); } sql.Append("') AND "); sql.Append(IoTHubRecord.CreateTypeQueryString(type)); return(await LookupAsync(sql.ToString(), ct).ConfigureAwait(false)); }
/// <summary> /// Updates a record in the device registry /// </summary> /// <param name="record"></param> /// <param name="ct"></param> /// <returns></returns> private async Task <IoTHubRecord> UpdateRecordAsync( INameRecord record, CancellationToken ct) { var hubRecord = record as IoTHubRecord; // // If the record is a generic record, add it first and retrieve the resulting // twin record from the registry. Then assign the generic record to it and // see if anything needs patching... // if (hubRecord == null) { // Create and convert generic record into hub record hubRecord = await AddRecordAsync(record, ct).ConfigureAwait(false); if (hubRecord == null) { return(null); } } var json = hubRecord.Patch; if (string.IsNullOrEmpty(json)) { // Nothing to patch... return(hubRecord); } // // If we logged changes to the record use the resulting patch json to patch // up the twin record. If we do not find the record anymore as part of // patching it, then it was deleted, in which case return null. Otherwise // return the returned patched up twin record // ProxyEventSource.Log.PatchingRecord(this, record, json); try { var stream = await _http.StreamAsync( CreateUri("/twins/" + hubRecord.Id), Http.Patch, async h => { h.Add(HttpRequestHeader.Authorization.ToString(), await IoTHubService.GetSasTokenAsync(_hubConnectionString, 3600).ConfigureAwait(false)); h.Add(HttpRequestHeader.UserAgent.ToString(), _clientId); h.IfMatch.Add(new EntityTagHeaderValue(@"""*""")); }, (sc, h) => { if (sc == HttpStatusCode.NotFound) { throw new TransientException(); } }, ct, json, "application/json").ConfigureAwait(false); using (stream) using (var sr = new StreamReader(stream)) using (JsonReader reader = new JsonTextReader(sr)) { hubRecord = new IoTHubRecord(new IoTHubRecord((JObject)JToken.ReadFrom(reader))); } } catch (TransientException) { ProxyEventSource.Log.RecordRemoved(this, record); hubRecord = null; } catch (Exception e) { throw ProxyEventSource.Log.Rethrow(e, this); } ProxyEventSource.Log.RecordPatched(this, record, json); return(hubRecord); }