view eventLogger.h @ 29:fabb3a5cdfc9

Timed session improvements. Desperate pathetic attempts to send a simple HTTP POST.
author Robert Tubb <rt300@eecs.qmul.ac.uk>
date Fri, 22 Feb 2013 17:41:38 +0000
parents e2c62db1e265
children c0a6f7c66719
line wrap: on
line source
//
//  eventLogger.h
//  oscSenderExample
//
//  Created by Robert Tubb on 05/11/2012.
//
//
// This class handle everything to do with loggin user actions,
// uploading logs to server, and storing locally if not uploaded

#ifndef __oscSenderExample__eventLogger__
#define __oscSenderExample__eventLogger__


#include "ofMain.h"
#include "ofxiPhone.h"
#include "2dvector.h"
#include "ofxiPhoneExtras.h"
#include <sys/time.h>
#include <iostream>
#include <string>
#include <cstring>
#include <map>
#include "2dvector.h"
#include "json.h"
#include "testApp.h"

#import "ServerComms.h"
 
#define EVENT_THIN_FACTOR 10
#define EVENT_LOG_FILENAME "log.json"
//#define LOGGING_SERVER_URL "http://www.isophonics.net/datacollector/"
#define LOGGING_SERVER_URL "http://127.0.0.1:8080/testservice/"
#define UPLOAD_CHUNK_SIZE 100

#define QUESTIONNAIRE_ENABLE_TIME 100000 // milliseconds
#define APP_CREATION_TIME 381429000000   // milliseconds to the time i wrote this wee blighter saves digits

#define PROGRAM_VERSION 0.1 // IMPORTANT TOCHNAGE!

// can add but don't change ordering - this will invalidate logs
enum leventType {SAVE_PRESET,  // 0
    SAVE_DESET,           // 1
    SCROLL,               // 2
    ZOOM,                 // 3
    SCROLL_STOPPED,        // 4
    ZOOM_STOPPED,           // 5
    SNAPPED_TO_PRESET,      // 6
    CHANGE_SLIDER,          // 7
    SWAP_VIEW,              // 8
    SET_MIN_ZOOM,           // 9
    SET_MAX_ZOOM,           // 10
    RANDOMISE,             // 11
    APP_STARTED,            // 12
    APP_EXITED,             // 13
    CONSENT_DENIED,         // 14
    SEQ_LOCKED,             // 15
    SYNTH_LOCKED,           // 16
    PLAY_PRESSED,           // 17
    PAUSE_PRESSED,         // 18
    HELP_PRESSED};          // 19

//---------------------------------------------------------------------------

class lEvent{
public:
    // try and make this as compact as possible.
    leventType eventType;
    double val1; // x coord, scale if zoom
    double val2; // y coord, 0 if zoom
    int sliderID; // xtra int 
    long long eventTime;
    lEvent(leventType eType, double v1 = 0.0, double v2 = 0.0,int sID = 0){
        eventType = eType;
        val1 = v1;
        val2 = v2;
        sliderID = sID;
        
        double timemsd = [NSDate timeIntervalSinceReferenceDate]; // MILLISECONDS
        eventTime = (unsigned long long)(timemsd*1000) - APP_CREATION_TIME;
        
    }

    lEvent(const Json::Value &jevt){
    // constructor takes "jsonToEvent" readfile function role
        eventType = (leventType)jevt["eType"].asInt();
        val1 = jevt["v1"].asDouble();
        val2 = jevt["v2"].asDouble();
        sliderID = jevt["sID"].asInt();
        eventTime = jevt["eTime"].asLargestInt();
        
        // TODO what happens if we try to read one that isn't there?
        
    }
    Json::Value eventToJson(){
        Json::Value jevt;
        jevt["eType"] = eventType;
        // here: should give a certain number of sig figs? 
        jevt["v1"] = val1;
        jevt["v2"] = val2;
        jevt["sID"] = sliderID;
        jevt["eTime"] = eventTime;
        return jevt;
    }
};
//---------------------------------------------------------------------------
// streams no longer used
inline istream& operator>>(istream & is, lEvent& e){
    is.setf(ios_base::fixed,ios_base::floatfield);
    is.precision(1);
    
    char delim;
    int eType;

    is >> eType >> delim >> e.val1 >> delim >> e.val2 >> delim >> e.sliderID;
    
    e.eventType = (leventType)eType;
    
    return is;
}

//---------------------------------------------------------------------------
inline ostream& operator<<(ostream & os, const lEvent& e){
    os.setf(ios_base::fixed,ios_base::floatfield);
    os.precision(1);
    
    os << e.eventType << ',' << e.val1 << ',' << e.val2 << ',' << e.sliderID << '\n';
    
    return os;
}
 //---------------------------------------------------------------------------   
    
    class ThreadedObject: ofThread{
        int i;
        int threadedFunc(int i){
            return i + 10;
        };
    };
    
    
class EventLogger{
public:
    int nextUploadNumber;
    bool loggingEnabled;
    bool logUploadInProgress;
    bool serverConnectionOK;
    bool consentGiven;
    bool questionnaireCompleted;
    bool questionnaireUploaded;
    unsigned int deviceID;
    unsigned int totalInteractionTime, savedInteractionTime, sessionTime, sessionStartTime;
    string userName; // not unique

    EventLogger();
    void init();
    void exitAndSave();
    void setUsername(const char *u);
    void newUser();
    void logEvent(const leventType& evtType,const TwoVector& centre = TwoVector(), const double& scale = 1.0, const int& sliderID = -1, const double& sliderVal = 0.0);
    void questionnaireAnswersObtained(vector<int> answers, const char* userComments);
    void urlResponse(ofHttpResponse & response);
private:

    int currentHTTPRequestID;
    
    
    vector<lEvent> theEvents;

    void thinnedLogEvent(lEvent nextEvent);
    unsigned int nextUploadQty;

    string questionnaireComments;
    
    ofxiPhoneDeviceType iOSdeviceType;
    
    bool testConnection();
    vector<int> questionnaireAnswers;
    int interfaceOrder;

    
    void checkLogFile();
    void deleteLogs(); // new user
    bool uploadEventLog(bool async);
    void firstEverAppOpen();
    void readJsonToLog(const string &jsonFile);
    void uploadQuestionnaire();
    bool sendToServer(string functionName, Json::Value jsonData, bool async);
    Json::Value logsToJson();
    Json::Value questionnaireToJson();
    void printAll(){
        cout << "ALL LOGGED EVENTS!: \n";
        vector<lEvent>::iterator evIter;
        for(evIter = theEvents.begin(); evIter < theEvents.end(); evIter++){
            cout << *evIter;
            
        }
    };
    
    //
    ServerComms *serverComms;
    
};


    //---------------------------------------------------------------------------
   

#endif /* defined(__oscSenderExample__eventLogger__) */