Skip to content

Commit b497040

Browse files
authored
Merge pull request #17 from Dustify/issue-14
basic duplicate detection
2 parents f11abe0 + b94a415 commit b497040

2 files changed

Lines changed: 31 additions & 3 deletions

File tree

Pocsag.Plugin/PocsagControl.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.ComponentModel;
66
using System.Windows.Forms;
7+
using System.Linq;
78

89
public partial class PocsagControl : UserControl
910
{
@@ -52,6 +53,12 @@ private void MessageReceived(Pocsag.Message message)
5253
new Action<Pocsag.Message>(
5354
(message) =>
5455
{
56+
// skip duplicate messages
57+
if (this.bindingList.Any(x => x.Hash == message.Hash))
58+
{
59+
return;
60+
}
61+
5562
int firstDisplayed = this.dataGridView1.FirstDisplayedScrollingRowIndex;
5663
int displayed = this.dataGridView1.DisplayedRowCount(true);
5764
int lastVisible = (firstDisplayed + displayed) - 1;

Pocsag/Message.cs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
namespace Pocsag
22
{
33
using System;
4-
using System.Linq;
5-
using System.Collections.Generic;
64
using System.Collections;
7-
using System.Text;
5+
using System.Collections.Generic;
6+
using System.Linq;
87

98
public class Message
109
{
@@ -36,6 +35,8 @@ public class Message
3635

3736
public bool IsValid => !this.HasBchError && !this.HasParityError;
3837

38+
public string Hash { get; private set; }
39+
3940
public Message(int baud)
4041
{
4142
try
@@ -215,6 +216,26 @@ public void ProcessPayload()
215216
}
216217

217218
this.Payload = result;
219+
220+
var textToHash = this.Payload;
221+
222+
// skip first 9 characters, typically contains time / date + another number which will mess up duplicate detection
223+
224+
if (textToHash.Length > 9)
225+
{
226+
textToHash = textToHash.Substring(8);
227+
}
228+
229+
var bytesToHash = System.Text.Encoding.ASCII.GetBytes(textToHash);
230+
231+
var sha256 = new System.Security.Cryptography.SHA256Managed();
232+
233+
var hashBytes = sha256.ComputeHash(bytesToHash);
234+
235+
sha256.Dispose();
236+
sha256 = null;
237+
238+
this.Hash = BitConverter.ToString(hashBytes).Replace("-", "");
218239
}
219240
catch (Exception exception)
220241
{

0 commit comments

Comments
 (0)