public async Task UpdateOccupantState(OccupantState occupantState)
        {
            using (await Lock.LockAsync())
            {
                switch (Config.Person)
                {
                case "Occupant":
                    if (_deviceTwin.Occupant.GetHashCode() == occupantState.GetHashCode())
                    {
                        AppCenterHelper.Track("UpdateOccupantState passed");
                        return;
                    }

                    break;
                }

                dynamic desiredState;
                switch (Config.Person)
                {
                case "Occupant":
                default:
                    desiredState = new
                    {
                        Occupant = occupantState
                    };
                    break;
                }

                var patch = new
                {
                    properties = new
                    {
                        desired = desiredState
                    }
                };


                // TODO: This can fail, add in resiliency
                var updatedTwin =
                    await _registryManager.UpdateTwinAsync(DeviceId, JsonConvert.SerializeObject(patch), "*");

                await GetDeviceTwin();

                AppCenterHelper.Track("Updated Occupant State");
            }
        }
Example #2
0
        private void UpdateOccupantStateReportedProperties(TwinCollection desiredProperties,
            TwinCollection reportedProperties)
        {
            const string occupantState = nameof(Config.Occupant);
            try
            {
                if (desiredProperties.Contains(occupantState))
                {
                    OccupantState newOccupantState = JsonConvert.DeserializeObject<OccupantState>(desiredProperties[occupantState].ToString());
                    _alarmState.OccupantStateChanged(Config.Occupant, newOccupantState);

                    Config.Occupant = newOccupantState;
                    reportedProperties[occupantState] = Config.Occupant;
                }
            }
            catch (Exception ex)
            {
                Log.Error($"Error on UpdateState '{occupantState}'", ex);
            }
        }