Esempio n. 1
0
        private void objectListView1_CellEditFinished(object sender, BrightIdeasSoftware.CellEditEventArgs e)
        {
            if (currentNode != null && e.Column.Text.Equals("Value"))
            {
                if (currentNode is ShieldTreeNode)
                {
                    ShieldTreeNode node   = (ShieldTreeNode)currentNode;
                    Shield         shield = shields.shieldFromNodeId((String)node.Tag);

                    ShieldProperty property = (ShieldProperty)e.RowObject;
                    if (property.Name.Equals(property_ShieldName))
                    {
                        shield.shieldName = (string)e.NewValue;
                    }
                    else if (property.Name.Equals(property_MACAddress))
                    {
                        shield.MACAddress = (string)e.NewValue;
                    }
                    else if (property.Name.Equals(property_Port))
                    {
                        shield.localPort = (string)e.NewValue;
                    }
                    else if (property.Name.Equals(property_TemperatureSensorEnabled))
                    {
                        if (e.NewValue.Equals("Enabled"))
                        {
                            shield.temperatureSensorsEnabled = true;
                        }
                        else
                        {
                            shield.temperatureSensorsEnabled = false;
                        }
                    }
                }
                else if (currentNode is SensorTreeNode)
                {
                    SensorTreeNode node   = (SensorTreeNode)currentNode;
                    Shield         shield = shields.shieldFromNodeId(node.shieldNodeId);
                    Sensor         sensor = shield.sensorFromNodeId((String)node.Tag);

                    ShieldProperty property = (ShieldProperty)e.RowObject;
                    if (property.Name.Equals(property_SensorName))
                    {
                        sensor.sensorname = (string)e.NewValue;
                    }
                    else if (sensor is DS18S20Sensor)
                    {
                        DS18S20Sensor tempSensor = (DS18S20Sensor)sensor;
                        if (property.Name.Equals(property_TemperatureSensor_Temperature))
                        {
                            tempSensor.temperature = float.Parse((string)e.NewValue);
                        }
                    }
                }
                else if (currentNode is ActuatorTreeNode)
                {
                    ActuatorTreeNode node     = (ActuatorTreeNode)currentNode;
                    Shield           shield   = shields.shieldFromNodeId(node.shieldNodeId);
                    Sensor           actuator = shield.sensorFromNodeId((String)node.Tag);

                    ShieldProperty property = (ShieldProperty)e.RowObject;
                    if (property.Name.Equals(property_ActuatorName))
                    {
                        actuator.sensorname = (string)e.NewValue;
                    }
                }
                else if (currentNode is SettingsTreeNode)
                {
                    SettingsTreeNode node     = (SettingsTreeNode)currentNode;
                    ShieldProperty   property = (ShieldProperty)e.RowObject;
                    if (property.Name.Equals(property_ServerIp))
                    {
                        Settings.serverIpAddress = (string)e.NewValue;
                        Settings.serverIpAddress = (string)e.NewValue;
                    }
                    else if (property.Name.Equals(property_ServerPort))
                    {
                        Settings.serverPort = Int32.Parse((string)e.NewValue);
                    }
                }
            }
        }
Esempio n. 2
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();
        }
Esempio n. 3
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();
        }
