Browse Source

Исправление ошибок для #EBT-21 #EBT-10
Добавление тестового функционала

Veloe 4 years ago
parent
commit
1cae54f9c1
2 changed files with 163 additions and 129 deletions
  1. 10 0
      EthermineBotTelegram/JsonEtherscanGas.cs
  2. 153 129
      EthermineBotTelegram/Program.cs

+ 10 - 0
EthermineBotTelegram/JsonEtherscanGas.cs

@@ -0,0 +1,10 @@
+namespace EthermineBotTelegram
+{
+    public class JsonEtherscanGas
+    {
+        public string status { get; set; }
+        public string message { get; set; }
+        public string result { get; set; }
+    }
+}
+

+ 153 - 129
EthermineBotTelegram/Program.cs

@@ -1,24 +1,16 @@
 using System;
-using System.Collections.Generic;
-using System.Configuration;
-using System.Data.Entity;
-using System.Data.Entity.Migrations;
 using System.Linq;
-using System.Text;
 using System.Threading.Tasks;
 using Telegram.Bot;
 using Telegram.Bot.Args;
 using Telegram.Bot.Types;
 using Telegram.Bot.Types.Enums;
-using Newtonsoft.Json;
-using System.Net;
 using System.Net.Http;
 using System.Threading;
 using EFDatabase;
 using MySql.Data.MySqlClient;
 using Quartz;
 using Quartz.Impl;
-using Quartz.Util;
 
 namespace EthermineBotTelegram
 {
@@ -159,13 +151,15 @@ namespace EthermineBotTelegram
                 var currnentStats = JsonDownloader._download_serialized_json_data<JsonCurrentStats>(url);
                 await botClient.SendTextMessageAsync(
                     chatId: e.Message.Chat,
-                    text: "Updated: " + DateTimeOffset.FromUnixTimeSeconds(currnentStats.data.time).LocalDateTime.ToString("f") + "\n"
+                    text: "Updated: " + DateTimeOffset.FromUnixTimeSeconds(currnentStats.data.time).LocalDateTime
+                              .ToString("f") + "\n"
                           + "Reported Hashrate: " + Math.Round(currnentStats.data.reportedHashrate / 1000000D, 3) +
                           " MH/s\n"
                           + "Current Hashrate: " + Math.Round(currnentStats.data.currentHashrate / 1000000D, 3) +
                           " MH/s\n"
                           + "Valid Shares: " + currnentStats.data.validShares + "\n"
                           + "Stale Shares: " + currnentStats.data.staleShares + "\n"
+                          + "Workers: " + currnentStats.data.activeWorkers + "\n"
                           + "Unpaid Balance: " + Math.Round(currnentStats.data.unpaid / Math.Pow(10, 18), 5) + " ETH\n");
             }
             catch (Exception exception)
@@ -173,7 +167,7 @@ namespace EthermineBotTelegram
                 await Console.Out.WriteLineAsync(exception.Message);
                 await botClient.SendTextMessageAsync(
                     chatId: e.Message.Chat,
-                    text:   "Something got wrong! Check entered wallet or try later.");
+                    text:   "An exception occurred while executing this command.");
             }
         }
         
@@ -193,14 +187,14 @@ namespace EthermineBotTelegram
                         //var lastWorkerRecord = new workers();
                         var lastTime = Botdb.workers.Where(w => w.wallet == wallet)
                             .Max(w => w.time);
+                        var lastWorkerRecord = Botdb.workers.Where(w => w.wallet == wallet && w.time == lastTime)
+                            .OrderByDescending(w => w.time).ToList();
                         
