Ejemplo n.º 1
0
        public void setTriggeredState(bool newTriggeredState)
        {
            if (Activated)
            {
                if (Triggered != newTriggeredState)
                {
                    //Log Raw data to database
                    ZoneGuardConfigContextFactory factory = new ZoneGuardConfigContextFactory();
                    using (ZoneGuardConfigContext context = factory.CreateDbContext())
                    {
                        SensorStateLogDAL stateLog = new SensorStateLogDAL();
                        stateLog.SensorName = Id;
                        stateLog.Triggered  = (newTriggeredState ? 1 : 0);
                        stateLog.Timestamp  = DateTime.UtcNow;

                        context.SensorStateLog.Add(stateLog);
                        context.SaveChanges();
                    }
                }

                Triggered = newTriggeredState;


                //OnTriggeredChanged(new SensorTriggeredEventArgs(Id, Triggered));
                RaiseTriggeredChanged(new SensorTriggeredEventArgs(Id, Triggered));
            }
        }
Ejemplo n.º 2
0
        public void onMqttMessage(string topic, string message, DateTime timestamp)
        {
            getLogger().LogDebug("Sensor '{0}' received MQTT message. topic='{1}', message='{2}', timestamp='{3}'", Id, topic, message, timestamp.ToString());
            getTriggeredFromString(message);


            //Log Raw data to database
            ZoneGuardConfigContextFactory factory = new ZoneGuardConfigContextFactory();

            using (ZoneGuardConfigContext context = factory.CreateDbContext())
            {
                SensorStateRawLogDAL rawLog = new SensorStateRawLogDAL();
                rawLog.SensorName     = Id;
                rawLog.State          = message;
                rawLog.SensorType     = sensorType;
                rawLog.AdditionalInfo = topicState;

                rawLog.Timestamp = DateTime.UtcNow;

                context.RawSensorStateLog.Add(rawLog);
                context.SaveChanges();
            }


            SensorStateMessage msg = new SensorStateMessage(Id, getTriggeredFromString(message), timestamp);

            getManager().PublishSensorState(msg);
        }
Ejemplo n.º 3
0
        protected override void OnInitializeDatabase()
        {
            ZoneGuardConfigContextFactory factory = new ZoneGuardConfigContextFactory();

            using (ZoneGuardConfigContext context = factory.CreateDbContext())
            {
                context.Database.EnsureCreated();
            }
        }
Ejemplo n.º 4
0
        private void ensureDatabaseCreated()
        {
            ZoneGuardConfigContextFactory factory = new ZoneGuardConfigContextFactory();


            using (ZoneGuardConfigContext context = factory.CreateDbContext())
            {
                context.Database.EnsureCreated();
            }
        }
Ejemplo n.º 5
0
        public static void ThingConfig_Create(ZoneGuardConfigContext context, ThingDAL thing, ThingParameterDAL[] parameters)
        {
            context.Thing.Add(thing);
            context.SaveChanges();

            foreach (ThingParameterDAL param in parameters)
            {
                param.Thing = thing;
                context.ThingParameter.Add(param);
            }
            context.SaveChanges();
        }
Ejemplo n.º 6
0
        public static void AddAlarmZone(ZoneGuardConfigContext context, AlarmZoneDAL alarmZone, AlarmZoneThingDAL[] alarmZoneThings)
        {
            context.AlarmZone.Add(alarmZone);
            context.SaveChanges();

            foreach (AlarmZoneThingDAL sensor in alarmZoneThings)
            {
                sensor.AlarmZone = alarmZone;
                context.AlarmZoneThing.Add(sensor);
            }
            context.SaveChanges();
        }
Ejemplo n.º 7
0
        //https://www.bmwbayer.de

        private void ClearConfigTables()
        {
            ZoneGuardConfigContextFactory factory = new ZoneGuardConfigContextFactory();

            using (ZoneGuardConfigContext ConfigContext = factory.CreateDbContext())
            {
                ConfigContext.Thing.RemoveRange(ConfigContext.Set <ThingDAL>());
                ConfigContext.AlarmZone.RemoveRange(ConfigContext.Set <AlarmZoneDAL>());

                ConfigContext.SaveChanges();
            }
        }
Ejemplo n.º 8
0
        private void CreateConfiguration_Services()
        {
            ZoneGuardConfigContextFactory factory = new ZoneGuardConfigContextFactory();
            Guid serverNodeGuid = new Guid("5c7a6580-45fc-4402-9ebe-07de9884f5b0");


            using (ZoneGuardConfigContext context = factory.CreateDbContext())
            {
                node = null;

                if (!context.Node.Any())
                {
                    node = new NodeDAL {
                        UniqueIdentifier = serverNodeGuid, Name = "Server"
                    };
                    context.Node.Add(node);

                    context.SaveChanges();
                }
                else
                {
                    node = context.Node.Where <NodeDAL>(n => n.UniqueIdentifier == serverNodeGuid).FirstOrDefault <NodeDAL>();
                }


                ThingDAL thing;
                thing = AddService(context, "MQ", "MQ Client", ThingType.Service, new Dictionary <string, string> {
                    { "category", "service" },
                    { "config_class", "ConfigServiceMQ" },
                    { "thing_class", "ServiceMQ" },
                    { "name", "MQ" },
                    { "host", "192.168.9.40" },
                    { "user", "svc-alarm-test" },
                    { "password", "Alarm01" },
                    { "vhost", "zoneguard-test" }
                });


                //ConfigServiceMQTT csMQTT = new ConfigServiceMQTT(paramMQTT);
                //addService(new ServiceMQTT(new ConfigServiceMQTT(paramMQTT), this));

                thing = AddService(context, "MQTT", "MQTT Client", ThingType.Service, new Dictionary <string, string> {
                    { "category", "service" },
                    { "config_class", "ConfigServiceMQTT" },
                    { "thing_class", "ServiceMQTT" },
                    { "name", "MQTT" },
                    { "host", "192.168.1.50" },
                    { "user", "svc-openhab" },
                    { "password", "#openHAB@Home#" }
                });
            }
        }
