示例#1
0
void AlarmDetection(Sensor* sn)
{		
	if(!sn)return;

	// Integer Sensor
	if(sn->type == integersensor)
	{
		iSensor* isn = (iSensor*) sn;
		
		if(isn->value > isn->ubound)
		{
			sendAlarm(sn, getCounterActionNr(unitbystring(sn->name), true));
			Sensor_enqueue(sn);
			Log(LOGT_SERVER, LOGL_ALARM, "%s: %s (%d > %d)", sn->name ,isn->ualarm, isn->value, isn->ubound);
		} 
		else if (isn->value < isn->lbound)
		{
			sendAlarm(sn, getCounterActionNr(unitbystring(sn->name), false));
			Sensor_enqueue(sn);
			Log(LOGT_SERVER, LOGL_ALARM, "%s: %s (%d < %d)", sn->name ,isn->lalarm, isn->value, isn->lbound);
		}
	} 
	
	// Binary Sensor 
	else if(sn->type == binarysensor)
	{
		bSensor* bsn = (bSensor*) sn;
		 
		if(bsn->value)
		{
			sendAlarm(sn, getCounterActionNr(unitbystring(sn->name), true));
			Sensor_enqueue(sn);
			Log(LOGT_SERVER, LOGL_ALARM, "%s: %s", sn->name ,bsn->alarm);
		}
	}
}
void main()
{
	// Placa Generica - Implementacion del protocolo
	init();

	// Init del protocol
	initProtocol();

	// FOREVER
	while(true)
	{

		// Ejecucion de la maquina de estados
		switch (state)
		{
			case STATE_FREE:
				/*
				* Analiza los paquetes y retransmite o lanza el pedido de lectura
				*/
				
#if TRIGGER_TYPE == SWITCH_SENSOR
				// Enviar alarma de trigger
				if (triggerAlarm == 1)
					sendAlarm();
#endif

				// Protocolo
				runProtocol(&command);

				if (readSensor != 0x00)
				{
					// Almacena el pedido sobre los sensores
					actualReadSensor = readSensor;
					readSensor = 0x00;
					actalTO = requestFrom;
					actalCmd = requestCmd;
					// Cambio de estado
					state = STATE_START_READING;
				}	
				break;
			case STATE_START_READING:
				/*
				* Genera un pedido de lectura a los sensores que lo necesiten
				* y setea los TIMERs para contabilizar los tiempos
				*/
				
				// Limpio la variable
				bufferedReadSensor = 0x00;

				// Inhabilita los sensores enmascarados
				actualReadSensor &= sensorMask;
				
				// Pedido de -START-
				startReading(actualReadSensor);
				
				// Flag de interrupcion
				intTMR = 0;
				
				// Seteo de TIMERs
				set_timer1(SENSORS_WAITING_TIME);
				enable_interrupts(INT_TIMER1);
				
				// Cambio de estado
				state = STATE_WAIT_TO_READ;
				
				break;
			case STATE_WAIT_TO_READ:
				/*
				* Espera el tiempo necesario para tomar las muestras en los sensores
				* Recibe nuevos pedidos y los agraga para una proxima lectura
				*/

#if TRIGGER_TYPE == SWITCH_SENSOR
				// Enviar alarma de trigger
				if (triggerAlarm == 1)
					sendAlarm();
#endif

				// Protocolo
				runProtocol(&command);
				
				// Almacena el pedido sobre los sensores si no hay otro ya
				if ((readSensor != 0x00) && (bufferedReadSensor == 0x00))
				{
					bufferedReadSensor = readSensor;
					bufferedFrom = requestFrom;
					bufferedCmd = requestCmd;
					readSensor = 0x00;
				}
				
				// Cambio de estado?
				if (intTMR == 1)
				{
					// El TIMER1 hizo timeout -> leer sensores
					state = STATE_READ_VALUES;
				}	
				
				break;
			case STATE_READ_VALUES:
				/*
				* Toma las muestras en los sensores y envia el paquete de respuesta
				*/
				
				// Toma las muestras de los sensores
				readSensors(actualReadSensor);
				
				// Mandar paquete de respuesta
				sendValues(actalTO, actalCMD, values, actualReadSensor);
				
				// Flag de interrupcion
				intTMR = 0;
				
				// Setea el tiempo de espera y pasa al estado de espera
				set_timer1(WAITING_TIME);
				enable_interrupts(INT_TIMER1);
				
				state = STATE_WAITING;
				
				break;
			case STATE_WAITING:
				/*
				* Espera a que pase el tiempo de espera entre lecturas para evitar rebotes
				* Recibe nuevos pedidos y los agraga para una proxima lectura
				*/
				
#if TRIGGER_TYPE == SWITCH_SENSOR
				// Enviar alarma de trigger
				if (triggerAlarm == 1)
					sendAlarm();
#endif

				// Protocolo
				runProtocol(&command);
				
				// Almacena el pedido sobre los sensores si no hay otro ya
				if ((readSensor != 0x00) && (bufferedReadSensor == 0x00))
				{
					bufferedReadSensor = readSensor;
					bufferedFrom = requestFrom;
					bufferedCmd = requestCmd;
					readSensor = 0x00;
				}
				
				// Cambio de estado?
				if (intTMR == 1)
				{
					// El TIMER1 hizo timeout
					if (bufferedReadSensor != 0x00)
					{
						// Hay un pedido pendiente y lo carga
						actualReadSensor = bufferedReadSensor;
						bufferedReadSensor = 0x00;
						actalTO = bufferedFrom;
						actalCmd = bufferedCmd;
						// Cambio de estado 
						state = STATE_START_READING;
					} else {
						state = STATE_FREE;
					}	
				}

				break;
			default:
				init();
				break;
		}
	}

	return;
}
示例#3
0
int main(void) {
    WDTCTL = WDTPW | WDTHOLD;	// Stop watchdog timer

    // Setup and pin configurations
    clkSetup();
    directionSetup();
    timerA0Setup();
    boardSetup();
    rtcSetup();
	initUART();
	pinGSM();
//	clkDebug();
	readDip();
	// test
	V4Stop();
	V5Stop();

	// Sensor variables
	int sensorValue = 0;
	int sensorData[LengthOfSensordata] = {0};
	char dataEnable = 0; 				// != 0 if the vector is filled ones
	int dataPosition = 0;
	int overflowCount = 0;
	char alarm = '0';

	// Parameters (From and to the flash)
	int lowerThresholds = readFlashLowTolerance();
	int upperThresholds = readFlashHighTolerance();
	int normalLvl = readFlashSensorOffset();			// Default higth over the water lvl

	// GSM decision variable
	char execution = '0';
	char disableAlarmFlag = '0';	// Disable = 1

	// RTC variable time offset1 = 1 min
	unsigned int rtcOffsetH = 0xFC6C;
	unsigned int rtcOffsetL = 0x78FF;

	__enable_interrupt();

	while(1)
	{
		V5Start();

		_no_operation();
		char c = '0';
		if(loop2Mode == '1' || startMode == '1')
		{	// Power on the GSM regulator
			V4Start(); 	// Enable power to GSMJa
			if(P8IN &= BIT4)
			{
				c = checkAT();
				if(c =='0') pwrOnOff();
			}
			else
			{
				pwrOnOff();
				Delay();
				c = '0';
			}
		}
		sensorValue = mainFunctionSensor(sensorData, LengthOfSensordata, &dataPosition, &dataEnable, &overflowCount);

		if (loop2Mode == '1' || startMode == '1')
		{	// wait for connection and check if SMS
			unsigned int count = 0;

			while(!(P8IN &= BIT4) || count < 40000)
			{
				count++;
				__delay_cycles(100);
			}
			if(c == '0') c = checkAT();

			if(c == '0')
			{
				V4Stop();
				Delay();
				Delay();
				V5Start();
				V4Start();
				if(P8IN &= BIT4)
				{
					c = checkAT();
					if(c =='0') pwrOnOff();
				}
				else
				{
					pwrOnOff();
					Delay();
				}
			}
			initGSM();
			execution = readSMS();

			if (execution == '0')
			{	// Nothing
				_no_operation();// test
			}
			else if (execution == 'S')
			{	// Status report
				responseStatus("STATUS i ", (normalLvl-sensorValue));
				deleteSMS();
			}
			else if (execution == 'N')
			{	// Confirm Nr change
				responseNrChange("Nummerlista uppdaterad i ");
				deleteSMS();
			}
			else if (execution == 'L')
			{	// Confirm changed normal level
				normalLvl = readFlashSensorOffset();
				responseLvlChange("Offset i ", normalLvl);
				deleteSMS();
			}
			else if (execution == 'T')
			{	// Confirm changed thresholds
				lowerThresholds = readFlashLowTolerance();
				upperThresholds = readFlashHighTolerance();
				responseThChange("Toleranser i ", lowerThresholds, upperThresholds);
				deleteSMS();
			}
			else if (execution == 'E')
			{	// Enable SMS
				sendSMS("Modulen har blivit aktiverad i ");
				disableAlarmFlag = '0';
				deleteSMS();
			}
			else if (execution == 'D')
			{	// Disable SMS
				sendSMS("Modulen har blivit inaktiverad i");
				disableAlarmFlag = '1';
				deleteSMS();
			}
			else if (execution == 'A')
			{	// Disable SMS with when alarm
				sendSMS("Larmet stoppat");
				deleteSMS();
				disableAlarmFlag = '1';		// Reseted when the lvl goes back to normal.
			}
			else
			{	/* Nothing */  }
		}

		// if the GSM mode disable turn off the power
		if (loop2Mode != '1' && startMode != '1')
		{
			V5Stop();
			V4Stop();
		}

		if (dataEnable != 0 && overflowCount == 0)
		{	// Process the sensor value
			alarm = evaluateData(sensorValue, normalLvl, upperThresholds, lowerThresholds, &rtcOffsetH, &rtcOffsetL, &timerAlarmFlag);
		}
		else if (overflowCount > 10)
		{	// Alarm overflow (Problem om man minskar RTC och något ligger ivägen!!!!)
			alarm = 'O';
		}
		else
		{}
		if (alarm != '0')
		{
			if (loop2Mode != '1' && startMode != '1' && disableAlarmFlag != '1' && timerAlarmFlag == '1')
				startGSMmodule();
			if(loop2Mode == '1' && startMode == '1' && disableAlarmFlag != '1' && timerAlarmFlag == '1')
			{
				c = checkAT();
				if(c == '0')
					startGSMmodule();
			}
			if (alarm == '+')
			{	// Alarm for high water lvl
				if (disableAlarmFlag != '1' && timerAlarmFlag == '1')
				{
					sendAlarm("Hog vattenniva i ", (normalLvl-sensorValue));
					timerAlarmFlag = '0';
				}
			}
			else if (alarm == '-')
			{	// Alarm for low water lvl
				if (disableAlarmFlag != '1' && timerAlarmFlag == '1')
				{
					sendAlarm("Lag vattenniva i ", (normalLvl-sensorValue));
					timerAlarmFlag = '0';
				}
			}
			else if (alarm == 'O' && timerAlarmFlag == '1')
			{	// Alarm for overflow
				sendSMS("Sensor kan vara ur funktion i ");
				timerAlarmFlag = '0';
			}
			else {}

			if (loop2Mode != '1' && startMode != '1' && timerAlarmFlag == '1')
			{
				V5Stop();
				V4Stop();
			}
		}
		else if (alarm == '0' && timerAlarmFlag == '1')
		{	// return to normal mode
			disableAlarmFlag = '0';
			// RTC for Repeat alarm
			// Send sms
		}
		rtcStart(rtcOffsetH, rtcOffsetL);
	}
}