Skip to content

Commit ba6ac73

Browse files
authored
Merge pull request #2 from eyalperry88/device-uuid
Device UUID
2 parents 764bacd + 654c42f commit ba6ac73

23 files changed

+1031
-74
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@
22
*.swp
33
*.DS_Store
44
*/node_modules/*
5+
**/prediction_server/data
6+
**/prediction_server/models

mobile_app/openSleep.xcodeproj/project.pbxproj

+4
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
3956DC0A21AC82690039E652 /* mind2.png in Resources */ = {isa = PBXBuildFile; fileRef = 3956DC0921AC82680039E652 /* mind2.png */; };
4646
3956DC0C21ACA48E0039E652 /* microphone_red.png in Resources */ = {isa = PBXBuildFile; fileRef = 3956DC0B21ACA48D0039E652 /* microphone_red.png */; };
4747
3956DC0E21ADC9050039E652 /* dormio.m4v in Resources */ = {isa = PBXBuildFile; fileRef = 3956DC0D21ADC8DA0039E652 /* dormio.m4v */; };
48+
39F0C10621B23F3B001F985A /* ID.swift in Sources */ = {isa = PBXBuildFile; fileRef = 39F0C10521B23F3B001F985A /* ID.swift */; };
4849
9051B70A218C8BE20018B3E1 /* simulatedData.csv in Resources */ = {isa = PBXBuildFile; fileRef = 9051B709218C8BE20018B3E1 /* simulatedData.csv */; };
4950
FF6F7C551FDA1BE60034F388 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = FF6F7C541FDA1BE60034F388 /* AppDelegate.swift */; };
5051
FF6F7C571FDA1BE60034F388 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = FF6F7C561FDA1BE60034F388 /* ViewController.swift */; };
@@ -93,6 +94,7 @@
9394
3956DC0921AC82680039E652 /* mind2.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = mind2.png; sourceTree = "<group>"; };
9495
3956DC0B21ACA48D0039E652 /* microphone_red.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = microphone_red.png; sourceTree = "<group>"; };
9596
3956DC0D21ADC8DA0039E652 /* dormio.m4v */ = {isa = PBXFileReference; lastKnownFileType = file; path = dormio.m4v; sourceTree = "<group>"; };
97+
39F0C10521B23F3B001F985A /* ID.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ID.swift; sourceTree = "<group>"; };
9698
9051B709218C8BE20018B3E1 /* simulatedData.csv */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = simulatedData.csv; sourceTree = "<group>"; };
9799
FF6F7C511FDA1BE60034F388 /* openSleep.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = openSleep.app; sourceTree = BUILT_PRODUCTS_DIR; };
98100
FF6F7C541FDA1BE60034F388 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
@@ -187,6 +189,7 @@
187189
3922130F21A9C4E00002A9DC /* SleepAPI.swift */,
188190
3922131121AA64630002A9DC /* FlowViewController.swift */,
189191
3956DBE821AB27B40039E652 /* Flow.swift */,
192+
39F0C10521B23F3B001F985A /* ID.swift */,
190193
);
191194
path = openSleep;
192195
sourceTree = "<group>";
@@ -295,6 +298,7 @@
295298
buildActionMask = 2147483647;
296299
files = (
297300
FF6F7C571FDA1BE60034F388 /* ViewController.swift in Sources */,
301+
39F0C10621B23F3B001F985A /* ID.swift in Sources */,
298302
3956DBE921AB27B40039E652 /* Flow.swift in Sources */,
299303
392212F621A5D5AF0002A9DC /* RecordingsTableViewController.swift in Sources */,
300304
3934E9C9219F5312007049D8 /* HeartQueue.swift in Sources */,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Bucket
3+
type = "1"
4+
version = "2.0">
5+
</Bucket>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Scheme
3+
LastUpgradeVersion = "1000"
4+
version = "1.3">
5+
<BuildAction
6+
parallelizeBuildables = "YES"
7+
buildImplicitDependencies = "YES">
8+
<BuildActionEntries>
9+
<BuildActionEntry
10+
buildForTesting = "YES"
11+
buildForRunning = "YES"
12+
buildForProfiling = "YES"
13+
buildForArchiving = "YES"
14+
buildForAnalyzing = "YES">
15+
<BuildableReference
16+
BuildableIdentifier = "primary"
17+
BlueprintIdentifier = "FF6F7C501FDA1BE60034F388"
18+
BuildableName = "openSleep.app"
19+
BlueprintName = "openSleep"
20+
ReferencedContainer = "container:openSleep.xcodeproj">
21+
</BuildableReference>
22+
</BuildActionEntry>
23+
</BuildActionEntries>
24+
</BuildAction>
25+
<TestAction
26+
buildConfiguration = "Debug"
27+
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
28+
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
29+
shouldUseLaunchSchemeArgsEnv = "YES">
30+
<Testables>
31+
</Testables>
32+
<MacroExpansion>
33+
<BuildableReference
34+
BuildableIdentifier = "primary"
35+
BlueprintIdentifier = "FF6F7C501FDA1BE60034F388"
36+
BuildableName = "openSleep.app"
37+
BlueprintName = "openSleep"
38+
ReferencedContainer = "container:openSleep.xcodeproj">
39+
</BuildableReference>
40+
</MacroExpansion>
41+
<AdditionalOptions>
42+
</AdditionalOptions>
43+
</TestAction>
44+
<LaunchAction
45+
buildConfiguration = "Debug"
46+
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
47+
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
48+
launchStyle = "0"
49+
useCustomWorkingDirectory = "NO"
50+
ignoresPersistentStateOnLaunch = "NO"
51+
debugDocumentVersioning = "YES"
52+
debugServiceExtension = "internal"
53+
allowLocationSimulation = "YES">
54+
<BuildableProductRunnable
55+
runnableDebuggingMode = "0">
56+
<BuildableReference
57+
BuildableIdentifier = "primary"
58+
BlueprintIdentifier = "FF6F7C501FDA1BE60034F388"
59+
BuildableName = "openSleep.app"
60+
BlueprintName = "openSleep"
61+
ReferencedContainer = "container:openSleep.xcodeproj">
62+
</BuildableReference>
63+
</BuildableProductRunnable>
64+
<AdditionalOptions>
65+
</AdditionalOptions>
66+
</LaunchAction>
67+
<ProfileAction
68+
buildConfiguration = "Release"
69+
shouldUseLaunchSchemeArgsEnv = "YES"
70+
savedToolIdentifier = ""
71+
useCustomWorkingDirectory = "NO"
72+
debugDocumentVersioning = "YES">
73+
<BuildableProductRunnable
74+
runnableDebuggingMode = "0">
75+
<BuildableReference
76+
BuildableIdentifier = "primary"
77+
BlueprintIdentifier = "FF6F7C501FDA1BE60034F388"
78+
BuildableName = "openSleep.app"
79+
BlueprintName = "openSleep"
80+
ReferencedContainer = "container:openSleep.xcodeproj">
81+
</BuildableReference>
82+
</BuildableProductRunnable>
83+
</ProfileAction>
84+
<AnalyzeAction
85+
buildConfiguration = "Debug">
86+
</AnalyzeAction>
87+
<ArchiveAction
88+
buildConfiguration = "Release"
89+
revealArchiveInOrganizer = "YES">
90+
</ArchiveAction>
91+
</Scheme>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>SchemeUserState</key>
6+
<dict>
7+
<key>openSleep.xcscheme</key>
8+
<dict>
9+
<key>orderHint</key>
10+
<integer>0</integer>
11+
</dict>
12+
</dict>
13+
<key>SuppressBuildableAutocreation</key>
14+
<dict>
15+
<key>FF6F7C501FDA1BE60034F388</key>
16+
<dict>
17+
<key>primary</key>
18+
<true/>
19+
</dict>
20+
</dict>
21+
</dict>
22+
</plist>

mobile_app/openSleep/Base.lproj/Main.storyboard

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="14313.18" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="xbA-pt-X6i">
2+
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="14460.31" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="xbA-pt-X6i">
33
<device id="retina4_7" orientation="portrait">
44
<adaptation id="fullscreen"/>
55
</device>
66
<dependencies>
77
<deployment identifier="iOS"/>
8-
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14283.14"/>
8+
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14460.20"/>
99
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
1010
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
1111
</dependencies>

mobile_app/openSleep/FlowViewController.swift

+44-10
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,18 @@ class FlowViewController:
7474
var isRecording = false
7575
var timer = Timer()
7676

77+
var deviceUUID: String = ""
78+
var sessionDateTime: String = ""
79+
var getParams = ["String": "String"]
80+
81+
func getDeviceUUID() {
82+
if UserDefaults.standard.object(forKey: "phoneUUID") == nil {
83+
UserDefaults.standard.set(UUID().uuidString, forKey: "phoneUUID")
84+
}
85+
deviceUUID = String(UserDefaults.standard.object(forKey: "phoneUUID") as! String)
86+
getParams["deviceUUID"] = deviceUUID
87+
}
88+
7789
override func viewDidLoad() {
7890
super.viewDidLoad()
7991

@@ -113,6 +125,8 @@ class FlowViewController:
113125
EDALabel.text = ""
114126
flexLabel.text = ""
115127
}
128+
129+
getDeviceUUID()
116130

117131
// Do any additional setup after loading the view.
118132
}
@@ -214,11 +228,15 @@ class FlowViewController:
214228
dreamButton.setTitle("Cancel", for: .normal)
215229
dreamButton.setTitleColor(UIColor.red, for: .normal)
216230
dreamLabel.text = "Enjoy your dreams :)"
231+
217232
currentStatus = "CALIBRATING"
218233