-                        if (lastTime != null)
+                        if (lastWorkerRecord != null)
                         {
-                            var lastWorkerRecord = Botdb.workers.Where(w => w.wallet == wallet && w.time == lastTime)
-                                .OrderByDescending(w => w.time).ToList();
                             await botClient.SendTextMessageAsync(
                                 chatId: e,
-                                text: $"Miner {lastMinerRecord.wallet} stats\n" +
+                                text: $"Miner {lastMinerRecord.wallet} stats:\n" +
                                       "Updated: " + DateTimeOffset.FromUnixTimeSeconds(lastMinerRecord.time)
                                           .LocalDateTime
                                           .ToString("f") + "\n"
@@ -210,6 +204,7 @@ namespace EthermineBotTelegram
                                       Math.Round(lastMinerRecord.current_hashrate / 1000000D, 3) + " MH/s\n"
                                       + "Valid Shares: " + lastMinerRecord.valid_shares + "\n"
                                       + "Stale Shares: " + lastMinerRecord.stale_shares + "\n"
+                                      + "Workers: " + lastMinerRecord.workers + "\n"
                                       + "Unpaid Balance: " + Math.Round(lastMinerRecord.unpaid / Math.Pow(10, 18), 5) +
                                       " ETH\n");
                             for (int i = 0; i < lastWorkerRecord.Count; i++)
@@ -221,7 +216,7 @@ namespace EthermineBotTelegram
                                 {
                                     await botClient.SendTextMessageAsync(
                                         chatId: e,
-                                        text: $"Worker {lastWorkerRecord[i].worker} stats\n" +
+                                        text: $"Worker {lastWorkerRecord[i].worker} stats:\n" +
                                               "Updated: " + DateTimeOffset.FromUnixTimeSeconds(lastWorkerRecord[i].time)
                                                   .LocalDateTime
                                                   .ToString("f") + "\n"
@@ -244,17 +239,23 @@ namespace EthermineBotTelegram
                         }
                         else
                         {
-                            //no worker rows
+                            await botClient.SendTextMessageAsync(
+                                chatId: e,
+                                text:   "No workers data for now!");
                         }
                     }
                     else
                     {
-                        //no miner rows
+                        await botClient.SendTextMessageAsync(
+                            chatId: e,
+                            text:   "No miner data for now!");
                     }
                 }
                 else
                 {
-                    //no wallet exeption
+                    await botClient.SendTextMessageAsync(
+                        chatId: e,
+                        text:   "Set wallet at first! Use /setwallet");
                 }
             //}
             //catch (Exception exception)
@@ -282,7 +283,7 @@ namespace EthermineBotTelegram
             {
                 await botClient.SendTextMessageAsync(
                     chatId: e.Message.Chat,
-                    text:   "Something got wrong! Check entered wallet or try later.");
+                    text:   "An exception occurred while executing this command.");
             }
         }
 
@@ -336,28 +337,42 @@ namespace EthermineBotTelegram
             catch (Exception exception)
             {
                 await Console.Out.WriteLineAsync(exception.Message);
+                await botClient.SendTextMessageAsync(
+                    chatId: e.Message.Chat,
+                    text:   "An exception occurred while executing this command.");
             }
         }
 
         static async void DeleteUser(Chat e)
         {
-            if (Botdb.users.Where(users => users.chat_id == e.Id).FirstOrDefault() != null)
+            try
             {
-                var deletableWallet = Botdb.users.Where(u => u.chat_id == e.Id).First().wallet;
-                Botdb.workers.RemoveRange(Botdb.workers.Where(w=> w.wallet == deletableWallet));
-                Botdb.miners.RemoveRange(Botdb.miners.Where(m=> m.wallet == deletableWallet));
-                Botdb.users.RemoveRange(Botdb.users.Where(u=> u.chat_id == e.Id));
-                Botdb.SaveChanges();
-                await botClient.SendTextMessageAsync(
-                    chatId: e,
-                    text:   "Done!");
+                if (Botdb.users.Where(users => users.chat_id == e.Id).FirstOrDefault() != null)
+                {
+                    var deletableWallet = Botdb.users.Where(u => u.chat_id == e.Id).First().wallet;
+                    Botdb.workers.RemoveRange(Botdb.workers.Where(w=> w.wallet == deletableWallet));
+                    Botdb.miners.RemoveRange(Botdb.miners.Where(m=> m.wallet == deletableWallet));
+                    Botdb.users.RemoveRange(Botdb.users.Where(u=> u.chat_id == e.Id));
+                    Botdb.SaveChanges();
+                    await botClient.SendTextMessageAsync(
+                        chatId: e,
+                        text:   "Done!");
+                }
+                else
+                {
+                    await botClient.SendTextMessageAsync(
+                        chatId: e,
+                        text:   "Already deleted");
+                }
             }
-            else
+            catch (Exception exception)
             {
+                await Console.Out.WriteLineAsync(exception.Message);
                 await botClient.SendTextMessageAsync(
                     chatId: e,
-                    text:   "Already deleted");
+                    text:   "An exception occurred while executing this command.");
             }
+            
         }
 
         public static async Task UpdateData()