Ejemplo n.º 9
0
        private async Task _InitializeServices()
        {
            ZoneGuardConfigContextFactory factory = new ZoneGuardConfigContextFactory();

            using (ZoneGuardConfigContext context = factory.CreateDbContext())
            {
                /***************************************************************
                *
                *  Load Services
                *
                ***************************************************************/
                List <ThingDAL> services = await context.Thing
                                           .Where <ThingDAL>(t => t.ThingType == ThingType.Service)
                                           .Include(p => p.Parameters)
                                           .ToListAsync <ThingDAL>();

                foreach (ThingDAL serviceDAL in services)
                {
                    Console.WriteLine(serviceDAL.Name);
                    ConfigService configService = ZoneGuardConfigContextFactory.CreateThingFromDAL <ConfigService>(serviceDAL);

                    Console.WriteLine(configService.toJSON());

#if warning
#endif
                    //TODO:: MUST
                    //TODOO:: MIGRATE                    addSensor(new SensorProxy(configSensor, this), false);
                    ServiceCore service = null;
                    String      s       = configService.ConfigClass;
                    if (configService.ConfigClass == "ConfigServiceMQTT")
                    {
                        service = new ServiceMQTT((ConfigServiceMQTT)configService, this);
                    }
                    else if (configService.ConfigClass == "ConfigServiceMQ")
                    {
                        service = new ServiceMQ((ConfigServiceMQ)configService, this);
                    }
                    if (service != null)
                    {
                        addService(service);
                    }
                    else
                    {
                        Logger.LogWarning("Service '{0}' not created.", configService.Id);
                    }
                }
            }
        }
Ejemplo n.º 10
0
        private void CreateConfiguration_AlarmZones()
        {
            //string topicOffset = "test/";
            //ThingDAL thing;

            ZoneGuardConfigContextFactory factory = new ZoneGuardConfigContextFactory();

            using (ZoneGuardConfigContext context = factory.CreateDbContext())
            {
                AddAlarmZone(context, "Perimeter", "House Fence Zone", new string[] { "LivingroomWindow1", "LivingroomWindow2", "LivingroomWindow3", "LivingroomDoor1", "OfficeWindow1", "HallwayWindow1", "FrederikWindow1", "CecilieWindow1", "SofieWindow1", "BedroomWindow1", "BackentranceWindow1", "BackentranceDoor1", "BathroomWindow1", "EntranceDoor1", "KitchenWindow1" });
                AddAlarmZone(context, "Front", "Outdoor front", new string[] { "FrontPir1" });
                AddAlarmZone(context, "Carport", "Carport", new string[] { "BackyardDoor1" });
                //AddAlarmZone(context, "Terrace", "Outdoor front", new string[] { "Pir1" });
                AddAlarmZone(context, "Backyard", "Backyard", new string[] { "BackyardPir1" });
            }
        }
Ejemplo n.º 11
0
        private ThingDAL AddService(ZoneGuardConfigContext context, String Name, String _description, ThingType _type, Dictionary <String, String> parameters)
        {
            ThingDAL thing = null;
            List <ThingParameterDAL> thingParameters = new List <ThingParameterDAL>();
            DateTime timestamp = DateTime.UtcNow;

            thing = new ThingDAL {
                Name = Name, Description = _description, ThingType = _type, NodeId = null, Timestamp = timestamp
            };

            foreach (KeyValuePair <String, String> kvp in parameters)
            {
                thingParameters.Add(new ThingParameterDAL {
                    Name = kvp.Key, Value = kvp.Value, Timestamp = timestamp
                });
            }

            DbInitializer.ThingConfig_Create(context, thing, thingParameters.ToArray());
            return(thing);
        }
Ejemplo n.º 12
0
        private void CreateConfiguration_Sensors()
        {
            string   topicOffset = "zwavebus/state/";
            ThingDAL thing;
            ZoneGuardConfigContextFactory factory = new ZoneGuardConfigContextFactory();


            using (ZoneGuardConfigContext context = factory.CreateDbContext())
            {
                thing = AddMQTTSensor(context, node, "LivingroomWindow1", "Window in Livingroom", ThingType.Sensor, "window", "livingroom", true, topicOffset);
                thing = AddMQTTSensor(context, node, "LivingroomWindow2", "Window in Livingroom", ThingType.Sensor, "window", "livingroom", true, topicOffset);
                thing = AddMQTTSensor(context, node, "LivingroomWindow3", "Window in Livingroom", ThingType.Sensor, "window", "livingroom", true, topicOffset);
                thing = AddMQTTSensor(context, node, "LivingroomDoor1", "Door in Livingroom", ThingType.Sensor, "door", "livingroom", true, topicOffset);

                thing = AddMQTTSensor(context, node, "OfficeWindow1", "Window in Office", ThingType.Sensor, "window", "office", true, topicOffset);

                thing = AddMQTTSensor(context, node, "HallwayWindow1", "Window in Hallway", ThingType.Sensor, "window", "hallway", true, topicOffset);
                thing = AddMQTTSensor(context, node, "FrederikWindow1", "Window in Frederiks room", ThingType.Sensor, "window", "frederik", true, topicOffset);
                thing = AddMQTTSensor(context, node, "CecilieWindow1", "Window in Cecilies room", ThingType.Sensor, "window", "cecilie", true, topicOffset);
                thing = AddMQTTSensor(context, node, "SofieWindow1", "Window in Sofies room", ThingType.Sensor, "window", "sofie", true, topicOffset);
                thing = AddMQTTSensor(context, node, "BedroomWindow1", "Window in Bedroom", ThingType.Sensor, "window", "bedroom", true, topicOffset);
                thing = AddMQTTSensor(context, node, "BackentranceWindow1", "Window in Backentrance", ThingType.Sensor, "window", "backentrance", true, topicOffset);
                thing = AddMQTTSensor(context, node, "BackentranceDoor1", "Door in Backentrance", ThingType.Sensor, "door", "backentrance", true, topicOffset);

                thing = AddMQTTSensor(context, node, "BathroomWindow1", "Window in Bathroom", ThingType.Sensor, "window", "bathroom", true, topicOffset);
                thing = AddMQTTSensor(context, node, "EntranceDoor1", "Door in Entrance", ThingType.Sensor, "door", "entrance", true, topicOffset);

                thing = AddMQTTSensor(context, node, "KitchenWindow1", "Window in Kitchen", ThingType.Sensor, "window", "kitchen", true, topicOffset);
                //                thing = AddMQTTSensor(context, "BackentranceWindow1", "Window in Backentrance", ThingType.Sensor, "window", true, topicOffset);

                thing = AddMQTTSensor(context, node, "HallwayPir1", "Pir sensor 1 in Hallway", ThingType.Sensor, "pir", "hallway", true, topicOffset);
                thing = AddMQTTSensor(context, node, "HallwayPir2", "Pir sensor 2 in Hallway", ThingType.Sensor, "pir", "hallway", true, topicOffset);

                thing = AddMQTTSensor(context, node, "BackyardPir1", "Pir in Backyard", ThingType.Sensor, "pir", "backyard", true, topicOffset);
                thing = AddMQTTSensor(context, node, "FrontPir1", "Pir in Front of house", ThingType.Sensor, "pir", "front", true, topicOffset);

                thing = AddMQTTSensor(context, node, "BackyardDoor1", "Door from Carport to backyard", ThingType.Sensor, "door", "backyard", true, topicOffset);
            }
        }