Esempio n. 4
0
        protected void treeView1_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
        {
            //currentNode = null;
            this.objectListView1.ClearObjects();
            propertyList = new List <ShieldProperty>();
            currentNode  = e.Node;

            if (e.Node is ShieldTreeNode)
            {
                ShieldTreeNode node   = (ShieldTreeNode)e.Node;
                Shield         shield = shields.shieldFromNodeId((String)node.Tag);
                // name
                ShieldProperty sp = new ShieldProperty();
                sp.Name        = property_ShieldName;
                sp.Description = "Shield name";
                sp.Value       = shield.shieldName;
                propertyList.Add(sp);
                // started
                sp             = new ShieldProperty();
                sp.Name        = property_ShieldStarted;
                sp.Description = "Scheda avviata";
                if (shield.started)
                {
                    sp.Value = "Started";
                }
                else
                {
                    sp.Value = "Not Started";
                }
                propertyList.Add(sp);
                objectListView1.DisableObject(sp);
                // id
                sp             = new ShieldProperty();
                sp.Name        = property_ShieldId;
                sp.Description = "Shield id";
                sp.Value       = "" + shield.id;
                sp.Action      = "register";
                propertyList.Add(sp);
                // port
                sp             = new ShieldProperty();
                sp.Name        = property_Port;
                sp.Description = "local port";
                sp.Value       = "" + shield.localPort;
                propertyList.Add(sp);
                // mac addrress
                sp             = new ShieldProperty();
                sp.Name        = property_MACAddress;
                sp.Description = "MAC Address";
                sp.Value       = shield.MACAddress;
                propertyList.Add(sp);
                // temperaturesensorsenabled
                sp             = new ShieldProperty();
                sp.Name        = property_TemperatureSensorEnabled;
                sp.Description = "Temperature sensor enabled";
                if (shield.temperatureSensorsEnabled)
                {
                    sp.Value = "Enabled";
                }
                else
                {
                    sp.Value = "Disabled";
                }
                propertyList.Add(sp);
            }
            else if (e.Node is SensorTreeNode)
            {
                SensorTreeNode node   = (SensorTreeNode)e.Node;
                Shield         shield = shields.shieldFromNodeId(node.shieldNodeId);
                Sensor         sensor = shield.sensorFromNodeId((String)node.Tag);

                ShieldProperty sp = new ShieldProperty();
                sp.ShieldNodeId = node.shieldNodeId;
                sp.NodeId       = (String)node.Tag; sp.Name = property_SensorName;
                sp.Description  = "Sensor name";
                sp.Value        = sensor.sensorname;
                propertyList.Add(sp);

                sp = new ShieldProperty();
                sp.ShieldNodeId = node.shieldNodeId;
                sp.NodeId       = (String)node.Tag; sp.Name = property_Enabled;
                sp.Description  = "Enabled";
                if (sensor.enabled)
                {
                    sp.Value  = "true";
                    sp.Action = "Disable";
                }
                else
                {
                    sp.Value  = "false";
                    sp.Action = "Enable";
                }
                propertyList.Add(sp);

                if (sensor is DS18S20Sensor)
                {
                    sp = new ShieldProperty();
                    sp.ShieldNodeId = node.shieldNodeId;
                    sp.NodeId       = (String)node.Tag;
                    sp.Name         = property_TemperatureSensor_Temperature;
                    sp.Description  = "Temperature";
                    DS18S20Sensor dsSensor = (DS18S20Sensor)sensor;
                    sp.Value  = "" + dsSensor.temperature;
                    sp.Action = "Update";

                    propertyList.Add(sp);
                }
                else if (sensor is DoorSensor)
                {
                    sp = new ShieldProperty();
                    sp.ShieldNodeId = node.shieldNodeId;
                    sp.NodeId       = (String)node.Tag;
                    sp.Name         = property_DoorSensor_Open;
                    sp.Description  = "Status";
                    DoorSensor doorSensor = (DoorSensor)sensor;

                    if (doorSensor.getStatusOpen())
                    {
                        sp.Value  = "open";
                        sp.Action = "close";
                    }
                    else
                    {
                        sp.Value  = "closed";
                        sp.Action = "open";
                    }
                    propertyList.Add(sp);
                }
            }
            else if (e.Node is ActuatorTreeNode)
            {
                ActuatorTreeNode node     = (ActuatorTreeNode)e.Node;
                Shield           shield   = shields.shieldFromNodeId(node.shieldNodeId);
                Sensor           actuator = shield.actuatorFromNodeId((String)node.Tag);

                ShieldProperty sp = new ShieldProperty();
                sp.ShieldNodeId = node.shieldNodeId;
                sp.NodeId       = (String)node.Tag;
                sp.Name         = property_ActuatorName;
                sp.Description  = "Actuator name";
                sp.Value        = actuator.sensorname;
                propertyList.Add(sp);
            }
            else if (e.Node is SettingsTreeNode)
            {
                SettingsTreeNode node = (SettingsTreeNode)e.Node;

                ShieldProperty sp = new ShieldProperty();
                sp.Name        = property_ServerIp;
                sp.Value       = Settings.serverIpAddress;
                sp.Description = "Server Ip Address";
                propertyList.Add(sp);
                sp             = new ShieldProperty();
                sp.Name        = property_ServerPort;
                sp.Description = "Server Port";
                sp.Value       = "" + Settings.serverPort;
                propertyList.Add(sp);
            }

            this.objectListView1.SetObjects(propertyList);
            this.objectListView1.Refresh();
        }
Esempio n. 5
0
        //public Shield shield;

        public ShieldTreeNode(Shield shield)
        {
            //Shield shield = Form1.shields.shieldFromNodeId(tag);
            this.Text = "shield-" + shield.nodeId;
            //this.shield = shield;
        }