Skip to content
Snippets Groups Projects
Scral.cs 4.96 KiB
Newer Older
philip.schell's avatar
philip.schell committed
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
        }
    }
}