home *** CD-ROM | disk | FTP | other *** search
/ business-86-101-185-173.business.broadband.hu / business-86-101-185-173.business.broadband.hu.zip / business-86-101-185-173.business.broadband.hu / SensorProject.ZIP / SensorDataProcessing.sql < prev    next >
Text File  |  2010-10-27  |  3KB  |  86 lines

  1. SET ANSI_NULLS ON
  2. GO
  3.  
  4. SET QUOTED_IDENTIFIER ON
  5. GO
  6.  
  7. CREATE PROCEDURE [dbo].[SensorDataProcessing] 
  8. AS
  9. BEGIN
  10.     SET NOCOUNT ON;
  11.     declare @value decimal(5,2)
  12.     declare @id int
  13.     declare @cnt int
  14.     declare @date datetime
  15.     declare @alertdate datetime
  16.     declare @message nvarchar(160)
  17.     declare @alertsent bit
  18.     declare @roundedvalue int
  19.     declare @reccount int
  20.     declare @min int
  21.     declare @minalertsent bit
  22.     declare @tsg varchar(30)
  23.     set @tsg = 'TSG'
  24.     
  25.     -- clear alerts
  26.     UPDATE dbo.SensorConfig SET alertsent = 1
  27.     
  28.     -- process collected temperature data
  29.     SET @cnt = 0
  30.     SET @roundedvalue = 0
  31.     SET @reccount = 0
  32.     DECLARE curData CURSOR FOR
  33.     SELECT [id],[date],[value] FROM dbo.SensorData WHERE [processed] = 0 ORDER BY [date] ASC
  34.     OPEN curData
  35.     FETCH NEXT FROM curData
  36.     INTO @id, @date, @value
  37.     WHILE @@FETCH_STATUS = 0 BEGIN
  38.         SET @reccount = @reccount + 1
  39.         IF @roundedvalue <> FLOOR(@value) BEGIN
  40.             SET @cnt = 0
  41.             SET @roundedvalue = FLOOR(@value)
  42.         END
  43.         SET @cnt = @cnt + 1
  44.         IF @cnt > 1 BEGIN
  45.             -- the last two record must equal for updating the table to avoid false jumping
  46.             UPDATE dbo.SensorConfig SET [alertsent] = 0, [alertdate] = @date, [actualvalue] = @value WHERE @value between [value1] and [value2] and [alertsent] = 1
  47.         END
  48.         -- clear processed flag
  49.         UPDATE dbo.SensorData SET [processed] = 1 WHERE [id] = @id
  50.         FETCH NEXT FROM curData INTO @id, @date, @value
  51.     END
  52.     CLOSE curData
  53.     DEALLOCATE curData
  54.     -- the last record remains unprocessed
  55.     UPDATE dbo.SensorData SET [processed] = 0 WHERE [id] = @id
  56.     PRINT CAST(@reccount AS VARCHAR(5)) + ' record(s) processed.'
  57.     
  58.     -- process alerts
  59.     SELECT TOP 1 @id = [id], @message = [message], @alertdate = [alertdate], @value = [actualvalue] FROM dbo.SensorConfig WHERE [alertsent] = 0 ORDER BY [alertdate] DESC
  60.     IF @@ROWCOUNT>0 BEGIN
  61.         PRINT 'Sending alert for ' + CAST(@alertdate AS VARCHAR(20)) + ' ' + @message
  62.         PRINT CAST(@value AS VARCHAR(5)) + ' Celsius degree'
  63.         SELECT @roundedvalue = ISNULL(value,0), @alertsent = ISNULL(alertsent,0), @min = minvalue, @minalertsent = minalertsent FROM SensorAlert WHERE id = 1
  64.         PRINT 'Last SMS sent for ' + CAST(@roundedvalue AS VARCHAR(5)) + ' Celsius degree'
  65.         IF @roundedvalue <> FLOOR(@value) BEGIN
  66.             IF FLOOR(@value)<=@min BEGIN
  67.                 IF @minalertsent = 1 BEGIN
  68.                     PRINT 'No SMS alert needed, it was sent already.'
  69.                 END ELSE BEGIN
  70.                     PRINT 'Sending SMS...'
  71.                     UPDATE dbo.SensorAlert SET value = FLOOR(@value), alertsent = 1, alertdate = @alertdate, minalertsent = 1 WHERE id = 1
  72.                     EXEC dbo.SendSMS @tsg, @message, 'EUR6MTO'
  73.                 END
  74.             END
  75.             ELSE BEGIN
  76.                 PRINT 'Sending SMS...'
  77.                 UPDATE dbo.SensorAlert SET value = FLOOR(@value), alertsent = 1, alertdate = @alertdate, minalertsent = 0 WHERE id = 1
  78.                 EXEC dbo.SendSMS @tsg, @message, 'EUR6MTO'
  79.             END
  80.         END
  81.         UPDATE dbo.SensorConfig SET [alertsent] = 1 WHERE [alertsent] = 0
  82.     END
  83. END
  84. GO
  85.  
  86.