|
@@ -13,6 +13,8 @@ using Newtonsoft.Json;
|
|
|
using System.Net;
|
|
|
using EFDatabase;
|
|
|
using MySql.Data.MySqlClient;
|
|
|
+using Quartz;
|
|
|
+using Quartz.Impl;
|
|
|
|
|
|
namespace EthermineBotTelegram
|
|
|
{
|
|
@@ -20,10 +22,14 @@ namespace EthermineBotTelegram
|
|
|
{
|
|
|
static ITelegramBotClient botClient;
|
|
|
static EFDatabase.botdb Botdb;
|
|
|
- static void Main(string[] args)
|
|
|
+
|
|
|
+ static async Task Main(string[] args)
|
|
|
{
|
|
|
botClient = new TelegramBotClient("1785154817:AAGhXD9yQVn9HPdWTcmGJUBeZ8nA50SzHbY");
|
|
|
var me = botClient.GetMeAsync().Result;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
Console.WriteLine(
|
|
|
$"Hello, World! I am user {me.Id} and my name is {me.FirstName}."
|
|
|
);
|
|
@@ -39,23 +45,52 @@ namespace EthermineBotTelegram
|
|
|
{
|
|
|
Botdb.Database.Connection.Open();
|
|
|
Botdb.Database.Connection.Close();
|
|
|
+
|
|
|
+
|
|
|
+ //InitUpdater("server=" + AppSettings.dbIp + ";port=3306;database=botdb;uid=" + user + "; pwd=" + pass);
|
|
|
+
|
|
|
+ // Grab the Scheduler instance from the Factory
|
|
|
+ StdSchedulerFactory factory = new StdSchedulerFactory();
|
|
|
+ IScheduler scheduler = await factory.GetScheduler();
|
|
|
+
|
|
|
+ // and start it off
|
|
|
+ await scheduler.Start();
|
|
|
+
|
|
|
+ // define the job and tie it to our HelloJob class
|
|
|
+ IJobDetail job = JobBuilder.Create<DataUpdater>()
|
|
|
+ .WithIdentity("job1", "group1")
|
|
|
+ .UsingJobData("connectionString", "server=" + AppSettings.dbIp + ";port=3306;database=botdb;uid=" + user + "; pwd=" + pass)
|
|
|
+ .Build();
|
|
|
+ // Trigger the job to run now, and then repeat every 10 seconds
|
|
|
+ ITrigger trigger = TriggerBuilder.Create()
|
|
|
+ .WithIdentity("trigger1", "group1")
|
|
|
+ .StartNow()
|
|
|
+ .WithSimpleSchedule(x => x
|
|
|
+ .WithIntervalInSeconds(240)
|
|
|
+ .RepeatForever())
|
|
|
+ .Build();
|
|
|
+ // Tell quartz to schedule the job using our trigger
|
|
|
+ await scheduler.ScheduleJob(job, trigger);
|
|
|
+
|
|
|
+
|
|
|
+ Console.WriteLine(("Scheduler started!"));
|
|
|
+
|
|
|
+ botClient.OnMessage += BotOnMessage;
|
|
|
+ botClient.StartReceiving();
|
|
|
+
|
|
|
+ Console.WriteLine("Press any key to exit");
|
|
|
+ Console.ReadKey();
|
|
|
+
|
|
|
+ await scheduler.Shutdown();
|
|
|
+ botClient.StopReceiving();
|
|
|
}
|
|
|
- catch(MySqlException)
|
|
|
+ catch (MySqlException)
|
|
|
{
|
|
|
Console.WriteLine("Don't connected! Check auth data and restart!");
|
|
|
}
|
|
|
-
|
|
|
- //ask for username and password for db
|
|
|
-
|
|
|
- botClient.OnMessage += BotOnMessage;
|
|
|
- botClient.StartReceiving();
|
|
|
-
|
|
|
- Console.WriteLine("Press any key to exit");
|
|
|
- Console.ReadKey();
|
|
|
|
|
|
- botClient.StopReceiving();
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
static async void BotOnMessage(object sender, MessageEventArgs e) {
|
|
|
if (e.Message.Text != null)
|
|
|
{
|
|
@@ -109,7 +144,7 @@ namespace EthermineBotTelegram
|
|
|
try
|
|
|
{
|
|
|
var url = AppSettings.poolApiUrl + "/miner/" + e.Message.Text.Split(' ')[1] + "/currentStats";
|
|
|
- var currnentStats = _download_serialized_json_data<JsonCurrnentStats>(url);
|
|
|
+ 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"
|
|
@@ -189,17 +224,88 @@ namespace EthermineBotTelegram
|
|
|
text: "Already deleted");
|
|
|
}
|
|
|
}
|
|
|
- private static T _download_serialized_json_data<T>(string url) where T : new() {
|
|
|
- using (var w = new WebClient()) {
|
|
|
- var jsonData = string.Empty;
|
|
|
- // attempt to download JSON data as a string
|
|
|
- try
|
|
|
+
|
|
|
+ public async static Task UpdateData()
|
|
|
+ {
|
|
|
+ var usersList = Botdb.users.Where(u=>u.wallet != null).ToList();
|
|
|
+ foreach (users u in usersList)
|
|
|
+ {
|
|
|
+ var url = AppSettings.poolApiUrl + "/miner/" + u.wallet + "/currentStats";
|
|
|
+ var currnentStats = JsonDownloader._download_serialized_json_data<JsonCurrentStats>(url);
|
|
|
+
|
|
|
+ if (currnentStats.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)
|
|
|
+ {
|
|
|
+ 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")
|
|
|
+ {
|
|
|
+ for (int i = 0; i < currentWorker.data.Count(); i++)
|
|
|
+ {
|
|
|
+ await Console.Out.WriteLineAsync($"Create new record for {currentWorker.data[i].worker}");
|
|
|
+ workers 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;
|
|
|
+ //add func to calc worker_unpaid here
|
|
|
+ newWorkerRecord.worker_unpaid = 0;
|
|
|
+
|
|
|
+ lastTime = 0;
|
|
|
+ if (Botdb.workers.Where(m => m.wallet == newMinerRecord.wallet).FirstOrDefault() != null)
|
|
|
+ lastTime = Botdb.workers.Where(m => m.wallet == newMinerRecord.wallet).Max(m => m.time);
|
|
|
+ var lastWokerRecord = Botdb.workers.Where(m => m.wallet == newWorkerRecord.worker && m.time == lastTime).FirstOrDefault();
|
|
|
+ if (lastWokerRecord == null || lastWokerRecord.time !=
|
|
|
+ newMinerRecord.time)
|
|
|
+ {
|
|
|
+ await Console.Out.WriteLineAsync(
|
|
|
+ $"Create new record for {currentWorker.data[i].worker} finished");
|
|
|
+ Botdb.workers.Add(newWorkerRecord);
|
|
|
+ await Botdb.SaveChangesAsync();
|
|
|
+ await Console.Out.WriteLineAsync($"Added new row for {currentWorker.data[i].worker}");
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ await Console.Out.WriteLineAsync($"Row with miner: {newWorkerRecord.worker} and time: {newWorkerRecord.time} already exists!");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ await Console.Out.WriteLineAsync($"Row with wallet: {newMinerRecord.wallet} and time: {newMinerRecord.time} already exists!");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
{
|
|
|
- jsonData = w.DownloadString(url);
|
|
|
+ await Console.Out.WriteLineAsync($"Error response from pool for {u.wallet}!");
|
|
|
}
|
|
|
- catch (Exception) { }
|
|
|
- // if string with JSON data is not empty, deserialize it to class and return its instance
|
|
|
- return !string.IsNullOrEmpty(jsonData) ? JsonConvert.DeserializeObject<T>(jsonData) : new T();
|
|
|
}
|
|
|
}
|
|
|
}
|