Skip to content

Commit

Permalink
add terminal user id (#59)
Browse files Browse the repository at this point in the history
and some refactor
  • Loading branch information
mrerro authored May 10, 2023
1 parent fb3476b commit 6c78fe5
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 32 deletions.
15 changes: 8 additions & 7 deletions PrinterApp/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ private App()
private void RebootHandler()
{
_printerModel.PrinterViewModel.DownloadNotInProgress = false;
_printerModel.PrinterViewModel.PrintQrVisibility = Visibility.Collapsed;
_printerModel.PrinterViewModel.CodeTextBoxText = "REBOOT";
_mainWindow.Close();
}
Expand Down Expand Up @@ -122,13 +123,13 @@ private static void ConfigureLogger(string fileName)
rollingInterval: RollingInterval.Day)
.CreateLogger();
#else
var log = new LoggerConfiguration()
.MinimumLevel.Information()
.WriteTo.Console()
.WriteTo.File(
$"{Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)}{Path.DirectorySeparatorChar}.printerAppLogs/{fileName}.txt",
rollingInterval: RollingInterval.Day)
.CreateLogger();
var log = new LoggerConfiguration()
.MinimumLevel.Information()
.WriteTo.Console()
.WriteTo.File(
$"{Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)}{Path.DirectorySeparatorChar}.printerAppLogs/{fileName}.txt",
rollingInterval: RollingInterval.Day)
.CreateLogger();
#endif
Log.Logger = log;
}
Expand Down
2 changes: 1 addition & 1 deletion PrinterApp/ConfigFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class ConfigFile
{
public string ExitCode { get; set; } = "dyakov";
public string TempSavePath { get; set; } = Path.GetTempPath() + ".printerApp";
public bool StartWithWindows { get; set; } = false;
public bool StartWithWindows { get; set; }
public bool AutoUpdate { get; set; } = true;
public string AuthorizationToken { get; set; } = "token";

Expand Down
8 changes: 5 additions & 3 deletions PrinterApp/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,10 @@ protected override void OnSourceInitialized(EventArgs e)
{
source.AddHook(WndProc);
}
else {
Log.Error($"{GetType().Name} {MethodBase.GetCurrentMethod()?.Name}: не удалось получить хэндл окна");
else
{
Log.Error(
$"{GetType().Name} {MethodBase.GetCurrentMethod()?.Name}: не удалось получить отслеживание окна");
Marketing.HwndSourceError();
}
}
Expand All @@ -228,4 +230,4 @@ private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref b

return IntPtr.Zero;
}
}
}
21 changes: 12 additions & 9 deletions PrinterApp/Marketing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,23 @@ public static class Marketing
BaseAddress = new Uri("https://api.test.profcomff.com/marketing/v1/"),
};
#else
private static readonly HttpClient SharedClient = new HttpClient
{
BaseAddress = new Uri("https://api.profcomff.com/marketing/v1/"),
};
private static readonly HttpClient SharedClient = new HttpClient
{
BaseAddress = new Uri("https://api.profcomff.com/marketing/v1/"),
};
#endif

private static readonly string AssemblyVersion =
Assembly.GetExecutingAssembly().GetName().Version?.ToString() ?? string.Empty;

public static string TerminalUserId = "";

private static void Post(string action, string status, string pathFrom,
string pathTo)
{
var body = new MarketingBody(action: action,
additional_data:
$"{{\"status\": \"{status}\",\"app_version\": \"{AssemblyVersion}\"}}",
$"{{\"status\": \"{status}\",\"app_version\": \"{AssemblyVersion}\",\"terminal_user_id\": \"{TerminalUserId}\"}}",
path_from: pathFrom, path_to: pathTo);
SharedClient.PostAsJsonAsync("action", body);
}
Expand All @@ -35,7 +38,7 @@ private static void Post(string action, string status)
{
var body = new MarketingBody(action: action,
additional_data:
$"{{\"status\": \"{status}\",\"app_version\": \"{AssemblyVersion}\"}}");
$"{{\"status\": \"{status}\",\"app_version\": \"{AssemblyVersion}\",\"terminal_user_id\": \"{TerminalUserId}\"}}");
SharedClient.PostAsJsonAsync("action", body);
}

Expand All @@ -44,7 +47,7 @@ private static void Post(string action, string status, float availableMem,
{
var body = new MarketingBody(action: action,
additional_data:
$"{{\"status\": \"{status}\",\"available_mem\": \"{availableMem}\",\"current_mem\": \"{currentMem}\",\"app_version\": \"{AssemblyVersion}\"}}");
$"{{\"status\": \"{status}\",\"available_mem\": \"{availableMem}\",\"current_mem\": \"{currentMem}\",\"app_version\": \"{AssemblyVersion}\",\"terminal_user_id\": \"{TerminalUserId}\"}}");
SharedClient.PostAsJsonAsync("action", body);
}