Ejemplo n.º 13
0
        private AlarmZoneDAL AddAlarmZone(ZoneGuardConfigContext context, String name, String description, String[] sensors)
        {
            AlarmZoneDAL             alarmZone        = null;
            List <AlarmZoneThingDAL> alarmZoneSensors = new List <AlarmZoneThingDAL>();
            DateTime timestamp = DateTime.UtcNow;

            alarmZone = new AlarmZoneDAL {
                Name = name, Description = description, Enabled = 1, Timestamp = timestamp
            };

            foreach (String sensorName in sensors)
            {
                ThingDAL curSensor = context.Thing.Where <ThingDAL>(n => n.ThingType == ThingType.Sensor).Where <ThingDAL>(n => n.Name == sensorName).FirstOrDefault <ThingDAL>();
                alarmZoneSensors.Add(new AlarmZoneThingDAL {
                    Enabled = 1, Thing = curSensor, Timestamp = timestamp
                });
            }
            ;
            DbInitializer.AddAlarmZone(context, alarmZone, alarmZoneSensors.ToArray());


            return(alarmZone);
        }
Ejemplo n.º 14
0
        //, Dictionary<string, string> parameters
        private ThingDAL AddMQTTSensor(ZoneGuardConfigContext context, NodeDAL node, String Name, String _description, ThingType _type, String sensorType, String locationId, Boolean isPerimeter, String topicOffset)
        {
            ThingDAL thing = null;
            List <ThingParameterDAL> thingParameters = new List <ThingParameterDAL>();
            DateTime timestamp = DateTime.UtcNow;

            thing = new ThingDAL {
                Name = Name, Description = _description, ThingType = _type, NodeId = node.Id, Timestamp = timestamp
            };
            thingParameters.Add(new ThingParameterDAL {
                Name = ConfigCore.PARAMETER_THING_CATEGORY, Value = "sensor", Timestamp = timestamp
            });
            thingParameters.Add(new ThingParameterDAL {
                Name = ConfigCore.PARAMETER_CONFIG_CLASS, Value = "ConfigSensor", Timestamp = timestamp
            });
            thingParameters.Add(new ThingParameterDAL {
                Name = ConfigCore.PARAMETER_THING_CLASS, Value = "SensorMQTT", Timestamp = timestamp
            });
            thingParameters.Add(new ThingParameterDAL {
                Name = ConfigCore.PARAMETER_TYPE, Value = sensorType, Timestamp = timestamp
            });
            thingParameters.Add(new ThingParameterDAL {
                Name = ConfigCore.PARAMETER_NAME, Value = Name, Timestamp = timestamp
            });
            thingParameters.Add(new ThingParameterDAL {
                Name = ConfigCore.PARAMETER_IS_PERIMETER, Value = isPerimeter.ToString().ToLower()
            });
            thingParameters.Add(new ThingParameterDAL {
                Name = ConfigCore.PARAMETER_TOPIC_STATE, Value = topicOffset + locationId.ToLower() + "/" + Name.ToLower() + "/state"
            });
            thingParameters.Add(new ThingParameterDAL {
                Name = ConfigCore.PARAMETER_TOPIC_COMMAND, Value = ""
            });

            DbInitializer.ThingConfig_Create(context, thing, thingParameters.ToArray());
            return(thing);
        }
Ejemplo n.º 15
0
        /*
         * public static void AddServiceConfig(ZoneGuardConfigContext context, ServiceDAL thing, ServiceParameterDAL[] parameters)
         * {
         *  context.Service.Add(thing);
         *  context.SaveChanges();
         *
         *  foreach (ServiceParameterDAL param in parameters)
         *  {
         *      param.Service = thing;
         *      context.ServiceParameter.Add(param);
         *  }
         *  context.SaveChanges();
         * }
         */


        public static void AddSensorStateLog(ZoneGuardConfigContext context, SensorStateLogDAL ssl)
        {
            context.SensorStateLog.Add(ssl);
            context.SaveChanges();
        }
