Merge branch 'master' of ssh://plrg.eecs.uci.edu/home/git/smartthings-infrastructure
[smartthings-infrastructure.git] / Extractor / ExtractorScript.py
index b53afda..4e51bec 100644 (file)
@@ -41,8 +41,10 @@ def ExtractFunctions(F, appName):
        while (Temp != "EOF"):
                if (Temp == "def" or Temp == "private"):
                        Temp = GetToken(F)
+                       if (Temp == "def" or Temp == "private"):
+                               Temp = GetToken(F)
                        NameofFunc = Temp
-                       if (GetToken(F) != "="): #We have a function to create object for
+                       if (GetToken(F) == "("): #We have a function to create object for
                                if (appName == "App1"):
                                        extractedFunctionsApp1.write("//Global Object for functions in subscribe method!\n")    
                                        extractedFunctionsApp1.write("def %s = this.&" % NameofFunc)
@@ -51,7 +53,7 @@ def ExtractFunctions(F, appName):
                                        extractedFunctionsApp2.write("//Global Object for functions in subscribe method!\n")    
                                        extractedFunctionsApp2.write("def %s = this.&" % NameofFunc)
                                        extractedFunctionsApp2.write("%s\n" % NameofFunc)
-               
+
                #Check subscribed events
                if (Temp == "subscribe"):
                        counter = 0
@@ -69,12 +71,146 @@ def ExtractFunctions(F, appName):
                AnalyzeCapabilities(Temp, appName)
 
                Temp = GetToken(F)
-       
+
        #Warn if there is a potential for physical interaction
        AnalyzePhysicalInteraction(app1Capabilities, app2Capabilities)
        AnalyzePhysicalInteraction(app2Capabilities, app1Capabilities)
 
