<commit_before>﻿//******************************************************************************************************
//  BenRunner.cs - Gbtc
//
//  Copyright © 2016, Grid Protection Alliance.  All Rights Reserved.
//
//  Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See
//  the NOTICE file distributed with this work for additional information regarding copyright ownership.
//  The GPA licenses this file to you under the MIT License (MIT), the "License"; you may
//  not use this file except in compliance with the License. You may obtain a copy of the License at:
//
//      http://opensource.org/licenses/MIT
//
//  Unless agreed to in writing, the subject software distributed under the License is distributed on an
//  "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the
//  License for the specific language governing permissions and limitations.
//
//  Code Modification History:
//  ----------------------------------------------------------------------------------------------------
//  10/06/2016 - <NAME>
//       Generated original version of source code.
//
//******************************************************************************************************


using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net.Mail;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using GSF;
using GSF.Web.Hubs;
using GSF.Identity;
using System.Security.Principal;
using GSF.Configuration;
using GSF.Data;
using GSF.Data.Model;
using GSF.IO;
using GSF.Parsing;
using GSF.Web.Model;
using Microsoft.VisualBasic;
using Microsoft.VisualBasic.FileIO;
using FileSystem = Microsoft.VisualBasic.FileIO.FileSystem;
namespace BenDownloader
{
    public class BenRunner
    {
        #region [Members]

        private const long BENMAXFILESIZE = 7000;
        private const int MAXFILELIMIT = 100;

        private readonly string m_ipAddress;
        private readonly string m_localPath;
        private readonly string m_folder;
        private readonly string m_siteName;
        private readonly string m_serialNumber;
        private readonly string m_tempDirectoryName;
        private readonly BenRecord m_lastFileDownloaded;
        private readonly DataRow m_deviceRecord;
        private readonly DataRow m_connectionProfile;
        private readonly Dictionary<string, string> m_connectionProfileTaskSettings;
        private string m_lastFileDownloadedThisSession;
        private readonly AdoDataConnection m_adoDataConnection;

        #endregion

        #region [Constructors]

        public BenRunner(int deviceId, int taskId)
        {
            m_adoDataConnection = new AdoDataConnection(Program.OpenMiConfigurationFile.Settings["systemSettings"]["ConnectionString"].Value, Program.OpenMiConfigurationFile.Settings["systemSettings"]["DataProviderString"].Value);

            try
            {

                string taskSettingsString = m_adoDataConnection.ExecuteScalar<string>("Select Settings From ConnectionProfileTask WHERE ID = {0}", taskId);
                m_connectionProfileTaskSettings = taskSettingsString.ParseKeyValuePairs();
                m_deviceRecord = m_adoDataConnection.RetrieveRow("Select * From Device WHERE ID = {0}", deviceId);
                Dictionary<string, string> deviceConnection = m_deviceRecord["connectionString"].ToString().ParseKeyValuePairs();

                m_connectionProfile = m_adoDataConnection.RetrieveRow("SELECT * FROM connectionProfile WHERE ID = {0}", deviceConnection["connectionProfileID"]);
                m_folder = m_deviceRecord["OriginalSource"].ToString();
                m_ipAddress = deviceConnection["connectionUserName"].Split('&')[0];
                m_localPath = m_connectionProfileTaskSettings["localPath"];
                m_siteName = m_deviceRecord["Name"].ToString();
                m_serialNumber = deviceConnection["connectionUserName"].Split('&')[1];

                string tempDirectory = System.IO.Path.GetTempPath();
                System.IO.Directory.CreateDirectory(tempDirectory + "\\BenDownloader\\" + m_siteName);
                m_tempDirectoryName = tempDirectory + "BenDownloader\\" + m_siteName + "\\";
                //Console.WriteLine(m_tempDirectoryName);
                m_lastFileDownloaded = GetLastDownloadedFile();
                m_lastFileDownloadedThisSession = "";

            }
            catch (Exception ex)
            {
                Program.Log(ex.ToString(), m_tempDirectoryName);
            }
        }

        ~BenRunner()
        {
            m_adoDataConnection.Dispose();
        }

        #endregion

