using System; using System.Collections.Generic; using System.Globalization; using System.Text.RegularExpressions; using BlubbFish.Utils.IoT.Connector; using LitJson; namespace BlubbFish.Utils.IoT.Sensor { class Flex4gridswitch : ASensor { private String id; public Flex4gridswitch(Dictionary settings, String name, ADataBackend backend) : base(settings, name, backend) { this.Datatypes = Types.Bool; this.id = settings["id"]; } protected override Boolean UpdateValue(MqttEventArgs e) { CultureInfo info = new CultureInfo("de-DE"); info.NumberFormat.NumberDecimalSeparator = "."; CultureInfo.DefaultThreadCurrentCulture = info; CultureInfo.DefaultThreadCurrentUICulture = info; System.Threading.Thread.CurrentThread.CurrentCulture = info; System.Threading.Thread.CurrentThread.CurrentUICulture = info; try { JsonData data = JsonMapper.ToObject(e.Message); if (data.Keys.Contains("id") && data["id"].ToString() == this.id) { this.GetBool = data["status"].ToString() == "active" ? true : false; return true; } if(data.Keys.Contains(this.id)) { this.GetBool = data[this.id].ToString() == "active" ? true : false; return true; } } catch (Exception) { } return false; } protected override void Poll() { if (this.pollcount++ >= this.Polling) { this.pollcount = 1; String hid = Regex.Match(this.topic, "/flex4grid/v1/households/([^/]*)/device/state").Groups[1].Value; this.backend.Send("/flex4grid/v1/households/" + hid + "/devices/state/request", "{\"timestamp\": \"" + DateTime.Now.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'ffffff'Z'") + "\"}"); } } public override void SetBool(Boolean v) { String hid = Regex.Match(this.topic, "/flex4grid/v1/households/([^/]*)/device/state").Groups[1].Value; this.backend.Send("/flex4grid/v1/households/" + hid + "/device/actuate", "{\"command\":\""+(v ? "ON" : "OFF") + "\",\"deviceId\":\""+ this.id + "\",\"timestamp\":\"" + DateTime.Now.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'ffffff'Z'") + "\"}"); } } }