Flatten the structure of event while-loop.
[smartthings-infrastructure.git] / Extractor / ExtractorScript.py
index 3567349..eb7fa99 100644 (file)
@@ -11,6 +11,66 @@ app2Capabilities = []
 app1Subscribe = False
 app2Subscribe = False
 
+# Mapping for event type and number of events
+# e.g., switch has 2 events: on and off
+eventTypeCounterMap =  {'lock': 2,
+                                               'unlock' : 1,
+                                               'contact.open' : 1,
+                                               'contact.closed' : 1,
+                                               'tamper.tampered' : 1,
+                                               'nfcTouch' : 1,
+                                               'app' : 1,
+                                               'button' : 2,
+                                               'water' : 2,
+                                               'water.dry' : 1,
+                                               'water.wet' : 1,
+                                               'presence' : 2,
+                                               'presence.present' : 1,
+                                               'doorState' : 2,
+                                               'motion' : 2,
+                                               'motion.active' : 1,
+                                               'motion.inactive' : 1,
+                                               'smoke' : 3,
+                                               'smoke.clear' : 1,
+                                               'smoke.detected' : 1,
+                                               'smoke.tested' : 1,
+                                               'carbonMonoxide' : 3,
+                                               'carbonMonoxide.clear' : 1,
+                                               'carbonMonoxide.detected' : 1,
+                                               'carbonMonoxide.tested' : 1,
+                                               'battery' : 1,
+                                               'thermostatMode' : 5,
+                                               'thermostatFanMode' : 5,
+                                               'thermostatOperatingState' : 5,
+                                               'switch' : 2,
+                                               'location' : 3,
+                                               'mode' : 3,
+                                               'acceleration' : 2,
+                                               'acceleration.active' : 1,
+                                               'acceleration.inactive' : 1,
+                                               'sleeping' : 2,
+                                               'goal' : 1,
+                                               'steps' : 1,
+                                               'color' : 1,
+                                               'colorTemperature' : 1,
+                                               'hue' : 1,
+                                               'saturation' : 1,
+                                               'energy' : 1,
+                                               'power' : 1,
+                                               'illuminance' : 1,
+                                               'humidity' : 1,
+                                               'alarm' : 4,
+                                               'contact' : 2,
+                                               'status' : 3,
+                                               'level' : 1,
+                                               'trackDescription' : 1,
+                                               'trackData' : 1,
+                                               'mute' : 2,
+                                               'temperature' : 1,
+                                               'heatingSetpoint' : 1,
+                                               'coolingSetpoint' : 1,
+                                               'thermostatSetpoint' : 1}
+
 def GetToken(f):
        global readyToReturn
        global ToReturn
@@ -166,30 +226,39 @@ def AnalyzePhysicalInteraction(app1Capab, app2Capab):
                        "capability.alarm" in app2Capab or "capability.speechSynthesis" in app2Capab):
                print ("\nWARNING: Potential PHYSICAL CONFLICT (sound) detected between App1 and App2!\n")
 
+def CountEvents():
+       global eventList
+       numOfActualEvents = 0
+       for event in eventList:
+               numOfActualEvents = numOfActualEvents + eventTypeCounterMap[event]
+       return numOfActualEvents
+
 def ExtractEvents(extractedEvents):
        global eventList
        global eventVarMap
        global capabilityMap
        global eventVarCounterMap
+       # Count the number of events
+       numOfActualEvents = CountEvents()
        extractedEvents.write("while(true) {\n")
-       extractedEvents.write("\tdef eventNumber = Verify.getInt(0,%d)\n" % (len(eventList) - 1))
+       extractedEvents.write("\tdef eventNumber = Verify.getInt(0,%d)\n" % (numOfActualEvents - 1))
        extractedEvents.write("\tswitch(eventNumber) {\n")
+       counter = 0
        for i in range(len(eventList)):
-               extractedEvents.write("\t\tcase %d:\n" % i)
+               extractedEvents.write("\t\tcase %d:\n" % counter)
                if eventList[i] == "lock":
                        #Write two events subsequently
-                       extractedEvents.write("\t\t\tdef event = Verify.getInt(0,1)\n")
-                       extractedEvents.write("\t\t\tif (event == 0) {\n")
                        event = open("eventSimulator/lockLockedEvent.groovy", "r")                      
                        for line in event:
-                               extractedEvents.write("\t\t" + line)
+                               extractedEvents.write(line)
                        event.close()
-                       extractedEvents.write("\t\t\t} else {\n")
+                       extractedEvents.write("\n\t\t\tbreak\n")
+                       counter = counter + 1
+                       extractedEvents.write("\t\tcase %d:\n" % counter)
                        event = open("eventSimulator/lockUnlockedEvent.groovy", "r")
                        for line in event:
-                               extractedEvents.write("\t\t" + line)
+                               extractedEvents.write(line)
                        event.close()
-                       extractedEvents.write("\t\t\t}\n")
                elif eventList[i] == "unlock":
                        event = open("eventSimulator/unlockEvent.groovy", "r")
                        for line in event:
