-
Notifications
You must be signed in to change notification settings - Fork 0
/
DittoManager.cs
126 lines (110 loc) · 3.8 KB
/
DittoManager.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
using System;
using Verhaeg.IoT.Processor;
using System.Net.Http;
using System.Threading.Tasks;
using System.Linq;
using Newtonsoft.Json;
using Verhaeg.IoT.Ditto.Api20;
namespace Verhaeg.IoT.Ditto
{
public class DittoManager : QueueManager
{
// SingleTon
private static DittoManager _instance = null;
private static readonly object padlock = new object();
private Configuration.Connection conf;
// Communication
private HttpClient hc;
private DittoClient dc;
private DittoManager(string name, string uri, string username, string password) : base(name)
{
// Initiate Configuration
conf = new Configuration.Connection(uri, username, password);
Log.Information("Starting DittoManager...");
hc = new HttpClient();
hc.BaseAddress = conf.ditto_uri;
var byteArray = System.Text.Encoding.ASCII.GetBytes(conf.username + ":" + conf.password);
hc.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
dc = new DittoClient(hc);
Log.Information("DittoManager started.");
}
public static void Start(string uri, string username, string password)
{
lock (padlock)
{
if (_instance == null)
{
_instance = new DittoManager("DittoManager", uri, username, password);
}
}
}
public static DittoManager Instance()
{
lock (padlock)
{
if (_instance == null)
{
return null;
}
else
{
return (DittoManager)_instance;
}
}
}
public Thing GetThing(string thingId)
{
try
{
Task<Thing> t = dc.Things2Async(thingId, "thingId,policyId,definition,attributes,features,_modified");
t.Wait();
return t.Result;
}
catch (Exception ex)
{
Log.Error("Could not find thing with thingId " + thingId);
Log.Error(ex.ToString());
return null;
}
}
public NewThing ConvertThing(Thing t)
{
NewThing nt = new NewThing();
nt.AdditionalProperties = t.AdditionalProperties;
nt.Attributes = t.Attributes;
nt.Definition = t.Definition;
nt.Features = t.Features;
nt.PolicyId = t.PolicyId;
return nt;
}
protected override void Dispose()
{
_instance = null;
}
protected override void Process(object obj)
{
if (obj != null)
{
NewThing t = (NewThing)obj;
var att = t.Attributes.AdditionalProperties.Where(a => a.Key == "name").FirstOrDefault();
//t.PolicyId = att.Value.ToString();
try
{
Log.Debug("Trying to update thing with thingId: " + att.Value.ToString());
Task<Thing> tt = dc.Things3Async(att.Value.ToString(), null, null, t);
tt.Wait();
Log.Debug("Thing with thingId: " + att.Value.ToString() + " updated.");
}
catch (Exception ex)
{
Log.Error("Couldn't modify thing with thingId " + att.Value.ToString());
Log.Error(ex.ToString());
}
}
else
{
throw new ArgumentNullException(nameof(obj));
}
}
}
}