219234
self.detectSleepTimer.invalidate()
220235

221-
SleepAPI.apiGet(endpoint: "init")
236+
SleepAPI.apiGet(endpoint: "init", params: getParams, onSuccess: {json in
237+
self.sessionDateTime = json["datetime"] as! String
238+
self.getParams["datetime"] = self.sessionDateTime
239+
})
222240
self.calibrateStart()
223241
self.numOnsets = 0
224242

@@ -231,7 +249,7 @@ class FlowViewController:
231249
self.currentStatus = "RUNNING"
232250
self.calibrateEnd()
233251

234-
SleepAPI.apiGet(endpoint: "train")
252+
SleepAPI.apiGet(endpoint: "train", params: self.getParams)
235253

236254
self.detectSleepTimerPause = false
237255
self.detectSleepTimer = Timer.scheduledTimer(timeInterval: 3, target: self, selector: #selector(self.detectSleep(sender:)), userInfo: nil, repeats: true)
@@ -252,33 +270,46 @@ class FlowViewController:
252270
}
253271

254272
@objc func detectSleep(sender: Timer) {
255-
SleepAPI.apiGet(endpoint: "predict", onSuccess: { json in
273+
SleepAPI.apiGet(endpoint: "predict", params: getParams, onSuccess: { json in
274+
var onsetTrigger: OnsetTrigger?
275+
256276
let score = Int((json["max_sleep"] as! NSNumber).floatValue.rounded())
257277
if (!self.detectSleepTimerPause && self.numOnsets == 0) {
258278
if (self.dreamDetectorControl.selectedSegmentIndex == 0 && score >= (UserDefaults.standard.object(forKey: "deltaHBOSS") as! Int)) {
259279
DispatchQueue.main.async {
260-
self.sleepDetected()
280+
self.sleepDetected(trigger: OnsetTrigger.HBOSS)
261281
}
262282
} else if (self.dreamDetectorControl.selectedSegmentIndex == 1 && abs(self.lastHR - self.meanHR) >= (UserDefaults.standard.object(forKey: "deltaHR") as! Int)) {
263283
DispatchQueue.main.async {
264-
self.sleepDetected()
284+
self.sleepDetected(trigger: OnsetTrigger.HR)
265285
}
266286
} else if (self.dreamDetectorControl.selectedSegmentIndex == 2 && abs(self.lastEDA - self.meanEDA) >= (UserDefaults.standard.object(forKey: "deltaEDA") as! Int)) {
267287
DispatchQueue.main.async {
268-
self.sleepDetected()
288+
self.sleepDetected(trigger: OnsetTrigger.EDA)
269289
}
270290
} else if (self.dreamDetectorControl.selectedSegmentIndex == 3 && abs(self.lastFlex - self.meanFlex) >= (UserDefaults.standard.object(forKey: "deltaFlex") as! Int)) {
271291
DispatchQueue.main.async {
272-
self.sleepDetected()
292+
self.sleepDetected(trigger: OnsetTrigger.FLEX)
273293
}
274294
}
275295
}
276296
})
277297
}
278298

279-
func sleepDetected() {
299+
func sleepDetected(trigger: OnsetTrigger) {
280300
self.timer.invalidate()
281301
print("Sleep!")
302+
303+
print("Sleep!")
304+
print("TRIGGER WAS", String(describing: trigger))
305+
306+
let json: [String : Any] = ["trigger" : String(describing: trigger),
307+
"currDateTime" : Date().timeIntervalSince1970,
308+
"legitimate" : true,
309+
"deviceUUID": deviceUUID,
310+
"datetime": sessionDateTime]
311+
SleepAPI.apiPost(endpoint: "reportTrigger", json: json)
312+
282313
if (!self.playedAudio) {
283314
self.playedAudio = true
284315
self.detectSleepTimerPause = true
@@ -305,7 +336,7 @@ class FlowViewController:
305336

306337
self.timer = Timer.scheduledTimer(withTimeInterval: Double(UserDefaults.standard.object(forKey: "waitForOnsetTime") as! Int), repeats: false, block: {
307338
t in
308-
self.sleepDetected()
339+
self.sleepDetected(trigger: OnsetTrigger.TIMER)
309340
})
310341
})
311342
}
@@ -356,7 +387,10 @@ class FlowViewController:
356387
// send buffer to server
357388
let json: [String : Any] = ["flex" : flexBuffer,
358389
"eda" : edaBuffer,
359-
"ecg" : hrBuffer]
390+
"ecg" : hrBuffer,
391+
"deviceUUID": getParams["deviceUUID"],
392+
"datetime": getParams["datetime"]
393+
]
360394
SleepAPI.apiPost(endpoint: "upload", json: json)
361395

362396
lastEDA = Int(Float(edaBuffer.reduce(0, +)) / Float(edaBuffer.count))

mobile_app/openSleep/ID.swift

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//
2+
// ID.swift
3+
// openSleep
4+
//
5+
// Created by Adam Haar Horowitz on 11/30/18.
6+
// Copyright © 2018 Tomas Vega. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
12+
public struct ID {
13+
var deviceID: String?
14+
var sessionDateTime: String?
15+
16+
init() {
17+
18+
}
19+
20+
mutating func newSessionId(){
21+
self.sessionDateTime = UUID().uuidString;
22+
}
23+
}

mobile_app/openSleep/SleepAPI.swift

+11-4
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,18 @@ import Foundation
1010

1111
final class SleepAPI {
1212
static let apiBaseURL: String = "http://68.183.114.149:5000/"
13-
14-
static func apiGet(endpoint: String, onSuccess: (([String : Any]) -> ())? = nil) {
15-
let url = URL(string: self.apiBaseURL + endpoint)
13+
// static let apiBaseURL: String = "http://0.0.0.0:5000/"
14+
15+
static func apiGet(endpoint: String, params: [String: String], onSuccess: (([String : Any]) -> ())? = nil) {
16+
17+
var url_comps = URLComponents(string: self.apiBaseURL + endpoint)
18+
url_comps!.queryItems = []
19+
for (key, val) in params {
20+
url_comps!.queryItems!.append(URLQueryItem(name: key, value: val))
21+
}
22+
1623
print("GET " + self.apiBaseURL + endpoint)
17-
let task = URLSession.shared.dataTask(with: url!){ (data, response, error) in
24+
let task = URLSession.shared.dataTask(with: url_comps!.url!){ (data, response, error) in
1825
guard error == nil else {
1926
print(error!)
2027
return

0 commit comments

Comments
 (0)