@@ -207,18 +276,17 @@ def ExtractEvents(extractedEvents):
                        event.close()
                elif eventList[i] == "tamper.tampered":
                        #Write two events subsequently
-                       extractedEvents.write("\t\t\tdef event = Verify.getInt(0,1)\n")
-                       extractedEvents.write("\t\t\tif (event == 0) {\n")
                        event = open("eventSimulator/tamperTamperedOpenEvent.groovy", "r")                      
                        for line in event:
-                               extractedEvents.write("\t\t" + line)
+                               extractedEvents.write(line)
                        event.close()
-                       extractedEvents.write("\t\t\t} else {\n")
+                       extractedEvents.write("\n\t\t\tbreak\n")
+                       counter = counter + 1
+                       extractedEvents.write("\t\tcase %d:\n" % counter)
                        event = open("eventSimulator/tamperTamperedClosedEvent.groovy", "r")
                        for line in event:
-                               extractedEvents.write("\t\t" + line)
+                               extractedEvents.write(line)
                        event.close()
-                       extractedEvents.write("\t\t\t}\n")
                elif eventList[i] == "nfcTouch":
                        event = open("eventSimulator/nfcTouchEvent.groovy", "r")
                        for line in event:
@@ -231,32 +299,30 @@ def ExtractEvents(extractedEvents):
                        event.close()
                elif eventList[i] == "button":
                        #Write two events subsequently
-                       extractedEvents.write("\t\t\tdef event = Verify.getInt(0,1)\n")
-                       extractedEvents.write("\t\t\tif (event == 0) {\n")
                        event = open("eventSimulator/buttonPushedEvent.groovy", "r")                    
                        for line in event:
-                               extractedEvents.write("\t\t" + line)
+                               extractedEvents.write(line)
                        event.close()
-                       extractedEvents.write("\t\t\t} else {\n")
+                       extractedEvents.write("\n\t\t\tbreak\n")
+                       counter = counter + 1
+                       extractedEvents.write("\t\tcase %d:\n" % counter)
                        event = open("eventSimulator/buttonHeldEvent.groovy", "r")
                        for line in event:
-                               extractedEvents.write("\t\t" + line)
+                               extractedEvents.write(line)
                        event.close()
-                       extractedEvents.write("\t\t\t}\n")
                elif eventList[i] == "water":
                        #Write two events subsequently
-                       extractedEvents.write("\t\t\tdef event = Verify.getInt(0,1)\n")
-                       extractedEvents.write("\t\t\tif (event == 0) {\n")
                        event = open("eventSimulator/waterDryEvent.groovy", "r")                        
                        for line in event:
-                               extractedEvents.write("\t\t" + line)
+                               extractedEvents.write(line)
                        event.close()
-                       extractedEvents.write("\t\t\t} else {\n")
+                       extractedEvents.write("\n\t\t\tbreak\n")
+                       counter = counter + 1
+                       extractedEvents.write("\t\tcase %d:\n" % counter)
                        event = open("eventSimulator/waterWetEvent.groovy", "r")
                        for line in event:
-                               extractedEvents.write("\t\t" + line)
+                               extractedEvents.write(line)
                        event.close()
-                       extractedEvents.write("\t\t\t}\n")
                elif eventList[i] == "water.dry":
                        event = open("eventSimulator/waterDefaultDryEvent.groovy", "r")
                        for line in event:
@@ -277,32 +343,28 @@ def ExtractEvents(extractedEvents):
                                eventVarCount = eventVarCounterMap[eventList[i]]
                                eventVarCounterMap[eventList[i]] = eventVarCount + 1
                        capability = capabilityMap[variable[eventVarCount]]
-                       #Write three events subsequently
-                       if capability == "capability.presenceSensor":
-                               extractedEvents.write("\t\t\tdef event = Verify.getInt(0,2)\n")
-                       elif capability == "capability.beacon":
-                               extractedEvents.write("\t\t\tdef event = Verify.getInt(0,1)\n")
-                       extractedEvents.write("\t\t\tif (event == 0) {\n")
+                       #Write two events subsequently
                        if capability == "capability.presenceSensor":
                                event = open("eventSimulator/presencePresentEvent.groovy", "r")
                        elif capability == "capability.beacon":
                                event = open("eventSimulator/beaconPresentEvent.groovy", "r")
                        for line in event:
-                               extractedEvents.write("\t\t" + line)
+                               extractedEvents.write(line)
                        event.close()
-                       extractedEvents.write("\t\t\t} else if (event == 1) {\n")
+                       extractedEvents.write("\n\t\t\tbreak\n")
+                       counter = counter + 1
+                       extractedEvents.write("\t\tcase %d:\n" % counter)
                        if capability == "capability.presenceSensor":
                                event = open("eventSimulator/presenceLeftEvent.groovy", "r")
                        elif capability == "capability.beacon":
                                event = open("eventSimulator/beaconLeftEvent.groovy", "r")
                        for line in event:
-                               extractedEvents.write("\t\t" + line)
+                               extractedEvents.write(line)
                        event.close()
-                       extractedEvents.write("\t\t\t}\n")
                elif eventList[i] == "presence.present": #Case for Touched event
                        event = open("eventSimulator/presencePresencePresentEvent.groovy", "r")
                        for line in event:
-                               extractedEvents.write("\t\t" + line)
+                               extractedEvents.write(line)
                        event.close()
                elif eventList[i] == "doorState":
                        #Write two events subsequently
@@ -310,71 +372,72 @@ def ExtractEvents(extractedEvents):
                        extractedEvents.write("\t\t\tif (event == 0) {\n")
                        event = open("eventSimulator/doorOpenEvent.groovy", "r")                        
                        for line in event:
-                               extractedEvents.write("\t\t" + line)
+                               extractedEvents.write(line)
                        event.close()
-                       extractedEvents.write("\t\t\t} else {\n")
+                       extractedEvents.write("\n\t\t\tbreak\n")
+                       counter = counter + 1
+                       extractedEvents.write("\t\tcase %d:\n" % counter)
                        event = open("eventSimulator/doorClosedEvent.groovy", "r")
                        for line in event:
-                               extractedEvents.write("\t\t" + line)
+                               extractedEvents.write(line)
                        event.close()
-                       extractedEvents.write("\t\t\t}\n")
                elif eventList[i] == "motion":
                        #Write two events subsequently
-                       extractedEvents.write("\t\t\tdef event = Verify.getInt(0,1)\n")
-                       extractedEvents.write("\t\t\tif (event == 0) {\n")
                        event = open("eventSimulator/motionActiveEvent.groovy", "r")                    
                        for line in event:
-                               extractedEvents.write("\t\t" + line)
+                               extractedEvents.write(line)
                        event.close()
-                       extractedEvents.write("\t\t\t} else {\n")
+                       extractedEvents.write("\n\t\t\tbreak\n")
+                       counter = counter + 1
+                       extractedEvents.write("\t\tcase %d:\n" % counter)
                        event = open("eventSimulator/motionInactiveEvent.groovy", "r")
                        for line in event:
-                               extractedEvents.write("\t\t" + line)
+                               extractedEvents.write(line)
                        event.close()
-                       extractedEvents.write("\t\t\t}\n")
                elif eventList[i] == "motion.active":
                        event = open("eventSimulator/motionDefaultActiveEvent.groovy", "r")
                        for line in event:
-                               extractedEvents.write("\t\t" + line)
+                               extractedEvents.write(line)
                        event.close()
                elif eventList[i] == "motion.inactive":
                        event = open("eventSimulator/motionDefaultInactiveEvent.groovy", "r")
                        for line in event:
-                               extractedEvents.write("\t\t" + line)
+                               extractedEvents.write(line)
                        event.close()
                elif eventList[i] == "smoke":
                        #Write three events subsequently
-                       extractedEvents.write("\t\t\tdef event = Verify.getInt(0,2)\n")
-                       extractedEvents.write("\t\t\tif (event == 0) {\n")
                        event = open("eventSimulator/smokeClearEvent.groovy", "r")                      
                        for line in event:
-                               extractedEvents.write("\t\t" + line)
+                               extractedEvents.write(line)
                        event.close()
-                       extractedEvents.write("\t\t\t} else if (event == 1) {\n")
+                       extractedEvents.write("\n\t\t\tbreak\n")
+                       counter = counter + 1
+                       extractedEvents.write("\t\tcase %d:\n" % counter)
                        event = open("eventSimulator/smokeDetectedEvent.groovy", "r")
                        for line in event:
-                               extractedEvents.write("\t\t" + line)
+                               extractedEvents.write(line)
                        event.close()
-                       extractedEvents.write("\t\t\t} else {\n")
+                       extractedEvents.write("\n\t\t\tbreak\n")
+                       counter = counter + 1
+                       extractedEvents.write("\t\tcase %d:\n" % counter)
                        event = open("eventSimulator/smokeTestedEvent.groovy", "r")
                        for line in event:
-                               extractedEvents.write("\t\t" + line)
+                               extractedEvents.write(line)
                        event.close()
-                       extractedEvents.write("\t\t\t}\n")
                elif eventList[i] == "smoke.clear":
                        event = open("eventSimulator/smokeDefaultClearEvent.groovy", "r")
                        for line in event:
-                               extractedEvents.write("\t\t" + line)
+                               extractedEvents.write(line)
                        event.close()
                elif eventList[i] == "smoke.detected":
                        event = open("eventSimulator/smokeDefaultDetectedEvent.groovy", "r")
                        for line in event:
-                               extractedEvents.write("\t\t" + line)
+                               extractedEvents.write(line)
                        event.close()
                elif eventList[i] == "smoke.tested":
                        event = open("eventSimulator/smokeDefaultTestedEvent.groovy", "r")
                        for line in event:
