Skip to content

Commit

Permalink
Merge pull request #2 from RepairShopr/outtake-form
Browse files Browse the repository at this point in the history
Add Outtake form
  • Loading branch information
yuske authored Feb 9, 2018
2 parents 9cd8966 + a711b86 commit 82d9ecd
Show file tree
Hide file tree
Showing 17 changed files with 299 additions and 121 deletions.
4 changes: 2 additions & 2 deletions AutoPrintr/AssemblyVersion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
[assembly: AssemblyVersion("2.0.21.0")]
[assembly: AssemblyFileVersion("2.0.21.0")]
[assembly: AssemblyVersion("2.0.23.0")]
[assembly: AssemblyFileVersion("2.0.23.0")]
7 changes: 4 additions & 3 deletions AutoPrintr/AutoPrintr.Core/IServices/IWindowsService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using AutoPrintr.Core.Models;
using System;
using AutoPrintr.Core.Models;
using System.Collections.Generic;
using System.ServiceModel;
using System.Threading.Tasks;
Expand All @@ -9,10 +10,10 @@ namespace AutoPrintr.Core.IServices
public interface IWindowsService
{
[OperationContract]
void Connect();
void Connect(Guid id);

[OperationContract]
void Disconnect();
void Disconnect(Guid id);

[OperationContract]
void Ping();
Expand Down
2 changes: 2 additions & 0 deletions AutoPrintr/AutoPrintr.Core/Models/Document.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public static string GetTypeTitle(DocumentType type)
case DocumentType.Estimate: return "Estimate";
case DocumentType.Ticket: return "Ticket";
case DocumentType.IntakeForm: return "Intake Form";
case DocumentType.OuttakeForm: return "Outtake Form";
case DocumentType.Receipt: return "Receipt";
case DocumentType.ZReport: return "Z Report";
case DocumentType.TicketReceipt: return "Ticket Receipt";
Expand All @@ -61,6 +62,7 @@ public static string GetSizeTitle(DocumentSize type)
case DocumentSize.Label: return "Size: 1.1x3";
case DocumentSize.Receipt: return "Size: 80mm";
case DocumentSize.IntakeForm: return "Intake Form";
case DocumentSize.OuttakeForm: return "Outtake Form";
case DocumentSize.PopDrawer: return "Pop Drawer";
default: return null;
}
Expand Down
3 changes: 2 additions & 1 deletion AutoPrintr/AutoPrintr.Core/Models/DocumentSize.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public enum DocumentSize : byte
Label,
Receipt,
IntakeForm,
PopDrawer
PopDrawer,
OuttakeForm
}
}
3 changes: 2 additions & 1 deletion AutoPrintr/AutoPrintr.Core/Models/DocumentType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public enum DocumentType : byte
Adjustment,
CustomerID,
Asset,
TicketLabel
TicketLabel,
OuttakeForm
}
}
9 changes: 8 additions & 1 deletion AutoPrintr/AutoPrintr.Core/Models/DocumentTypeSettings.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
namespace AutoPrintr.Core.Models
using System.Runtime.Serialization;

namespace AutoPrintr.Core.Models
{
[DataContract]
public class DocumentTypeSettings : BaseModel
{
[DataMember]
public bool Enabled { get; set; }
[DataMember]
public int Quantity { get; set; }
[DataMember]
public bool AutoPrint { get; set; }
[DataMember]
public DocumentType DocumentType { get; set; }
}
}
3 changes: 2 additions & 1 deletion AutoPrintr/AutoPrintr.Service/Helpers/Commands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ internal enum Commands
Install,
Uninstall,
Start,
Stop
Stop,
Debug
}
}
87 changes: 62 additions & 25 deletions AutoPrintr/AutoPrintr.Service/Service.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
using System;
using System.Configuration.Install;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.ServiceProcess;
using System.Threading;

