-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
62 lines (51 loc) · 2.56 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
using System.Diagnostics;
using SyncHuduSyncro.Hudu;
using SyncHuduSyncro.Hudu.Models;
using SyncHuduSyncro.Syncro;
using System.Configuration;
var syncroUrl = ConfigurationManager.AppSettings.Get("Syncro_URL");
var sw = new Stopwatch();
sw.Start();
Console.WriteLine("Caching all Syncro Assets");
var syncro = new SyncroAPI();
syncro.UpdateAssetList();
syncro.UpdateContactsList();
Console.WriteLine($"Successfully Cached {syncro.AllAssets.Count} Assets and {syncro.AllContacts.Count} Contacts from Syncro. Duration: {sw.ElapsedMilliseconds / 1000} Seconds.");
sw.Stop();
sw.Reset();
sw.Start();
Console.WriteLine("Caching Hudu Data");
var hudu = new HuduAPI();
hudu.UpdateAssetList();
Console.WriteLine($"Successfully Cached {hudu.AllAssets.Count} Assets from Hudu. Duration: {sw.ElapsedMilliseconds / 1000} Seconds.");
sw.Stop();
sw.Reset();
Console.WriteLine("Syncro<->Hudu Sync Process Beginning");
sw.Start();
foreach (var syncroDevice in syncro.AllAssets)
{
var huduDevice = hudu.AllAssets.FirstOrDefault(I => I.cards.Any(c => c.sync_id == syncroDevice.id));
if (huduDevice == null)
continue;
//Update syncro asset to link to hudu. Only run if not set/has changed
if (string.IsNullOrEmpty(syncroDevice.properties.Hudu) || syncroDevice.properties.Hudu != huduDevice.url)
syncro.SetHuduAssetLink(syncroDevice.id, huduDevice.url);
//Try to get asset -> contact relation from syncro and re-create it on Hudu
// Note: uses asset field relations inside Hudu, NOT relation API.
HuduAsset? huduContact = null;
var syncroContact = syncro.AllContacts.FirstOrDefault(I => I.id == syncroDevice.contact_id);
if (syncroContact != null)
huduContact = hudu.AllAssets.FirstOrDefault(I => I.cards.Any(c => c.sync_id == syncroContact.id));
//TODO: Only update hudu asset when the syncro asset has been updated (name or relation)
// For now, only run if the name is not updated (won't account for re-assigning device without renaming it)
// Digging into custom asset fields for comparison is needed but I don't want to work on that right now.
if (huduDevice.name != syncroDevice.name)
{
//Enforce assets to be named the same in Hudu as on Syncro (Syncro as authoritative friendly names)
huduDevice.name = syncroDevice.name;
//Update the asset inside hudu. Optionally add contact relation.
hudu.UpdateHuduAsset(new HuduUpdateAssetRequest(huduDevice, huduContact));
}
}
sw.Stop();
Console.WriteLine($"Completed Syncro<->Hudu Sync Process. Duration: {sw.ElapsedMilliseconds / 1000} Seconds.");