void addCurrentSensor_Click(object sender, EventArgs e) { CurrentSensor sensor = new CurrentSensor(); sensor.nodeId = Guid.NewGuid().ToString(); sensor.sensorname = "Current sensor "; sensor.setSensorAddress(DateTime.Now.ToString("yy:MM:dd:hh:mm:ss")); if (currentNode != null && currentNode is ShieldTreeNode) { Shield shield = shields.shieldFromNodeId((String)currentNode.Tag); shield.sensorList.Add(sensor); } InitializeTreeView(); }
private void load() { byte[] buffer; try { FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read); try { int length = (int)fileStream.Length; // get file length buffer = new byte[length]; // create buffer int count; // actual number of bytes read int sum = 0; // total number of bytes read // read until Read method returns 0 (end of the stream has been reached) while ((count = fileStream.Read(buffer, sum, length - sum)) > 0) { sum += count; // sum is a buffer offset for next reading } } finally { fileStream.Close(); } string result = System.Text.Encoding.UTF8.GetString(buffer); JObject strJson = JObject.Parse(result); String strSettings = strJson.GetValue("settings").ToString(); JObject jsonSettings = JObject.Parse(strSettings); Settings.serverIpAddress = (String)jsonSettings.GetValue("serverip"); Settings.serverPort = Int32.Parse((String)jsonSettings.GetValue("port")); String strShields = strJson.GetValue("shields").ToString(); JArray jarray = JArray.Parse(strShields); foreach (JObject json in jarray) { Shield shield = new Shield(); shield.nodeId = Guid.NewGuid().ToString(); shield.shieldName = (String)json.GetValue("shieldName"); shield.MACAddress = (String)json.GetValue("MAC"); shield.localPort = (String)json.GetValue("localPort"); if (json.GetValue("temperatureSensorsEnabled") != null) { shield.temperatureSensorsEnabled = (bool)json.GetValue("temperatureSensorsEnabled"); } else { shield.temperatureSensorsEnabled = false; } JArray sensors = (JArray)json.GetValue("sensors"); foreach (JObject sensor in sensors) { Sensor newSensor; String type = (String)sensor.GetValue("type"); if (type.Equals("temperature")) { newSensor = new DS18S20Sensor(); } else if (type.Equals("doorsensor")) { newSensor = new DoorSensor(); } else if (type.Equals("pressuresensor")) { newSensor = new PressureSensor(); } else if (type.Equals("humiditysensor")) { newSensor = new HumiditySensor(); } else if (type.Equals("currentsensor")) { newSensor = new CurrentSensor(); } else if (type.Equals("pirsensor")) { newSensor = new PIRSensor(); } else { continue; } newSensor.nodeId = Guid.NewGuid().ToString(); newSensor.sensorname = (String)sensor.GetValue("name"); newSensor.enabled = (bool)sensor.GetValue("enabled"); newSensor.setSensorAddress((String)sensor.GetValue("addr")); shield.sensorList.Add(newSensor); } JArray actuators = (JArray)json.GetValue("actuators"); foreach (JObject actuator in actuators) { Sensor newActuator; String type = (String)actuator.GetValue("type"); if (type.Equals("heater")) { newActuator = new Heater(); } else if (type.Equals("releactuator")) { newActuator = new ReleActuator(); } else { continue; } newActuator.nodeId = Guid.NewGuid().ToString(); newActuator.sensorname = (String)actuator.GetValue("name"); newActuator.setSensorAddress((String)actuator.GetValue("addr")); shield.actuatorList.Add(newActuator); } shields.shieldList.Add(shield); //InitializeTreeView(); shield.oAlpha.port = shield.localPort; shield.localIP = shield.oAlpha.localIP; shield.oAlpha.nodeId = shield.nodeId; Thread trd = new Thread(new ThreadStart(shield.oAlpha.Beta)); trd.IsBackground = true; trd.Start(); shield.registerShield(); } } catch (Exception e) { ///fileStream.Close(); } InitializeTreeView(); }