示例#1
0
        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);
        }
示例#3
0
 public void RegisterDeviceTypeProvider(DeviceTypeProvider deviceTypeProvider)
 {
     _deviceTypeProviders.Add(deviceTypeProvider);
 }
示例#4
0
        private static async Task <DeviceStateInfo> CreateAndAddDevice(RequestContext requestContext, DeviceTypeProvider deviceTypeProvider)
        {
            Device device = await deviceTypeProvider
                            .CreateDevice(requestContext)
                            .ConfigureAwait(false);

            return(new DeviceStateInfo(device, DeviceState.Added));
        }
示例#5
0
        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));
        }