-                               extractedEvents.write("\t\t" + line)
+                               extractedEvents.write(line)
                        event.close()
                elif eventList[i] == "carbonMonoxide":
                        #Check which capability
@@ -386,33 +449,34 @@ def ExtractEvents(extractedEvents):
                                eventVarCount = eventVarCounterMap[eventList[i]]
                                eventVarCounterMap[eventList[i]] = eventVarCount + 1
                        capability = capabilityMap[variable[eventVarCount]]
-                       #Write three events subsequently
-                       extractedEvents.write("\t\t\tdef event = Verify.getInt(0,2)\n")
-                       extractedEvents.write("\t\t\tif (event == 0) {\n")                      
+                       #Write three events subsequently        
                        if capability == "capability.smokeDetector":
                                event = open("eventSimulator/smokeCarbonMonoxideClearEvent.groovy", "r")
                        elif capability == "capability.carbonMonoxideDetector":
                                event = open("eventSimulator/carbonMonoxideClearEvent.groovy", "r")
                        for line in event:
-                               extractedEvents.write("\t\t" + line)
+                               extractedEvents.write(line)
                        event.close()
-                       extractedEvents.write("\t\t\t} else if (event == 1) {\n")
+                       extractedEvents.write("\n\t\t\tbreak\n")
+                       counter = counter + 1
+                       extractedEvents.write("\t\tcase %d:\n" % counter)
                        if capability == "capability.smokeDetector":
                                event = open("eventSimulator/smokeCarbonMonoxideDetectedEvent.groovy", "r")
                        elif capability == "capability.carbonMonoxideDetector":
                                event = open("eventSimulator/carbonMonoxideDetectedEvent.groovy", "r")
                        for line in event:
-                               extractedEvents.write("\t\t" + line)
+                               extractedEvents.write(line)
                        event.close()
-                       extractedEvents.write("\t\t\t} else {\n")
+                       extractedEvents.write("\n\t\t\tbreak\n")
+                       counter = counter + 1
+                       extractedEvents.write("\t\tcase %d:\n" % counter)
                        if capability == "capability.smokeDetector":
                                event = open("eventSimulator/smokeCarbonMonoxideTestedEvent.groovy", "r")
                        elif capability == "capability.carbonMonoxideDetector":
                                event = open("eventSimulator/carbonMonoxideTestedEvent.groovy", "r")
                        for line in event:
-                               extractedEvents.write("\t\t" + line)
+                               extractedEvents.write(line)
                        event.close()
-                       extractedEvents.write("\t\t\t}\n")
                elif eventList[i] == "carbonMonoxide.clear":
                        #Check which capability
                        variable = eventVarMap[eventList[i]]
@@ -428,7 +492,7 @@ def ExtractEvents(extractedEvents):
                        elif capability == "capability.carbonMonoxideDetector":
                                event = open("eventSimulator/carbonMonoxideDefaultClearEvent.groovy", "r")
                        for line in event:
-                               extractedEvents.write("\t\t" + line)
+                               extractedEvents.write(line)
                        event.close()
                elif eventList[i] == "carbonMonoxide.detected":
                        #Check which capability
@@ -445,7 +509,7 @@ def ExtractEvents(extractedEvents):
                        elif capability == "capability.carbonMonoxideDetector":
                                event = open("eventSimulator/carbonMonoxideDefaultDetectedEvent.groovy", "r")
                        for line in event:
-                               extractedEvents.write("\t\t" + line)
+                               extractedEvents.write(line)
                        event.close()
                elif eventList[i] == "carbonMonoxide.tested":
                        #Check which capability
@@ -462,7 +526,7 @@ def ExtractEvents(extractedEvents):
                        elif capability == "capability.carbonMonoxideDetector":
                                event = open("eventSimulator/carbonMonoxideDefaultTestedEvent.groovy", "r")
                        for line in event:
-                               extractedEvents.write("\t\t" + line)
+                               extractedEvents.write(line)
                        event.close()
                elif eventList[i] == "battery":
                        #Check which capability
@@ -483,91 +547,106 @@ def ExtractEvents(extractedEvents):
                        event.close()
                elif eventList[i] == "thermostatMode":
                        #Write five events subsequently
-                       extractedEvents.write("\t\t\tdef event = Verify.getInt(0,4)\n")
-                       extractedEvents.write("\t\t\tif (event == 0) {\n")
                        event = open("eventSimulator/thermostatAutoModeEvent.groovy", "r")                      
                        for line in event:
-                               extractedEvents.write("\t\t" + line)
+                               extractedEvents.write(line)
                        event.close()
-                       extractedEvents.write("\t\t\t} else if (event == 1) {\n")
+                       extractedEvents.write("\n\t\t\tbreak\n")
+                       counter = counter + 1
+                       extractedEvents.write("\t\tcase %d:\n" % counter)
                        event = open("eventSimulator/thermostatCoolModeEvent.groovy", "r")
                        for line in event:
-                               extractedEvents.write("\t\t" + line)
+                               extractedEvents.write(line)
                        event.close()
