annotate eventLogger.h @ 38:0dfe9e0c01aa

Evnt trails fit with uploads. Smooth button.
author Robert Tubb <rt300@eecs.qmul.ac.uk>
date Wed, 10 Apr 2013 18:57:05 +0100
parents 790939017078
children df7c08faf541
rev   line source
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@1 14
rt300@4 15 #include "ofMain.h"
rt300@4 16 #include "ofxiPhone.h"
rt300@4 17 #include "2dvector.h"
rt300@4 18 #include "ofxiPhoneExtras.h"
rt300@4 19 #include <sys/time.h>
rt300@4 20 #include <iostream>
rt300@4 21 #include <string>
rt300@22 22 #include <cstring>
rt300@4 23 #include <map>
rt300@4 24 #include "2dvector.h"
rt300@8 25 #include "json.h"
rt300@24 26 #include "testApp.h"
rt300@32 27 #include "presetManager.h"
rt300@29 28 #import "ServerComms.h"
rt300@33 29 #include "grid.h"
rt300@33 30
rt300@38 31 #define EVENT_THIN_FACTOR 12
rt300@25 32 #define EVENT_LOG_FILENAME "log.json"
rt300@35 33 #define UPLOAD_CHUNK_SIZE 100
rt300@33 34 #define APP_CREATION_TIME 381429000000 // milliseconds to the time i wrote this wee blighter. saves digits
rt300@33 35 #define SCROLL_TRAIL_LENGTH 200
rt300@35 36 #define PROGRAM_VERSION 0.5 // NOW USES NEW HILBERT CURVE. ALL OLD PRESETS WILL BE INVALID :(
rt300@29 37
rt300@33 38 #define SUPERVISED // this def will save files
rt300@25 39
rt300@25 40
rt300@9 41 // can add but don't change ordering - this will invalidate logs
rt300@25 42 enum leventType {SAVE_PRESET, // 0
rt300@25 43 SAVE_DESET, // 1
rt300@25 44 SCROLL, // 2
rt300@25 45 ZOOM, // 3
rt300@25 46 SCROLL_STOPPED, // 4
rt300@25 47 ZOOM_STOPPED, // 5
rt300@25 48 SNAPPED_TO_PRESET, // 6
rt300@25 49 CHANGE_SLIDER, // 7
rt300@25 50 SWAP_VIEW, // 8
rt300@25 51 SET_MIN_ZOOM, // 9
rt300@25 52 SET_MAX_ZOOM, // 10
rt300@25 53 RANDOMISE, // 11
rt300@25 54 APP_STARTED, // 12
rt300@25 55 APP_EXITED, // 13
rt300@25 56 CONSENT_DENIED, // 14
rt300@25 57 SEQ_LOCKED, // 15
rt300@25 58 SYNTH_LOCKED, // 16
rt300@25 59 PLAY_PRESSED, // 17
rt300@27 60 PAUSE_PRESSED, // 18
rt300@31 61 HELP_PRESSED, // 19
rt300@33 62 QUESTIONNAIRE_COMPLETED, // 20
rt300@33 63 EVALUATION_POINT, // 21
rt300@33 64 EMPTY_EVENT}; // 22
rt300@1 65
rt300@8 66 //---------------------------------------------------------------------------
rt300@8 67
rt300@4 68 class lEvent{
rt300@5 69 public:
rt300@1 70 // try and make this as compact as possible.
rt300@4 71 leventType eventType;
rt300@1 72 double val1; // x coord, scale if zoom
rt300@1 73 double val2; // y coord, 0 if zoom
rt300@25 74 int sliderID; // xtra int
rt300@32 75 long long eventTime; // MILLISECONDS since app creation
rt300@8 76 lEvent(leventType eType, double v1 = 0.0, double v2 = 0.0,int sID = 0){
rt300@5 77 eventType = eType;
rt300@5 78 val1 = v1;
rt300@5 79 val2 = v2;
rt300@5 80 sliderID = sID;
rt300@9 81
rt300@28 82 double timemsd = [NSDate timeIntervalSinceReferenceDate]; // MILLISECONDS
rt300@25 83 eventTime = (unsigned long long)(timemsd*1000) - APP_CREATION_TIME;
rt300@9 84
rt300@5 85 }
rt300@25 86
rt300@8 87 lEvent(const Json::Value &jevt){
rt300@9 88 // constructor takes "jsonToEvent" readfile function role
rt300@25 89 eventType = (leventType)jevt["eType"].asInt();
rt300@25 90 val1 = jevt["v1"].asDouble();
rt300@25 91 val2 = jevt["v2"].asDouble();
rt300@25 92 sliderID = jevt["sID"].asInt();
rt300@25 93 eventTime = jevt["eTime"].asLargestInt();
rt300@9 94
rt300@15 95 // TODO what happens if we try to read one that isn't there?
rt300@15 96
rt300@8 97 }
rt300@8 98 Json::Value eventToJson(){
rt300@8 99 Json::Value jevt;
rt300@25 100 jevt["eType"] = eventType;
rt300@25 101 // here: should give a certain number of sig figs?
rt300@25 102 jevt["v1"] = val1;
rt300@25 103 jevt["v2"] = val2;
rt300@25 104 jevt["sID"] = sliderID;
rt300@25 105 jevt["eTime"] = eventTime;
rt300@8 106 return jevt;
rt300@8 107 }
rt300@33 108 void draw();
rt300@5 109 };
rt300@5 110 //---------------------------------------------------------------------------
rt300@8 111 // streams no longer used
rt300@5 112 inline istream& operator>>(istream & is, lEvent& e){
rt300@5 113 is.setf(ios_base::fixed,ios_base::floatfield);
rt300@5 114 is.precision(1);
rt300@4 115
rt300@5 116 char delim;
rt300@5 117 int eType;
rt300@0 118
rt300@5 119 is >> eType >> delim >> e.val1 >> delim >> e.val2 >> delim >> e.sliderID;
rt300@5 120
rt300@5 121 e.eventType = (leventType)eType;
rt300@5 122
rt300@5 123 return is;
rt300@5 124 }
rt300@4 125
rt300@5 126 //---------------------------------------------------------------------------
rt300@5 127 inline ostream& operator<<(ostream & os, const lEvent& e){
rt300@5 128 os.setf(ios_base::fixed,ios_base::floatfield);
rt300@5 129 os.precision(1);
rt300@5 130
rt300@5 131 os << e.eventType << ',' << e.val1 << ',' << e.val2 << ',' << e.sliderID << '\n';
rt300@5 132
rt300@5 133 return os;
rt300@7 134 }
rt300@5 135 //---------------------------------------------------------------------------
rt300@32 136
rt300@29 137
rt300@0 138 class EventLogger{
rt300@0 139 public:
rt300@25 140 int nextUploadNumber;
rt300@14 141 bool loggingEnabled;
rt300@31 142
rt300@27 143 bool logUploadInProgress;
rt300@22 144 bool serverConnectionOK;
rt300@25 145 bool consentGiven;
rt300@29 146 bool questionnaireCompleted;
rt300@29 147 bool questionnaireUploaded;
rt300@31 148
rt300@29 149 unsigned int deviceID;
rt300@25 150 unsigned int totalInteractionTime, savedInteractionTime, sessionTime, sessionStartTime;
rt300@14 151 string userName; // not unique
rt300@27 152
rt300@31 153 // constr
rt300@14 154 EventLogger();
rt300@31 155
rt300@31 156 // public methods:
rt300@14 157 void init();
rt300@14 158 void exitAndSave();
rt300@14 159 void setUsername(const char *u);
rt300@27 160 void newUser();
rt300@14 161 void logEvent(const leventType& evtType,const TwoVector& centre = TwoVector(), const double& scale = 1.0, const int& sliderID = -1, const double& sliderVal = 0.0);
rt300@28 162 void questionnaireAnswersObtained(vector<int> answers, const char* userComments);
rt300@27 163 void urlResponse(ofHttpResponse & response);
rt300@30 164
rt300@30 165 void questionnaireOK();
rt300@30 166 void eventlogOK();
rt300@30 167 void testConnectionOK();
rt300@30 168 void questionnaireNotOK();
rt300@30 169 void eventlogNotOK();
rt300@30 170 void testConnectionNotOK();
rt300@30 171
rt300@30 172 void printAll();
rt300@32 173 void saveSessionToFile();
rt300@33 174 void drawTrail(const TwoVector min, const TwoVector max);
rt300@33 175 vector<lEvent> getDrawableEventsInRange(const TwoVector min, const TwoVector max);
rt300@33 176 vector<TwoVector> getRecentPath(int numEvents);
rt300@38 177
rt300@38 178 void clearTrail();
rt300@14 179 private:
rt300@31 180
rt300@38 181 vector<lEvent> theEvents; // all logged but not uploaded events
rt300@38 182 deque<lEvent> eventsToDraw; // 200 or so drawable events, maybe uploaded maybe not
rt300@29 183
rt300@28 184 void thinnedLogEvent(lEvent nextEvent);
rt300@33 185 void thinnedScrollEvent(lEvent nextEvent);
rt300@14 186 unsigned int nextUploadQty;
rt300@14 187
rt300@28 188 string questionnaireComments;
rt300@8 189
rt300@22 190 ofxiPhoneDeviceType iOSdeviceType;
rt300@14 191
rt300@38 192 void testConnection();
rt300@22 193 vector<int> questionnaireAnswers;
rt300@28 194 int interfaceOrder;
rt300@9 195
rt300@31 196 // private methods
rt300@7 197 void checkLogFile();
rt300@29 198 void deleteLogs(); // new user
rt300@27 199 bool uploadEventLog(bool async);
rt300@7 200 void firstEverAppOpen();
rt300@30 201 void checkExistingLogFile(const string &jsonFile);
rt300@27 202 void uploadQuestionnaire();
rt300@27 203 bool sendToServer(string functionName, Json::Value jsonData, bool async);
rt300@30 204
rt300@30 205
rt300@30 206
rt300@30 207
rt300@8 208 Json::Value logsToJson();
rt300@22 209 Json::Value questionnaireToJson();
rt300@30 210
rt300@29 211
rt300@29 212 //
rt300@29 213 ServerComms *serverComms;
rt300@29 214
rt300@0 215 };
rt300@0 216
rt300@0 217
rt300@8 218 //---------------------------------------------------------------------------
rt300@8 219
rt300@5 220
rt300@0 221 #endif /* defined(__oscSenderExample__eventLogger__) */