126 lines
3.4 KiB
Swift
126 lines
3.4 KiB
Swift
public enum Capacitor {
|
|
|
|
public static let description = """
|
|
Calculate run capacitor values based on electrical measurements.
|
|
"""
|
|
|
|
public enum Mode: String, CaseIterable, Codable, Equatable, Sendable, Hashable {
|
|
case test
|
|
case size
|
|
}
|
|
|
|
public enum Request: Codable, Equatable, Sendable, Hashable {
|
|
|
|
case size(SizeRequest)
|
|
case test(TestRequest)
|
|
|
|
public struct SizeRequest: Codable, Equatable, Sendable, Hashable {
|
|
|
|
public let runningAmps: Double
|
|
public let lineVoltage: Double
|
|
public let powerFactor: Double
|
|
|
|
public init(runningAmps: Double, lineVoltage: Double, powerFactor: Double) {
|
|
self.runningAmps = runningAmps
|
|
self.lineVoltage = lineVoltage
|
|
self.powerFactor = powerFactor
|
|
}
|
|
}
|
|
|
|
public struct TestRequest: Codable, Equatable, Sendable, Hashable {
|
|
|
|
public let startWindingAmps: Double
|
|
public let runToCommonVoltage: Double
|
|
public let ratedCapacitorSize: Int?
|
|
|
|
public init(startWindingAmps: Double, runToCommonVoltage: Double, ratedCapacitorSize: Int? = nil) {
|
|
self.startWindingAmps = startWindingAmps
|
|
self.runToCommonVoltage = runToCommonVoltage
|
|
self.ratedCapacitorSize = ratedCapacitorSize
|
|
}
|
|
}
|
|
}
|
|
|
|
public enum Response: Codable, Equatable, Sendable {
|
|
|
|
case size(result: SizeResponse)
|
|
case test(result: TestResponse)
|
|
|
|
public struct SizeResponse: Codable, Equatable, Sendable {
|
|
|
|
public let capacitance: Double
|
|
public let standardSize: Double
|
|
public let tolerance: Tolerance
|
|
|
|
public init(capacitance: Double, standardSize: Double, tolerance: Capacitor.Tolerance) {
|
|
self.capacitance = capacitance
|
|
self.standardSize = standardSize
|
|
self.tolerance = tolerance
|
|
}
|
|
}
|
|
|
|
public struct TestResponse: Codable, Equatable, Sendable {
|
|
|
|
public let capacitance: Double
|
|
public let tolerance: Tolerance
|
|
public let ratedComparison: RatedComparison?
|
|
|
|
public init(
|
|
capacitance: Double,
|
|
tolerance: Capacitor.Tolerance,
|
|
ratedComparison: Capacitor.RatedComparison? = nil
|
|
) {
|
|
self.capacitance = capacitance
|
|
self.tolerance = tolerance
|
|
self.ratedComparison = ratedComparison
|
|
}
|
|
}
|
|
}
|
|
|
|
public struct Tolerance: Codable, Equatable, Sendable {
|
|
|
|
public let minimum: Double
|
|
public let maximum: Double
|
|
|
|
public init(minimum: Double, maximum: Double) {
|
|
self.minimum = minimum
|
|
self.maximum = maximum
|
|
}
|
|
}
|
|
|
|
public struct RatedComparison: Codable, Equatable, Sendable {
|
|
|
|
public let value: Int
|
|
public let isInRange: Bool
|
|
public let percentDeviation: Double
|
|
|
|
public init(value: Int, isInRange: Bool, percentDeviation: Double) {
|
|
self.value = value
|
|
self.isInRange = isInRange
|
|
self.percentDeviation = percentDeviation
|
|
}
|
|
}
|
|
}
|
|
|
|
#if DEBUG
|
|
public extension Capacitor.Response {
|
|
|
|
static func mock(mode: Capacitor.Mode) -> Self {
|
|
switch mode {
|
|
case .size:
|
|
return .size(result: .init(
|
|
capacitance: 57.5,
|
|
standardSize: 60,
|
|
tolerance: .init(minimum: 54.1, maximum: 61)
|
|
))
|
|
case .test:
|
|
return .test(result: .init(
|
|
capacitance: 34.8,
|
|
tolerance: .init(minimum: 32.7, maximum: 36.9),
|
|
ratedComparison: .init(value: 35, isInRange: Bool.random(), percentDeviation: 0.6)
|
|
))
|
|
}
|
|
}
|
|
}
|
|
#endif
|