-                       extractedEvents.write("\t\t\t} else if (event == 2) {\n")
+                       extractedEvents.write("\n\t\t\tbreak\n")
+                       counter = counter + 1
+                       extractedEvents.write("\t\tcase %d:\n" % counter)
                        event = open("eventSimulator/thermostatEmergencyHeatModeEvent.groovy", "r")
                        for line in event:
-                               extractedEvents.write("\t\t" + line)
+                               extractedEvents.write(line)
                        event.close()
-                       extractedEvents.write("\t\t\t} else if (event == 3) {\n")
+                       extractedEvents.write("\n\t\t\tbreak\n")
+                       counter = counter + 1
+                       extractedEvents.write("\t\tcase %d:\n" % counter)
                        event = open("eventSimulator/thermostatHeatModeEvent.groovy", "r")
                        for line in event:
-                               extractedEvents.write("\t\t" + line)
+                               extractedEvents.write(line)
                        event.close()
-                       extractedEvents.write("\t\t\t} else {\n")
+                       extractedEvents.write("\n\t\t\tbreak\n")
+                       counter = counter + 1
+                       extractedEvents.write("\t\tcase %d:\n" % counter)
                        event = open("eventSimulator/thermostatOffModeEvent.groovy", "r")
                        for line in event:
-                               extractedEvents.write("\t\t" + line)
+                               extractedEvents.write(line)
                        event.close()
-                       extractedEvents.write("\t\t\t}\n")
                elif eventList[i] == "thermostatFanMode":
                        #Write five events subsequently
-                       extractedEvents.write("\t\t\tdef event = Verify.getInt(0,4)\n")
-                       extractedEvents.write("\t\t\tif (event == 0) {\n")
                        event = open("eventSimulator/thermostatAutoFanModeEvent.groovy", "r")                   
                        for line in event:
-                               extractedEvents.write("\t\t" + line)
+                               extractedEvents.write(line)
                        event.close()
-                       extractedEvents.write("\t\t\t} else if (event == 1) {\n")
+                       extractedEvents.write("\n\t\t\tbreak\n")
+                       counter = counter + 1
+                       extractedEvents.write("\t\tcase %d:\n" % counter)
                        event = open("eventSimulator/thermostatFanCirculateFanModeEvent.groovy", "r")
                        for line in event:
-                               extractedEvents.write("\t\t" + line)
+                               extractedEvents.write(line)
                        event.close()
-                       extractedEvents.write("\t\t\t} else if (event == 2) {\n")
+                       extractedEvents.write("\n\t\t\tbreak\n")
+                       counter = counter + 1
+                       extractedEvents.write("\t\tcase %d:\n" % counter)
                        event = open("eventSimulator/thermostatCirculateFanModeEvent.groovy", "r")
                        for line in event:
-                               extractedEvents.write("\t\t" + line)
+                               extractedEvents.write(line)
                        event.close()
-                       extractedEvents.write("\t\t\t} else if (event == 3) {\n")
+                       extractedEvents.write("\n\t\t\tbreak\n")
+                       counter = counter + 1
+                       extractedEvents.write("\t\tcase %d:\n" % counter)
                        event = open("eventSimulator/thermostatFanOnFanModeEvent.groovy", "r")
                        for line in event:
-                               extractedEvents.write("\t\t" + line)
+                               extractedEvents.write(line)
                        event.close()
-                       extractedEvents.write("\t\t\t} else {\n")
+                       extractedEvents.write("\n\t\t\tbreak\n")
+                       counter = counter + 1
+                       extractedEvents.write("\t\tcase %d:\n" % counter)
                        event = open("eventSimulator/thermostatOnFanModeEvent.groovy", "r")
                        for line in event:
-                               extractedEvents.write("\t\t" + line)
+                               extractedEvents.write(line)
                        event.close()
-                       extractedEvents.write("\t\t\t}\n")
                elif eventList[i] == "thermostatOperatingState":
                        #Write five events subsequently
-                       extractedEvents.write("\t\t\tdef event = Verify.getInt(0,4)\n")
-                       extractedEvents.write("\t\t\tif (event == 0) {\n")
                        event = open("eventSimulator/thermostatOperatingStateAutoEvent.groovy", "r")                    
                        for line in event:
-                               extractedEvents.write("\t\t" + line)
+                               extractedEvents.write(line)
                        event.close()
-                       extractedEvents.write("\t\t\t} else if (event == 1) {\n")
+                       extractedEvents.write("\n\t\t\tbreak\n")
+                       counter = counter + 1
+                       extractedEvents.write("\t\tcase %d:\n" % counter)
                        event = open("eventSimulator/thermostatOperatingStateCoolEvent.groovy", "r")
                        for line in event:
-                               extractedEvents.write("\t\t" + line)
+                               extractedEvents.write(line)
                        event.close()
-                       extractedEvents.write("\t\t\t} else if (event == 2) {\n")
+                       extractedEvents.write("\n\t\t\tbreak\n")
+                       counter = counter + 1
+                       extractedEvents.write("\t\tcase %d:\n" % counter)
                        event = open("eventSimulator/thermostatOperatingStateOffEvent.groovy", "r")
                        for line in event:
-                               extractedEvents.write("\t\t" + line)
+                               extractedEvents.write(line)
                        event.close()
-                       extractedEvents.write("\t\t\t} else if (event == 3) {\n")
+                       extractedEvents.write("\n\t\t\tbreak\n")
+                       counter = counter + 1
+                       extractedEvents.write("\t\tcase %d:\n" % counter)
                        event = open("eventSimulator/thermostatOperatingStateEmergencyHeatEvent.groovy", "r")
                        for line in event:
-                               extractedEvents.write("\t\t" + line)
+                               extractedEvents.write(line)
                        event.close()
-                       extractedEvents.write("\t\t\t} else {\n")
+                       extractedEvents.write("\n\t\t\tbreak\n")
+                       counter = counter + 1
+                       extractedEvents.write("\t\tcase %d:\n" % counter)
                        event = open("eventSimulator/thermostatOperatingStateHeatEvent.groovy", "r")
                        for line in event:
-                               extractedEvents.write("\t\t" + line)
+                               extractedEvents.write(line)
                        event.close()
-                       extractedEvents.write("\t\t\t}\n")
                elif eventList[i] == "switch":
                        #Check which capability
                        variable = eventVarMap[eventList[i]]
@@ -579,8 +658,6 @@ def ExtractEvents(extractedEvents):
                                eventVarCounterMap[eventList[i]] = eventVarCount + 1
                        capability = capabilityMap[variable[eventVarCount]]
                        #Write two events subsequently
-                       extractedEvents.write("\t\t\tdef event = Verify.getInt(0,1)\n")
-                       extractedEvents.write("\t\t\tif (event == 0) {\n")
                        if capability == "capability.switch":
                                event = open("eventSimulator/switchOnEvent.groovy", "r")
                        elif capability == "capability.switchLevel":
@@ -590,9 +667,11 @@ def ExtractEvents(extractedEvents):
                        elif capability == "capability.colorControl":
                                event = open("eventSimulator/colorControlSwitchOnEvent.groovy", "r")
                        for line in event:
-                               extractedEvents.write("\t\t" + line)
+                               extractedEvents.write(line)
                        event.close()
-                       extractedEvents.write("\t\t\t} else {\n")
+                       extractedEvents.write("\n\t\t\tbreak\n")
+                       counter = counter + 1
+                       extractedEvents.write("\t\tcase %d:\n" % counter)
                        if capability == "capability.switch":
                                event = open("eventSimulator/switchOffEvent.groovy", "r")
                        elif capability == "capability.switchLevel":
@@ -602,85 +681,84 @@ def ExtractEvents(extractedEvents):
                        elif capability == "capability.colorControl":
                                event = open("eventSimulator/colorControlSwitchOffEvent.groovy", "r")
                        for line in event:
-                               extractedEvents.write("\t\t" + line)
+                               extractedEvents.write(line)
                        event.close()
-                       extractedEvents.write("\t\t\t}\n")
                elif eventList[i] == "location": #Case for Location event
                        #Write three events subsequently
-                       extractedEvents.write("\t\t\tdef event = Verify.getInt(0,2)\n")
-                       extractedEvents.write("\t\t\tif (event == 0) {\n")
                        event = open("eventSimulator/locationHomeEvent.groovy", "r")                    
                        for line in event:
-                               extractedEvents.write("\t\t" + line)
+                               extractedEvents.write(line)
                        event.close()
-                       extractedEvents.write("\t\t\t} else if (event == 1) {\n")
+                       extractedEvents.write("\n\t\t\tbreak\n")
+                       counter = counter + 1
+                       extractedEvents.write("\t\tcase %d:\n" % counter)
                        event = open("eventSimulator/locationAwayEvent.groovy", "r")
                        for line in event:
-                               extractedEvents.write("\t\t" + line)
+                               extractedEvents.write(line)
                        event.close()
-                       extractedEvents.write("\t\t\t} else {\n")
+                       extractedEvents.write("\n\t\t\tbreak\n")
+                       counter = counter + 1
+                       extractedEvents.write("\t\tcase %d:\n" % counter)
                        event = open("eventSimulator/locationNightEvent.groovy", "r")
                        for line in event:
-                               extractedEvents.write("\t\t" + line)
+                               extractedEvents.write(line)
                        event.close()
-                       extractedEvents.write("\t\t\t}\n")
                elif eventList[i] == "mode":
                        #Write three events subsequently
-                       extractedEvents.write("\t\t\tdef event = Verify.getInt(0,2)\n")
-                       extractedEvents.write("\t\t\tif (event == 0) {\n")
                        event = open("eventSimulator/modeHomeEvent.groovy", "r")                        
                        for line in event:
-                               extractedEvents.write("\t\t" + line)
+                               extractedEvents.write(line)
                        event.close()
