Skip to content

Commit d584b8c

Browse files
committed
Circuit Sim has no internet checker, updates
1 parent e8d12e9 commit d584b8c

File tree

9 files changed

+202
-8
lines changed

9 files changed

+202
-8
lines changed

SciTool.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
589EE55D27BD316300D38DF2 /* blank.png in Resources */ = {isa = PBXBuildFile; fileRef = 589EE55C27BD316300D38DF2 /* blank.png */; };
1414
589EE56027BDA78900D38DF2 /* MasterViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 589EE55E27BDA78900D38DF2 /* MasterViewController.swift */; };
1515
589EE56127BDA78900D38DF2 /* SideBarController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 589EE55F27BDA78900D38DF2 /* SideBarController.swift */; };
16+
58AB347C27F102F80082AA54 /* Reachability.swift in Sources */ = {isa = PBXBuildFile; fileRef = 58AB347B27F102F80082AA54 /* Reachability.swift */; };
1617
58D9049027B17A5C0051C600 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 58D9048F27B17A5C0051C600 /* AppDelegate.swift */; };
1718
58D9049227B17A5C0051C600 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 58D9049127B17A5C0051C600 /* ViewController.swift */; };
1819
58D9049427B17A5D0051C600 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 58D9049327B17A5D0051C600 /* Assets.xcassets */; };
@@ -28,6 +29,7 @@
2829
589EE55C27BD316300D38DF2 /* blank.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = blank.png; sourceTree = "<group>"; };
2930
589EE55E27BDA78900D38DF2 /* MasterViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MasterViewController.swift; sourceTree = "<group>"; };
3031
589EE55F27BDA78900D38DF2 /* SideBarController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SideBarController.swift; sourceTree = "<group>"; };
32+
58AB347B27F102F80082AA54 /* Reachability.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Reachability.swift; sourceTree = "<group>"; };
3133
58D9048C27B17A5C0051C600 /* SciTool.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = SciTool.app; sourceTree = BUILT_PRODUCTS_DIR; };
3234
58D9048F27B17A5C0051C600 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
3335
58D9049127B17A5C0051C600 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = "<group>"; };
@@ -80,6 +82,7 @@
8082
58D904A327B17FB80051C600 /* voltage.py */,
8183
589EE55C27BD316300D38DF2 /* blank.png */,
8284
5804211227D799BE00126EC3 /* voltage3D.py */,
85+
58AB347B27F102F80082AA54 /* Reachability.swift */,
8386
);
8487
path = SciTool;
8588
sourceTree = "<group>";
@@ -187,6 +190,7 @@
187190
files = (
188191
589EE56027BDA78900D38DF2 /* MasterViewController.swift in Sources */,
189192
58D9049227B17A5C0051C600 /* ViewController.swift in Sources */,
193+
58AB347C27F102F80082AA54 /* Reachability.swift in Sources */,
190194
581FF40027E8FB5E00EC1D62 /* DependenciesViewController.swift in Sources */,
191195
589EE56127BDA78900D38DF2 /* SideBarController.swift in Sources */,
192196
58D9049027B17A5C0051C600 /* AppDelegate.swift in Sources */,

SciTool/.DS_Store

6 KB
Binary file not shown.
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
{
2+
"images" : [
3+
{
4+
"idiom" : "universal",
5+
"scale" : "1x"
6+
},
7+
{
8+
"appearances" : [
9+
{
10+
"appearance" : "luminosity",
11+
"value" : "light"
12+
}
13+
],
14+
"filename" : "NotConnectLight.png",
15+
"idiom" : "universal",
16+
"scale" : "1x"
17+
},
18+
{
19+
"appearances" : [
20+
{
21+
"appearance" : "luminosity",
22+
"value" : "dark"
23+
}
24+
],
25+
"filename" : "NotConnectDark.png",
26+
"idiom" : "universal",
27+
"scale" : "1x"
28+
},
29+
{
30+
"idiom" : "universal",
31+
"scale" : "2x"
32+
},
33+
{
34+
"appearances" : [
35+
{
36+
"appearance" : "luminosity",
37+
"value" : "light"
38+
}
39+
],
40+
"idiom" : "universal",
41+
"scale" : "2x"
42+
},
43+
{
44+
"appearances" : [
45+
{
46+
"appearance" : "luminosity",
47+
"value" : "dark"
48+
}
49+
],
50+
"idiom" : "universal",
51+
"scale" : "2x"
52+
},
53+
{
54+
"idiom" : "universal",
55+
"scale" : "3x"
56+
},
57+
{
58+
"appearances" : [
59+
{
60+
"appearance" : "luminosity",
61+
"value" : "light"
62+
}
63+
],
64+
"idiom" : "universal",
65+
"scale" : "3x"
66+
},
67+
{
68+
"appearances" : [
69+
{
70+
"appearance" : "luminosity",
71+
"value" : "dark"
72+
}
73+
],
74+
"idiom" : "universal",
75+
"scale" : "3x"
76+
}
77+
],
78+
"info" : {
79+
"author" : "xcode",
80+
"version" : 1
81+
}
82+
}
Loading
Loading

SciTool/Base.lproj/Main.storyboard

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2717,22 +2717,40 @@
27172717
<action selector="reload:" target="2zw-0e-OdV" id="gAO-If-vhK"/>
27182718
</connections>
27192719
</button>
2720-
<progressIndicator ambiguous="YES" maxValue="100" style="bar" translatesAutoresizingMaskIntoConstraints="NO" id="1N0-7h-kzF">
2721-
<rect key="frame" x="46" y="374" width="302" height="20"/>
2720+
<progressIndicator maxValue="100" style="bar" translatesAutoresizingMaskIntoConstraints="NO" id="1N0-7h-kzF">
2721+
<rect key="frame" x="46" y="374" width="390" height="20"/>
27222722
</progressIndicator>
2723+
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="oQA-9z-ddy">
2724+
<rect key="frame" x="442" y="376" width="119" height="16"/>
2725+
<textFieldCell key="cell" lineBreakMode="clipping" title="(Internet Required)" id="S3B-cI-QmU">
2726+
<font key="font" metaFont="system"/>
2727+
<color key="textColor" name="labelColor" catalog="System" colorSpace="catalog"/>
2728+
<color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
2729+
</textFieldCell>
2730+
</textField>
2731+
<imageView horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="csZ-3a-CiK">
2732+
<rect key="frame" x="0.0" y="0.0" width="571" height="367"/>
2733+
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
2734+
<imageCell key="cell" refusesFirstResponder="YES" alignment="left" imageScaling="proportionallyUpOrDown" image="NotConnect" id="tr5-n7-U95"/>
2735+
</imageView>
27232736
</subviews>
27242737
<constraints>
27252738
<constraint firstItem="kef-tc-uaT" firstAttribute="leading" secondItem="cq5-NH-AVn" secondAttribute="leading" id="05a-Uh-nUR"/>
2739+
<constraint firstItem="oQA-9z-ddy" firstAttribute="leading" secondItem="1N0-7h-kzF" secondAttribute="trailing" constant="8" symbolic="YES" id="OdT-VQ-caC"/>
27262740
<constraint firstItem="QjO-Rn-4a0" firstAttribute="top" secondItem="cq5-NH-AVn" secondAttribute="top" constant="6" id="Rc6-6a-7YH"/>
27272741
<constraint firstAttribute="trailing" secondItem="kef-tc-uaT" secondAttribute="trailing" id="WiD-ch-dc4"/>
27282742
<constraint firstAttribute="bottom" secondItem="kef-tc-uaT" secondAttribute="bottom" id="X7a-Q3-C2F"/>
2743+
<constraint firstItem="kef-tc-uaT" firstAttribute="top" secondItem="oQA-9z-ddy" secondAttribute="bottom" constant="9" id="jlF-tu-wtw"/>
27292744
<constraint firstItem="1N0-7h-kzF" firstAttribute="top" secondItem="cq5-NH-AVn" secondAttribute="top" constant="7" id="kzW-9B-trF"/>
2745+
<constraint firstItem="oQA-9z-ddy" firstAttribute="top" secondItem="cq5-NH-AVn" secondAttribute="top" constant="8" id="mKk-39-TyI"/>
27302746
<constraint firstItem="QjO-Rn-4a0" firstAttribute="leading" secondItem="cq5-NH-AVn" secondAttribute="leading" constant="6" id="pFc-pD-1tW"/>
27312747
<constraint firstItem="kef-tc-uaT" firstAttribute="top" secondItem="QjO-Rn-4a0" secondAttribute="bottom" constant="7" id="syj-Rh-eYc"/>
2748+
<constraint firstAttribute="trailing" secondItem="oQA-9z-ddy" secondAttribute="trailing" constant="12" id="xRb-Q3-Dat"/>
27322749
<constraint firstItem="1N0-7h-kzF" firstAttribute="leading" secondItem="QjO-Rn-4a0" secondAttribute="trailing" constant="10" id="xZQ-Aa-KdW"/>
27332750
</constraints>
27342751
</view>
27352752
<connections>
2753+
<outlet property="noInternetImg" destination="csZ-3a-CiK" id="FF5-dn-blD"/>
27362754
<outlet property="progbar" destination="1N0-7h-kzF" id="moW-eM-Fav"/>
27372755
<outlet property="webview" destination="kef-tc-uaT" id="piJ-fg-p4t"/>
27382756
</connections>
@@ -3115,6 +3133,7 @@
31153133
<resources>
31163134
<image name="NSAdvanced" width="32" height="32"/>
31173135
<image name="NSRefreshTemplate" width="14" height="16"/>
3136+
<image name="NotConnect" width="1920" height="1080"/>
31183137
<image name="Python" width="473" height="473"/>
31193138
<image name="PythonYellow" width="473" height="473"/>
31203139
<image name="arrow.up.forward.app" catalog="system" width="15" height="14"/>

SciTool/Reachability.swift

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//
2+
// Reachability.swift
3+
// SciTool
4+
//
5+
// Created by Marc Nich on 3/27/22.
6+
//
7+
8+
import Foundation
9+
import SystemConfiguration
10+
import SystemConfiguration
11+
12+
public class Reachability {
13+
14+
class func isConnectedToNetwork() -> Bool {
15+
16+
var zeroAddress = sockaddr_in(sin_len: 0, sin_family: 0, sin_port: 0, sin_addr: in_addr(s_addr: 0), sin_zero: (0, 0, 0, 0, 0, 0, 0, 0))
17+
zeroAddress.sin_len = UInt8(MemoryLayout.size(ofValue: zeroAddress))
18+
zeroAddress.sin_family = sa_family_t(AF_INET)
19+
20+
let defaultRouteReachability = withUnsafePointer(to: &zeroAddress) {
21+
$0.withMemoryRebound(to: sockaddr.self, capacity: 1) {zeroSockAddress in
22+
SCNetworkReachabilityCreateWithAddress(nil, zeroSockAddress)
23+
}
24+
}
25+
26+
var flags: SCNetworkReachabilityFlags = SCNetworkReachabilityFlags(rawValue: 0)
27+
if SCNetworkReachabilityGetFlags(defaultRouteReachability!, &flags) == false {
28+
return false
29+
}
30+
31+
/* Only Working for WIFI
32+
let isReachable = flags == .reachable
33+
let needsConnection = flags == .connectionRequired
34+
35+
return isReachable && !needsConnection
36+
*/
37+
38+
// Working for Cellular and WIFI
39+
let isReachable = (flags.rawValue & UInt32(kSCNetworkFlagsReachable)) != 0
40+
let needsConnection = (flags.rawValue & UInt32(kSCNetworkFlagsConnectionRequired)) != 0
41+
let ret = (isReachable && !needsConnection)
42+
43+
return ret
44+
45+
}
46+
}

SciTool/ViewController.swift

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,7 @@ class SplashViewController: NSViewController {
9999
// self.view.window?.close()
100100
// TODO
101101
}
102-
func getWifiStrength() -> Int
103-
{
104-
((CWWiFiClient.shared().interfaces()?[0].rssiValue() ?? -40)+50)/20
105-
}
102+
106103

107104

108105
}
@@ -455,30 +452,43 @@ class EquipotViewController: NSViewController {
455452
}
456453

