public async Task <bool> AddAbpDevice(Program.AddAbpOptions opts, ConfigurationHelper configurationHelper) { Console.WriteLine($"Adding ABP device to IoT Hub: {opts.DevEui} ..."); bool isSuccess = false; var twinProperties = new TwinProperties(); twinProperties.Desired["AppEUI"] = opts.AppEui; twinProperties.Desired["AppKey"] = opts.AppKey; twinProperties.Desired["GatewayID"] = opts.GatewayId; twinProperties.Desired["SensorDecoder"] = opts.SensorDecoder; if (!string.IsNullOrEmpty(opts.ClassType)) { twinProperties.Desired["ClassType"] = opts.ClassType; } var twin = new Twin(); twin.Properties = twinProperties; var device = new Device(opts.DevEui); BulkRegistryOperationResult result; try { result = await configurationHelper.RegistryManager.AddDeviceWithTwinAsync(device, twin); } catch (Exception ex) { Console.WriteLine($"Error: {ex.Message}\n"); return(false); } if (result.IsSuccessful) { Console.WriteLine($"Success!\n"); isSuccess = true; } else { Console.WriteLine($"Error adding device:"); foreach (var error in result.Errors) { Console.WriteLine($"Device Id: {error.DeviceId}, Code: {error.ErrorCode}, Error: {error.ErrorStatus}"); } Console.WriteLine(); isSuccess = false; } return(isSuccess); }
public Program.AddAbpOptions CompleteMissingAbpOptionsAndClean(Program.AddAbpOptions opts) { if (string.IsNullOrEmpty(opts.DevEui)) { opts.DevEui = Keygen.Generate(16); Console.WriteLine($"Info: Generating missing DevEUI: {opts.DevEui}"); } else { opts.DevEui = ValidationHelper.CleanString(opts.DevEui); } if (string.IsNullOrEmpty(opts.AppEui)) { opts.AppEui = Keygen.Generate(16); Console.WriteLine($"Info: Generating missing AppEUI: {opts.AppEui}"); } else { opts.AppEui = ValidationHelper.CleanString(opts.AppEui); } if (string.IsNullOrEmpty(opts.AppKey)) { opts.AppKey = Keygen.Generate(16); Console.WriteLine($"Info: Generating missing AppKey: {opts.AppKey}"); } else { opts.AppKey = ValidationHelper.CleanString(opts.AppKey); } if (!string.IsNullOrEmpty(opts.GatewayId)) { opts.GatewayId = ValidationHelper.CleanString(opts.GatewayId); } if (!string.IsNullOrEmpty(opts.SensorDecoder)) { opts.SensorDecoder = ValidationHelper.CleanString(opts.SensorDecoder); } if (!string.IsNullOrEmpty(opts.ClassType)) { opts.ClassType = ValidationHelper.CleanString(opts.ClassType); } return(opts); }
public bool ValidateAbpDevice(Program.AddAbpOptions opts) { var validationError = string.Empty; bool isValid = true; if (string.IsNullOrEmpty(opts.AppEui)) { Console.WriteLine("Error: AppEUI is missing."); isValid = false; } if (!ValidationHelper.ValidateKey(opts.AppEui, 16, out validationError)) { Console.WriteLine($"Error: AppEUI is invalid. {validationError}"); isValid = false; } else { Console.WriteLine($"Info: AppEui is valid: {opts.AppEui}"); } if (string.IsNullOrEmpty(opts.AppKey)) { Console.WriteLine("Error: AppKey is missing"); isValid = false; } if (!ValidationHelper.ValidateKey(opts.AppKey, 16, out validationError)) { Console.WriteLine($"Error: AppKey is invalid. {validationError}"); isValid = false; } else { Console.WriteLine($"Info: AppKey is valid: {opts.AppKey}"); } if (!ValidationHelper.ValidateSensorDecoder(opts.SensorDecoder, out validationError)) { isValid = false; } if (!string.IsNullOrEmpty(validationError)) { Console.WriteLine(validationError); } else { Console.WriteLine($"Info: SensorDecoder is valid: {opts.SensorDecoder}"); } if (!string.IsNullOrEmpty(opts.ClassType)) { if (!string.Equals(opts.ClassType, "C", StringComparison.CurrentCultureIgnoreCase)) { isValid = false; Console.WriteLine("Error: If ClassType is set, it needs to be \"C\"."); } else { Console.WriteLine($"Info: ClassType is valid: {opts.ClassType}"); } } else { Console.WriteLine($"Info: ClassType is empty."); } return(isValid); }