public async Task <ResponseData <ApplianceDTO> > SetupThink([FromBody] ApplianceDTO appliance) { try { var response = new ResponseData <ApplianceDTO>(); var things = await TelitApi.ThingList(); bool isMatching = false; bool hasSerialNumber = false; Telit.ThingList.Result thing = new Telit.ThingList.Result(); if (things != null && things.things != null && things.things.success && things.things.@params != null && [email protected] != null && [email protected]() > 0) { foreach (var item in [email protected]) { if (item.key == appliance.SerialNumber) { hasSerialNumber = true; if (item.loc != null && item.loc.addr != null) { isMatching = await calculateAddressAsync(appliance.Street1.Trim(), appliance.City.Trim(), _stateService.GetStateById(appliance.StateId.Value).Name, appliance.ZipCode.Trim(), item.loc.lat, item.loc.lng); thing = item; break; } } } } if (!hasSerialNumber) { response.Message = ResponseMessage.SerialNumberInCorrect; response.Status = ResponseStatus.Success.ToString(); return(response); } if (isMatching) { var isSerialNumberExist = _applianceService.GetApplianceBySerialNumber(appliance.SerialNumber); var thingFind = await TelitApi.ThingFind(thing.key); bool thingIsOn = false; bool thingIsConnected = false; bool thingCellular = false; bool thingPower = false; bool thingWifi = false; bool thingWifiInternet = false; int? thingTrustLevel = null; Telit.ThingFind.Params _params = new Telit.ThingFind.Params(); string applianceEnvironment = string.Empty; if (thingFind != null && thingFind.Things != null && thingFind.Things.success && thingFind.Things.@params != null) { _params = thingFind.Things.@params; } if (_params != null && _params.alarms != null && _params.alarms.env != null && _params.alarms.env.state >= 0 && _params.alarms.env.state < 16) { applianceEnvironment = Convert.ToString(_params.alarms.env.state, 2).PadLeft(4, '0'); var array = !string.IsNullOrEmpty(applianceEnvironment) ? applianceEnvironment.ToArray() : new char[] { }; thingIsOn = Convert.ToBoolean(_params.alarms.on.state); thingIsConnected = _params.connected; thingCellular = array.Length == 4 ? Convert.ToBoolean(Convert.ToInt16(array[2].ToString())) : false; thingPower = array.Length == 4 ? Convert.ToBoolean(Convert.ToInt16(array[3].ToString())) : false; thingWifi = array.Length == 4 ? Convert.ToBoolean(Convert.ToInt16(array[1].ToString())) : false; thingWifiInternet = array.Length == 4 ? Convert.ToBoolean(Convert.ToInt16(array[0].ToString())) : false; thingTrustLevel = _params.alarms.trust != null ? (int?)_params.alarms.trust.state : null; } if (isSerialNumberExist == null) { var app = _applianceService.Insert(new Appliance() { AccountId = appliance.AccountId, Cellular = thingCellular, IsConnected = thingIsConnected, City = appliance.City, DeviceName = _params != null && _params.attrs != null && _params.attrs.name != null ? _params.attrs.name.value : string.Empty, OsVersion = "", GeoFenceEnabled = _params.alarms != null && _params.alarms.timerState != null && _params.alarms.timerState.state != 0 ? true : false, Lat = _params.loc != null ? _params.loc.lat : 0, Lon = _params.loc != null ? _params.loc.lng : 0, SerialNumber = appliance.SerialNumber, Power = thingPower, StateId = appliance.StateId, Street1 = appliance.Street1, Street2 = appliance.Street2, TimerEnabled = _params.alarms != null && _params.alarms.timerState != null && _params.alarms.timerState.state != 0 ? true : false, TriggerMile = Convert.ToDecimal(_appSettings.TriggerMiles), Wifi = thingWifi, WiFiInternet = thingWifiInternet, ZipCode = appliance.ZipCode, TrustLevel = thingTrustLevel != null ? thingTrustLevel : 0, IsOn = thingIsOn }); if (app != null) { var accountAppliance = _accountApplianceService.Insert(new AccountAppliance() { AccountId = app.AccountId, AirGapVersion = "", ApplianceId = app.Id, IsQRCodeScaned = false, IsVerified = false }); appliance.Id = app.Id; if (_params.attrs != null && _params.attrs.timerSchedule != null) { var timerScheduleDTO = JsonConvert.DeserializeObject <TimerScheduleFromTelit>(_params.attrs.timerSchedule.value); char[] weekDayArray = timerScheduleDTO.Weekdays.ToCharArray(); char[] weekEndArray = timerScheduleDTO.Weekends.ToCharArray(); string valueWeekDay = string.Empty; string valueWeekEnd = string.Empty; for (int i = 0; i < weekDayArray.Length; i++) { if (weekDayArray[i] == '1') { valueWeekDay += i + 1 + ","; } } for (int i = 0; i < weekEndArray.Length; i++) { if (weekEndArray[i] == '1') { valueWeekEnd += i + 1 + ","; } } if (!string.IsNullOrEmpty(valueWeekDay)) { var timerScheduleOfWeekDays = new TimerSchedule() { TimerTypeId = Configuration.Weekdays, ApplianceId = app.Id, ActiveValues = !string.IsNullOrEmpty(valueWeekDay) ? valueWeekDay.Remove(valueWeekDay.Length - 1) : string.Empty }; _timerScheduleService.Insert(timerScheduleOfWeekDays); } if (!string.IsNullOrEmpty(valueWeekEnd)) { var timerScheduleOfWeekEnd = new TimerSchedule() { TimerTypeId = Configuration.Weekends, ApplianceId = app.Id, ActiveValues = !string.IsNullOrEmpty(valueWeekEnd) ? valueWeekEnd.Remove(valueWeekEnd.Length - 1) : string.Empty }; _timerScheduleService.Insert(timerScheduleOfWeekEnd); } } } } else { if (isSerialNumberExist.AccountId == null) { isSerialNumberExist.AccountId = appliance.AccountId; isSerialNumberExist.Street1 = appliance.Street1; isSerialNumberExist.Street2 = appliance.Street2; isSerialNumberExist.City = appliance.City; isSerialNumberExist.StateId = appliance.StateId; isSerialNumberExist.ZipCode = appliance.ZipCode; isSerialNumberExist.Lat = _params.loc != null ? _params.loc.lat : 0; isSerialNumberExist.Lon = _params.loc != null ? _params.loc.lng : 0; isSerialNumberExist.Cellular = thingCellular; isSerialNumberExist.IsConnected = thingIsConnected; isSerialNumberExist.DeviceName = _params != null && _params.attrs != null && _params.attrs.name != null ? _params.attrs.name.value : string.Empty; isSerialNumberExist.OsVersion = ""; isSerialNumberExist.GeoFenceEnabled = _params.alarms != null && _params.alarms.timerState != null && _params.alarms.timerState.state != 0 ? true : false; isSerialNumberExist.Power = thingPower; isSerialNumberExist.TimerEnabled = _params.alarms != null && _params.alarms.timerState != null && _params.alarms.timerState.state != 0 ? true : false; isSerialNumberExist.Wifi = thingWifi; isSerialNumberExist.WiFiInternet = thingWifiInternet; isSerialNumberExist.ZipCode = appliance.ZipCode; isSerialNumberExist.TrustLevel = thingTrustLevel != null ? thingTrustLevel : 0; isSerialNumberExist.IsOn = thingIsOn; isSerialNumberExist.TriggerMile = Convert.ToDecimal(_appSettings.TriggerMiles); _applianceService.Update(isSerialNumberExist); var accountAppliance = _accountApplianceService.Insert(new AccountAppliance() { AccountId = appliance.AccountId, AirGapVersion = "", ApplianceId = isSerialNumberExist.Id, IsQRCodeScaned = false, IsVerified = false }); appliance.Id = isSerialNumberExist.Id; if (_params.attrs != null && _params.attrs.timerSchedule != null) { var timerScheduleDTO = JsonConvert.DeserializeObject <TimerScheduleFromTelit>(_params.attrs.timerSchedule.value); char[] weekDayArray = timerScheduleDTO.Weekdays.ToCharArray(); char[] weekEndArray = timerScheduleDTO.Weekends.ToCharArray(); string valueWeekDay = string.Empty; string valueWeekEnd = string.Empty; for (int i = 0; i < weekDayArray.Length; i++) { if (weekDayArray[i] == '1') { valueWeekDay += i + 1 + ","; } } for (int i = 0; i < weekEndArray.Length; i++) { if (weekEndArray[i] == '1') { valueWeekEnd += i + 1 + ","; } } var timerSchedule = _timerScheduleService.GetTimerScheduleByApplianceId(appliance.Id); if (timerSchedule != null && timerSchedule.Count > 0) { foreach (var item in timerSchedule) { if (item.TimerTypeId == Configuration.Weekdays) { item.ActiveValues = !string.IsNullOrEmpty(valueWeekDay) ? valueWeekDay.Remove(valueWeekDay.Length - 1) : string.Empty; } if (item.TimerTypeId == Configuration.Weekends) { item.ActiveValues = !string.IsNullOrEmpty(valueWeekEnd) ? valueWeekEnd.Remove(valueWeekEnd.Length - 1) : string.Empty; } _timerScheduleService.Update(item); } } else { if (!string.IsNullOrEmpty(valueWeekDay)) { var timerScheduleOfWeekDays = new TimerSchedule() { TimerTypeId = Configuration.Weekdays, ApplianceId = isSerialNumberExist.Id, ActiveValues = !string.IsNullOrEmpty(valueWeekDay) ? valueWeekDay.Remove(valueWeekDay.Length - 1) : string.Empty }; _timerScheduleService.Insert(timerScheduleOfWeekDays); } if (!string.IsNullOrEmpty(valueWeekEnd)) { var timerScheduleOfWeekEnd = new TimerSchedule() { TimerTypeId = Configuration.Weekends, ApplianceId = isSerialNumberExist.Id, ActiveValues = !string.IsNullOrEmpty(valueWeekEnd) ? valueWeekEnd.Remove(valueWeekEnd.Length - 1) : string.Empty }; _timerScheduleService.Insert(timerScheduleOfWeekEnd); } } } } else { response.Status = ResponseStatus.Success.ToString(); response.Message = ResponseMessage.SerialNumberIsExist; return(response); } } } appliance.IsMatching = isMatching; appliance.HasSerialNumber = hasSerialNumber; response.Data = appliance; response.Status = ResponseStatus.Success.ToString(); return(response); } catch (Exception ex) { var response = new ResponseData <ApplianceDTO>(); response.Message = ex.Message; response.Status = ResponseStatus.Error.ToString(); return(response); } }