Ejemplo n.º 16
0
        private void originalloadTestConfigData()
        {
            ZoneGuardConfigContextFactory factory = new ZoneGuardConfigContextFactory();
            Guid serverNodeGuid = new Guid("5c7a6580-45fc-4402-9ebe-07de9884f5b0");

            using (ZoneGuardConfigContext context = factory.CreateDbContext())
            {
                /*
                 * NodeDAL node = null;
                 *
                 * if (!context.Node.Any())
                 * {
                 *  node = new NodeDAL { UniqueIdentifier = serverNodeGuid, Name = "Server" };
                 *  context.Node.Add(node);
                 *
                 *  context.SaveChanges();
                 * }
                 * else
                 * {
                 *  node = context.Node.Where<NodeDAL>(n => n.UniqueIdentifier == serverNodeGuid).FirstOrDefault<NodeDAL>();
                 * }
                 *
                 */

                // Look for any students.
                SensorDAL thing = null;

                if (!context.Thing.Any())
                {
                    //                  ThingParameterDAL paramCategory = new ThingParameterDAL { Name = "category", Value = "sensor", Timestamp = DateTime.UtcNow };
                    //                   ThingParameterDAL paramConfigClass = new ThingParameterDAL { Name = "config_class", Value = "ConfigSensor", Timestamp = DateTime.UtcNow };
                    //                    ThingParameterDAL paramThingClass= new ThingParameterDAL { Name = "thing_class", Value = "SensorMQTT", Timestamp = DateTime.UtcNow };

                    List <SensorDAL> sensorsPerimeter = new List <SensorDAL>();
                    //List<String> sensorsPerimeter = new List<String>();
                    List <SensorDAL> sensorsHighRisk = new List <SensorDAL>();

                    String topicOffset = "test/";

                    SensorParameterDAL[] thingParameters;


                    thing = new SensorDAL {
                        Name = "OfficeWindow1", Description = "Window in Office", ThingType = ThingType.Sensor /*, Node = node*/, Timestamp = DateTime.UtcNow
                    };
                    sensorsPerimeter.Add(thing);
                    sensorsHighRisk.Add(thing);
                    thingParameters = new SensorParameterDAL[]
                    {
                        new SensorParameterDAL {
                            Name = "category", Value = "sensor", Timestamp = DateTime.UtcNow
                        },
                        new SensorParameterDAL {
                            Name = "config_class", Value = "ConfigSensor", Timestamp = DateTime.UtcNow
                        },
                        new SensorParameterDAL {
                            Name = "thing_class", Value = "SensorMQTT", Timestamp = DateTime.UtcNow
                        },
                        new SensorParameterDAL {
                            Name = "name", Value = "OfficeWindow1", Timestamp = DateTime.UtcNow
                        },
                        new SensorParameterDAL {
                            Name = "topic", Value = topicOffset + "officewindow1/state", Timestamp = DateTime.UtcNow
                        }
                    };
                    DbInitializer.AddThing(context, thing, thingParameters);

                    /* ***********
                     *
                     * Sensor 2
                     *
                     * ************/

                    thing = new SensorDAL {
                        Name = "LivingroomWindow1", Description = "First Window in Livingroom", ThingType = ThingType.Sensor /*, Node = node*/, Timestamp = DateTime.UtcNow
                    };
                    sensorsPerimeter.Add(thing);
                    thingParameters = new SensorParameterDAL[]
                    {
                        new SensorParameterDAL {
                            Name = "category", Value = "sensor", Timestamp = DateTime.UtcNow
                        },
                        new SensorParameterDAL {
                            Name = "config_class", Value = "ConfigSensor", Timestamp = DateTime.UtcNow
                        },
                        new SensorParameterDAL {
                            Name = "thing_class", Value = "SensorMQTT", Timestamp = DateTime.UtcNow
                        },
                        new SensorParameterDAL {
                            Name = "name", Value = "LivingroomWindow1", Timestamp = DateTime.UtcNow
                        },
                        new SensorParameterDAL {
                            Name = "topic", Value = topicOffset + "livingroomwindow1/state", Timestamp = DateTime.UtcNow
                        }
                    };
                    DbInitializer.AddThing(context, thing, thingParameters);


                    thing = AddThing(context, "LivingroomWindow2", "Second Window in Livingroom", ThingType.Sensor, new Dictionary <string, string> {
                        { "category", "sensor" },
                        { "config_class", "ConfigSensor" },
                        { "thing_class", "SensorMQTT" },
                        { "name", "LivingroomWindow2" },
                        { "topic", topicOffset + "livingroomwindow2/state" }
                    });

                    sensorsPerimeter.Add(thing);

                    /*
                     * thing = new ThingDAL { Name = "LivingroomWindow2", Description = "Second Window in Livingroom", ThingType = ThingType.Sensor, Timestamp = DateTime.UtcNow };
                     * sensorsPerimeter.Add(thing);
                     * thingParameters = new ThingParameterDAL[]
                     * {
                     *  new ThingParameterDAL{Name="category", Value="sensor", Timestamp=DateTime.UtcNow},
                     *  new ThingParameterDAL{Name="config_class", Value="ConfigSensor", Timestamp=DateTime.UtcNow },
                     *  new ThingParameterDAL{Name="thing_class", Value="SensorMQTT", Timestamp=DateTime.UtcNow },
                     *  new ThingParameterDAL{Name="name", Value = "LivingroomWindow2", Timestamp=DateTime.UtcNow },
                     *  new ThingParameterDAL{Name="topic", Value = topicOffset + "livingroomwindow2/state", Timestamp=DateTime.UtcNow }
                     * };
                     * DbInitializer.AddThing(context, thing, thingParameters);
                     */

                    thing = new SensorDAL {
                        Name = "LivingroomWindow3", Description = "Third Window in Livingroom", ThingType = ThingType.Sensor /*, Node = node*/, Timestamp = DateTime.UtcNow
                    };
                    sensorsPerimeter.Add(thing);
                    thingParameters = new SensorParameterDAL[]
                    {
                        new SensorParameterDAL {
                            Name = "category", Value = "sensor", Timestamp = DateTime.UtcNow
                        },
                        new SensorParameterDAL {
                            Name = "config_class", Value = "ConfigSensor", Timestamp = DateTime.UtcNow
                        },
                        new SensorParameterDAL {
                            Name = "thing_class", Value = "SensorMQTT", Timestamp = DateTime.UtcNow
                        },
                        new SensorParameterDAL {
                            Name = "name", Value = "LivingroomWindow3", Timestamp = DateTime.UtcNow
                        },
                        new SensorParameterDAL {
                            Name = "topic", Value = topicOffset + "livingroomwindow3/state", Timestamp = DateTime.UtcNow
                        }
                    };
                    DbInitializer.AddThing(context, thing, thingParameters);


                    /* ***********
                     *
                     * LivingroomDoor1
                     *
                     * ************/

                    thing = new SensorDAL {
                        Name = "LivingroomDoor1", Description = "Terracedoor in Livingroom", ThingType = ThingType.Sensor /*, Node = node*/, Timestamp = DateTime.UtcNow
                    };
                    sensorsPerimeter.Add(thing);
                    sensorsHighRisk.Add(thing);
                    thingParameters = new SensorParameterDAL[]
                    {
                        new SensorParameterDAL {
                            Name = "category", Value = "sensor", Timestamp = DateTime.UtcNow
                        },
                        new SensorParameterDAL {
                            Name = "config_class", Value = "ConfigSensor", Timestamp = DateTime.UtcNow
                        },
                        new SensorParameterDAL {
                            Name = "thing_class", Value = "SensorMQTT", Timestamp = DateTime.UtcNow
                        },
                        new SensorParameterDAL {
                            Name = "name", Value = "LivingroomDoor1", Timestamp = DateTime.UtcNow
                        },
                        new SensorParameterDAL {
                            Name = "topic", Value = topicOffset + "livingroomdoor1/state", Timestamp = DateTime.UtcNow
                        }
                    };
                    DbInitializer.AddThing(context, thing, thingParameters);


                    /* ***********
                     *
                     * FrederikWindow1
                     *
                     * ************/

                    thing = new SensorDAL {
                        Name = "FrederikWindow1", Description = "Window (Frederik)", ThingType = ThingType.Sensor /*, Node = node*/, Timestamp = DateTime.UtcNow
                    };
                    sensorsPerimeter.Add(thing);
                    thingParameters = new SensorParameterDAL[]
                    {
                        new SensorParameterDAL {
                            Name = "category", Value = "sensor", Timestamp = DateTime.UtcNow
                        },
                        new SensorParameterDAL {
                            Name = "config_class", Value = "ConfigSensor", Timestamp = DateTime.UtcNow
                        },
                        new SensorParameterDAL {
                            Name = "thing_class", Value = "SensorMQTT", Timestamp = DateTime.UtcNow
                        },
                        new SensorParameterDAL {
                            Name = "name", Value = "FrederikWindow1", Timestamp = DateTime.UtcNow
                        },
                        new SensorParameterDAL {
                            Name = "topic", Value = topicOffset + "frederikwindow1/state", Timestamp = DateTime.UtcNow
                        }
                    };
                    DbInitializer.AddThing(context, thing, thingParameters);


                    /* ***********
                     * CecilieWindow1
                     * ************/
                    thing = new SensorDAL {
                        Name = "CecilieWindow1", Description = "Window (Cecilie)", ThingType = ThingType.Sensor /*, Node = node*/, Timestamp = DateTime.UtcNow
                    };
                    sensorsPerimeter.Add(thing);
                    sensorsHighRisk.Add(thing);
                    thingParameters = new SensorParameterDAL[]
                    {
                        new SensorParameterDAL {
                            Name = "category", Value = "sensor", Timestamp = DateTime.UtcNow
                        },
                        new SensorParameterDAL {
                            Name = "config_class", Value = "ConfigSensor", Timestamp = DateTime.UtcNow
                        },
                        new SensorParameterDAL {
                            Name = "thing_class", Value = "SensorMQTT", Timestamp = DateTime.UtcNow
                        },
                        new SensorParameterDAL {
                            Name = "name", Value = "CecilieWindow1", Timestamp = DateTime.UtcNow
                        },
                        new SensorParameterDAL {
                            Name = "topic", Value = topicOffset + "ceciliewindow1/state", Timestamp = DateTime.UtcNow
                        }
                    };
                    DbInitializer.AddThing(context, thing, thingParameters);


                    /* ***********
                     * SofieWindow1
                     * ************/
                    thing = new SensorDAL {
                        Name = "SofieWindow1", Description = "Window (Sofie)", ThingType = ThingType.Sensor /*, Node = node*/, Timestamp = DateTime.UtcNow
                    };
                    sensorsPerimeter.Add(thing);
                    sensorsHighRisk.Add(thing);
                    thingParameters = new SensorParameterDAL[]
                    {
                        new SensorParameterDAL {
                            Name = "category", Value = "sensor", Timestamp = DateTime.UtcNow
                        },
                        new SensorParameterDAL {
                            Name = "config_class", Value = "ConfigSensor", Timestamp = DateTime.UtcNow
                        },
                        new SensorParameterDAL {
                            Name = "thing_class", Value = "SensorMQTT", Timestamp = DateTime.UtcNow
                        },
                        new SensorParameterDAL {
                            Name = "name", Value = "SofieWindow1", Timestamp = DateTime.UtcNow
                        },
                        new SensorParameterDAL {
                            Name = "topic", Value = topicOffset + "sofiewindow1/state", Timestamp = DateTime.UtcNow
                        }
                    };
                    DbInitializer.AddThing(context, thing, thingParameters);


                    /* ***********
                     * BedroomWindow1
                     * ************/
                    thing = new SensorDAL {
                        Name = "BedroomWindow1", Description = "Window (Bedroom)", ThingType = ThingType.Sensor /*, Node = node*/, Timestamp = DateTime.UtcNow
                    };
                    sensorsPerimeter.Add(thing);
                    sensorsHighRisk.Add(thing);
                    thingParameters = new SensorParameterDAL[]
                    {
                        new SensorParameterDAL {
                            Name = "category", Value = "sensor", Timestamp = DateTime.UtcNow
                        },
                        new SensorParameterDAL {
                            Name = "config_class", Value = "ConfigSensor", Timestamp = DateTime.UtcNow
                        },
                        new SensorParameterDAL {
                            Name = "thing_class", Value = "SensorMQTT", Timestamp = DateTime.UtcNow
                        },
                        new SensorParameterDAL {
                            Name = "name", Value = "BedroomWindow1", Timestamp = DateTime.UtcNow
                        },
                        new SensorParameterDAL {
                            Name = "topic", Value = topicOffset + "bedroomwindow1/state", Timestamp = DateTime.UtcNow
                        }
                    };
                    DbInitializer.AddThing(context, thing, thingParameters);


                    /* ***********
                     * BackentranceWindow1
                     * ************/
                    thing = new SensorDAL {
                        Name = "BackentranceWindow1", Description = "Window (Backentrance)", ThingType = ThingType.Sensor /*, Node = node*/, Timestamp = DateTime.UtcNow
                    };
                    sensorsPerimeter.Add(thing);
                    sensorsHighRisk.Add(thing);
                    thingParameters = new SensorParameterDAL[]
                    {
                        new SensorParameterDAL {
                            Name = "category", Value = "sensor", Timestamp = DateTime.UtcNow
                        },
                        new SensorParameterDAL {
                            Name = "config_class", Value = "ConfigSensor", Timestamp = DateTime.UtcNow
                        },
                        new SensorParameterDAL {
                            Name = "thing_class", Value = "SensorMQTT", Timestamp = DateTime.UtcNow
                        },
                        new SensorParameterDAL {
                            Name = "name", Value = "BackentranceWindow1", Timestamp = DateTime.UtcNow
                        },
                        new SensorParameterDAL {
                            Name = "topic", Value = topicOffset + "backentrancewindow1/state", Timestamp = DateTime.UtcNow
                        }
                    };
                    DbInitializer.AddThing(context, thing, thingParameters);


                    /* ***********
                     * BackentranceDoor1
                     * ************/
                    thing = new SensorDAL {
                        Name = "BackentranceDoor1", Description = "Door (Backentrance)", ThingType = ThingType.Sensor /*, Node = node*/, Timestamp = DateTime.UtcNow
                    };
                    sensorsPerimeter.Add(thing);
                    sensorsHighRisk.Add(thing);
                    thingParameters = new SensorParameterDAL[]
                    {
                        new SensorParameterDAL {
                            Name = "category", Value = "sensor", Timestamp = DateTime.UtcNow
                        },
                        new SensorParameterDAL {
                            Name = "config_class", Value = "ConfigSensor", Timestamp = DateTime.UtcNow
                        },
                        new SensorParameterDAL {
                            Name = "thing_class", Value = "SensorMQTT", Timestamp = DateTime.UtcNow
                        },
                        new SensorParameterDAL {
                            Name = "name", Value = "BackentranceDoor1", Timestamp = DateTime.UtcNow
                        },
                        new SensorParameterDAL {
                            Name = "topic", Value = topicOffset + "backentrancedoor1/state", Timestamp = DateTime.UtcNow
                        }
                    };
                    DbInitializer.AddThing(context, thing, thingParameters);


                    /* ***********
                     * BathroomWindow1
                     * ************/
                    thing = new SensorDAL {
                        Name = "BathroomWindow1", Description = "Window (Bathroom)", ThingType = ThingType.Sensor /*, Node = node*/, Timestamp = DateTime.UtcNow
                    };
                    sensorsPerimeter.Add(thing);
                    thingParameters = new SensorParameterDAL[]
                    {
                        new SensorParameterDAL {
                            Name = "category", Value = "sensor", Timestamp = DateTime.UtcNow
                        },
                        new SensorParameterDAL {
                            Name = "config_class", Value = "ConfigSensor", Timestamp = DateTime.UtcNow
                        },
                        new SensorParameterDAL {
                            Name = "thing_class", Value = "SensorMQTT", Timestamp = DateTime.UtcNow
                        },
                        new SensorParameterDAL {
                            Name = "name", Value = "BathroomWindow1", Timestamp = DateTime.UtcNow
                        },
                        new SensorParameterDAL {
                            Name = "topic", Value = topicOffset + "bathroomwindow1/state", Timestamp = DateTime.UtcNow
                        }
                    };
                    DbInitializer.AddThing(context, thing, thingParameters);


                    /* ***********
                     * EntranceDoor1
                     * ************/
                    thing = new SensorDAL {
                        Name = "EntranceDoor1", Description = "Window (Entrancedoor)", ThingType = ThingType.Sensor /*, Node = node*/, Timestamp = DateTime.UtcNow
                    };
                    sensorsPerimeter.Add(thing);
                    thingParameters = new SensorParameterDAL[]
                    {
                        new SensorParameterDAL {
                            Name = "category", Value = "sensor", Timestamp = DateTime.UtcNow
                        },
                        new SensorParameterDAL {
                            Name = "config_class", Value = "ConfigSensor", Timestamp = DateTime.UtcNow
                        },
                        new SensorParameterDAL {
                            Name = "thing_class", Value = "SensorMQTT", Timestamp = DateTime.UtcNow
                        },
                        new SensorParameterDAL {
                            Name = "name", Value = "EntranceDoor1", Timestamp = DateTime.UtcNow
                        },
                        new SensorParameterDAL {
                            Name = "topic", Value = topicOffset + "frontentrancedoor1/state", Timestamp = DateTime.UtcNow
                        }
                    };
                    DbInitializer.AddThing(context, thing, thingParameters);


                    AlarmZoneDAL alarmZonePerimeter = new AlarmZoneDAL {
                        Name = "Perimeter", Description = "", Enabled = 1, Timestamp = DateTime.UtcNow
                    };
                    List <AlarmZoneThingDAL> alarmZoneSensors = new List <AlarmZoneThingDAL>();

                    foreach (SensorDAL sensor in sensorsPerimeter)
                    {
                        alarmZoneSensors.Add(new AlarmZoneThingDAL {
                            Enabled = 1, Thing = sensor, Timestamp = DateTime.UtcNow
                        });
                    }
                    ;
                    DbInitializer.AddAlarmZone(context, alarmZonePerimeter, alarmZoneSensors.ToArray());


                    AlarmZoneDAL alarmZoneHighRisk = new AlarmZoneDAL {
                        Name = "High Risk", Description = "", Enabled = 1, Timestamp = DateTime.UtcNow
                    };
                    alarmZoneSensors = new List <AlarmZoneThingDAL>();

                    foreach (SensorDAL sensor in sensorsHighRisk)
                    {
                        alarmZoneSensors.Add(new AlarmZoneThingDAL {
                            Enabled = 1, Thing = sensor, Timestamp = DateTime.UtcNow
                        });
                    }
                    ;
                    DbInitializer.AddAlarmZone(context, alarmZoneHighRisk, alarmZoneSensors.ToArray());
                }
            }
        }
