<commit_before>﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using Admo.classes.lib;
using NLog;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using PubNubMessaging.Core;

namespace Admo.classes
{
    class Config
    {
        public class Keys
        {
            public const string Environment = "environment";
            public const string WebUiServer = "web_ui_server";
            public const string PodFile = "pod_file";
            public const string App = "app";
            public const string LoadingPage = "loading_page";
            public const string KinectElevation = "kinect_elevation";
            public const string PubnubSubscribeKey = "pubnub_subscribe_key";
            public const string ScreenshotInterval = "screenshot_interval";
            public const string FovCropTop = "fov_crop_top";
            public const string FovCropLeft = "fov_crop_left";
            public const string FovCropWidth = "fov_crop_width";
            public const string CalibrationActive = "calibration_active";
            public const string UnitName = "name";
        }

        private static Pubnub pubnub;
        public const int CheckingInterval = 5 * 60; //Once every 5mins
        private const int ScreenshotInterval = 30 * 60; //Once every 30mins
        private static readonly Logger Log = LogManager.GetCurrentClassLogger();

        //variable dictating whether facetracking is activated
        public static readonly bool RunningFacetracking = false;

        private static String _enviroment = null;
        private static String _webServer = null;

        private const String PodFolder = @"C:\smartroom\pods\";
        private const String BaseDropboxFolder = @"C:\Dropbox\Admo-Units\";

        private static CmsApi _api;

        //Event handler when a config option changed.
        //Currently can't pick up which config event changed.
        public static event ConfigOptionChanged OptionChanged;
        public delegate void ConfigOptionChanged();

        public static String GetHostName()
        {
            return Environment.MachineName;
        }


        public static void Init()
        {
            // 09/09/2013 -- Config was moved from dropbox to local storage folder.
            // This is only a temp code change to allow for automagic folder and migration to new folder
            MigratedLegacyConfig();

            _api = new CmsApi(GetApiKey());
            UpdateConfigCache();

            pubnub = new Pubnub("", GetPubNubSubKey(), "", "", false);
            pubnub.Subscribe<string>(GetApiKey(), OnPubNubMessage, OnPubNubConnection);

            var pod = new PodWatcher(GetPodFile(), PodFolder);
            pod.StartWatcher();
            pod.Changed += NewWebContent;
            OptionChanged += pod.OnConfigChange;
        }

        private static void MigratedLegacyConfig()
        {
            if (Directory.Exists(GetBaseConfigPath())) return;
            Log.Info(GetBaseConfigPath() + " Doesn't exsist creating it");

            Directory.CreateDirectory(GetBaseConfigPath());

            //Move the config from the old dropbox folder to the new folder.
            File.Move(Path.Combine(BaseDropboxFolder,GetHostName(),"ApiKey.txt"), Path.Combine(GetBaseConfigPath(),"ApiKey.txt"));
            File.Move(Path.Combine(BaseDropboxFolder, GetHostName(), "configcache.json"), Path.Combine(GetBaseConfigPath(), "configcache.json"));
        }

        public static void NewWebContent(String file)
        {
            Log.Debug("New server data "+ file);
            SocketServer.SendReloadEvent();
        }


        private static void OnPubNubConnection(string result)
        {
            //List order is  
            // 0,1 connected disconnected
            //message
            //api key
            var list = JsonConvert.DeserializeObject<List<object>>(result);
            if (list[0].ToString().Equals("1"))
            {
                UpdateConfigCache();
                _api.CheckIn();
                Log.Debug("Pubnub connected [" + list[1]+"]");
            }
            else
            {
                Log.Debug("Pubnub disconnected [" + list[1] + "]");
            }
        }

        
        private static void OnPubNubMessage(string result)
        {
            var list = JsonConvert.DeserializeObject<List<String>>(result);
            var command = CommandFactory.ParseCommand(list[0]);
            //Performs the command
            command.Perform();
        }

        //Production mode by default. 
        //Text field can be used to change enviroment only once per startup
        public static Boolean IsDevMode()
        {
            if (_enviroment == null)
            {
                _enviroment = ReadConfigOption(Keys.Environment,"production");
            }
            return _enviroment.Equals("development");
        }

        public static String GetWebServer()
        {
            if (_webServer != null) return _webServer;
            _webServer = ReadConfigOption(Keys.WebUiServer, "https://localhost:4001");
            return _webServer;
        }

        public static String GetBaseConfigPath()
        {
            var appData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
            return Path.Combine(appData, "Admo");
        }

        public static int GetScreenshotInterval()
        {
            var val = ReadConfigOption(Keys.ScreenshotInterval, ScreenshotInterval.ToString());
            var result = ScreenshotInterval;
            int.TryParse(val, out result);
            return result;
        }


