Repository Public
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project : MacLustr SMS Gateway9// File : ios/MacLustrSMS/Core/PollSchedule.swift10// Description : Cadence de rechargement du suivi d'un envoi, indépendante11// du flux temps réel. Pure logique, testée.12// Module : ios13// Repository : https://git.spboucher.ai/maclustr-sms14// Project Website : https://sms.spboucher.ai15// Created : 2026-12-0116// Last Modified : 2026-12-0117//18// =============================================================================19// AUTHOR & PROFESSIONAL AFFILIATION20// =============================================================================21//22// Author : Simon-Pierre Boucher23// Position : Professor of Finance24// Institution : Université du Québec en Outaouais (UQO)25// Department : Département des sciences administratives26// Organization : SP Boucher27//28// Website : https://spboucher.ai29// Git : https://git.spboucher.ai30//31// =============================================================================32// OFFICIAL CONTACT DIRECTORY33// =============================================================================34//35// General : info@spboucher.ai36// Professional : contact@spboucher.ai37// Direct : simon@spboucher.ai38// Research : research@spboucher.ai39// Development : dev@spboucher.ai40// Support : support@spboucher.ai41// Security/Admin : admin@spboucher.ai42// Notifications : notifications@spboucher.ai43//44// Preferred Contact: admin@spboucher.ai45//46// =============================================================================47// COPYRIGHT & INTELLECTUAL PROPERTY48// =============================================================================49//50// Copyright (c) 202651// Simon-Pierre Boucher52//53// All rights reserved where applicable.54//55// This file is governed by the applicable project license,56// copyright notices, institutional agreements and relevant law.57//58// Unless expressly authorized by the applicable license or59// rights holder, copying, redistribution, sublicensing,60// publication and commercial exploitation are not permitted.61//62// Existing third-party rights and notices remain unaffected.63//64// =============================================================================65// CONFIDENTIALITY & SECURITY66// =============================================================================67//68// Classification : Public69// Distribution : Public repository; use is governed by the project license70// Security Contact : admin@spboucher.ai71//72// Access to this source does not independently grant permission73// to redistribute or disclose confidential materials.74//75// Do not embed credentials, private keys, access tokens,76// certificates or confidential personal information.77//78// =============================================================================79// LICENSE & MAINTENANCE80// =============================================================================81//82// License : None specified (no license file in this repository)83// Maintainer : Simon-Pierre Boucher84// Technical Contact: dev@spboucher.ai85//86// =============================================================================87// END OF SP BOUCHER SOURCE HEADER88// =============================================================================8990import Foundation9192/// Le suivi ne dépend jamais du seul flux temps réel : il recharge le message lui-même.93/// - tout de suite, puis chaque seconde pendant 15 s (tout se joue souvent en moins d'une seconde) ;94/// - ensuite toutes les 5 s tant que l'état n'est pas final ;95/// - « remis au réseau » est stable : on continue 60 s au cas où un accusé de livraison arriverait, puis on s'arrête ;96/// - un état final arrête le sondage.97enum PollSchedule {98 static let fastWindow: TimeInterval = 1599 static let fastInterval: TimeInterval = 1100 static let slowInterval: TimeInterval = 5101 static let submittedGrace: TimeInterval = 60102 /// Au-delà, on n'insiste plus même sans issue (le flux temps réel et le retour au premier plan prennent le relais).103 static let hardStop: TimeInterval = 15 * 60104105 /// Délai avant le prochain rechargement, ou nil s'il faut arrêter.106 static func nextDelay(status: MessageStatus, elapsed: TimeInterval, sinceSubmitted: TimeInterval?) -> TimeInterval? {107 if status.isFinal { return nil }108 if elapsed >= hardStop { return nil }109 if status == .submitted {110 guard let s = sinceSubmitted, s < submittedGrace else { return nil }111 return elapsed < fastWindow ? fastInterval : slowInterval112 }113 return elapsed < fastWindow ? fastInterval : slowInterval114 }115 /// Une liste (historique) mérite d'être rechargée tant qu'un message récent n'a pas d'issue.116 static func listNeedsPolling(_ messages: [SMSMessage], now: Date = .now) -> Bool {117 messages.contains { $0.isOutbound && !$0.status.isFinal && $0.status != .submitted && now.timeIntervalSince($0.createdAt) < hardStop }118 }119}120121/// Présentation de l'accusé de livraison : jamais « livré » sans rapport de l'opérateur, jamais d'attente sans fin.122enum DeliveryReceipt: Equatable {123 case confirmed(Date?) // rapport reçu124 case waiting // remis au réseau depuis peu : un accusé peut encore arriver125 case notReceived // rien après 30 s : normal chez certains opérateurs126 case notApplicable // le message n'est pas (encore) parti127 static let patience: TimeInterval = 30128129 static func state(status: MessageStatus, submittedAt: Date?, deliveredAt: Date?, now: Date = .now) -> DeliveryReceipt {130 if status == .delivered { return .confirmed(deliveredAt) }131 guard status == .submitted else { return .notApplicable }132 guard let s = submittedAt else { return .waiting }133 return now.timeIntervalSince(s) >= patience ? .notReceived : .waiting134 }135 var label: String {136 switch self {137 case .confirmed: "Livré (accusé de l'opérateur)"138 case .waiting: "Accusé de livraison : en attente"139 case .notReceived: "Accusé de livraison : non reçu (certains opérateurs n'en envoient pas)"140 case .notApplicable: "Livré"141 }142 }143}144