<commit_before>/*jslint node:true,vars:true,bitwise:true */
'use strict';

var VIEW_HARDWARE = true; // is this running on official VIEW hardware?

console.log('Starting up...');

var async = require('async');
var exec = require('child_process').exec;
var crypto = require('crypto');
var fs = require('fs');
console.log('Server modules loaded');

var sys = require('sys')
console.log('System modules loaded');

var noble = require('noble');
console.log('BT modules loaded');

require('rootpath')();
var camera = require('./camera/camera.js');
//var screen = require('./hardware/screen.js');
var updates = require('./system/updates.js');
var intervalometer = require('./intervalometer/intervalometer.js');
var nmx = require('./drivers/nmx.js');
var image = require('./camera/image/image.js');
if (VIEW_HARDWARE) {
    var light = require('./hardware/light.js');
    var oled = require('./hardware/oled.js');
    var ui = require('./interface/ui.js');
    var help = require('./interface/help.js');
    var inputs = require('./hardware/inputs.js');
    var power = require('./hardware/power.js');
}
intervalometer.addNmx(nmx);

var wifi = require('./system/wifi.js');
wifi.power = power; // allow wifi module to control power
var app = require("./system/app.js");
var db = require("./system/db.js");

var previewImage = null;
var liveviewOn = false;

var cache = {};

var Segfault = require('segfault');
Segfault.registerHandler("segfault.log");

console.log('Modules loaded.');