        public static String GetPodFile()
        {
            var def = Path.Combine(GetBaseConfigPath(), "pods", "dist.pod.zip");
            var pod = ReadConfigOption(Keys.PodFile, def);<commit_msg>Changing default webserver port to be the one we use internally<commit_after>﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using Admo.classes.lib;
using NLog;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using PubNubMessaging.Core;

namespace Admo.classes
{
    class Config
    {
        public class Keys
        {
            public const string Environment = "environment";
            public const string WebUiServer = "web_ui_server";
            public const string PodFile = "pod_file";
            public const string App = "app";
            public const string LoadingPage = "loading_page";
            public const string KinectElevation = "kinect_elevation";
            public const string PubnubSubscribeKey = "pubnub_subscribe_key";
            public const string ScreenshotInterval = "screenshot_interval";
            public const string FovCropTop = "fov_crop_top";
            public const string FovCropLeft = "fov_crop_left";
            public const string FovCropWidth = "fov_crop_width";
            public const string CalibrationActive = "calibration_active";
            public const string UnitName = "name";
        }

        private static Pubnub pubnub;
        public const int CheckingInterval = 5 * 60; //Once every 5mins
        private const int ScreenshotInterval = 30 * 60; //Once every 30mins
        private static readonly Logger Log = LogManager.GetCurrentClassLogger();

        //variable dictating whether facetracking is activated
        public static readonly bool RunningFacetracking = false;

        private static String _enviroment = null;
        private static String _webServer = null;

        private const String PodFolder = @"C:\smartroom\pods\";
        private const String BaseDropboxFolder = @"C:\Dropbox\Admo-Units\";

        private static CmsApi _api;

        //Event handler when a config option changed.
        //Currently can't pick up which config event changed.
        public static event ConfigOptionChanged OptionChanged;
        public delegate void ConfigOptionChanged();

        public static String GetHostName()
        {
            return Environment.MachineName;
        }


        public static void Init()
        {
            // 09/09/2013 -- Config was moved from dropbox to local storage folder.
            // This is only a temp code change to allow for automagic folder and migration to new folder
            MigratedLegacyConfig();

            _api = new CmsApi(GetApiKey());
            UpdateConfigCache();

            pubnub = new Pubnub("", GetPubNubSubKey(), "", "", false);
            pubnub.Subscribe<string>(GetApiKey(), OnPubNubMessage, OnPubNubConnection);

            var pod = new PodWatcher(GetPodFile(), PodFolder);
            pod.StartWatcher();
            pod.Changed += NewWebContent;
            OptionChanged += pod.OnConfigChange;
        }

        private static void MigratedLegacyConfig()
        {
            if (Directory.Exists(GetBaseConfigPath())) return;
            Log.Info(GetBaseConfigPath() + " Doesn't exsist creating it");

            Directory.CreateDirectory(GetBaseConfigPath());

            //Move the config from the old dropbox folder to the new folder.
            File.Move(Path.Combine(BaseDropboxFolder,GetHostName(),"ApiKey.txt"), Path.Combine(GetBaseConfigPath(),"ApiKey.txt"));
            File.Move(Path.Combine(BaseDropboxFolder, GetHostName(), "configcache.json"), Path.Combine(GetBaseConfigPath(), "configcache.json"));
        }

        public static void NewWebContent(String file)
        {
            Log.Debug("New server data "+ file);
            SocketServer.SendReloadEvent();
        }


        private static void OnPubNubConnection(string result)
        {
            //List order is  
            // 0,1 connected disconnected
            //message
            //api key
            var list = JsonConvert.DeserializeObject<List<object>>(result);
            if (list[0].ToString().Equals("1"))
            {
                UpdateConfigCache();
                _api.CheckIn();
                Log.Debug("Pubnub connected [" + list[1]+"]");
            }
            else
            {
                Log.Debug("Pubnub disconnected [" + list[1] + "]");
            }
        }

        
        private static void OnPubNubMessage(string result)
        {
            var list = JsonConvert.DeserializeObject<List<String>>(result);
            var command = CommandFactory.ParseCommand(list[0]);
            //Performs the command
            command.Perform();
        }

        //Production mode by default. 
        //Text field can be used to change enviroment only once per startup
        public static Boolean IsDevMode()
        {
            if (_enviroment == null)
            {
                _enviroment = ReadConfigOption(Keys.Environment,"production");
            }
            return _enviroment.Equals("development");
        }

        public static String GetWebServer()
        {
            if (_webServer != null) return _webServer;
            _webServer = ReadConfigOption(Keys.WebUiServer, "https://localhost:5001");
            return _webServer;
        }

        public static String GetBaseConfigPath()
        {
            var appData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
            return Path.Combine(appData, "Admo");
        }

        public static int GetScreenshotInterval()
        {
            var val = ReadConfigOption(Keys.ScreenshotInterval, ScreenshotInterval.ToString());
            var result = ScreenshotInterval;
            int.TryParse(val, out result);
            return result;
        }


        public static String GetPodFile()
        {
            var def = Path.Combine(GetBaseConfigPath(), "pods", "dist.pod.zip");
            var pod = ReadConfigOption(Keys.PodFile, def);
{'fraction_non_alphanumeric': 0.08797554347826086, 'fraction_numerical': 0.004925271739130435, 'mean_word_length': 2.1082079704407497, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 2, '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': '869364', 'n_tokens_mistral': 3060, 'n_tokens_neox': 2926, 'n_words': 815}