        #region [Methods]
        public bool XferAllFiles()
        {
            DataRow log;
            try
            {
                //XferDataFiles();
                UpdateTimestamps();
                ExecNotepad();

                log = m_adoDataConnection.RetrieveRow("SELECT * FROM StatusLog WHERE DeviceID = {0}", int.Parse(m_deviceRecord["ID"].ToString()));

                try
                {
                    if (m_deviceRecord["ID"].ToString() == log["DeviceID"].ToString() && m_lastFileDownloadedThisSession != "")
                        m_adoDataConnection.ExecuteNonQuery("Update StatusLog SET LastSuccess = {0}, LastFile = {1}", DateTime.UtcNow, m_lastFileDownloadedThisSession);
                    else if(m_deviceRecord["ID"].ToString() == log["DeviceID"].ToString() && m_lastFileDownloadedThisSession == "")
                        m_adoDataConnection.ExecuteNonQuery("Update StatusLog SET LastSuccess = {0}", DateTime.UtcNow);
                    else
                        m_adoDataConnection.ExecuteNonQuery("INSERT INTO StatusLog (LastSuccess , LastFile, DeviceID) VALUES ({0},{1},{2})", DateTime.UtcNow, m_lastFileDownloadedThisSession, m_deviceRecord["ID"]);
                }
                catch (Exception ex)
                {
                    Program.Log("StatusLog Update (" + m_siteName + "): " + ex.Message, m_tempDirectoryName);
                }

                return true;
            }
            catch (Exception ex)
            {
                Program.Log("Ben5K XferAllFiles (" + m_siteName + "): " + ex.ToString(), m_tempDirectoryName);

                log = m_adoDataConnection.RetrieveRow("SELECT * FROM StatusLog WHERE DeviceID = {0}", int.Parse(m_deviceRecord["ID"].ToString()));

                try
                {
                    if (m_deviceRecord["ID"].ToString() == log["DeviceID"].ToString())
                        m_adoDataConnection.ExecuteNonQuery("Update StatusLog SET LastFailure = {0}, Message = {1}", DateTime.UtcNow, ex.Message);<commit_msg>fixed bug with statuslog updates<commit_after>﻿//******************************************************************************************************
//  BenRunner.cs - Gbtc
//
//  Copyright © 2016, Grid Protection Alliance.  All Rights Reserved.
//
//  Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See
//  the NOTICE file distributed with this work for additional information regarding copyright ownership.
//  The GPA licenses this file to you under the MIT License (MIT), the "License"; you may
//  not use this file except in compliance with the License. You may obtain a copy of the License at:
//
//      http://opensource.org/licenses/MIT
//
//  Unless agreed to in writing, the subject software distributed under the License is distributed on an
//  "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the
//  License for the specific language governing permissions and limitations.
//
//  Code Modification History:
//  ----------------------------------------------------------------------------------------------------
//  10/06/2016 - <NAME>
//       Generated original version of source code.
//
//******************************************************************************************************


using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net.Mail;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using GSF;
using GSF.Web.Hubs;
using GSF.Identity;
using System.Security.Principal;
using GSF.Configuration;
using GSF.Data;
using GSF.Data.Model;
using GSF.IO;
using GSF.Parsing;
using GSF.Web.Model;
using Microsoft.VisualBasic;
using Microsoft.VisualBasic.FileIO;
using FileSystem = Microsoft.VisualBasic.FileIO.FileSystem;
namespace BenDownloader
{
    public class BenRunner
    {
        #region [Members]

        private const long BENMAXFILESIZE = 7000;
        private const int MAXFILELIMIT = 100;

        private readonly string m_ipAddress;
        private readonly string m_localPath;
        private readonly string m_folder;
        private readonly string m_siteName;
        private readonly string m_serialNumber;
        private readonly string m_tempDirectoryName;
        private readonly BenRecord m_lastFileDownloaded;
        private readonly DataRow m_deviceRecord;
        private readonly DataRow m_connectionProfile;
        private readonly Dictionary<string, string> m_connectionProfileTaskSettings;
        private string m_lastFileDownloadedThisSession;
        private readonly AdoDataConnection m_adoDataConnection;

        #endregion

        #region [Constructors]

        public BenRunner(int deviceId, int taskId)
        {
            m_adoDataConnection = new AdoDataConnection(Program.OpenMiConfigurationFile.Settings["systemSettings"]["ConnectionString"].Value, Program.OpenMiConfigurationFile.Settings["systemSettings"]["DataProviderString"].Value);

            try
            {

                string taskSettingsString = m_adoDataConnection.ExecuteScalar<string>("Select Settings From ConnectionProfileTask WHERE ID = {0}", taskId);
                m_connectionProfileTaskSettings = taskSettingsString.ParseKeyValuePairs();
                m_deviceRecord = m_adoDataConnection.RetrieveRow("Select * From Device WHERE ID = {0}", deviceId);
                Dictionary<string, string> deviceConnection = m_deviceRecord["connectionString"].ToString().ParseKeyValuePairs();

                m_connectionProfile = m_adoDataConnection.RetrieveRow("SELECT * FROM connectionProfile WHERE ID = {0}", deviceConnection["connectionProfileID"]);
                m_folder = m_deviceRecord["OriginalSource"].ToString();
                m_ipAddress = deviceConnection["connectionUserName"].Split('&')[0];
                m_localPath = m_connectionProfileTaskSettings["localPath"];
                m_siteName = m_deviceRecord["Name"].ToString();
                m_serialNumber = deviceConnection["connectionUserName"].Split('&')[1];

                string tempDirectory = System.IO.Path.GetTempPath();
                System.IO.Directory.CreateDirectory(tempDirectory + "\\BenDownloader\\" + m_siteName);
                m_tempDirectoryName = tempDirectory + "BenDownloader\\" + m_siteName + "\\";
                //Console.WriteLine(m_tempDirectoryName);
                m_lastFileDownloaded = GetLastDownloadedFile();
                m_lastFileDownloadedThisSession = "";

            }
            catch (Exception ex)
            {
                Program.Log(ex.ToString(), m_tempDirectoryName);
            }
        }

        ~BenRunner()
        {
            m_adoDataConnection.Dispose();
        }

        #endregion

        #region [Methods]
        public bool XferAllFiles()
        {
            DataRow log;
            try
            {
                XferDataFiles();
                //UpdateTimestamps();
                //ExecNotepad();

                log = m_adoDataConnection.RetrieveRow("SELECT * FROM StatusLog WHERE DeviceID = {0}", int.Parse(m_deviceRecord["ID"].ToString()));

                try
                {
                    if (m_deviceRecord["ID"].ToString() == log["DeviceID"].ToString() && m_lastFileDownloadedThisSession != "")
                        m_adoDataConnection.ExecuteNonQuery("Update StatusLog SET LastSuccess = {0}, LastFile = {1} WHERE DeviceID = {2}", DateTime.UtcNow, m_lastFileDownloadedThisSession, m_deviceRecord["ID"]);
                    else if(m_deviceRecord["ID"].ToString() == log["DeviceID"].ToString() && m_lastFileDownloadedThisSession == "")
                        m_adoDataConnection.ExecuteNonQuery("Update StatusLog SET LastSuccess = {0} WHERE DeviceID = {1}", DateTime.UtcNow, m_deviceRecord["ID"]);
                    else
                        m_adoDataConnection.ExecuteNonQuery("INSERT INTO StatusLog (LastSuccess , LastFile, DeviceID) VALUES ({0},{1},{2})", DateTime.UtcNow, m_lastFileDownloadedThisSession, m_deviceRecord["ID"]);
                }
                catch (Exception ex)
                {
                    Program.Log("StatusLog Update (" + m_siteName + "): " + ex.Message, m_tempDirectoryName);
                }

                return true;
            }
            catch (Exception ex)
            {
                Program.Log("Ben5K XferAllFiles (" + m_siteName + "): " + ex.ToString(), m_tempDirectoryName);

                log = m_adoDataConnection.RetrieveRow("SELECT * FROM StatusLog WHERE DeviceID = {0}", int.Parse(m_deviceRecord["ID"].ToString()));

                try
                {
                    if (m_deviceRecord["ID"].ToString() == log["DeviceID"].ToString())
                        m_adoDataConnection.ExecuteNonQuery("Update StatusLog SET LastFailure = {0}, Message = {1} WHERE DeviceID = {2}", DateTime.UtcNow, ex.Message, m_deviceRecord["ID"]);
{'fraction_non_alphanumeric': 0.13776173285198556, 'fraction_numerical': 0.005270758122743682, 'mean_word_length': 2.584627329192547, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 24, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5202367', 'n_tokens_mistral': 3449, 'n_tokens_neox': 3293, 'n_words': 841}