457454

458-
class CircuitSimViewController: NSViewController, WKUIDelegate
455+
class CircuitSimViewController: NSViewController, WKUIDelegate,CWEventDelegate
459456
{
457+
@IBOutlet weak var wifiStrength: NSLevelIndicator!
460458
// https://www.falstad.com/circuit/circuitjs.html
461459
override func viewDidLoad() {
462460
// loaeded
461+
checkInternet()
463462
webview.load(URLRequest(url: URL(string: "https://www.falstad.com/circuit/circuitjs.html")!))
464463
webview.uiDelegate = self
465464
//ConnectButton
465+
CWWiFiClient.shared().delegate = self
466+
do { try CWWiFiClient.shared().startMonitoringEvent(with: CWEventType.linkQualityDidChange)}
467+
catch {print("eh well sry")}
466468
webview.addObserver(self, forKeyPath:
467469
#keyPath(WKWebView.estimatedProgress), options: .new, context: nil)
468-
470+
469471
}
470472
override func viewDidDisappear() {
471473
webview.removeObserver(self, forKeyPath:
472474
#keyPath(WKWebView.estimatedProgress))
473475
progbar.doubleValue = 0
476+
do { try CWWiFiClient.shared().stopMonitoringAllEvents()
477+
}
478+
catch {print("eh well sry")}
474479
self.view.window?.close()
475480
}
481+
override func viewDidAppear() {
482+
do { try CWWiFiClient.shared().startMonitoringEvent(with: CWEventType.linkQualityDidChange)}
483+
catch {print("eh well sry")}
484+
}
476485

477486
@IBOutlet weak var progbar: NSProgressIndicator!
478487

479488
@IBOutlet weak var webview: WKWebView!
480489

481490
@IBAction func reload(_ sender: Any) {
491+
checkInternet()
482492
webview.reload()
483493
}
484494
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
@@ -487,7 +497,40 @@ class CircuitSimViewController: NSViewController, WKUIDelegate
487497
print("estimatedProgress")
488498
progbar.doubleValue = Double(webview.estimatedProgress)*100
489499
}
500+
}
501+
502+
@IBOutlet weak var noInternetImg: NSImageView!
503+
func checkInternet()
504+
{
505+
if Reachability.isConnectedToNetwork(){
506+
webview.isHidden = false
507+
noInternetImg.isHidden = true
508+
}else {
509+
webview.isHidden = true
510+
webview.stopLoading()
511+
noInternetImg.isHidden = false
512+
}
513+
514+
515+
}
516+
517+
518+
// MARK: Misc Helper Methods
519+
func safeShell(_ command: String) throws -> String {
520+
let task = Process()
521+
let pipe = Pipe()
522+
523+
task.standardOutput = pipe
524+
task.standardError = pipe
525+
task.arguments = ["-c", command]
526+
task.executableURL = URL(fileURLWithPath: "/bin/zsh") //<--updated
527+
528+
try task.run() //<--updated
529+
530+
let data = pipe.fileHandleForReading.readDataToEndOfFile()
531+
let output = String(data: data, encoding: .utf8)!
490532

533+
return output
491534
}
492535
}
493536

0 commit comments

Comments
 (0)