namespace AutoPrintr.Service
{
Expand Down Expand Up @@ -78,36 +80,71 @@ private static void CurrentDomain_UnhandledException(object sender, UnhandledExc

private static void RunCommand(Commands command)
{
switch (command)
try
{
case Commands.Install:
if (!ServiceController.GetServices().Any(x => x.ServiceName == SERVICE_NAME))
ManagedInstallerClass.InstallHelper(new string[] { Assembly.GetExecutingAssembly().Location });
break;
case Commands.Start:
using (ServiceController sc = new ServiceController(SERVICE_NAME))
{
if (ServiceController.GetServices().Any(x => x.ServiceName == SERVICE_NAME))
switch (command)
{
case Commands.Install:
if (!ServiceController.GetServices().Any(x => x.ServiceName == SERVICE_NAME))
ManagedInstallerClass.InstallHelper(new string[] { Assembly.GetExecutingAssembly().Location });
break;
case Commands.Start:
using (ServiceController sc = new ServiceController(SERVICE_NAME))
{
if (sc.Status != ServiceControllerStatus.Running)
sc.Start();
if (ServiceController.GetServices().Any(x => x.ServiceName == SERVICE_NAME))
{
if (sc.Status != ServiceControllerStatus.Running)
sc.Start();
}
}
}
break;
case Commands.Stop:
using (var sc = new ServiceController(SERVICE_NAME))
{
if (ServiceController.GetServices().Any(x => x.ServiceName == SERVICE_NAME))

break;
case Commands.Stop:
using (var sc = new ServiceController(SERVICE_NAME))
{
if (sc.Status != ServiceControllerStatus.Stopped)
sc.Stop();
if (ServiceController.GetServices().Any(x => x.ServiceName == SERVICE_NAME))
{
if (sc.Status != ServiceControllerStatus.Stopped)
{
sc.Stop();
}

try
{
sc.WaitForStatus(ServiceControllerStatus.Stopped, TimeSpan.FromMinutes(1));
}
catch (System.ServiceProcess.TimeoutException)
{
foreach (var process in Process.GetProcessesByName(Path.GetFileNameWithoutExtension(Assembly.GetExecutingAssembly().Location)))
{
if (process.Id == 0 ||
process.Id == Process.GetCurrentProcess().Id)
{
continue;
}

process.Kill();
process.WaitForExit(60 * 1000);
}
}
}
}
}
break;
case Commands.Uninstall:
if (ServiceController.GetServices().Any(x => x.ServiceName == SERVICE_NAME))
ManagedInstallerClass.InstallHelper(new string[] { "/u", Assembly.GetExecutingAssembly().Location });
break;

break;
case Commands.Uninstall:
if (ServiceController.GetServices().Any(x => x.ServiceName == SERVICE_NAME))
ManagedInstallerClass.InstallHelper(new string[] { "/u", Assembly.GetExecutingAssembly().Location });
break;
case Commands.Debug:
var service = new Service();
service.OnStart(new string[0]);
Thread.Sleep(Timeout.Infinite);
break;
}
}
catch (Exception exception)
{
// ignore errors
}
}
#endregion
Expand Down
24 changes: 16 additions & 8 deletions AutoPrintr/AutoPrintr.Service/Services/PrinterService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,25 @@ public PrinterService(ISettingsService settingsService,
#region Methods
public IEnumerable<Printer> GetPrinters()
{
var printers = GetInstalledPrinters();
try
{
var printers = GetInstalledPrinters();

var result = new List<Printer>();
foreach (var printer in printers)
{
var existing = _settingsService.Settings.Printers.SingleOrDefault(x => string.Compare(x.Name, printer.Name, true) == 0);
var existingCopy = existing?.Clone() as Printer;
result.Add(existingCopy ?? printer);
}

var result = new List<Printer>();
foreach (var printer in printers)
return result;
}
catch (Exception e)
{
var existing = _settingsService.Settings.Printers.SingleOrDefault(x => string.Compare(x.Name, printer.Name, true) == 0);
var existingCopy = existing?.Clone() as Printer;
result.Add(existingCopy ?? printer);
_loggingService.WriteError($"Error getting printers: {e}");
return null;
}

return result;
}

public async Task PrintDocumentAsync(Printer printer, Document document, int count, Action<bool, Exception> completed = null)
Expand Down
70 changes: 28 additions & 42 deletions AutoPrintr/AutoPrintr.Service/WindowsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using AutoPrintr.Service.IServices;
using GalaSoft.MvvmLight.Ioc;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.ServiceModel;
Expand All @@ -14,8 +15,7 @@ namespace AutoPrintr.Service
internal class WindowsService : IWindowsService
{
#region Properties
private readonly List<IWindowsServiceCallback> _callbacks = new List<IWindowsServiceCallback>();
private readonly object guardCallbacks = new object();
private readonly ConcurrentDictionary<Guid, IWindowsServiceCallback> _callbacks = new ConcurrentDictionary<Guid, IWindowsServiceCallback>();

private IJobsService JobsService => SimpleIoc.Default.GetInstance<IJobsService>();
private IPrinterService PrintersService => SimpleIoc.Default.GetInstance<IPrinterService>();
Expand All @@ -26,33 +26,22 @@ internal class WindowsService : IWindowsService
#endregion

#region Methods
public void Connect()
public void Connect(Guid id)
{
LoggerService.WriteInformation($"Add WCF connection from the '{id}' client.");
var callback = OperationContext.Current.GetCallbackChannel<IWindowsServiceCallback>();
if (callback == null)
{
return;
}

lock (guardCallbacks)
{
_callbacks.Add(callback);
}
_callbacks.AddOrUpdate(id, callback, (key, value) => callback);
}

public void Disconnect()
public void Disconnect(Guid id)
{
var callback = OperationContext.Current.GetCallbackChannel<IWindowsServiceCallback>();
if (callback == null)
{
return;
}

lock (guardCallbacks)
{
if (_callbacks.Contains(callback))
_callbacks.Remove(callback);
}
LoggerService.WriteInformation($"Remove WCF connection from the '{id}' client.");
_callbacks.TryRemove(id, out _);
}

public void Ping()
Expand Down Expand Up @@ -148,31 +137,28 @@ public static void OnConnectionFailed()

private void ForEachCallback(Action<IWindowsServiceCallback> call, bool store = false)
{
lock (guardCallbacks)
foreach (var entry in _callbacks)
{
for (int i = _callbacks.Count - 1; i >= 0; i--)
try
{
call(entry.Value);
}
catch (ObjectDisposedException ex)
{
_callbacks.TryRemove(entry.Key, out _);
Debug.WriteLine($"Error in {nameof(WindowsService)}: {ex.Message}");
LoggerService.WriteWarning($"Error in {nameof(WindowsService)}: {ex.Message}");
}
catch (CommunicationException ex)
{
_callbacks.TryRemove(entry.Key, out _);
Debug.WriteLine($"Error in {nameof(WindowsService)}: {ex.Message}");
LoggerService.WriteWarning($"Error in {nameof(WindowsService)}: {ex.Message}");
}
catch (Exception ex)
{
try
{
call(_callbacks[i]);
}
catch (ObjectDisposedException ex)
{
_callbacks.RemoveAt(i);
Debug.WriteLine($"Error in {nameof(WindowsService)}: {ex.Message}");
LoggerService.WriteWarning($"Error in {nameof(WindowsService)}: {ex.Message}");
}
catch (CommunicationException ex)
{
_callbacks.RemoveAt(i);
Debug.WriteLine($"Error in {nameof(WindowsService)}: {ex.Message}");
LoggerService.WriteWarning($"Error in {nameof(WindowsService)}: {ex.Message}");
}
catch (Exception ex)
{
Debug.WriteLine($"Error in {nameof(WindowsService)}: {ex}");
LoggerService.WriteError($"Error in {nameof(WindowsService)}: {ex}");
}
Debug.WriteLine($"Error in {nameof(WindowsService)}: {ex}");
LoggerService.WriteError($"Error in {nameof(WindowsService)}: {ex}");
}
}
}
Expand Down
16 changes: 16 additions & 0 deletions AutoPrintr/AutoPrintr/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ protected override async void OnStartup(StartupEventArgs e)
{
trayIcon.ShowBalloonTip();
}

RefreshTrayArea();
}

protected override async void OnExit(ExitEventArgs e)
Expand All @@ -70,6 +72,20 @@ private bool CheckIsNet45Installed()
return netVersion >= 378389;
}
}

private static void RefreshTrayArea()
{
ILoggerService logger = null;
try
{
logger = SimpleIoc.Default.GetInstance<ILoggerService>();
Task.Run(() => SystemTrayUpdater.Refresh(logger));
}
catch (Exception e)
{
logger?.WriteError($"Error refresh tray area. {e}");
}
}
#endregion

#region Exceptions
Expand Down
1 change: 1 addition & 0 deletions AutoPrintr/AutoPrintr/AutoPrintr.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@
<Compile Include="Helpers\HideControlMessage.cs" />
<Compile Include="Helpers\ReliableService.cs" />
<Compile Include="Helpers\ShowControlMessage.cs" />
<Compile Include="Helpers\SystemTrayUpdater.cs" />
<Compile Include="Helpers\ViewType.cs" />
<Compile Include="Helpers\VisualTreeHelperExtensions.cs" />
<Compile Include="Helpers\WindowsServiceClient.cs" />
Expand Down
Loading

0 comments on commit 82d9ecd

Please sign in to comment.