Skip to content

Commit

Permalink
Enabled Strict Swift Concurrency.
Browse files Browse the repository at this point in the history
  • Loading branch information
jollyjinx committed Mar 22, 2024
1 parent 5bd4d56 commit fb1f253
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 8 deletions.
15 changes: 9 additions & 6 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version: 5.6
// swift-tools-version: 5.10
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription
Expand All @@ -20,7 +20,7 @@ let package = Package(
.package(url: "https://github.com/apple/swift-argument-parser", .upToNextMajor(from: "1.2.2")),
.package(url: "https://github.com/sroebert/mqtt-nio.git", from: "2.8.0"),
.package(url: "https://github.com/jollyjinx/JLog", .upToNextMajor(from: "0.0.5")),
.package(url: "https://github.com/jollyjinx/SwiftLibModbus", from:"2.0.0"),
.package(url: "https://github.com/jollyjinx/SwiftLibModbus", from:"2.0.2"),
],
targets: [
.executableTarget(
Expand All @@ -31,10 +31,13 @@ let package = Package(
.product(name: "JLog", package: "JLog"),
.product(name: "SwiftLibModbus", package: "SwiftLibModbus")
],
resources: [
.copy("Resources/")
]
),
resources: [
.copy("Resources/")
],
swiftSettings: [
.enableExperimentalFeature("StrictConcurrency")
]
),
.testTarget(
name: "modbus2mqttTests",
dependencies: ["modbus2mqtt"]),
Expand Down
29 changes: 27 additions & 2 deletions Sources/modbus2mqtt/ModbusDefinition.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// ModbusDefinition.swift
//

@preconcurrency import Foundation
import Foundation
import JLog
import SwiftLibModbus

Expand Down Expand Up @@ -124,10 +124,35 @@ extension ModbusDefinition
return returnValue
}

static var modbusDefinitions: [Int: ModbusDefinition]!
private static let modbusDefinitionStore = ModbusDefinitionStore()

static var modbusDefinitions:[Int: ModbusDefinition]
{
get { modbusDefinitionStore.definitions }
set { modbusDefinitionStore.definitions = newValue }
}

}

extension ModbusDefinition
{
var hasFactor: Bool { factor != nil && factor! != 0 && factor! != 1 }
}


private final class ModbusDefinitionStore : @unchecked Sendable
{
private let lock = NSLock()
private var _modbusDefinitions: [Int: ModbusDefinition] = [:]

var definitions: [Int: ModbusDefinition]
{
get { lock.lock(); defer { lock.unlock() }; return _modbusDefinitions }
set { lock.lock(); defer { lock.unlock() };_modbusDefinitions = newValue }
}

init()
{

}
}
2 changes: 2 additions & 0 deletions Sources/modbus2mqtt/modbus2mqtt.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ struct modbus2mqtt: AsyncParsableCommand
@Option(name: .long, help: "Modbus Device Description file (JSON).")
var deviceDescriptionFile = "sma.sunnyboy.json"

@MainActor
func run() async throws
{
JLog.loglevel = logLevel
Expand Down Expand Up @@ -151,6 +152,7 @@ func handleSIGUSR1(signal: Int32)
}
}

@MainActor
func startServing(modbusDevice: ModbusDevice, mqttServer: JNXMQTTServer, options: modbus2mqtt) async throws
{
let deviceDescriptionURL = try fileURLFromPath(path: options.deviceDescriptionFile)
Expand Down

0 comments on commit fb1f253

Please sign in to comment.