rt300@0
|
1 //
|
rt300@0
|
2 // eventLogger.h
|
rt300@0
|
3 // oscSenderExample
|
rt300@0
|
4 //
|
rt300@0
|
5 // Created by Robert Tubb on 05/11/2012.
|
rt300@0
|
6 //
|
rt300@0
|
7 //
|
rt300@0
|
8 // This class handle everything to do with loggin user actions,
|
rt300@0
|
9 // uploading logs to server, and storing locally if not uploaded
|
rt300@0
|
10
|
rt300@0
|
11 #ifndef __oscSenderExample__eventLogger__
|
rt300@0
|
12 #define __oscSenderExample__eventLogger__
|
rt300@0
|
13
|
rt300@0
|
14
|
rt300@0
|
15 #include "ofMain.h"
|
rt300@0
|
16 #include "ofxiPhone.h"
|
rt300@0
|
17 #include "2dvector.h"
|
rt300@0
|
18 #include "ofxiPhoneExtras.h"
|
rt300@0
|
19 #include <sys/time.h>
|
rt300@0
|
20 #include <iostream>
|
rt300@0
|
21 #include <string>
|
rt300@0
|
22 #include <cstring>
|
rt300@0
|
23 #include <map>
|
rt300@0
|
24 #include "2dvector.h"
|
rt300@0
|
25 #include "json.h"
|
rt300@0
|
26 #import "ServerComms.h"
|
rt300@0
|
27 #include "boost/bind.hpp"
|
rt300@0
|
28 #include "boost/function.hpp"
|
rt300@0
|
29 #include "Poco/Mutex.h"
|
rt300@0
|
30
|
rt300@0
|
31 #define EVENT_THIN_FACTOR 30
|
rt300@0
|
32 #define EVENT_LOG_FILENAME "log.json"
|
rt300@0
|
33 #define UPLOAD_CHUNK_SIZE 1000
|
rt300@0
|
34 #define APP_CREATION_TIME 0 // ignore this, pointless
|
rt300@0
|
35 #define PROGRAM_NAME "TWEAKATHLON"
|
rt300@0
|
36 #define PROGRAM_VERSION 0.2
|
rt300@0
|
37
|
rt300@0
|
38 #define SUPERVISED // this def will save files
|
rt300@0
|
39
|
rt300@0
|
40
|
rt300@0
|
41 // can add but don't change ordering - this will invalidate logs
|
rt300@0
|
42 enum leventType {APP_LOADED, // 0
|
rt300@0
|
43 INTRO_CONSENTED, // 1
|
rt300@0
|
44 APP_EXITED, // 2
|
rt300@0
|
45 HELP_PRESSED, // 3
|
rt300@0
|
46 QUESTIONNAIRE_COMPLETED, // 4
|
rt300@0
|
47
|
rt300@0
|
48 NEW_TEST, // 5
|
rt300@0
|
49 // al the set up of the new test
|
rt300@0
|
50 TARGET_PARAM_SET, // 6 setting up the target sound, param id and value
|
rt300@0
|
51 CANDIDATE_PARAM_SET, // 7 setting up the cand sound starting pos, subset of param id and value
|
rt300@0
|
52 CANDIDATE_CHANGEABLE_IDX, // 8 list of changeable indexes
|
rt300@0
|
53 CANDIDATE_MAPPING_IDS, // 9 which kind of control was mapped to which param
|
rt300@0
|
54 CONTROL_LIST, // 10 list of control types
|
rt300@0
|
55
|
rt300@0
|
56 TEST_TIMER_STARTED, // 11 we're off!!
|
rt300@0
|
57
|
rt300@0
|
58 CANDIDATE_PARAM_ADJUSTED, // 12 interactions changing params
|
rt300@0
|
59 SUBMIT_PRESSED, // 13 this is the suer saying they've found it, along with the actual answer
|
rt300@0
|
60 DISTANCE_TIME_SCORE, // 14 list of the results, now we should go back to NEW_TEST
|
rt300@0
|
61 COUNTDOWN_INITIATED, // 15 just in case
|
rt300@0
|
62 ALL_TESTS_COMPLETED, // 16 ?
|
rt300@0
|
63 TARGET_PLAYED, // 17 however this happens it might be good to know when this happens
|
rt300@0
|
64 CANDIDATE_PLAYED, // 18
|
rt300@9
|
65 START_THE_SEARCH_TESTS, // 19 probably superfluous
|
rt300@0
|
66 CRAP_TEST, // eliminate these coords somehow ???
|
rt300@0
|
67 EMPTY_EVENT,
|
rt300@0
|
68 SPEED_CHANGED, // 22 ms between sounds
|
rt300@9
|
69 SAVE_PRESET, // 23 save a preset
|
rt300@9
|
70 START_THE_TRAINING_TESTS,
|
rt300@0
|
71
|
rt300@0
|
72 };
|
rt300@0
|
73
|
rt300@0
|
74 //---------------------------------------------------------------------------
|
rt300@0
|
75
|
rt300@0
|
76 class lEvent{
|
rt300@0
|
77 public:
|
rt300@0
|
78 // try and make this as compact as possible.
|
rt300@0
|
79 leventType eventType;
|
rt300@0
|
80 double val1; // x coord, scale if zoom
|
rt300@0
|
81 double val2; // y coord, 0 if zoom
|
rt300@0
|
82 int sliderID; // xtra int
|
rt300@0
|
83 long long eventTime; // MILLISECONDS since ref date
|
rt300@0
|
84 vector<int> eventData; // variable length data int
|
rt300@0
|
85
|
rt300@0
|
86 // old sonic zoom style event
|
rt300@0
|
87 lEvent(leventType eType, double v1 = 0.0, double v2 = 0.0,int sID = 0){
|
rt300@0
|
88 eventType = eType;
|
rt300@0
|
89 val1 = v1;
|
rt300@0
|
90 val2 = v2;
|
rt300@0
|
91 sliderID = sID;
|
rt300@0
|
92
|
rt300@0
|
93 double timemsd = [NSDate timeIntervalSinceReferenceDate]; // MILLISECONDS, but with fractional part (
|
rt300@0
|
94 eventTime = (unsigned long long)(timemsd*1000) - APP_CREATION_TIME;
|
rt300@0
|
95
|
rt300@0
|
96 }
|
rt300@0
|
97
|
rt300@0
|
98 // new tweakathlon event
|
rt300@0
|
99 lEvent(leventType eType, vector<int> theData){
|
rt300@0
|
100 eventType = eType;
|
rt300@0
|
101 eventData = theData;
|
rt300@0
|
102
|
rt300@0
|
103 double timemsd = [NSDate timeIntervalSinceReferenceDate]; // MILLISECONDS, but with fractional part (
|
rt300@0
|
104 eventTime = (unsigned long long)(timemsd*1000) - APP_CREATION_TIME;
|
rt300@0
|
105
|
rt300@0
|
106 }
|
rt300@0
|
107
|
rt300@0
|
108 lEvent(const Json::Value &jevt){
|
rt300@0
|
109 // constructor takes "jsonToEvent" readfile function role
|
rt300@0
|
110 eventType = (leventType)jevt["eType"].asInt();
|
rt300@0
|
111 val1 = jevt["v1"].asDouble();
|
rt300@0
|
112 val2 = jevt["v2"].asDouble();
|
rt300@0
|
113 sliderID = jevt["sID"].asInt();
|
rt300@0
|
114 eventTime = jevt["eTime"].asLargestInt();
|
rt300@0
|
115
|
rt300@0
|
116 // TODO what happens if we try to read one that isn't there?
|
rt300@0
|
117
|
rt300@0
|
118 }
|
rt300@0
|
119 // new tweak event to json
|
rt300@0
|
120 Json::Value eventToJson(){
|
rt300@0
|
121 Json::Value jevt;
|
rt300@0
|
122 jevt["eType"] = eventType;
|
rt300@0
|
123 // convert vector to json array
|
rt300@0
|
124 for(auto it = eventData.begin(); it < eventData.end(); it++){
|
rt300@0
|
125 jevt["eData"].append(*it);
|
rt300@0
|
126 }
|
rt300@0
|
127 jevt["eTime"] = eventTime;
|
rt300@0
|
128 return jevt;
|
rt300@0
|
129 }
|
rt300@0
|
130 Json::Value eventToJsonOld(){
|
rt300@0
|
131 Json::Value jevt;
|
rt300@0
|
132 jevt["eType"] = eventType;
|
rt300@0
|
133 // here: should give a certain number of sig figs?
|
rt300@0
|
134 jevt["v1"] = val1;
|
rt300@0
|
135 jevt["v2"] = val2;
|
rt300@0
|
136 jevt["sID"] = sliderID;
|
rt300@0
|
137 jevt["eTime"] = eventTime;
|
rt300@0
|
138 return jevt;
|
rt300@0
|
139 }
|
rt300@0
|
140 };
|
rt300@0
|
141
|
rt300@0
|
142
|
rt300@0
|
143 //------------------------------------------
|
rt300@0
|
144
|
rt300@0
|
145 class EventLogger{
|
rt300@0
|
146 public:
|
rt300@0
|
147 Poco::Mutex _mutex;
|
rt300@0
|
148
|
rt300@0
|
149 int nextUploadNumber;
|
rt300@0
|
150 int nextUploadQty;
|
rt300@0
|
151 bool loggingEnabled;
|
rt300@0
|
152
|
rt300@0
|
153 bool logUploadInProgress;
|
rt300@0
|
154 bool serverConnectionOK;
|
rt300@0
|
155 bool consentGiven;
|
rt300@0
|
156 bool questionnaireCompleted;
|
rt300@0
|
157 bool questionnaireUploaded;
|
rt300@0
|
158
|
rt300@0
|
159 unsigned int deviceID;
|
rt300@0
|
160 unsigned int totalInteractionTime, savedInteractionTime, sessionTime, sessionStartTime;
|
rt300@0
|
161 string userName; // not unique
|
rt300@0
|
162 string questionnaireComments;
|
rt300@0
|
163 // constr
|
rt300@0
|
164 EventLogger();
|
rt300@0
|
165
|
rt300@0
|
166 // public methods:
|
rt300@0
|
167 void startLoadAll();
|
rt300@0
|
168 void exitAndSave();
|
rt300@0
|
169 void setUsername(const char *u);
|
rt300@0
|
170 string getUsername(){
|
rt300@0
|
171 return userName;
|
rt300@0
|
172 }
|
rt300@0
|
173 void newUser();
|
rt300@0
|
174 void logEvent(const leventType& evtType);
|
rt300@0
|
175 void logEvent(const leventType& evtType, const vector<int> eventData);
|
rt300@0
|
176
|
rt300@0
|
177 void questionnaireAnswersObtained(vector<int> answers, const char* userComments);
|
rt300@0
|
178 void urlResponse(ofHttpResponse & response);
|
rt300@0
|
179
|
rt300@0
|
180 void questionnaireOK();
|
rt300@0
|
181 void eventlogOK();
|
rt300@0
|
182 void testConnectionOK();
|
rt300@0
|
183 void questionnaireNotOK();
|
rt300@0
|
184 void eventlogNotOK();
|
rt300@0
|
185 void testConnectionNotOK();
|
rt300@0
|
186
|
rt300@0
|
187 void printAll();
|
rt300@0
|
188 void saveSessionToFile();
|
rt300@0
|
189
|
rt300@0
|
190 private:
|
rt300@0
|
191 void thinnedSliderEvent(lEvent newEvent);
|
rt300@0
|
192 vector<lEvent> theEvents; // all logged but not uploaded events
|
rt300@0
|
193
|
rt300@0
|
194 vector<int> questionnaireAnswers;
|
rt300@0
|
195
|
rt300@0
|
196 // private methods
|
rt300@0
|
197 void testConnection();
|
rt300@0
|
198 void checkLogFile();
|
rt300@0
|
199 void deleteLogs(); // new user
|
rt300@0
|
200 bool uploadEventLog(bool async);
|
rt300@0
|
201 void firstEverAppOpen();
|
rt300@0
|
202 void loadExistingLogFile(const string &jsonFile);
|
rt300@0
|
203 void uploadQuestionnaire();
|
rt300@0
|
204 bool sendToServer(string functionName, Json::Value jsonData, bool async);
|
rt300@0
|
205
|
rt300@0
|
206 Json::Value logsToJson();
|
rt300@0
|
207 Json::Value questionnaireToJson();
|
rt300@0
|
208
|
rt300@0
|
209
|
rt300@0
|
210 template<class T> bool matchID(T thing);
|
rt300@0
|
211 bool matchID2();
|
rt300@0
|
212 //
|
rt300@0
|
213 ServerComms *serverComms;
|
rt300@0
|
214
|
rt300@0
|
215 };
|
rt300@0
|
216
|
rt300@0
|
217
|
rt300@0
|
218 //---------------------------------------------------------------------------
|
rt300@0
|
219
|
rt300@0
|
220
|
rt300@0
|
221 #endif /* defined(__oscSenderExample__eventLogger__) */
|