public async Task <DeviceStateInfo> AddOrUpdate(RequestContext requestContext) { string deviceId = GetDeviceId(requestContext.Data); string deviceType = GetDeviceType(requestContext.Data); DeviceTypeProvider deviceTypeProvider = _deviceTypeProviders.Find(dtp => dtp.DeviceType == deviceType); if (deviceTypeProvider != null) { DeviceStateInfo state; Device device = _devices.Find(d => d.DeviceId == deviceId); if (device != null) { state = await UpdateDevice(requestContext, deviceTypeProvider, device).ConfigureAwait(false); } else { state = await CreateAndAddDevice(requestContext, deviceTypeProvider).ConfigureAwait(false); _devices.Add(state.Device); } return(state); } else { throw new Exception("DeviceTypeProvider not found."); } }
public async Task FetchAsync() { _deviceTypeProvider = _deviceTypeProvider ?? this.GetDeviceTypeProvider(); string str = await SendCommand(Commands.GetSysInfo).ConfigureAwait(false); Newtonsoft.Json.Linq.JObject obj = ParseGetSysInfo(str); var requestContext = new RequestContext(obj, IPAddress); await _deviceTypeProvider.UpdateDevice(this, requestContext).ConfigureAwait(false); }
public void RegisterDeviceTypeProvider(DeviceTypeProvider deviceTypeProvider) { _deviceTypeProviders.Add(deviceTypeProvider); }
private static async Task <DeviceStateInfo> CreateAndAddDevice(RequestContext requestContext, DeviceTypeProvider deviceTypeProvider) { Device device = await deviceTypeProvider .CreateDevice(requestContext) .ConfigureAwait(false); return(new DeviceStateInfo(device, DeviceState.Added)); }
private static async Task <DeviceStateInfo> UpdateDevice(RequestContext requestContext, DeviceTypeProvider deviceTypeProvider, Device device) { bool hasChanged = await deviceTypeProvider.UpdateDevice(device, requestContext).ConfigureAwait(false); return(new DeviceStateInfo(device, hasChanged ? DeviceState.Updated : DeviceState.Unchanged)); }