Skip to content

Commit

Permalink
Updated to use Strict Concurrency
Browse files Browse the repository at this point in the history
  • Loading branch information
jollyjinx committed Mar 22, 2024
1 parent 80dfe59 commit 6505e8b
Show file tree
Hide file tree
Showing 21 changed files with 89 additions and 104 deletions.
116 changes: 56 additions & 60 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,65 +1,61 @@
// swift-tools-version:5.7
// swift-tools-version:5.10
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "sma2mqtt",
platforms: [
.macOS(.v13)
// .iOS(.v13),
// .tvOS(.v13),
// .watchOS(.v6)
],
products: [
.executable(name: "sma2mqtt", targets: ["sma2mqtt"]),
.library(name: "sma2mqttLibrary", targets: ["sma2mqttLibrary"]),
],
dependencies: [
.package(url: "https://github.com/nicklockwood/SwiftFormat", .upToNextMajor(from: "0.51.12")),
.package(url: "https://github.com/apple/swift-argument-parser", .upToNextMajor(from: "1.2.2")),
.package(url: "https://github.com/swift-server/async-http-client.git", from: "1.18.0"),
.package(url: "https://github.com/swift-server-community/mqtt-nio", .upToNextMajor(from: "2.8.0")),
.package(url: "https://github.com/jollyjinx/BinaryCoder", .upToNextMajor(from: "2.3.1")),
.package(url: "https://github.com/jollyjinx/JLog", .upToNextMajor(from: "0.0.5")),
],
targets: [
.executableTarget(
name: "sma2mqtt",
dependencies: [
"sma2mqttLibrary",
.product(name: "ArgumentParser", package: "swift-argument-parser"),
.product(name: "JLog", package: "JLog"),
]
),
.target(
name: "sma2mqttLibrary",
dependencies: [
"CNative",
.product(name: "BinaryCoder", package: "BinaryCoder"),
.product(name: "AsyncHTTPClient", package: "async-http-client"),
.product(name: "MQTTNIO", package: "mqtt-nio"),
.product(name: "JLog", package: "JLog"),
let package = Package(name: "sma2mqtt",
platforms: [
.macOS(.v13),
// .iOS(.v13),
// .tvOS(.v13),
// .watchOS(.v6)
],
products: [
.executable(name: "sma2mqtt", targets: ["sma2mqtt"]),
.library(name: "sma2mqttLibrary", targets: ["sma2mqttLibrary"]),
],
dependencies: [
.package(url: "https://github.com/nicklockwood/SwiftFormat", .upToNextMajor(from: "0.51.12")),
.package(url: "https://github.com/apple/swift-argument-parser", .upToNextMajor(from: "1.2.2")),
.package(url: "https://github.com/swift-server/async-http-client.git", from: "1.18.0"),
.package(url: "https://github.com/swift-server-community/mqtt-nio", .upToNextMajor(from: "2.8.0")),
.package(url: "https://github.com/jollyjinx/BinaryCoder", .upToNextMajor(from: "2.3.1")),
.package(url: "https://github.com/jollyjinx/JLog", .upToNextMajor(from: "0.0.5")),
],
targets: [
.executableTarget(name: "sma2mqtt",
dependencies: [
"sma2mqttLibrary",
.product(name: "ArgumentParser", package: "swift-argument-parser"),
.product(name: "JLog", package: "JLog"),
],
swiftSettings: [
.enableExperimentalFeature("StrictConcurrency"),
]),
.target(name: "sma2mqttLibrary",
dependencies: [
"CNative",
.product(name: "BinaryCoder", package: "BinaryCoder"),
.product(name: "AsyncHTTPClient", package: "async-http-client"),
.product(name: "MQTTNIO", package: "mqtt-nio"),
.product(name: "JLog", package: "JLog"),

],
resources: [
.copy("Obis/Resources/obisdefinition.json"),
.copy("DataObjects/Resources/sma.data.objectMetaData.json"),
.copy("DataObjects/Resources/sma.data.Translation_Names.json"),
.copy("SMAPacket/Resources/SMANetPacketDefinitions.json"),
]
),
.target(
name: "CNative",
dependencies: []
),
.testTarget(
name: "sma2mqttTests",
dependencies: [
"sma2mqttLibrary",
.product(name: "BinaryCoder", package: "BinaryCoder"),
.product(name: "JLog", package: "JLog"),
]
),
]
)
],
resources: [
.copy("Obis/Resources/obisdefinition.json"),
.copy("DataObjects/Resources/sma.data.objectMetaData.json"),
.copy("DataObjects/Resources/sma.data.Translation_Names.json"),
.copy("SMAPacket/Resources/SMANetPacketDefinitions.json"),
],
swiftSettings: [
.enableExperimentalFeature("StrictConcurrency"),
]),
.target(name: "CNative",
dependencies: []),
.testTarget(name: "sma2mqttTests",
dependencies: [
"sma2mqttLibrary",
.product(name: "BinaryCoder", package: "BinaryCoder"),
.product(name: "JLog", package: "JLog"),
]),
])
6 changes: 5 additions & 1 deletion Sources/sma2mqtt/sma2mqtt.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ extension JLog.Level: ExpressibleByArgument {}
let defaultLoglevel: JLog.Level = .notice
#endif