if (VIEW_HARDWARE) {
    oled.init();
    inputs.start();

    var configureWifi = function() {
        db.get('wifi-status', function(err, wifiStatus) {
            if(wifiStatus) {
                if(wifiStatus.enabled) {
                    wifi.btEnabled = true;
                    wifi.enable(function(){
                        db.get('bt-status', function(err, status) {
                            if(status && status.enabled) {
                                wifi.enableBt();
                            } else {
                                wifi.disableBt();
                            }
                        });
                        setTimeout(function(){
                            if(!wifi.connected) {
                                if(wifiStatus.apMode) {
                                    wifi.enableAP();
                                } else if(wifiStatus.connect) {
                                    wifi.connect(wifiStatus.connect, wifiStatus.password);
                                }
                            }
                        },5000);
                    });
                } else {
                    wifi.disable();
                }
            } else {
                wifi.btEnabled = true;
                wifi.enable(function(){
                    setTimeout(function(){
                        db.get('bt-status', function(err, status) {
                            if(status && status.enabled) {
                                wifi.enableBt();
                            } else {
                                wifi.disableBt();
                            }
                        });

                        if(!wifi.connected) {
                            wifi.enableAP();
                        }
                    },5000);
                });
            }
        });
    }
    configureWifi();

    var updateLibGPhoto2 = function() {
        var ERRORCOMPILING = "An error occurred while building the latest libgphoto2 code for camera support. Please report this to <EMAIL>.\nSystem message:\n";
        var ERRORDOWNLOADING = "An error occurred while downloading the latest code from libgphoto2 github.  Please make sure the VIEW is connected to the internet.\nSystem message:\n";
        var SUCCESS = "The camera support library has been successfully updated!  Your VIEW intervalometer can now support the latest camera models.";

        ui.confirmationPrompt("Update camera support library?", "Update", "cancel", help.saveXMPs, function(cb){
            cb();
            var backupStatus = oled.defaultStatusString;
            oled.defaultStatus("updating camera support");
            db.get('libgphoto2-update-in-progress', function(err, val){
                if(val) {
                    console.log("compiling libgphoto2...");
                    updates.installLibGPhoto(function(err){
                        oled.defaultStatus(backupStatus);
                        process.nextTick(function(){
                            if(err) { // error compiling
                                console.log("error compiling libgphoto2", err);
                                ui.alert('Error', ERRORCOMPILING + err);
                            } else {
                                db.set('libgphoto2-update-in-progress', false);
                                console.log("successfully installed libgphoto2");
                                ui.alert('Success', SUCCESS);
                            }
                        });
                    });
                } else {
                    console.log("downloading libgphoto2...");
                    updates.downloadLibGPhoto(function(err) {
                        oled.defaultStatus(backupStatus);
                        if(err) { // error downloading
                            console.log("error downloading libgphoto2", err);
                            process.nextTick(function(){
                                ui.alert('Error', ERRORDOWNLOADING + err);
                            });
                        } else {
                            console.log("successfully downloaded libgphoto2");
                            db.set('libgphoto2-update-in-progress', true);
                            console.log("compiling libgphoto2...");
                            updates.installLibGPhoto(function(err){
                                process.nextTick(function(){
                                    if(err) { // error compiling
                                        console.log("error compiling libgphoto2", err);
                                        ui.alert('Error', ERRORCOMPILING + err);
                                    } else {
                                        db.set('libgphoto2-update-in-progress', false);
                                        console.log("successfully installed libgphoto2");
                                        ui.alert('Success', SUCCESS);
                                    }
                                });
                            });
                        }
                    });
                }
            });


        }, null);
    }

    var wifiConnectionTime = 0;
    wifi.on('connect', function(ssid) {
        wifiConnectionTime = new Date().getTime();
        oled.status('wifi connected to ' + ssid);
        updates.checkLibGPhotoUpdate(function(err, needUpdate){
            if(!err && needUpdate) {
                if(updates.downloadingLibGphoto) {
                    console.log("libgphoto2 update already downloading");
                } else {
                    db.set('libgphoto2-update-in-progress', false);
                    console.log("libgphoto2 update available!");
                    updateLibGPhoto2();
                }
            } else {
                db.get('libgphoto2-update-in-progress', function(err, val){
                    if(val) {
                        if(updates.updatingLibGphoto) {
                            console.log("libgphoto2 update in progress!");
                        } else {
                            console.log("resuming libgphoto2 update...");
                            updateLibGPhoto2();
                        }
                    }
                });
            }
        });
        ui.reload();
    });
    wifi.on('enabled', function(enabled) {
        ui.reload();
    });
    wifi.on('disconnect', function(previousConnection) {
        oled.status('wifi disconnected');
        if(previousConnection && previousConnection.address) {
            var currentTime = wifiConnectionTime = new Date().getTime();
            if(currentTime - wifiConnectionTime < 30 * 1000) {
                // show alert -- authentication probably failed
            } else {
                wifiConnectionTime = new Date().getTime();
                wifi.disable(function(){
                    setTimeout(configureWifi, 2000);
                });
            }
        }
        ui.reload();
    });

    power.on('charging', function(status) {
        oled.chargeStatus(status);
    });

    power.on('percentage', function(percentage) {
        oled.batteryPercentage(percentage);
    });

    var rampingOptions = {
        name: "timelapse mode",<commit_msg>added alert for failed wifi<commit_after>/*jslint node:true,vars:true,bitwise:true */
'use strict';

var VIEW_HARDWARE = true; // is this running on official VIEW hardware?

console.log('Starting up...');

var async = require('async');
var exec = require('child_process').exec;
var crypto = require('crypto');
var fs = require('fs');
console.log('Server modules loaded');

var sys = require('sys')
console.log('System modules loaded');

var noble = require('noble');
console.log('BT modules loaded');

require('rootpath')();
var camera = require('./camera/camera.js');
//var screen = require('./hardware/screen.js');
var updates = require('./system/updates.js');
var intervalometer = require('./intervalometer/intervalometer.js');
var nmx = require('./drivers/nmx.js');
var image = require('./camera/image/image.js');
if (VIEW_HARDWARE) {
    var light = require('./hardware/light.js');
    var oled = require('./hardware/oled.js');
    var ui = require('./interface/ui.js');
    var help = require('./interface/help.js');
    var inputs = require('./hardware/inputs.js');
    var power = require('./hardware/power.js');
}
intervalometer.addNmx(nmx);

var wifi = require('./system/wifi.js');
wifi.power = power; // allow wifi module to control power
var app = require("./system/app.js");
var db = require("./system/db.js");

var previewImage = null;
var liveviewOn = false;

var cache = {};

var Segfault = require('segfault');
Segfault.registerHandler("segfault.log");

console.log('Modules loaded.');

if (VIEW_HARDWARE) {
    oled.init();
    inputs.start();

    var configureWifi = function() {
        db.get('wifi-status', function(err, wifiStatus) {
            if(wifiStatus) {
                if(wifiStatus.enabled) {
                    wifi.btEnabled = true;
                    wifi.enable(function(){
                        db.get('bt-status', function(err, status) {
                            if(status && status.enabled) {
                                wifi.enableBt();
                            } else {
                                wifi.disableBt();
                            }
                        });
                        setTimeout(function(){
                            if(!wifi.connected) {
                                if(wifiStatus.apMode) {
                                    wifi.enableAP();
                                } else if(wifiStatus.connect) {
                                    wifi.connect(wifiStatus.connect, wifiStatus.password);
                                }
                            }
                        },5000);
                    });
                } else {
                    wifi.disable();
                }
            } else {
                wifi.btEnabled = true;
                wifi.enable(function(){
                    setTimeout(function(){
                        db.get('bt-status', function(err, status) {
                            if(status && status.enabled) {
                                wifi.enableBt();
                            } else {
                                wifi.disableBt();
                            }
                        });

                        if(!wifi.connected) {
                            wifi.enableAP();
                        }
                    },5000);
                });
            }
        });
    }
    configureWifi();

    var updateLibGPhoto2 = function() {
        var ERRORCOMPILING = "An error occurred while building the latest libgphoto2 code for camera support. Please report this to <EMAIL>.\nSystem message:\n";
        var ERRORDOWNLOADING = "An error occurred while downloading the latest code from libgphoto2 github.  Please make sure the VIEW is connected to the internet.\nSystem message:\n";
        var SUCCESS = "The camera support library has been successfully updated!  Your VIEW intervalometer can now support the latest camera models.";

        ui.confirmationPrompt("Update camera support library?", "Update", "cancel", help.saveXMPs, function(cb){
            cb();
            var backupStatus = oled.defaultStatusString;
            oled.defaultStatus("updating camera support");
            db.get('libgphoto2-update-in-progress', function(err, val){
                if(val) {
                    console.log("compiling libgphoto2...");
                    updates.installLibGPhoto(function(err){
                        oled.defaultStatus(backupStatus);
                        process.nextTick(function(){
                            if(err) { // error compiling
                                console.log("error compiling libgphoto2", err);
                                ui.alert('Error', ERRORCOMPILING + err);
                            } else {
                                db.set('libgphoto2-update-in-progress', false);
                                console.log("successfully installed libgphoto2");
                                ui.alert('Success', SUCCESS);
                            }
                        });
                    });
                } else {
                    console.log("downloading libgphoto2...");
                    updates.downloadLibGPhoto(function(err) {
                        oled.defaultStatus(backupStatus);
                        if(err) { // error downloading
                            console.log("error downloading libgphoto2", err);
                            process.nextTick(function(){
                                ui.alert('Error', ERRORDOWNLOADING + err);
                            });
                        } else {
                            console.log("successfully downloaded libgphoto2");
                            db.set('libgphoto2-update-in-progress', true);
                            console.log("compiling libgphoto2...");
                            updates.installLibGPhoto(function(err){
                                process.nextTick(function(){
                                    if(err) { // error compiling
                                        console.log("error compiling libgphoto2", err);
                                        ui.alert('Error', ERRORCOMPILING + err);
                                    } else {
                                        db.set('libgphoto2-update-in-progress', false);
                                        console.log("successfully installed libgphoto2");
                                        ui.alert('Success', SUCCESS);
                                    }
                                });
                            });
                        }
                    });
                }
            });


        }, null);
    }

    var wifiConnectionTime = 0;
    wifi.on('connect', function(ssid) {
        wifiConnectionTime = new Date().getTime();
        oled.status('wifi connected to ' + ssid);
        updates.checkLibGPhotoUpdate(function(err, needUpdate){
            if(!err && needUpdate) {
                if(updates.downloadingLibGphoto) {
                    console.log("libgphoto2 update already downloading");
                } else {
                    db.set('libgphoto2-update-in-progress', false);
                    console.log("libgphoto2 update available!");
                    updateLibGPhoto2();
                }
            } else {
                db.get('libgphoto2-update-in-progress', function(err, val){
                    if(val) {
                        if(updates.updatingLibGphoto) {
                            console.log("libgphoto2 update in progress!");
                        } else {
                            console.log("resuming libgphoto2 update...");
                            updateLibGPhoto2();
                        }
                    }
                });
            }
        });
        ui.reload();
    });
    wifi.on('enabled', function(enabled) {
        ui.reload();
    });
    wifi.on('disconnect', function(previousConnection) {
        oled.status('wifi disconnected');
        if(previousConnection && previousConnection.address) {
            var currentTime = wifiConnectionTime = new Date().getTime();
            if(currentTime - wifiConnectionTime < 30 * 1000) {
                // show alert -- authentication probably failed
                ui.alert('Error', "WiFi failed to connect to " + previousConnection.ssid + ".\nTry again, double-checking that the password is correct.\nNote: if it continues to fail to connect, try a different access point if possible.  There might be an issue with connection to Apple Airport wireless routers.  This will hopefully be resolved in the near future.");
            } else {
                wifiConnectionTime = new Date().getTime();
                wifi.disable(function(){
                    setTimeout(configureWifi, 2000);
                });
            }
        }
        ui.reload();
    });

    power.on('charging', function(status) {
        oled.chargeStatus(status);
    });

    power.on('percentage', function(percentage) {
        oled.batteryPercentage(percentage);
    });

    var rampingOptions = {
        name: "timelapse mode",
{'fraction_non_alphanumeric': 0.11467710371819961, 'fraction_numerical': 0.004808498741962539, 'mean_word_length': 1.297200102748523, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 38, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4363438', 'n_tokens_mistral': 4388, 'n_tokens_neox': 4150, 'n_words': 863}