// ************************************************************************** //
//                                                                            //
//    eses                   eses                                             //
//   eses                     eses                                            //
//  eses    eseses  esesese    eses   Embedded Systems Group                  //
//  ese    ese  ese ese         ese                                           //
//  ese    eseseses eseseses    ese   Department of Computer Science          //
//  eses   eses          ese   eses                                           //
//   eses   eseses  eseseses  eses    University of Kaiserslautern            //
//    eses                   eses                                             //
//                                                                            //
// ************************************************************************** //
//  Service LightControl determines "LightControlCfg"                         //
// ************************************************************************** //

package LightControl;

macro LAMPBRIGHTNESS_dimmable    = 2;
macro LAMPBRIGHTNESS_switchable  = 1;
macro LAMPBRIGHTNESS_unavailable = 0;

macro OCCUPANCY_LogicalDetection     = 4;
macro OCCUPANCY_TransponderDetection = 3;
macro OCCUPANCY_MotionDetection      = 2;
macro OCCUPANCY_EntryExitDetection   = 1;
macro OCCUPANCY_Off                  = 0;

macro LIGHTCONTROL_DimmerControl            = 3;
macro LIGHTCONTROL_IntelligentSwitchControl = 2;
macro LIGHTCONTROL_SwitchControl            = 1;
macro LIGHTCONTROL_Off                      = 0;

module LightControl(
   bool ?hasDesiredIlluminance,
   bool ?hasCurrentIlluminance,
   nat{5} ?OccupancyMode,
   nat{3} ?lampBrightness,
   event nat{4} !LightControlCfg) {
   LightControlCfg = LIGHTCONTROL_Off;
   loop {
      pause;
      case
         // -----------------------------------------------------------------------
         // switch to mode LIGHTCONTROL_DimmerControl
         // -----------------------------------------------------------------------
         (hasDesiredIlluminance & hasCurrentIlluminance &
          ((OccupancyMode==OCCUPANCY_LogicalDetection) |
           (OccupancyMode==OCCUPANCY_TransponderDetection) |
           (OccupancyMode==OCCUPANCY_MotionDetection) |
           (OccupancyMode==OCCUPANCY_EntryExitDetection)) &
          (lampBrightness==LAMPBRIGHTNESS_dimmable))
         do
            LightControlCfg = LIGHTCONTROL_DimmerControl;
         // -----------------------------------------------------------------------
         // switch to mode LIGHTCONTROL_IntelligentSwitchControl
         // -----------------------------------------------------------------------
         (hasCurrentIlluminance &
          (lampBrightness==LAMPBRIGHTNESS_switchable) &
          ((OccupancyMode==OCCUPANCY_LogicalDetection) |
           (OccupancyMode==OCCUPANCY_MotionDetection) |
           (OccupancyMode==OCCUPANCY_TransponderDetection) |
           (OccupancyMode==OCCUPANCY_EntryExitDetection)))
         do
            LightControlCfg = LIGHTCONTROL_IntelligentSwitchControl;
         // -----------------------------------------------------------------------
         // switch to mode LIGHTCONTROL_SwitchControl
         // -----------------------------------------------------------------------
         ((lampBrightness==LAMPBRIGHTNESS_switchable) &
          ((OccupancyMode==OCCUPANCY_LogicalDetection) |
           (OccupancyMode==OCCUPANCY_MotionDetection) |
           (OccupancyMode==OCCUPANCY_TransponderDetection) |
           (OccupancyMode==OCCUPANCY_EntryExitDetection)))
         do
            LightControlCfg = LIGHTCONTROL_SwitchControl;
         // -----------------------------------------------------------------------
         // switch to mode LIGHTCONTROL_Off
         // -----------------------------------------------------------------------
         default
            LightControlCfg = LIGHTCONTROL_Off;
    }
}