Ejemplo n.º 17
0
        private async Task LoadFromDatabase()
        {
            ZoneGuardConfigContextFactory factory = new ZoneGuardConfigContextFactory();

            using (ZoneGuardConfigContext context = factory.CreateDbContext())
            {
                /***************************************************************
                *
                *  Load Sensors
                *
                ***************************************************************/
                List <ThingDAL> things = await context.Thing
                                         .Where <ThingDAL>(t => t.ThingType == ThingType.Sensor)
                                         .Include(p => p.Parameters)
                                         .ToListAsync <ThingDAL>();

                foreach (ThingDAL thing in things)
                {
                    Console.WriteLine(thing.Name);
                    ConfigSensor configSensor = ZoneGuardConfigContextFactory.CreateThingFromDAL <ConfigSensor>(thing);

                    Console.WriteLine(configSensor.toJSON());

                    //TODO:: More Dynamic
                    addMQTTSensor(new SensorMQTT(configSensor, this), true);

                    addProxySensor(new SensorProxy(configSensor, this), false);
                }



                /***************************************************************
                *
                *  Load AlarmZones
                *
                ***************************************************************/

                List <AlarmZoneDAL> alarmZones = await context.AlarmZone
                                                 .Include(az => az.Sensors)
                                                 .ThenInclude(pa => pa.Thing)
                                                 .ToListAsync <AlarmZoneDAL>();


                foreach (AlarmZoneDAL zone in alarmZones)
                {
                    ConfigAlarmZone configAlarmZone = ZoneGuardConfigContextFactory.CreateConfigAlarmZoneFromDAL(zone);

                    AlarmZone alarmZone = new AlarmZone(configAlarmZone, this);
                    alarmManager.addAlarmZone(alarmZone);

                    /*
                     *              //if (zone.Enabled == 1)
                     *              {
                     *                  Console.WriteLine("Creating Alarm Zone {0}", zone.Name);
                     *                  foreach (AlarmZoneThingDAL sensor in zone.Sensors)
                     *                  {
                     *                      SensorCore sensorProxy = getSensorByName(sensor.Thing.Name);
                     *                      SensorLink sensorLink = new SensorLink(sensorProxy, alarmZone, this);
                     *
                     *
                     *                      //ConfigLink configLink = new ConfigLink();
                     *
                     *                      //SensorProxy sensorProxy = new SensorProxy(configSensor, this);
                     *                      //HEST
                     *                      //alarmZone.addSensor(sensorProxy);
                     *                      //if (sensor.Enabled == 1)
                     *                      //{
                     *                       //   Console.WriteLine("   Adding Sensor '{0}'", sensor.Thing.Name);
                     *                     // }
                     *
                     * }
                     * }*/
                }
            }
        }
