Newer
Older
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
127
128
129
130
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using BlubbFish.Utils;
using BlubbFish.Utils.IoT.Bots;
using BlubbFish.Utils.IoT.Bots.Moduls;
using Fraunhofer.Fit.Iot.Lora;
using Fraunhofer.Fit.Iot.Lora.Devices;
using LitJson;
namespace Fraunhofer.Fit.IoT.Bots.LoraBot.Moduls
{
public class Scral : AModul<LoraController>
{
private readonly List<String> nodes = new List<String>();
public override event ModulEvent Update;
private Object getLock = new Object();
private String server = "https://portal.monica-cloud.eu/";
public Scral(LoraController lib, InIReader settings) : base(lib, settings)
{
lib.Update += Lib_Update;
}
private void Lib_Update(object sender, Iot.Lora.Events.DeviceUpdateEvent e)
{
LoraClient l = (LoraClient)e.Parent;
if (!nodes.Contains(l.Name))
{
this.Register(l);
this.nodes.Add(l.Name);
}
this.SendUpdate(l);
}
private void SendUpdate(LoraClient l) {
if (l.Gps.Fix) {
Dictionary<String, Object> d = new Dictionary<String, Object> {
{ "type", "uwb"},
{ "tagId", l.Name},
{ "timestamp", DateTime.Now.ToString("o") },
{ "lat", l.Gps.Latitude},
{ "lon", l.Gps.Longitude},
{ "bearing", l.Rssi },
{ "herr",l.Gps.Hdop},
{ "battery_level",l.Snr}
};
this.RequestString("scral/puetz/dexels/wearable/localization", JsonMapper.ToJson(d), false, RequestMethod.PUT);
this.Update?.Invoke(this, new BlubbFish.Utils.IoT.Bots.Events.ModulEventArgs("scral/puetz/dexels/wearable/localization", "PUT", JsonMapper.ToJson(d), "SCRAL"));
}
}
private void Register(LoraClient l) {
Dictionary<String, Object> d = new Dictionary<String, Object> {
{ "device", "wearable" },
{ "sensor", "tag" },
{ "type", "uwb" },
{ "tagId", l.Name},
{ "timestamp", DateTime.Now.ToString("o") },
{ "unitOfMeasurements", "meters" },
{ "observationType", "propietary" },
{ "state", "active" }
};
this.RequestString("scral/puetz/dexels/wearable", JsonMapper.ToJson(d), false, RequestMethod.POST);
this.Update?.Invoke(this, new BlubbFish.Utils.IoT.Bots.Events.ModulEventArgs("scral/puetz/dexels/wearable", "POST", JsonMapper.ToJson(d), "SCRAL"));
}
public override void Dispose()
{
//throw new NotImplementedException();
}
protected override void UpdateConfig() { }
private String RequestString(String address, String json = "", Boolean withoutput = true, RequestMethod method = RequestMethod.GET)
{
String ret = null;
lock (this.getLock)
{
HttpWebRequest request = WebRequest.CreateHttp(this.server + address);
request.Timeout = 5000;
if (method == RequestMethod.POST || method == RequestMethod.PUT)
{
Byte[] requestdata = Encoding.ASCII.GetBytes(json);
request.ContentLength = requestdata.Length;
request.Method = method.ToString();
request.ContentType = "application/json";
using (Stream stream = request.GetRequestStream())
{
stream.Write(requestdata, 0, requestdata.Length);
}
}
try
{
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
if (response.StatusCode == HttpStatusCode.Unauthorized)
{
Console.Error.WriteLine("Benutzer oder Passwort falsch!");
throw new Exception("Benutzer oder Passwort falsch!");
}
if (withoutput)
{
StreamReader reader = new StreamReader(response.GetResponseStream());
ret = reader.ReadToEnd();
}
}
}
catch (Exception e)
{
Helper.WriteError("Konnte keine Verbindung zum Razzbery Server herstellen. Resource: \"" + this.server + address + "\" Fehler: " + e.Message);
return null;
//throw new Exceptions.ConnectionException("Konnte keine Verbindung zum Razzbery Server herstellen: " + e.Message);
}
}
return ret;
}
private enum RequestMethod
{
GET,
POST,
PUT
}
}
}