Expand Down Expand Up @@ -203,11 +206,11 @@ public static void QrGeneratorException(string status)
action: "print terminal qr generator exception",
status: status);
}

public static void HwndSourceError()
{
Post(
action: "print terminal failed to get window HwndSource",
status: "error");
}
}
}
19 changes: 12 additions & 7 deletions PrinterApp/MouseOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ public enum MouseEventFlags
private static extern bool GetCursorPos(out MousePoint lpMousePoint);

[DllImport("user32.dll")]
private static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);
private static extern void
mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);

public static void SetCursorPosition(int x, int y)
{
Expand All @@ -43,7 +44,11 @@ public static MousePoint GetCursorPosition()
{
MousePoint currentMousePoint;
var gotPoint = GetCursorPos(out currentMousePoint);
if (!gotPoint) { currentMousePoint = new MousePoint(0, 0); }
if (!gotPoint)
{
currentMousePoint = new MousePoint(0, 0);
}

return currentMousePoint;
}

Expand All @@ -53,10 +58,10 @@ public static void MouseEvent(MouseEventFlags value)

mouse_event
((int)value,
position.X,
position.Y,
0,
0)
position.X,
position.Y,
0,
0)
;
}

Expand All @@ -72,4 +77,4 @@ public MousePoint(int x, int y)
Y = y;
}
}
}
}
35 changes: 30 additions & 5 deletions PrinterApp/PrinterModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,22 @@
using System.Windows;
using QRCoder;
using QRCoder.Xaml;
using System.Collections.Generic;

namespace PrinterApp;

public class PrinterModel
{
#if DEBUG
private const string ApiUrl = "https://api.test.profcomff.com";
private const string FileUrl = "https://api.test.profcomff.com/print/file";
private const string StaticUrl = "https://api.test.profcomff.com/print/static";
private const string WebSockUrl = "wss://api.test.profcomff.com/print/qr";
#else
private const string FileUrl = "https://api.profcomff.com/print/file";
private const string StaticUrl = "https://api.profcomff.com/print/static";
private const string WebSockUrl = "wss://api.profcomff.com/print/qr";
private const string ApiUrl = "https://api.profcomff.com";
private const string FileUrl = "https://api.profcomff.com/print/file";
private const string StaticUrl = "https://api.profcomff.com/print/static";
private const string WebSockUrl = "wss://api.profcomff.com/print/qr";
#endif
private const string CodeError = "Некорректный код";
private const string HttpError = "Ошибка сети";
Expand Down Expand Up @@ -65,7 +68,28 @@ public PrinterModel(ConfigFile configFile, AutoUpdater autoUpdater)

_httpClient = new HttpClient();
_httpClient.DefaultRequestHeaders.Authorization
= new AuthenticationHeaderValue("token", _configFile.AuthorizationToken);
= new AuthenticationHeaderValue(_configFile.AuthorizationToken);

try
{
var response = _httpClient.GetAsync($"{ApiUrl}/auth/me");
response.Wait(5000);
response.Result.EnsureSuccessStatusCode();
var responseBody = response.Result.Content.ReadAsStringAsync();
responseBody.Wait(1000);
var responceString = responseBody.Result;
var htmlAttributes =
JsonConvert.DeserializeObject<Dictionary<string, string>>(responceString) ??
throw new InvalidOperationException();
Log.Information(htmlAttributes["id"]);
Marketing.TerminalUserId = htmlAttributes["id"];
}
catch (Exception e)
{
Log.Error($"{GetType().Name} {MethodBase.GetCurrentMethod()?.Name}: {e}");
throw;
}


if (SearchSumatraPdf() == "")
{
Expand Down Expand Up @@ -197,6 +221,7 @@ await _httpClient.GetStreamAsync(
PrinterViewModel.ErrorTextBlockVisibility = Visibility.Visible;
PrinterViewModel.ErrorTextBlockText = HttpError;
}

PrinterViewModel.DownloadNotInProgress = true;
Log.Debug(
$"{GetType().Name} {MethodBase.GetCurrentMethod()?.Name}: End response code {PrinterViewModel.CodeTextBoxText}");
Expand Down Expand Up @@ -293,7 +318,7 @@ private void ShowComplement()
PrinterViewModel.Compliment = Compliments.GetRandomCompliment();
await Task.Delay(5000);
PrinterViewModel.Compliment = "";
if(PrinterViewModel.DownloadNotInProgress)
if (PrinterViewModel.DownloadNotInProgress)
PrinterViewModel.PrintQrVisibility = Visibility.Visible;
}).Start();
}
Expand Down

0 comments on commit 6c78fe5

Please sign in to comment.