-                       extractedEvents.write("\t\t\t} else if (event == 1) {\n")
+                       extractedEvents.write("\n\t\t\tbreak\n")
+                       counter = counter + 1
+                       extractedEvents.write("\t\tcase %d:\n" % counter)
                        event = open("eventSimulator/modeAwayEvent.groovy", "r")
                        for line in event:
-                               extractedEvents.write("\t\t" + line)
+                               extractedEvents.write(line)
                        event.close()
-                       extractedEvents.write("\t\t\t} else {\n")
+                       extractedEvents.write("\n\t\t\tbreak\n")
+                       counter = counter + 1
+                       extractedEvents.write("\t\tcase %d:\n" % counter)
                        event = open("eventSimulator/modeNightEvent.groovy", "r")
                        for line in event:
-                               extractedEvents.write("\t\t" + line)
+                               extractedEvents.write(line)
                        event.close()
-                       extractedEvents.write("\t\t\t}\n")
                elif eventList[i] == "acceleration":
                        #Write two events subsequently
-                       extractedEvents.write("\t\t\tdef event = Verify.getInt(0,1)\n")
-                       extractedEvents.write("\t\t\tif (event == 0) {\n")
                        event = open("eventSimulator/accelerationActiveEvent.groovy", "r")                      
                        for line in event:
-                               extractedEvents.write("\t\t" + line)
+                               extractedEvents.write(line)
                        event.close()
-                       extractedEvents.write("\t\t\t} else {\n")
+                       extractedEvents.write("\n\t\t\tbreak\n")
+                       counter = counter + 1
+                       extractedEvents.write("\t\tcase %d:\n" % counter)
                        event = open("eventSimulator/accelerationInactiveEvent.groovy", "r")
                        for line in event:
-                               extractedEvents.write("\t\t" + line)
+                               extractedEvents.write(line)
                        event.close()
-                       extractedEvents.write("\t\t\t}\n")
                elif eventList[i] == "acceleration.active":
                        event = open("eventSimulator/accelerationDefaultActiveEvent.groovy", "r")
                        for line in event:
-                               extractedEvents.write("\t\t" + line)
+                               extractedEvents.write(line)
                        event.close()
                elif eventList[i] == "acceleration.inactive":
                        event = open("eventSimulator/accelerationDefaultInactiveEvent.groovy", "r")
                        for line in event:
-                               extractedEvents.write("\t\t" + line)
+                               extractedEvents.write(line)
                        event.close()
                elif eventList[i] == "sleeping":
                        #Write two events subsequently
-                       extractedEvents.write("\t\t\tdef event = Verify.getInt(0,1)\n")
-                       extractedEvents.write("\t\t\tif (event == 0) {\n")
                        event = open("eventSimulator/sleepSleepingEvent.groovy", "r")                   
                        for line in event:
-                               extractedEvents.write("\t\t" + line)
+                               extractedEvents.write(line)
                        event.close()
-                       extractedEvents.write("\t\t\t} else {\n")
+                       extractedEvents.write("\n\t\t\tbreak\n")
+                       counter = counter + 1
+                       extractedEvents.write("\t\tcase %d:\n" % counter)
                        event = open("eventSimulator/sleepNotSleepingEvent.groovy", "r")
                        for line in event:
-                               extractedEvents.write("\t\t" + line)
+                               extractedEvents.write(line)
                        event.close()
-                       extractedEvents.write("\t\t\t}\n")
                elif eventList[i] == "goal":
                        event = open("eventSimulator/stepGoalEvent.groovy", "r")
                        for line in event:
@@ -733,28 +811,31 @@ def ExtractEvents(extractedEvents):
                        event.close()
                elif eventList[i] == "alarm":
                        #Write four events subsequently
-                       extractedEvents.write("\t\t\tdef event = Verify.getInt(0,3)\n")
-                       extractedEvents.write("\t\t\tif (event == 0) {\n")
                        event = open("eventSimulator/alarmBothEvent.groovy", "r")                       
                        for line in event:
-                               extractedEvents.write("\t\t" + line)
+                               extractedEvents.write(line)
                        event.close()
-                       extractedEvents.write("\t\t\t} else if (event == 1) {\n")
+                       extractedEvents.write("\n\t\t\tbreak\n")
+                       counter = counter + 1
+                       extractedEvents.write("\t\tcase %d:\n" % counter)
                        event = open("eventSimulator/alarmSirenEvent.groovy", "r")
                        for line in event:
-                               extractedEvents.write("\t\t" + line)
+                               extractedEvents.write(line)
                        event.close()
-                       extractedEvents.write("\t\t\t} else if (event == 2) {\n")
+                       extractedEvents.write("\n\t\t\tbreak\n")
+                       counter = counter + 1
+                       extractedEvents.write("\t\tcase %d:\n" % counter)
                        event = open("eventSimulator/alarmStrobeEvent.groovy", "r")
                        for line in event:
-                               extractedEvents.write("\t\t" + line)
+                               extractedEvents.write(line)
                        event.close()
