Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private let queue = DispatchQueue(label: "gcdwebserver_queue")
- private func setupServer(){
- webServer.delegate = self
- webServer.addDefaultHandler(forMethod: "GET", request: GCDWebServerRequest.self) { (req, completion) in
- if let resp = self.response(for: req) {
- return completion(resp)
- }
- }
- queue.async {
- self.webServer.start(withPort: 8521, bonjourName: "GCD Web Server")
- }
- }
- private let modbusQueue = DispatchQueue(label: "modbus_queue")
- private func initiateTimer() {
- polling()
- timer?.invalidate()
- if #available(OSX 10.12, *) {
- timer = Timer.scheduledTimer(withTimeInterval: pollingInterval, repeats: true) { (_) in
- self.polling()
- }
- } else {
- timer = Timer.scheduledTimer(timeInterval: pollingInterval, target: self, selector: #selector(polling), userInfo: nil, repeats: true)
- }
- }
- @objc private func polling() {
- for device in self.devices {
- if !device.isInProcess && device.isEnabled {
- self.modbusQueue.async {
- self.connectAndReadValues(device: device)
- }
- }
- }
- }
- private func connectAndReadValues(device: Device) {
- device.isInProcess = true
- let connect = device.modbus.connect()
- //handling connection status
- //...
- readValues(forDevice: device)
- }
- private func readValues(forDevice device: Device){
- //some constants
- do {
- let registers = try device.modbus.readRegisters(from: startAddress, count: registersCount)
- device.readState = .success
- device.modbus.close()
- //parse and save data to the app memory just as a dictionary. It saves only one small dictionary per device
- parseRegisters(controllerIP: device.modbus.host, vendor: vendor, registers: registers, startAdd: startAddress)
- } catch let error {
- //handling errors
- }
- //refreshing interface in the main queue
- }
Add Comment
Please, Sign In to add comment