Beispiel #1
0
        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();
        }
Beispiel #2
0
        public void cellButtonClick(object sender, CellClickEventArgs e)
        {
            Debug.WriteLine(String.Format("Button clicked: ({0}, {1}, {2})", e.RowIndex, e.SubItem, e.Model));

            // Take some action on e.Model based on which button (e.ColumnIndex) was clicked

            ShieldProperty property = (ShieldProperty)e.Model;

            if (property.Name.Equals(property_TemperatureSensor_Temperature))
            {
                Shield shield = shields.shieldFromNodeId(property.NodeId);
                shield.sendSensorsStatus();
            }
            else if (property.Name.Equals(property_ShieldId))
            {
                Shield shield = shields.shieldFromNodeId(property.NodeId);
                shield.registerShield();
            }
            else if (property.Name.Equals(property_Enabled))
            {
                Shield shield = shields.shieldFromNodeId(property.ShieldNodeId);
                if (shield != null)
                {
                    Sensor sensor = (Sensor)shield.sensorFromNodeId(property.NodeId);
                    if (sensor != null)
                    {
                        sensor.enabled = !sensor.enabled;
                        if (sensor.enabled)
                        {
                            property.Value  = "true";
                            property.Action = "Disable";
                        }
                        else
                        {
                            property.Value  = "false";
                            property.Action = "Enable";
                        }
                        shield.sendSensorsStatus();
                    }
                }
            }
            else if (property.Name.Equals(property_DoorSensor_Open))
            {
                Shield     shield     = shields.shieldFromNodeId(property.ShieldNodeId);
                DoorSensor doorSensor = (DoorSensor)shield.sensorFromNodeId(property.NodeId);
                doorSensor.setStatusOpen(!doorSensor.getStatusOpen());
                if (doorSensor.getStatusOpen())
                {
                    property.Value  = "open";
                    property.Action = "close";
                }
                else
                {
                    property.Value  = "closed";
                    property.Action = "open";
                }
                shield.sendSensorsStatus();
            }
            // If something about the object changed, you probably want to refresh the model
            this.objectListView1.RefreshObject(e.Model);
            //this.objectListView1.Update();
            //this.objectListView1.SetObjects(propertyList);
            //this.objectListView1.Refresh();
        }