-                       extractedEvents.write("\t\t\t} else {\n")
+                       extractedEvents.write("\n\t\t\tbreak\n")
+                       counter = counter + 1
+                       extractedEvents.write("\t\tcase %d:\n" % counter)
                        event = open("eventSimulator/alarmOffEvent.groovy", "r")
                        for line in event:
-                               extractedEvents.write("\t\t" + line)
+                               extractedEvents.write(line)
                        event.close()
-                       extractedEvents.write("\t\t\t}\n")
                elif eventList[i] == "contact":
                        #Check which capability
                        variable = eventVarMap[eventList[i]]
@@ -766,43 +847,43 @@ def ExtractEvents(extractedEvents):
                                eventVarCounterMap[eventList[i]] = eventVarCount + 1
                        capability = capabilityMap[variable[eventVarCount]]
                        #Write two events subsequently
-                       extractedEvents.write("\t\t\tdef event = Verify.getInt(0,1)\n")
-                       extractedEvents.write("\t\t\tif (event == 0) {\n")
                        if capability == "capability.contactSensor":
                                event = open("eventSimulator/contactDefaultClosedEvent.groovy", "r")
                        elif capability == "capability.valve":
                                event = open("eventSimulator/valveClosedEvent.groovy", "r")
                        for line in event:
-                               extractedEvents.write("\t\t" + line)
+                               extractedEvents.write(line)
                        event.close()
-                       extractedEvents.write("\t\t\t} else {\n")
+                       extractedEvents.write("\n\t\t\tbreak\n")
+                       counter = counter + 1
+                       extractedEvents.write("\t\tcase %d:\n" % counter)
                        if capability == "capability.contactSensor":
                                event = open("eventSimulator/contactDefaultOpenEvent.groovy", "r")
                        elif capability == "capability.valve":
                                event = open("eventSimulator/valveOpenEvent.groovy", "r")
                        for line in event:
-                               extractedEvents.write("\t\t" + line)
+                               extractedEvents.write(line)
                        event.close()
-                       extractedEvents.write("\t\t\t}\n")
                elif eventList[i] == "status":
                        #Write three events subsequently
-                       extractedEvents.write("\t\t\tdef event = Verify.getInt(0,2)\n")
-                       extractedEvents.write("\t\t\tif (event == 0) {\n")
                        event = open("eventSimulator/musicPlayerPlayingEvent.groovy", "r")                      
                        for line in event:
-                               extractedEvents.write("\t\t" + line)
+                               extractedEvents.write(line)
                        event.close()
-                       extractedEvents.write("\t\t\t} else if (event == 1) {\n")
+                       extractedEvents.write("\n\t\t\tbreak\n")
+                       counter = counter + 1
+                       extractedEvents.write("\t\tcase %d:\n" % counter)
                        event = open("eventSimulator/musicPlayerStoppedEvent.groovy", "r")
                        for line in event:
-                               extractedEvents.write("\t\t" + line)
+                               extractedEvents.write(line)
                        event.close()
-                       extractedEvents.write("\t\t\t} else {\n")
+                       extractedEvents.write("\n\t\t\tbreak\n")
+                       counter = counter + 1
+                       extractedEvents.write("\t\tcase %d:\n" % counter)
                        event = open("eventSimulator/musicPlayerPausedEvent.groovy", "r")
                        for line in event:
-                               extractedEvents.write("\t\t" + line)
+                               extractedEvents.write(line)
                        event.close()
-                       extractedEvents.write("\t\t\t}\n")
                elif eventList[i] == "level":
                        #Check which capability
                        variable = eventVarMap[eventList[i]]
@@ -832,18 +913,17 @@ def ExtractEvents(extractedEvents):
                        event.close()
                elif eventList[i] == "mute":
                        #Write two events subsequently
-                       extractedEvents.write("\t\t\tdef event = Verify.getInt(0,1)\n")
-                       extractedEvents.write("\t\t\tif (event == 0) {\n")
                        event = open("eventSimulator/musicPlayerUnmutedEvent.groovy", "r")                      
                        for line in event:
-                               extractedEvents.write("\t\t" + line)
+                               extractedEvents.write(line)
                        event.close()
-                       extractedEvents.write("\t\t\t} else {\n")
+                       extractedEvents.write("\n\t\t\tbreak\n")
+                       counter = counter + 1
+                       extractedEvents.write("\t\tcase %d:\n" % counter)
                        event = open("eventSimulator/musicPlayerMutedEvent.groovy", "r")
                        for line in event:
-                               extractedEvents.write("\t\t" + line)
+                               extractedEvents.write(line)
                        event.close()
-                       extractedEvents.write("\t\t\t}\n")
                elif eventList[i] == "temperature":
                        #Check which capability
                        variable = eventVarMap[eventList[i]]
@@ -879,6 +959,7 @@ def ExtractEvents(extractedEvents):
                        
                ###TODO: Add more events later
                extractedEvents.write("\n\t\t\tbreak\n")
+               counter = counter + 1
        extractedEvents.write("\t}\n")
        extractedEvents.write("}\n")