Skip to content

Commit 668567c

Browse files
committed
Creating updater classes in Common library
- Created a new interface and class to handle checking for updates on GitHub (does not install yet) - Added Octokit library
1 parent f5c0ce6 commit 668567c

File tree

6 files changed

+232
-0
lines changed

6 files changed

+232
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
' WinNUT-Client is a NUT windows client for monitoring your ups hooked up to your favorite linux server.
2+
' Copyright (C) 2019-2021 Gawindx (Decaux Nicolas)
3+
'
4+
' This program is free software: you can redistribute it and/or modify it under the terms of the
5+
' GNU General Public License as published by the Free Software Foundation, either version 3 of the
6+
' License, or any later version.
7+
'
8+
' This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY
9+
10+
Imports System.ComponentModel
11+
12+
''' <summary>
13+
''' Represent important details resulting from a completed async request to check for updates.
14+
''' </summary>
15+
Public Class CheckForUpdateCompletedEventArgs
16+
Inherits AsyncCompletedEventArgs
17+
18+
Private ReadOnly _LatestRelease As Octokit.Release
19+
20+
ReadOnly Property LatestRelease As Octokit.Release
21+
Get
22+
Return _LatestRelease
23+
End Get
24+
End Property
25+
26+
Public Sub New(latestRelease As Octokit.Release, Optional [error] As Exception = Nothing,
27+
Optional cancelled As Boolean = False)
28+
29+
MyBase.New([error], cancelled, Nothing)
30+
_LatestRelease = latestRelease
31+
End Sub
32+
33+
End Class
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
' WinNUT-Client is a NUT windows client for monitoring your ups hooked up to your favorite linux server.
2+
' Copyright (C) 2019-2021 Gawindx (Decaux Nicolas)
3+
'
4+
' This program is free software: you can redistribute it and/or modify it under the terms of the
5+
' GNU General Public License as published by the Free Software Foundation, either version 3 of the
6+
' License, or any later version.
7+
'
8+
' This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY
9+
10+
Imports System.ComponentModel
11+
Imports System.Threading
12+
Imports Octokit
13+
14+
Namespace Updater
15+
16+
Public Class GitHubUpdateProvider
17+
Implements IUpdateProvider
18+
19+
Private Const REPOSITORY_OWNER = "nutdotnet"
20+
Private Const REPOSITORY_NAME = "WinNUT-Client"
21+
22+
Private onCompletedDelegate As SendOrPostCallback
23+
Private Delegate Sub CheckForUpdateDelegate()
24+
' Only one instance of a check or install procedure at a time.
25+
Private asyncOpInstance As AsyncOperation
26+
Private disposedValue As Boolean
27+
Private Shared _gitHubClient As GitHubClient
28+
29+
Public Event OnCheckForUpdateCompleted(sender As IUpdateProvider, eventArgs As CheckForUpdateCompletedEventArgs) _
30+
Implements IUpdateProvider.OnCheckForUpdateCompleted
31+
32+
Public Event Disposed As EventHandler Implements IComponent.Disposed
33+
34+
Protected Overridable Sub InitializeDelegates()
35+
onCompletedDelegate = New SendOrPostCallback(AddressOf CheckForUpdateAsyncCompleted)
36+
End Sub
37+
38+
Public Function CheckForUpdate() As Release Implements IUpdateProvider.CheckForUpdate
39+
Dim checkTask = _gitHubClient.Repository.Release.GetLatest(REPOSITORY_OWNER, REPOSITORY_NAME)
40+
41+
checkTask.RunSynchronously()
42+
Return checkTask.Result
43+
End Function
44+
45+
Public Sub CheckForUpdateAsync() Implements IUpdateProvider.CheckForUpdateAsync
46+
If asyncOpInstance IsNot Nothing Then
47+
Throw New InvalidOperationException("Instance of AsyncOperation already exists.")
48+
End If
49+
50+
asyncOpInstance = AsyncOperationManager.CreateOperation(Nothing)
51+
Dim checkDelegate As New CheckForUpdateDelegate(AddressOf CheckForUpdateWorker)
52+
checkDelegate.BeginInvoke(Nothing, Nothing)
53+
End Sub
54+
55+
Private Sub CheckForUpdateWorker() Implements IUpdateProvider.CheckForUpdateWorker
56+
Dim checkCompleteEvent = New CheckForUpdateCompletedEventArgs(CheckForUpdate())
57+
CheckForUpdateAsyncCompleted(checkCompleteEvent)
58+
End Sub
59+
60+
' Doesn't really do anything. May need to implement real cancellation logic.
61+
Public Sub CheckForUpdateAsyncCancel() Implements IUpdateProvider.CheckForUpdateAsyncCancel
62+
If asyncOpInstance IsNot Nothing Then
63+
asyncOpInstance = Nothing
64+
End If
65+
End Sub
66+
67+
Protected Sub CheckForUpdateAsyncCompleted(eventArguments As CheckForUpdateCompletedEventArgs) _
68+
Implements IUpdateProvider.CheckForUpdateAsyncCompleted
69+
70+
RaiseEvent OnCheckForUpdateCompleted(Me, eventArguments)
71+
End Sub
72+
73+
Public Function UpdateWorker() As Boolean Implements IUpdateProvider.UpdateWorker
74+
Throw New NotImplementedException()
75+
End Function
76+
77+
Public Function Update() As Boolean Implements IUpdateProvider.Update
78+
Throw New NotImplementedException()
79+
End Function
80+
81+
Public Sub UpdateAsync() Implements IUpdateProvider.UpdateAsync
82+
Throw New NotImplementedException()
83+
End Sub
84+
85+
Public Sub UpdateAsyncCancel() Implements IUpdateProvider.UpdateAsyncCancel
86+
Throw New NotImplementedException()
87+
End Sub
88+
89+
Sub New()
90+
InitializeDelegates()
91+
92+
_gitHubClient = New GitHubClient(New ProductHeaderValue(REPOSITORY_NAME))
93+
End Sub
94+
95+
Public Property Site As ISite Implements IComponent.Site
96+
Get
97+
Throw New NotImplementedException()
98+
End Get
99+
Set(value As ISite)
100+
Throw New NotImplementedException()
101+
End Set
102+
End Property
103+
104+
Protected Overridable Sub Dispose(disposing As Boolean)
105+
If Not disposedValue Then
106+
If disposing Then
107+
' TODO: dispose managed state (managed objects)
108+
End If
109+
110+
' TODO: free unmanaged resources (unmanaged objects) and override finalizer
111+
' TODO: set large fields to null
112+
disposedValue = True
113+
End If
114+
End Sub
115+
116+
' ' TODO: override finalizer only if 'Dispose(disposing As Boolean)' has code to free unmanaged resources
117+
' Protected Overrides Sub Finalize()
118+
' ' Do not change this code. Put cleanup code in 'Dispose(disposing As Boolean)' method
119+
' Dispose(disposing:=False)
120+
' MyBase.Finalize()
121+
' End Sub
122+
123+
Public Sub Dispose() Implements IDisposable.Dispose
124+
' Do not change this code. Put cleanup code in 'Dispose(disposing As Boolean)' method
125+
Dispose(disposing:=True)
126+
GC.SuppressFinalize(Me)
127+
End Sub
128+
End Class
129+
130+
End Namespace
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
' WinNUT-Client is a NUT windows client for monitoring your ups hooked up to your favorite linux server.
2+
' Copyright (C) 2019-2021 Gawindx (Decaux Nicolas)
3+
'
4+
' This program is free software: you can redistribute it and/or modify it under the terms of the
5+
' GNU General Public License as published by the Free Software Foundation, either version 3 of the
6+
' License, or any later version.
7+
'
8+
' This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY
9+
10+
Imports System.ComponentModel
11+
Imports Octokit
12+
13+
Namespace Updater
14+
15+
''' <summary>
16+
''' Defines an object that handles checking and retrieving updates for WinNUT.
17+
''' </summary>
18+
Public Interface IUpdateProvider
19+
Inherits IComponent
20+
21+
Function CheckForUpdate() As Release
22+
Sub CheckForUpdateWorker()
23+
Sub CheckForUpdateAsync()
24+
Sub CheckForUpdateAsyncCancel()
25+
Sub CheckForUpdateAsyncCompleted(eventArguments As CheckForUpdateCompletedEventArgs)
26+
Event OnCheckForUpdateCompleted(sender As IUpdateProvider, eventArguments As CheckForUpdateCompletedEventArgs)
27+
28+
Function UpdateWorker() As Boolean
29+
Function Update() As Boolean
30+
Sub UpdateAsync()
31+
Sub UpdateAsyncCancel()
32+
33+
End Interface
34+
35+
End Namespace
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
' WinNUT-Client is a NUT windows client for monitoring your ups hooked up to your favorite linux server.
2+
' Copyright (C) 2019-2021 Gawindx (Decaux Nicolas)
3+
'
4+
' This program is free software: you can redistribute it and/or modify it under the terms of the
5+
' GNU General Public License as published by the Free Software Foundation, either version 3 of the
6+
' License, or any later version.
7+
'
8+
' This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY
9+
10+
Imports System.ComponentModel
11+
12+
''' <summary>
13+
''' Notify a listener that an async method for either checking or installing an update has progress to report.
14+
''' NOTE: Currently just a wrapper for <see cref="ProgressChangedEventArgs"/>
15+
''' </summary>
16+
Public Class UpdateProgressChangedEventArgs
17+
Inherits ProgressChangedEventArgs
18+
19+
Public Sub New(progressPercentage As Integer, userState As Object)
20+
MyBase.New(progressPercentage, userState)
21+
End Sub
22+
End Class

