private void OnSensorUpdatedEvent(Sensor sensor)
 {
     foreach (var link in links)
     {
         if (link.fromNodeId == sensor.nodeId
             && link.fromSensorId == sensor.sensorId)
         {
             string state = sensor.ConvertSensorData(link.toDataType);
             gateway.SendSensorState(link.toNodeId, link.toSensorId, state);
         }
     }
 }
        public void AddInputOutput(Sensor sensor)
        {
            HardwareInput input = new HardwareInput { Name = sensor.GetSimpleName1() };
            input.sensorId = sensor.sensorId;
            input.nodeId = sensor.nodeId;
            Inputs.Add(input);

            HardwareOutput output = new HardwareOutput { Name = sensor.GetSimpleName1() };
            output.sensorId = sensor.sensorId;
            output.nodeId = sensor.nodeId;
               //todo output.Value = sensor.state;
            Outputs.Add(output);
        }
 private void OnSensorUpdated(Sensor sensor)
 {
     if (writeInterval == 0) AddOrUpdateSensor(sensor);
     else
     {
         if (!updatedNodesId.Contains(sensor.nodeId))
             updatedNodesId.Add(sensor.nodeId);
     }
 }
 public void UpdateSensorSettings(Sensor sensor)
 {
     using (var db = new SqlConnection(connectionString))
     {
         db.Open();
         var sqlQuery =
             "UPDATE Sensors SET " +
             "description = @description, " +
             "storeHistoryEnabled = @storeHistoryEnabled, " +
             "storeHistoryEveryChange = @storeHistoryEveryChange, " +
             "storeHistoryWithInterval = @storeHistoryWithInterval, " +
             "invertData = @invertData, " +
             "remapEnabled = @remapEnabled, " +
             "remapFromMin = @remapFromMin, " +
             "remapFromMax = @remapFromMax, " +
             "remapToMin = @remapToMin, " +
             "remapToMax = @remapToMax " +
             "WHERE nodeId = @nodeId AND sensorId = @sensorId";
         db.Execute(sqlQuery, new
         {
             description = sensor.description,
             storeHistoryEnabled = sensor.storeHistoryEnabled,
             storeHistoryEveryChange = sensor.storeHistoryEveryChange,
             storeHistoryWithInterval = sensor.storeHistoryWithInterval,
             invertData = sensor.invertData,
             remapEnabled = sensor.remapEnabled,
             remapFromMin = sensor.remapFromMin,
             remapFromMax = sensor.remapFromMax,
             remapToMin = sensor.remapToMin,
             remapToMax = sensor.remapToMax,
             sensorId = sensor.sensorId,
             nodeId = sensor.nodeId
         });
     }
 }
        public void AddOrUpdateSensor(Sensor sensor)
        {
            using (var db = new SqlConnection(connectionString))
            {
                db.Open();

                Sensor oldSensor =
                    db.Query<Sensor>("SELECT * FROM Sensors WHERE nodeId = @nodeId AND sensorId = @sensorId",
                        new { nodeId = sensor.nodeId, sensorId = sensor.sensorId }).SingleOrDefault();
                int node_db_id =
                    db.Query<Sensor>("SELECT * FROM Nodes WHERE nodeId = @nodeId", new { nodeId = sensor.nodeId })
                        .SingleOrDefault()
                        .db_Id;

                if (oldSensor == null)
                {
                    var sqlQuery = "INSERT INTO Sensors (nodeId, sensorId, type, dataType,state, description, storeHistoryEnabled, storeHistoryEveryChange, storeHistoryWithInterval, invertData, remapEnabled, remapFromMin, remapFromMax, remapToMin, remapToMax, Node_db_Id) "
                                   +
                                   "VALUES(@nodeId, @sensorId, @type, @dataType ,@state, @description,  @storeHistoryEnabled, @storeHistoryEveryChange, @storeHistoryWithInterval, @invertData, @remapEnabled, @remapFromMin, @remapFromMax, @remapToMin, @remapToMax, @Node_db_Id); "
                                   + "SELECT CAST(SCOPE_IDENTITY() as int)";
                    int dbId = db.Query<int>(sqlQuery, new
                    {
                        nodeId = sensor.nodeId,
                        sensorId = sensor.sensorId,
                         type= sensor.type,
                        dataType= sensor.dataType,
                        state= sensor.state,
                        description = sensor.description,
                        storeHistoryEnabled = sensor.storeHistoryEnabled,
                        storeHistoryEveryChange = sensor.storeHistoryEveryChange,
                        storeHistoryWithInterval = sensor.storeHistoryWithInterval,
                        invertData = sensor.invertData,
                        remapEnabled = sensor.remapEnabled,
                        remapFromMin = sensor.remapFromMin,
                        remapFromMax = sensor.remapFromMax,
                        remapToMin = sensor.remapToMin,
                        remapToMax = sensor.remapToMax,
                        Node_db_Id = node_db_id
                    }).Single();

                    gateway.SetSensorDbId(sensor.nodeId, sensor.sensorId, dbId);
                }
                else
                {
                    var sqlQuery =
                        "UPDATE Sensors SET " +
                        "nodeId = @nodeId, " +
                        "sensorId  = @sensorId, " +
                        "type = @type, " +
                        "dataType = @dataType, " +
                        "state = @state, " +
                        "description = @description, " +
                        "storeHistoryEnabled = @storeHistoryEnabled, " +
                        "storeHistoryWithInterval = @storeHistoryWithInterval, " +
                        "storeHistoryEveryChange = @storeHistoryEveryChange, " +
                        "invertData = @invertData, " +
                        "remapEnabled = @remapEnabled, " +
                        "remapFromMin = @remapFromMin, " +
                        "remapFromMax = @remapFromMax, " +
                        "remapToMin = @remapToMin, " +
                        "remapToMax = @remapToMax, " +
                        "Node_db_Id = @Node_db_Id " +
                        "WHERE nodeId = @nodeId AND sensorId = @sensorId";
                    db.Execute(sqlQuery, new
                    {
                        nodeId = sensor.nodeId,
                        sensorId = sensor.sensorId,
                        type = sensor.type,
                        dataType = sensor.dataType,
                        state = sensor.state,
                        description = sensor.description,
                        storeHistoryEnabled = sensor.storeHistoryEnabled,
                        storeHistoryWithInterval = sensor.storeHistoryWithInterval,
                        storeHistoryEveryChange = sensor.storeHistoryEveryChange,
                        invertData = sensor.invertData,
                        remapEnabled = sensor.remapEnabled,
                        remapFromMin = sensor.remapFromMin,
                        remapFromMax = sensor.remapFromMax,
                        remapToMin = sensor.remapToMin,
                        remapToMax = sensor.remapToMax,
                        Node_db_Id = node_db_id
                    });
                }
            }
        }
 private static void OnSensorUpdatedEvent(Sensor sensor)
 {
     hub.Clients.All.OnSensorUpdatedEvent(sensor);
 }
 private static void OnNewSensorEvent(Sensor sensor)
 {
     hub.Clients.All.OnNewSensorEvent(sensor);
 }
