feat: Adds capacitor calculations.
This commit is contained in:
121
Sources/Routes/Models/Capacitor.swift
Normal file
121
Sources/Routes/Models/Capacitor.swift
Normal file
@@ -0,0 +1,121 @@
|
||||
public enum Capacitor {
|
||||
|
||||
public enum Mode: String, CaseIterable, Codable, Equatable, Sendable {
|
||||
case size
|
||||
case test
|
||||
}
|
||||
|
||||
public enum Request: Codable, Equatable, Sendable {
|
||||
|
||||
case size(SizeRequest)
|
||||
case test(TestRequest)
|
||||
|
||||
public struct SizeRequest: Codable, Equatable, Sendable {
|
||||
|
||||
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 {
|
||||
|
||||
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
|
||||
@@ -23,6 +23,7 @@ public extension SiteRoute {
|
||||
|
||||
enum Api: Equatable, Sendable {
|
||||
|
||||
case calculateCapacitor(Capacitor.Request)
|
||||
case calculateDehumidifierSize(DehumidifierSize.Request)
|
||||
case calculateHVACSystemPerformance(HVACSystemPerformance.Request)
|
||||
case calculateMoldRisk(MoldRisk.Request)
|
||||
@@ -31,6 +32,16 @@ public extension SiteRoute {
|
||||
static let rootPath = Path { "api"; "v1" }
|
||||
|
||||
public static let router = OneOf {
|
||||
Route(.case(Self.calculateCapacitor)) {
|
||||
Path { "api"; "v1"; "calculateRoomPressure" }
|
||||
Method.post
|
||||
OneOf {
|
||||
Body(.json(Capacitor.Request.SizeRequest.self))
|
||||
.map(.case(Capacitor.Request.size))
|
||||
Body(.json(Capacitor.Request.TestRequest.self))
|
||||
.map(.case(Capacitor.Request.test))
|
||||
}
|
||||
}
|
||||
Route(.case(Self.calculateDehumidifierSize)) {
|
||||
Path { "api"; "v1"; "calculateDehumidifierSize" }
|
||||
Method.post
|
||||
@@ -64,6 +75,7 @@ public extension SiteRoute {
|
||||
enum View: Equatable, Sendable {
|
||||
|
||||
case index
|
||||
case capacitor(Capacitor)
|
||||
case dehumidifierSize(DehumidifierSize)
|
||||
case hvacSystemPerformance(HVACSystemPerformance)
|
||||
case moldRisk(MoldRisk)
|
||||
@@ -73,6 +85,9 @@ public extension SiteRoute {
|
||||
Route(.case(Self.index)) {
|
||||
Method.get
|
||||
}
|
||||
Route(.case(Self.capacitor)) {
|
||||
Capacitor.router
|
||||
}
|
||||
Route(.case(Self.dehumidifierSize)) {
|
||||
DehumidifierSize.router
|
||||
}
|
||||
@@ -87,6 +102,50 @@ public extension SiteRoute {
|
||||
}
|
||||
}
|
||||
|
||||
public enum Capacitor: Equatable, Sendable {
|
||||
case index(mode: Routes.Capacitor.Mode? = nil)
|
||||
case submit(Routes.Capacitor.Request)
|
||||
|
||||
public static var index: Self { .index() }
|
||||
|
||||
static let rootPath = "capacitor-calculator"
|
||||
|
||||
public static let router = OneOf {
|
||||
Route(.case(Self.index)) {
|
||||
Path { rootPath }
|
||||
Method.get
|
||||
Query {
|
||||
Optionally { Field("mode") { Routes.Capacitor.Mode.parser() } }
|
||||
}
|
||||
}
|
||||
Route(.case(Self.submit)) {
|
||||
Path { rootPath }
|
||||
Method.post
|
||||
Body {
|
||||
OneOf {
|
||||
FormData {
|
||||
Field("runningAmps") { Double.parser() }
|
||||
Field("lineVoltage") { Double.parser() }
|
||||
Field("powerFactor") { Double.parser() }
|
||||
}
|
||||
.map(.memberwise(Routes.Capacitor.Request.SizeRequest.init))
|
||||
.map(.case(Routes.Capacitor.Request.size))
|
||||
|
||||
FormData {
|
||||
Field("startWindingAmps") { Double.parser() }
|
||||
Field("runToCommonVoltage") { Double.parser() }
|
||||
Optionally {
|
||||
Field("ratedCapacitorSize") { Int.parser() }
|
||||
}
|
||||
}
|
||||
.map(.memberwise(Routes.Capacitor.Request.TestRequest.init))
|
||||
.map(.case(Routes.Capacitor.Request.test))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public enum DehumidifierSize: Equatable, Sendable {
|
||||
case index
|
||||
case submit(Routes.DehumidifierSize.Request)
|
||||
|
||||
Reference in New Issue
Block a user