@MainActor
var globalLighthouse: SMALighthouse?

@main struct sma2mqtt: AsyncParsableCommand
@main
struct sma2mqtt: AsyncParsableCommand
{
@Option(help: "Set the log level.") var logLevel: JLog.Level = defaultLoglevel

Expand Down Expand Up @@ -49,6 +51,8 @@ var globalLighthouse: SMALighthouse?
"battery/battery-charge/battery-charge:20",
// "*:600", // all once
]

@MainActor
func run() async throws
{
JLog.loglevel = logLevel
Expand Down
2 changes: 1 addition & 1 deletion Sources/sma2mqttLibrary/DataObjects/SMADataObject.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Foundation
import JLog
import RegexBuilder

public struct SMADataObject
public struct SMADataObject: Sendable
{
let object: Int
let lri: Int
Expand Down
7 changes: 5 additions & 2 deletions Sources/sma2mqttLibrary/DataObjects/SMATagTranslator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import Foundation
import JLog

struct SMATagTranslator
struct SMATagTranslator: Sendable
{
typealias ObjectIDString = String

Expand All @@ -18,8 +18,11 @@ struct SMATagTranslator
let path: String
let format: Int
}
}

static var shared: SMATagTranslator = .init(definitionData: nil, translationData: nil)
extension SMATagTranslator
{
static let shared: SMATagTranslator = .init(definitionData: nil, translationData: nil)

init(definitionData: Data?, translationData: Data?)
{
Expand Down
2 changes: 1 addition & 1 deletion Sources/sma2mqttLibrary/Obis/ObisDefinition.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import Foundation
import JLog

struct ObisDefinition: Encodable, Decodable
struct ObisDefinition: Encodable, Decodable, Sendable
{
enum ObisDefinitionType: String, Encodable, Decodable
{
Expand Down
6 changes: 3 additions & 3 deletions Sources/sma2mqttLibrary/Obis/ObisValue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@ import BinaryCoder
import Foundation
import JLog

public enum ObisType: Encodable, Decodable
public enum ObisType: Encodable, Decodable, Sendable
{
case string(String)
case uint(UInt64)
case int(Int64)
}

public struct ObisValue: Decodable
public struct ObisValue: Decodable, Sendable
{
let id: String
let value: ObisType

public var includeTopicInJSON = false
public var topic: String { ObisDefinition.obisDefinitions[id]?.topic ?? "id/\(id)" }

public enum MQTTVisibilty: String, Encodable, Decodable { case invisible, visible, retained }
public enum MQTTVisibilty: String, Encodable, Decodable, Sendable { case invisible, visible, retained }

public var mqtt: MQTTVisibilty { ObisDefinition.obisDefinitions[id]?.mqtt ?? .invisible }
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/sma2mqttLibrary/SMADevice.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import RegexBuilder

public typealias ObjectId = String

public actor SMADevice
public actor SMADevice: Sendable
{
let id = UUID().uuidString
let initDate = Date()
Expand Down
2 changes: 1 addition & 1 deletion Sources/sma2mqttLibrary/SMALighthouse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import Foundation
import JLog

public enum UserRight: String
public enum UserRight: String, Sendable
{
case user = "usr"
case installer = "istl"
Expand Down
2 changes: 1 addition & 1 deletion Sources/sma2mqttLibrary/SMAPacket/SMANetPacket.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import BinaryCoder
import Foundation
import JLog

public struct SMANetPacket: Codable
public struct SMANetPacket: Codable, Sendable
{
let header: SMANetPacketHeader
let valuesheader: [Int]
Expand Down
2 changes: 1 addition & 1 deletion Sources/sma2mqttLibrary/SMAPacket/SMANetPacketValue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import BinaryCoder
import Foundation
import JLog

public struct SMANetPacketValue
public struct SMANetPacketValue: Sendable
{
let number: UInt8
let address: UInt16
Expand Down
2 changes: 1 addition & 1 deletion Sources/sma2mqttLibrary/SMAPacket/SMAPacket.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import BinaryCoder
import Foundation
import JLog

public struct SMAPacket: Codable
public struct SMAPacket: Codable, Sendable
{
enum MagicHeader: UInt32
{
Expand Down
4 changes: 3 additions & 1 deletion Sources/sma2mqttLibrary/SMAPacket/SMAPacketGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import JLog
import RegexBuilder

struct SMAPacketGenerator {}
extension SMAPacketGenerator: Sendable {}

extension SMAPacketGenerator
{
Expand Down Expand Up @@ -72,9 +73,10 @@ extension SMAPacketGenerator
return smapacket // .hexStringToData()
}

private static let regex: Regex = { #/([a-fA-F\d]{2})([a-fA-F\d]{2})_([a-fA-F\d]{2})([a-fA-F\d]{2})([a-fA-F\d]{2})([a-fA-F\d]{2})/# }()
static func objectID2Command(objectId: String) throws -> String
{
let regex: Regex = { #/([a-fA-F\d]{2})([a-fA-F\d]{2})_([a-fA-F\d]{2})([a-fA-F\d]{2})([a-fA-F\d]{2})([a-fA-F\d]{2})/# }()

if let match = objectId.firstMatch(of: regex)
{
let (_, a, b, c, d, e, f) = match.output
Expand Down
2 changes: 1 addition & 1 deletion Sources/sma2mqttLibrary/SMAPacket/SMATagPacket.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import BinaryCoder
import Foundation
import JLog

public struct SMATagPacket: Codable
public struct SMATagPacket: Codable, Sendable
{
let tag: UInt16
let data: Data
Expand Down
2 changes: 1 addition & 1 deletion Sources/sma2mqttLibrary/Tools/DatedQueue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import Foundation

struct DatedQueue<Element: Identifiable> where Element: Codable
struct DatedQueue<Element: Identifiable> where Element: Codable, Element: Sendable
{
internal struct InternalElement: Codable
{
Expand Down
4 changes: 2 additions & 2 deletions Sources/sma2mqttLibrary/Tools/HTTPClientProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import Foundation
// import NIOHTTP1
import NIOSSL

enum HTTPClientProvider
enum HTTPClientProvider: Sendable
{
static var sharedHttpClient: HTTPClient = { var tlsConfiguration = TLSConfiguration.makeClientConfiguration()
static let sharedHttpClient: HTTPClient = { var tlsConfiguration = TLSConfiguration.makeClientConfiguration()
tlsConfiguration.certificateVerification = .none

return HTTPClient(eventLoopGroupProvider: .createNew, configuration: .init(tlsConfiguration: tlsConfiguration,
Expand Down
2 changes: 1 addition & 1 deletion Sources/sma2mqttLibrary/Tools/IntervalLoop.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import Foundation

public class IntervalLoop
public actor IntervalLoop
{
public let loopTime: Double
private var lastWorkDate: Date = .distantPast
Expand Down
2 changes: 1 addition & 1 deletion Sources/sma2mqttLibrary/Tools/MQTTPublisher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import JLog
import MQTTNIO
import NIO

public protocol SMAPublisher
public protocol SMAPublisher: Sendable
{
func publish(to topic: String, payload: String, qos: MQTTQoS, retain: Bool) async throws
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/sma2mqttLibrary/Tools/MutlicastReceiver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public protocol UDPEmitter
func sendPacket(data: [UInt8], packetcounter: Int, address: String, port: UInt16) async
}

public struct Packet
public struct Packet: Sendable
{
let data: Data
let sourceAddress: String
Expand Down
2 changes: 1 addition & 1 deletion Sources/sma2mqttLibrary/Tools/QueryQueue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ extension ConnectionState
}
}

struct QueryQueue
struct QueryQueue: Sendable
{
let address: String
let minimumRequestInterval: TimeInterval
Expand Down
4 changes: 2 additions & 2 deletions Sources/sma2mqttLibrary/Tools/SMAUDPPort.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import CNative
// let sourceAddress: String
// }

private enum UDPReceiverError: Error
private enum UDPReceiverError: Error, Sendable
{
case socketCreationFailed(Int32)
case socketOptionReuseAddressFailed(Int32)
Expand All @@ -30,7 +30,7 @@ private enum UDPReceiverError: Error
case timeoutErrorReceived([SMAPacket])
}

class SMAUDPPort: UDPEmitter
actor SMAUDPPort: UDPEmitter
{
private let socketFileDescriptor: Int32
private let bufferSize: Int
Expand Down
20 changes: 0 additions & 20 deletions Tests/sma2mqttTests/sma2mqttTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,26 +93,6 @@ final class sma2mqttTests: XCTestCase

func testSMAPacketGeneration() throws
{
let data1 = """
534d 4100
0004 02a0
0000 0001
0026 0010
6065
09
a0
ffff ffff ffff 00
00
7401 10e7 f0b2 00
00
0000
0000
60e2 0002 8061 0048 2100 ff4a 4100
0000 0000
"""

let dataString = try SMAPacketGenerator.generatePacketForObjectID(packetcounter: 1, objectID: "6180_08414E00")
let data = dataString.hexStringToData()

Expand Down

0 comments on commit 6505e8b

Please sign in to comment.