Example #8
0
 private void SendSensorState(Sensor sensor)
 {
     Message message = new Message();
     message.ack = false;
     message.messageType = MessageType.C_SET;
     message.nodeId = sensor.nodeId;
     message.payload = sensor.state;
     message.sensorId = sensor.sensorId;
     message.subType = (int)sensor.dataType;
     SendMessage(message);
 }
Example #9
0
        private bool CheckLinkPossible(Sensor fromSensor, Sensor toSensor)
        {
            //if it's the same sensor
            if (fromSensor.nodeId == toSensor.nodeId
                && fromSensor.sensorId == toSensor.sensorId)
                return false;

            List<SensorLink> links = linksDb.GetAllLinks();
            foreach (var link in links)
            {
                //prevent infinite loop
                if (link.fromNodeId == toSensor.nodeId
                    && link.fromSensorId == toSensor.sensorId
                    && link.toNodeId == fromSensor.nodeId
                    && link.toSensorId == fromSensor.sensorId)
                    return false;

                //prevend duplicates
                if (link.fromNodeId == fromSensor.nodeId
                    && link.fromSensorId == fromSensor.sensorId
                    && link.toNodeId == toSensor.nodeId
                    && link.toSensorId == toSensor.sensorId)
                    return false;
            }

            return true;
        }
        private void WriteSensorDataToHistory(Sensor sensor)
        {
            using (var db = new SqlConnection(connectionString))
            {
                db.Open();

                CreateTableForSensor(sensor);

                if (sensor.state == null)
                    return;

                var sqlQuery = $"INSERT INTO SensorHistory{sensor.db_Id} (dataType, state, dateTime) "
                    + "VALUES(@dataType,@state, @dateTime); "
                    + "SELECT CAST(SCOPE_IDENTITY() as int)";
                db.Execute(sqlQuery,new
                {
                    dataType= sensor.dataType,
                    state = sensor.state,
                    dateTime =DateTime.Now
                });

            }
        }
 private void OnSensorUpdatedEvent(Sensor sensor)
 {
     if (sensor.storeHistoryEnabled && sensor.storeHistoryEveryChange)
         WriteSensorDataToHistory(sensor);
 }
        private void CreateTableForSensor(Sensor sensor)
        {
            using (var db = new SqlConnection(connectionString))
            {
                db.Open();

                try
                {
                    string req = $@"CREATE TABLE [dbo].[SensorHistory{sensor.db_Id}](
                [db_Id] [int] IDENTITY(1,1) NOT NULL,
                [dataType] [int] NULL,
                [state] [nvarchar](max) NULL,
                [dateTime] [datetime] NOT NULL ) ON [PRIMARY] ";

                    db.Query(req);
                }
                catch
                {
                }
            }
        }
Example #13
0
        public Sensor AddSensor(int sensorId)
        {
            Sensor sensor = GetSensor(sensorId);
            if (sensor != null) return sensor;

            sensor = new Sensor(sensorId, this);
            sensors.Add(sensor);
            return sensor;
        }