WinNUT_V2/WinNUT-Client_Common/WinNUT-Client_Common.vbproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@
4545
<OptionInfer>On</OptionInfer>
4646
</PropertyGroup>
4747
<ItemGroup>
48+
<Reference Include="Octokit, Version=5.0.4.0, Culture=neutral, PublicKeyToken=0be8860aee462442, processorArchitecture=MSIL">
49+
<HintPath>..\packages\Octokit.5.0.4\lib\netstandard2.0\Octokit.dll</HintPath>
50+
</Reference>
4851
<Reference Include="System" />
4952
<Reference Include="System.Data" />
5053
<Reference Include="System.Drawing" />
@@ -94,6 +97,10 @@
9497
<DesignTimeSharedInput>True</DesignTimeSharedInput>
9598
</Compile>
9699
<Compile Include="Nut_Socket.vb" />
100+
<Compile Include="Updater\GitHubUpdateProvider.vb" />
101+
<Compile Include="Updater\IUpdateProvider.vb" />
102+
<Compile Include="Updater\CheckForUpdateCompletedEventArgs.vb" />
103+
<Compile Include="Updater\UpdateProgressChangedEventArgs.vb" />
97104
<Compile Include="UPS_Device.vb" />
98105
<Compile Include="WinNUT_Globals.vb" />
99106
<Compile Include="WinNUT_Params.vb" />
@@ -117,6 +124,7 @@
117124
<CustomToolNamespace>My</CustomToolNamespace>
118125
<LastGenOutput>Settings.Designer.vb</LastGenOutput>
119126
</None>
127+
<None Include="packages.config" />
120128
</ItemGroup>
121129
<Import Project="$(MSBuildToolsPath)\Microsoft.VisualBasic.targets" />
122130
</Project>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<packages>
3+
<package id="Octokit" version="5.0.4" targetFramework="net48" />
4+
</packages>

0 commit comments

Comments
 (0)