@@ -365,133 +380,142 @@ namespace EthermineBotTelegram
             var usersList = Botdb.users.Where(u=>u.wallet != null).ToList();
             foreach (users u in usersList)
             {
-                //try
-               // {
-                var url = AppSettings.poolApiUrl + "/miner/" + u.wallet + "/currentStats";
-                var currnentStats = JsonDownloader._download_serialized_json_data<JsonCurrentStats>(url);
-                
-                if (currnentStats.status == "OK")
+                try
                 {
-                    await Console.Out.WriteLineAsync($"Create new record for {u.wallet}");
-                    miners newMinerRecord = new miners();
-                    newMinerRecord.wallet = u.wallet;
-                    newMinerRecord.time = currnentStats.data.time;
-                    newMinerRecord.reported_hashrate = currnentStats.data.reportedHashrate;
-                    newMinerRecord.current_hashrate = currnentStats.data.currentHashrate;
-                    newMinerRecord.valid_shares = currnentStats.data.validShares;
-                    newMinerRecord.stale_shares = currnentStats.data.staleShares;
-                    newMinerRecord.invalid_shares = currnentStats.data.invalidShares;
-                    newMinerRecord.workers = currnentStats.data.activeWorkers;
-                    newMinerRecord.unpaid = currnentStats.data.unpaid;
-                    await Console.Out.WriteLineAsync($"New record creating complete {newMinerRecord.wallet}");
-                    long lastTime = 0;
-                    if (Botdb.miners.Where(m => m.wallet == newMinerRecord.wallet).FirstOrDefault() != null) 
-                        lastTime = Botdb.miners.Where(m => m.wallet == newMinerRecord.wallet).Max(m => m.time);
-                    var lastMinerRecord = Botdb.miners.Where(m => m.wallet == newMinerRecord.wallet && m.time == lastTime).FirstOrDefault();
-                    if (lastMinerRecord == null || lastMinerRecord.time !=
-                        newMinerRecord.time)
+                    var url = AppSettings.poolApiUrl + "/miner/" + u.wallet + "/currentStats";
+                    var currnentStats = JsonDownloader._download_serialized_json_data<JsonCurrentStats>(url);
+                    
+                    if (currnentStats.status == "OK")
                     {
-                        Botdb.miners.Add(newMinerRecord);
-                        //await Botdb.SaveChangesAsync();
-                        await Console.Out.WriteLineAsync($"Added new row for {newMinerRecord.wallet}");
-                        url = AppSettings.poolApiUrl + "/miner/" + u.wallet + "/workers";
-                        var currentWorker = JsonDownloader._download_serialized_json_data<JsonWorker>(url);
-                        
-                        if (currentWorker.status == "OK")
+                        await Console.Out.WriteLineAsync($"Create new record for {u.wallet}");
+                        miners newMinerRecord = new miners();
+                        newMinerRecord.wallet = u.wallet;
+                        newMinerRecord.time = currnentStats.data.time;
+                        newMinerRecord.reported_hashrate = currnentStats.data.reportedHashrate;
+                        newMinerRecord.current_hashrate = currnentStats.data.currentHashrate;
+                        newMinerRecord.valid_shares = currnentStats.data.validShares;
+                        newMinerRecord.stale_shares = currnentStats.data.staleShares;
+                        newMinerRecord.invalid_shares = currnentStats.data.invalidShares;
+                        newMinerRecord.workers = currnentStats.data.activeWorkers;
+                        newMinerRecord.unpaid = currnentStats.data.unpaid;
+                        await Console.Out.WriteLineAsync($"New record creating complete {newMinerRecord.wallet}");
+                        long lastTime = 0;
+                        if (Botdb.miners.Where(m => m.wallet == newMinerRecord.wallet).FirstOrDefault() != null) 
+                            lastTime = Botdb.miners.Where(m => m.wallet == newMinerRecord.wallet).Max(m => m.time);
+                        var lastMinerRecord = Botdb.miners.Where(m => m.wallet == newMinerRecord.wallet && m.time == lastTime).FirstOrDefault();
+                        if (lastMinerRecord == null || lastMinerRecord.time !=
+                            newMinerRecord.time)
                         {
-                            for (int i = 0; i < currentWorker.data.Count(); i++)
+                            Botdb.miners.Add(newMinerRecord);
+                            //await Botdb.SaveChangesAsync();
+                            await Console.Out.WriteLineAsync($"Added new row for {newMinerRecord.wallet}");
+                            url = AppSettings.poolApiUrl + "/miner/" + u.wallet + "/workers";
+                            var currentWorker = JsonDownloader._download_serialized_json_data<JsonWorker>(url);
+                            
+                            if (currentWorker.status == "OK")
                             {
-                                await Console.Out.WriteLineAsync($"Create new record for {currentWorker.data[i].worker}");
-                                var newWorkerRecord = new workers();
-                                newWorkerRecord.wallet = newMinerRecord.wallet;
-                                newWorkerRecord.time = currentWorker.data[i].time;
-                                newWorkerRecord.worker = currentWorker.data[i].worker;
-                                newWorkerRecord.reported_hashrate = currentWorker.data[i].reportedHashrate;
-                                newWorkerRecord.current_hashrate = currentWorker.data[i].currentHashrate;
-                                newWorkerRecord.valid_shares = currentWorker.data[i].validShares;
-                                newWorkerRecord.stale_shares = currentWorker.data[i].staleShares;
-                                newWorkerRecord.invalid_shares = currentWorker.data[i].invalidShares;
-
-                                var lastWorkerRecord = Botdb.workers.Where(w => w.worker == newWorkerRecord.worker)
-                                    .OrderByDescending(w => w.time)
-                                    .FirstOrDefault();
-                                if (lastWorkerRecord != null)
+                                for (int i = 0; i < currentWorker.data.Count(); i++)
                                 {
-                                    await Console.Out.WriteLineAsync($"lastWorkerRecord time = {lastWorkerRecord.time}");
-                                    lastMinerRecord = Botdb.miners.Where(w => w.wallet == newMinerRecord.wallet)
+                                    await Console.Out.WriteLineAsync($"Create new record for {currentWorker.data[i].worker}");
+                                    var newWorkerRecord = new workers();
+                                    newWorkerRecord.wallet = newMinerRecord.wallet;
+                                    newWorkerRecord.time = currentWorker.data[i].time;
+                                    newWorkerRecord.worker = currentWorker.data[i].worker;
+                                    newWorkerRecord.reported_hashrate = currentWorker.data[i].reportedHashrate;
+                                    newWorkerRecord.current_hashrate = currentWorker.data[i].currentHashrate;
+                                    newWorkerRecord.valid_shares = currentWorker.data[i].validShares;
+                                    newWorkerRecord.stale_shares = currentWorker.data[i].staleShares;
+                                    newWorkerRecord.invalid_shares = currentWorker.data[i].invalidShares;
+
+                                    var lastWorkerRecord = Botdb.workers.Where(w => w.worker == newWorkerRecord.worker)
                                         .OrderByDescending(w => w.time)
                                         .FirstOrDefault();
-                                    if (lastMinerRecord != null)
+                                    if (lastWorkerRecord != null)
                                     {
-                                        await Console.Out.WriteLineAsync($"lastMinerRecord time = {lastMinerRecord.time}");
-                                        //check for payout
-                                        if (newMinerRecord.unpaid < lastMinerRecord.unpaid)
+                                        await Console.Out.WriteLineAsync($"lastWorkerRecord time = {lastWorkerRecord.time}");
+                                        lastMinerRecord = Botdb.miners.Where(w => w.wallet == newMinerRecord.wallet)
+                                            .OrderByDescending(w => w.time)
+                                            .FirstOrDefault();
+                                        if (lastMinerRecord != null)
                                         {
-                                        await botClient.SendTextMessageAsync(
-                                                chatId: Botdb.users.Where(user=>user.wallet == newMinerRecord.wallet).FirstOrDefault().chat_id,
-                                                text:   "Payout detected!");
-                                            
-                                            url = AppSettings.poolApiUrl + "/miner/" + u.wallet + "/payouts";
-                                            var payouts = JsonDownloader._download_serialized_json_data<JsonPayouts>(url);
-                                            
-                                            await Console.Out.WriteLineAsync($"Last payout time = {DateTimeOffset.FromUnixTimeSeconds(payouts.data[0].paidOn).LocalDateTime.ToString("f")}");
+                                            await Console.Out.WriteLineAsync($"lastMinerRecord time = {lastMinerRecord.time}");
+                                            //check for payout
+                                            if (newMinerRecord.unpaid < lastMinerRecord.unpaid)
+                                            {
+                                            await botClient.SendTextMessageAsync(
+                                                    chatId: Botdb.users.Where(user=>user.wallet == newMinerRecord.wallet).FirstOrDefault().chat_id,
+                                                    text:   "Payout detected!");
+                                                
+                                                url = AppSettings.poolApiUrl + "/miner/" + u.wallet + "/payouts";
+                                                var payouts = JsonDownloader._download_serialized_json_data<JsonPayouts>(url);
+                                                
+                                                await Console.Out.WriteLineAsync($"Last payout time = {DateTimeOffset.FromUnixTimeSeconds(payouts.data[0].paidOn).LocalDateTime.ToString("f")}");
+                                            }
+                                            else
+                                            {
+                                                //no check that last balance and prev balance are the same
+                                                newWorkerRecord.worker_unpaid = lastWorkerRecord.worker_unpaid +
+                                                                                (newMinerRecord.unpaid -
+                                                                                 lastMinerRecord.unpaid) *
+                                                                                (newWorkerRecord.reported_hashrate /
+                                                                                 ((double) newMinerRecord.reported_hashrate));
+                                                if (Double.IsNaN(newWorkerRecord.worker_unpaid) ||
+                                                    Double.IsInfinity(newWorkerRecord.worker_unpaid))
+                                                    newWorkerRecord.worker_unpaid = lastWorkerRecord.worker_unpaid;
+                                                await Console.Out.WriteLineAsync($"newWorkerRecord unpaid change = {newMinerRecord.unpaid - lastMinerRecord.unpaid}");
+                                            }
                                         }
                                         else
                                         {
-                                            //no check that last balance and prev balance are the same
-                                            newWorkerRecord.worker_unpaid = lastWorkerRecord.worker_unpaid +
-                                                                            (newMinerRecord.unpaid -
-                                                                             lastMinerRecord.unpaid) *
-                                                                            (newWorkerRecord.reported_hashrate /
-                                                                             ((double) newMinerRecord.reported_hashrate));
-                                            if (Double.IsNaN(newWorkerRecord.worker_unpaid) ||
-                                                Double.IsInfinity(newWorkerRecord.worker_unpaid))
-                                                newWorkerRecord.worker_unpaid = lastWorkerRecord.worker_unpaid;
-                                            await Console.Out.WriteLineAsync($"newWorkerRecord unpaid change = {newMinerRecord.unpaid - lastMinerRecord.unpaid}");
+                                            newWorkerRecord.worker_unpaid = 0;
                                         }
                                     }
                                     else
                                     {
                                         newWorkerRecord.worker_unpaid = 0;
                                     }
-                                }
-                                else
-                                {
-                                    newWorkerRecord.worker_unpaid = 0;
-                                }
-                                await Console.Out.WriteLineAsync($"newWorkerRecord worker unpaid = {newWorkerRecord.worker_unpaid}");
-
-                                if (lastWorkerRecord == null || newWorkerRecord.time != lastWorkerRecord.time)
-                                {
-                                    Botdb.workers.Add(newWorkerRecord);
-                                    //await Botdb.SaveChangesAsync();
-                                }
-                                //Botdb.SaveChanges();
-                                else
-                                {
-                                    Botdb.workers.Add(newWorkerRecord);
-                                    
+                                    await Console.Out.WriteLineAsync($"newWorkerRecord worker unpaid = {newWorkerRecord.worker_unpaid}");
+                                    //this if does nothing
+                                    if (lastWorkerRecord == null || newWorkerRecord.time != lastWorkerRecord.time)
+                                    {
+                                        Botdb.workers.Add(newWorkerRecord);
+                                        //await Botdb.SaveChangesAsync();
+                                    }
+                                    //Botdb.SaveChanges();
+                                    else
+                                    {
+                                        Botdb.workers.Add(newWorkerRecord);
+                                        
+                                    }
                                 }
                             }
                         }
+                        else
+                        {
+                            await Console.Out.WriteLineAsync($"Row with wallet: {newMinerRecord.wallet} and time: {newMinerRecord.time} already exists!");
+                        }
+
+                        try
+                        {
+                            await Botdb.SaveChangesAsync();
+                            await Console.Out.WriteLineAsync($"Saved");
+                        }
+                        catch (Exception exception)
+                        {
+                            await Console.Out.WriteLineAsync(exception.Message);
+                        }
+                        
                     }
                     else
                     {
-                        await Console.Out.WriteLineAsync($"Row with wallet: {newMinerRecord.wallet} and time: {newMinerRecord.time} already exists!");
+                        await Console.Out.WriteLineAsync($"Error response from pool for {u.wallet}!");
                     }
-                    await Botdb.SaveChangesAsync();
-                    await Console.Out.WriteLineAsync($"Saved");
+                
                 }
-                else
+                catch (Exception exception)
                 {
-                    await Console.Out.WriteLineAsync($"Error response from pool for {u.wallet}!");
+                 await Console.Out.WriteLineAsync(exception.Message);
                 }
-                
-                //}
-             //catch (Exception exception)
-             //{
-             //    await Console.Out.WriteLineAsync(exception.Message);
-             //}
             }