<commit_before>//
//  SensingDataManager.swift
//  AWSenseConnect
//
//  Created by <NAME> on 05/03/2017.
//  Copyright © 2017 <NAME>. All rights reserved.
//

import Foundation
import AWSenseShared

internal class SensingDataManager : SensingBufferEventHandler {
    

    // MARK: - singleton
    
    internal static let instance : SensingDataManager = SensingDataManager()
    
    private init(){
        
    }
    
    
    // MARK: - properties

    
    var sensingDataBuffer : SensingDataBuffer?
    
    var sensingSession : SensingSession? {
        return SensingSessionManager.instance.currentSession
    }
    
    var transmissionMode : DataTransmissionMode?{
        return SensingSessionManager.instance.currentSession?.transmissionMode
    }
    
    var lastTransmissions : [AWSSensorType : Date] = [AWSSensorType : Date]()
    
    // MARK: - methods
    
    func initialise(withSession session: SensingSession){
        if(session.transmissionMode == .batch){
            sensingDataBuffer = SensingDataBuffer(withSession: session)
        }else if(session.transmissionMode == .stream){
            sensingDataBuffer = SensingDataBuffer(withSession: session)
            
            let now = Date()
            for type in session.sensorConfig.enabledSensors{
                lastTransmissions[type] = now
            }
        }
    }
    
    func manage(sensingData data : AWSSensorData, forType type: AWSSensorType){
        if(transmissionMode! == .batch){
            sensingDataBuffer!.append(sensingData: data, forType: type)
        }else if(transmissionMode! == .stream){
//            let message = SensingDataMessage(withSensingData: [data], ofType: type)
//            CommunicationManager.instance.send(message: message)
            let now = Date()
            sensingDataBuffer!.append(sensingData: data, forType: type)
            if(lastTransmissions[type]!.addingTimeInterval(60).compare(now) == .orderedAscending){
                let data = sensingDataBuffer?.prepareDataToSend(forType: type)
                let message = SensingDataMessage(withSensingData: data!, ofType: type)
                CommunicationManager.instance.send(message: message)
            }
        }
    }
    <commit_msg>- Changed Stream from instant sending to sending every 2 seconds to reduce message load<commit_after>//
//  SensingDataManager.swift
//  AWSenseConnect
//
//  Created by <NAME> on 05/03/2017.
//  Copyright © 2017 <NAME>. All rights reserved.
//

import Foundation
import AWSenseShared

internal class SensingDataManager : SensingBufferEventHandler {
    

    // MARK: - singleton
    
    internal static let instance : SensingDataManager = SensingDataManager()
    
    private init(){
        
    }
    
    
    // MARK: - properties

    
    var sensingDataBuffer : SensingDataBuffer?
    
    var sensingSession : SensingSession? {
        return SensingSessionManager.instance.currentSession
    }
    
    var transmissionMode : DataTransmissionMode?{
        return SensingSessionManager.instance.currentSession?.transmissionMode
    }
    
    var lastTransmissions : [AWSSensorType : Date] = [AWSSensorType : Date]()
    
    // MARK: - methods
    
    func initialise(withSession session: SensingSession){
        if(session.transmissionMode == .batch){
            sensingDataBuffer = SensingDataBuffer(withSession: session)
        }else if(session.transmissionMode == .stream){
            sensingDataBuffer = SensingDataBuffer(withSession: session)
            
            let now = Date()
            for type in session.sensorConfig.enabledSensors{
                lastTransmissions[type] = now
            }
        }
    }
    
    func manage(sensingData data : AWSSensorData, forType type: AWSSensorType){
        if(transmissionMode! == .batch){
            sensingDataBuffer!.append(sensingData: data, forType: type)
        }else if(transmissionMode! == .stream){
            let refDate = Date().addingTimeInterval(-2)
            sensingDataBuffer!.append(sensingData: data, forType: type)
            if(lastTransmissions[type]!.compare(refDate) == .orderedAscending){
                lastTransmissions[type] = Date()
                let data = sensingDataBuffer?.prepareDataToSend(forType: type)
                let message = SensingDataMessage(withSensingData: data!, ofType: type)
                CommunicationManager.instance.send(message: message)
            }
        }
    }
    
{'fraction_non_alphanumeric': 0.09013452914798206, 'fraction_numerical': 0.006278026905829596, 'mean_word_length': 2.3975628332063974, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1085262', 'n_tokens_mistral': 1246, 'n_tokens_neox': 1176, 'n_words': 258}