-               
+
+def AnalyzeCapabilities(Temp, appName):
+                       #Illuminance related
+       if (Temp == "capability.switch" or
+                       Temp == "capability.switchLevel" or
+                       Temp == "capability.illuminanceMeasurement" or
+                       #Motion related
+                       Temp == "capability.motionSensor" or
+                       #Water related
+                       Temp == "capability.valve" or
+                       Temp == "capability.waterSensor" or
+                       #Sound related
+                       Temp == "capability.musicPlayer" or
+                       Temp == "capability.alarm" or
+                       Temp == "capability.speechSynthesis" or
+                       Temp == "capability.soundSensor"):
+               if (appName == "App1"):
+                       app1Capabilities.append(Temp)
+               else:
+                       app2Capabilities.append(Temp)
+                       
+def AnalyzePhysicalInteraction(app1Capab, app2Capab):
+       #Light
+       if ("capability.illuminanceMeasurement" in app1Capab) and ("capability.switch" in app2Capab or 
+                       "capability.switchLevel" in app2Capab):
+               print ("\nWARNING: Potential PHYSICAL CONFLICT (light) detected between App1 and App2!\n")
+       #Motion
+       if ("capability.motionSensor" in app1Capab):
+               print ("\nWARNING: Potential PHYSICAL CONFLICT (motion) detected between App1 and App2!\n")             
+       #Water
+       if ("capability.waterSensor" in app1Capab) and ("capability.valve" in app2Capab or 
+                       "capability.switch" in app2Capab):
+               print ("\nWARNING: Potential PHYSICAL CONFLICT (water) detected between App1 and App2!\n")
+       #Sound
+       if ("capability.soundSensor" in app1Capab) and ("capability.musicPlayer" in app2Capab or 
+                       "capability.alarm" in app2Capab or "capability.speechSynthesis" in app2Capab):
+               print ("\nWARNING: Potential PHYSICAL CONFLICT (sound) detected between App1 and App2!\n")
+
+def ExtractEvents(extractedEvents):
+       global eventMap
+       extractedEvents.write("Random random = new Random(42)\n")
+       extractedEvents.write("counter = 1000\n")
+       extractedEvents.write("while(counter > 0) {\n")
+       extractedEvents.write("\tdef eventNumber = random.nextInt(%d)\n" % (len(eventMap) - 1))
+       extractedEvents.write("\tswitch(eventNumber) {\n")
+       eventCounter = 0;
+       for i in range(len(eventMap)):
+               extractedEvents.write("\t\tcase %d:\n" % eventCounter)
+               if eventMap[i] == "lock":
+                       event = open("eventSimulator/lockEvent.groovy", "r")
+                       for line in event:
+                               extractedEvents.write(line)
+                       event.close()
+               elif eventMap[i] == "unlock":
+                       event = open("eventSimulator/unlockEvent.groovy", "r")
+                       for line in event:
+                               extractedEvents.write(line)
+                       event.close()
+               elif eventMap[i] == "contact.open":
+                       event = open("eventSimulator/contactOpenEvent.groovy", "r")
+                       for line in event:
+                               extractedEvents.write(line)
+                       event.close()
+               elif eventMap[i] == "contact.closed":
+                       event = open("eventSimulator/contactClosedEvent.groovy", "r")
+                       for line in event:
+                               extractedEvents.write(line)
+                       event.close()
+               elif eventMap[i] == "switch.on":
+                       event = open("eventSimulator/switchOnEvent.groovy", "r")
+                       for line in event:
+                               extractedEvents.write(line)
+                       event.close()
+               elif eventMap[i] == "switch.off":
+                       event = open("eventSimulator/switchOffEvent.groovy", "r")
+                       for line in event:
+                               extractedEvents.write(line)
+                       event.close()
+               elif eventMap[i] == "nfcTouch":
+                       event = open("eventSimulator/nfcTouchEvent.groovy", "r")
+                       for line in event:
+                               extractedEvents.write(line)
+                       event.close()
+               elif eventMap[i] == "Touched":
+                       event = open("eventSimulator/appTouchEvent.groovy", "r")
+                       for line in event:
+                               extractedEvents.write(line)
+                       event.close()
+               elif eventMap[i] == "button":
+                       #Write two events subsequently
+                       event = open("eventSimulator/buttonPushedEvent.groovy", "r")
+                       for line in event:
+                               extractedEvents.write(line)
+                       event.close()
+                       extractedEvents.write("\t\t\tbreak\n")
+                       eventCounter = eventCounter + 1
+                       extractedEvents.write("\t\tcase %d:\n" % eventCounter)
+                       event = open("eventSimulator/buttonHeldEvent.groovy", "r")
+                       for line in event:
+                               extractedEvents.write(line)
+                       event.close()
+               elif eventMap[i] == "presence":
+                       #Write two events subsequently
+                       event = open("eventSimulator/presencePresentEvent.groovy", "r")
+                       for line in event:
+                               extractedEvents.write(line)
+                       event.close()
+                       extractedEvents.write("\t\t\tbreak\n")
+                       eventCounter = eventCounter + 1
+                       extractedEvents.write("\t\tcase %d:\n" % eventCounter)
+                       event = open("eventSimulator/presenceLeftEvent.groovy", "r")
+                       for line in event:
+                               extractedEvents.write(line)
+                       event.close()
+               elif eventMap[i] == "doorState":
+                       #Write two events subsequently
+                       event = open("eventSimulator/doorOpenEvent.groovy", "r")
+                       for line in event:
+                               extractedEvents.write(line)
+                       event.close()
+                       extractedEvents.write("\t\t\tbreak\n")
+                       eventCounter = eventCounter + 1
+                       extractedEvents.write("\t\tcase %d:\n" % eventCounter)
+                       event = open("eventSimulator/doorClosedEvent.groovy", "r")
+                       for line in event:
+                               extractedEvents.write(line)
+                       event.close()
+               eventCounter = eventCounter + 1
+
+               ###TODO: Add more events later
+               extractedEvents.write("\n\t\t\tbreak\n")
+       extractedEvents.write("\t}\n")
+       extractedEvents.write("\tcounter--\n")
+       extractedEvents.write("}\n")
+
 
 def AnalyzeCapabilities(Temp, appName):
                        #Illuminance related