Ejemplo n.º 18
0
        private void InitializeConfigData_NA(ZoneGuardConfigContext context)
        {
            SensorDAL thing = null;

            if (!context.Sensor.Any())
            {
                List <SensorDAL> sensors = new List <SensorDAL>();

                String topicOffset = "test/";

                SensorParameterDAL[] thingParameters;


                thing = new SensorDAL {
                    Name = "OfficeWindow1", Description = "Window in Office", ThingType = ThingType.Sensor /*, Node = node*/, Timestamp = DateTime.UtcNow
                };
                sensors.Add(thing);
                thingParameters = new SensorParameterDAL[]
                {
                    new SensorParameterDAL {
                        Name = "topic", Value = "test/officewindows1/state", Timestamp = DateTime.UtcNow
                    }
                    //new ConfigThingParameter{Name="Action 1", ThingType=ThingType.Action, Timestamp=DateTime.UtcNow}
                };
                AddThing(context, thing, thingParameters);

                /* ***********
                 *
                 * Sensor 2
                 *
                 * ************/

                thing = new SensorDAL {
                    Name = "LivingroomWindow1", Description = "First Window in Livingroom", ThingType = ThingType.Sensor /*, Node = node*/, Timestamp = DateTime.UtcNow
                };
                sensors.Add(thing);
                thingParameters = new SensorParameterDAL[]
                {
                    new SensorParameterDAL {
                        Name = "topic", Value = topicOffset + "livingroomwindows1/state", Timestamp = DateTime.UtcNow
                    },
                    new SensorParameterDAL {
                        Name = "a_value", Value = "Some value", Timestamp = DateTime.UtcNow
                    }

                    //new ConfigThingParameter{Name="Action 1", ThingType=ThingType.Action, Timestamp=DateTime.UtcNow}
                };
                AddThing(context, thing, thingParameters);



                thing = new SensorDAL {
                    Name = "Livingroom2", Description = "Second Window in Livingroom", ThingType = ThingType.Sensor /*, Node = node*/, Timestamp = DateTime.UtcNow
                };
                sensors.Add(thing);
                thingParameters = new SensorParameterDAL[]
                {
                    new SensorParameterDAL {
                        Name = "topic", Value = topicOffset + "livingroomwindows2/state", Timestamp = DateTime.UtcNow
                    }
                    //new ConfigThingParameter{Name="Action 1", ThingType=ThingType.Action, Timestamp=DateTime.UtcNow}
                };
                AddThing(context, thing, thingParameters);


                thing = new SensorDAL {
                    Name = "Livingroom3", Description = "Third Window in Livingroom", ThingType = ThingType.Sensor /*, Node = node*/, Timestamp = DateTime.UtcNow
                };
                sensors.Add(thing);
                thingParameters = new SensorParameterDAL[]
                {
                    new SensorParameterDAL {
                        Name = "topic", Value = topicOffset + "livingroomwindows3/state", Timestamp = DateTime.UtcNow
                    }
                    //new ConfigThingParameter{Name="Action 1", ThingType=ThingType.Action, Timestamp=DateTime.UtcNow}
                };
                AddThing(context, thing, thingParameters);



                AlarmZoneDAL alarmZone = null;
                alarmZone = new AlarmZoneDAL {
                    Name = "Perimeter", Description = "", Enabled = 1, Timestamp = DateTime.UtcNow
                };
                List <AlarmZoneThingDAL> alarmZoneSensors = new List <AlarmZoneThingDAL>();

                foreach (SensorDAL sensor in sensors)
                {
                    alarmZoneSensors.Add(new AlarmZoneThingDAL {
                        Enabled = 1, Thing = sensor, Timestamp = DateTime.UtcNow
                    });
                }
                ;
                AddAlarmZone(context, alarmZone, alarmZoneSensors.ToArray());
            }



            SensorStateLogDAL ssl = null;


            thing = context.Thing.Where(s => s.Id == 2).First <SensorDAL>();

            ssl = new SensorStateLogDAL {
                Triggered = 1, Sensor = thing, CreatedTimestamp = DateTime.UtcNow.AddHours(-1)
            };
            AddSensorStateLog(context, ssl);

            ssl = new SensorStateLogDAL {
                Triggered = 0, Sensor = thing, CreatedTimestamp = DateTime.UtcNow.AddHours(-1).AddMinutes(1)
            };
            AddSensorStateLog(context, ssl);

            return;   // DB has been seeded

            //}

            /*
             *
             * DETTE VIRKER
             * var things = new ConfigThing[]
             * {
             * new ConfigThing{Name="Sensor 1", ThingType=ThingType.Sensor, Timestamp=DateTime.UtcNow},
             * new ConfigThing{Name="Sensor 2", ThingType=ThingType.Sensor, Timestamp=DateTime.UtcNow},
             * new ConfigThing{Name="Action 1", ThingType=ThingType.Action, Timestamp=DateTime.UtcNow}
             * };
             * foreach (ConfigThing s in things)
             * {
             *  context.Thing.Add(s);
             *  context.SaveChanges();
             *  int id = s.ID;
             *  Console.WriteLine("Id is {0}", id);
             * }
             * context.SaveChanges();
             * IEnumerable<ConfigThing> things1 = context.Thing.AsEnumerable<ConfigThing>();
             * int count = things.Count<ConfigThing>();
             */

            /*
             *          var things = new ConfigThingParameter[]
             * {
             *          new ConfigThingParameter{ThingId=1, Name="Topic", Key="test/officewindow1/state", Timestamp=DateTime.UtcNow},
             *          new ConfigThing{Name="Sensor 2", ThingType=ThingType.Sensor, Timestamp=DateTime.UtcNow},
             *          new ConfigThing{Name="Action 1", ThingType=ThingType.Action, Timestamp=DateTime.UtcNow}
             * };Ø/
             *
             *          /*
             *
             *          var courses = new Course[]
             *          {
             *          new Course{CourseID=1050,Title="Chemistry",Credits=3},
             *          new Course{CourseID=4022,Title="Microeconomics",Credits=3},
             *          new Course{CourseID=4041,Title="Macroeconomics",Credits=3},
             *          new Course{CourseID=1045,Title="Calculus",Credits=4},
             *          new Course{CourseID=3141,Title="Trigonometry",Credits=4},
             *          new Course{CourseID=2021,Title="Composition",Credits=3},
             *          new Course{CourseID=2042,Title="Literature",Credits=4}
             *          };
             *          foreach (Course c in courses)
             *          {
             *              context.Course.Add(c);
             *          }
             *          context.SaveChanges();
             *
             *          var enrollments = new Enrollment[]
             *          {
             *          new Enrollment{StudentID=1,CourseID=1050,Grade=Grade.A},
             *          new Enrollment{StudentID=1,CourseID=4022,Grade=Grade.C},
             *          new Enrollment{StudentID=1,CourseID=4041,Grade=Grade.B},
             *          new Enrollment{StudentID=2,CourseID=1045,Grade=Grade.B},
             *          new Enrollment{StudentID=2,CourseID=3141,Grade=Grade.F},
             *          new Enrollment{StudentID=2,CourseID=2021,Grade=Grade.F},
             *          new Enrollment{StudentID=3,CourseID=1050},
             *          new Enrollment{StudentID=4,CourseID=1050},
             *          new Enrollment{StudentID=4,CourseID=4022,Grade=Grade.F},
             *          new Enrollment{StudentID=5,CourseID=4041,Grade=Grade.C},
             *          new Enrollment{StudentID=6,CourseID=1045},
             *          new Enrollment{StudentID=7,CourseID=3141,Grade=Grade.A},
             *          };
             *          foreach (Enrollment e in enrollments)
             *          {
             *              context.Enrollment.Add(e);
             *          }
             *          context.SaveChanges();
             */
        }