2c965908705c6ce01e97c1a12739009886ffa5c4
[smartthings-infrastructure.git] / Extractor / ExtractorScript.py
1 import os
2 readyToReturn = 0
3 ToReturn = ""
4 eventList = []
5 eventAppList = [] # This list maps App1 or App2 to a specific event
6 eventVarMap = {}
7 eventVarCounterMap = {}
8 capabilityMap = {}
9 capabilityList = []
10 app1Capabilities = []
11 app2Capabilities = []
12 app1Subscribe = False
13 app2Subscribe = False
14
15 # Mapping for event type and number of events
16 # e.g., switch has 2 events: on and off
17 eventTypeCounterMap =  {'lock': 2,
18                                                 'nfcTouch' : 1,
19                                                 'app' : 1,
20                                                 'button' : 2,
21                                                 'water' : 2,
22                                                 'water.dry' : 1,
23                                                 'water.wet' : 1,
24                                                 'presence' : 2,
25                                                 'doorState' : 2,
26                                                 'motion' : 2,
27                                                 'smoke' : 3,
28                                                 'carbonMonoxide' : 3,
29                                                 'battery' : 1,
30                                                 'thermostatMode' : 5,
31                                                 'thermostatFanMode' : 5,
32                                                 'thermostatOperatingState' : 5,
33                                                 'switch' : 2,
34                                                 'location' : 3,
35                                                 'mode' : 3,
36                                                 'sunset' : 1,
37                                                 'sunsetTime' : 1,
38                                                 'sunrise' : 1,
39                                                 'sunriseTime' : 1,
40                                                 'acceleration' : 2,
41                                                 'sleeping' : 2,
42                                                 'goal' : 2,
43                                                 'steps' : 2,
44                                                 'color' : 1,
45                                                 'colorTemperature' : 1,
46                                                 'hue' : 1,
47                                                 'saturation' : 1,
48                                                 'energy' : 1,
49                                                 'power' : 2,
50                                                 'illuminance' : 1,
51                                                 'humidity' : 2,
52                                                 'alarm' : 4,
53                                                 'contact' : 2,
54                                                 'status' : 3,
55                                                 'level' : 1,
56                                                 'trackDescription' : 1,
57                                                 'trackData' : 1,
58                                                 'mute' : 2,
59                                                 'temperature' : 2,
60                                                 'heatingSetpoint' : 1,
61                                                 'coolingSetpoint' : 1,
62                                                 'thermostatSetpoint' : 1,
63                                                 'threeAxis' : 1}
64
65 # Mapping for specific event types
66 eventTypesMap =            {'alarm.both': 'alarm',
67                                                 'alarm.siren' : 'alarm',
68                                                 'alarm.strobe' : 'alarm',
69                                                 'alarm.off' : 'alarm',
70                                                 'contact.open' : 'contact',
71                                                 'contact.closed' : 'contact',
72                                                 'tamper.tampered' : 'contact',
73                                                 'doorState.open' : 'doorState',
74                                                 'doorState.closed' : 'doorState',
75                                                 'position' : 'location',
76                                                 'mode' : 'location',
77                                                 'mode.away' : 'location',
78                                                 'mode.home' : 'location',
79                                                 'mode.night' : 'location',
80                                                 'lock.locked' : 'lock',
81                                                 'lock.unlocked' : 'lock',
82                                                 'motion.active' : 'motion',
83                                                 'motion.inactive' : 'motion',
84                                                 'status.playing' : 'status',
85                                                 'status.stopped' : 'status',
86                                                 'status.paused' : 'status',
87                                                 'mute.muted' : 'mute',
88                                                 'mute.unmuted' : 'mute',
89                                                 'presence.present' : 'presence',
90                                                 'presence.not present' : 'presence',
91                                                 'presence.not' : 'presence',
92                                                 'smoke.detected' : 'smoke',
93                                                 'smoke.clear' : 'smoke',
94                                                 'smoke.tested' : 'smoke',
95                                                 'carbonMonoxide.detected' : 'carbonMonoxide',
96                                                 'carbonMonoxide.clear' : 'carbonMonoxide',
97                                                 'carbonMonoxide.tested' : 'carbonMonoxide',
98                                                 'thermostatMode.cool' : 'thermostatMode',
99                                                 'thermostatMode.auto' : 'thermostatMode',
100                                                 'thermostatMode.emergencyHeat' : 'thermostatMode',
101                                                 'thermostatMode.heat' : 'thermostatMode',
102                                                 'thermostatMode.off' : 'thermostatMode',
103                                                 'thermostatFanMode.auto' : 'thermostatFanMode',
104                                                 'thermostatFanMode.fanCirculate' : 'thermostatFanMode',
105                                                 'thermostatFanMode.circulate' : 'thermostatFanMode',
106                                                 'thermostatFanMode.fanOn' : 'thermostatFanMode',
107                                                 'thermostatFanMode.on' : 'thermostatFanMode',
108                                                 'fanMode' : 'thermostatFanMode',
109                                                 'fanMode.auto' : 'thermostatFanMode',
110                                                 'fanMode.fanCirculate' : 'thermostatFanMode',
111                                                 'fanMode.circulate' : 'thermostatFanMode',
112                                                 'fanMode.fanOn' : 'thermostatFanMode',
113                                                 'fanMode.on' : 'thermostatFanMode',
114                                                 'switch.on' : 'switch',
115                                                 'switch.off' : 'switch',
116                                                 'button.pushed' : 'button',
117                                                 'button.held' : 'button',
118                                                 'acceleration.active' : 'acceleration',
119                                                 'acceleration.inactive' : 'acceleration',
120                                                 'sleeping.sleeping' : 'sleeping',
121                                                 'sleeping.not sleeping' : 'sleeping',
122                                                 'water.dry' : 'water',
123                                                 'water.wet' : 'water'}
124
125 def GetToken(f):
126         global readyToReturn
127         global ToReturn
128         Skip = ['\n', '\t', ' ']
129         Special = ["(", "\"", ":", ",", "{", "}", ")", "/", "*"]
130         S = ""
131         if (readyToReturn):
132                 readyToReturn = 0
133                 return ToReturn
134         ToReturn = ""
135         c = f.read(1)
136         while(True):
137                 if (c in Special):
138                         if (S != ""):
139                                 readyToReturn = 1
140                                 ToReturn = c
141                                 return S
142                         else:
143                                 return c
144                 elif (c in Skip):
145                         if (S != ""):
146                                 return S        
147                         else:
148                                 c = f.read(1)
149                                 continue
150                 S += c
151                 c = f.read(1)
152                 if not c:
153                         return "EOF"
154
155 def ExtractFunctions(F, appName):
156         global eventList
157         global eventAppList
158         global eventTypesMap
159         global app1Subscribe
160         global app2Subscribe
161         Temp = GetToken(F)
162         while (Temp != "EOF"):
163                 if (Temp == "def" or Temp == "private"):
164                         Temp = GetToken(F)
165                         if (Temp == "def" or Temp == "private"):
166                                 Temp = GetToken(F)
167                         NameofFunc = Temp
168                         if (GetToken(F) == "("): #We have a function to create object for
169                                 if (appName == "App1"):
170                                         extractedFunctionsApp1.write("//Global Object for functions in subscribe method!\n")    
171                                         extractedFunctionsApp1.write("def %s = this.&" % NameofFunc)
172                                         extractedFunctionsApp1.write("%s\n" % NameofFunc)
173                                 else:
174                                         extractedFunctionsApp2.write("//Global Object for functions in subscribe method!\n")    
175                                         extractedFunctionsApp2.write("def %s = this.&" % NameofFunc)
176                                         extractedFunctionsApp2.write("%s\n" % NameofFunc)
177                 
178                 #Check input capability
179                 if (Temp == "input"):
180                         while Temp != "\"":
181                                 Temp = GetToken(F)
182                         variable = GetToken(F)
183                         Temp = GetToken(F) #Get '"'
184                         Temp = GetToken(F) #Get ','
185                         while Temp != "\"":
186                                 Temp = GetToken(F)
187                         capability = GetToken(F)
188                         capabilityMap[variable] = capability
189                 
190                 #Check subscribed events
191                 if (Temp == "subscribe"):
192                         if (appName == "App1"):
193                                 app1Subscribe = True
194                         else:
195                                 app2Subscribe = True
196                         Temp = GetToken(F)
197                         if (Temp == "("):
198                                 Temp = GetToken(F)
199                         variable = Temp
200                         while (Temp != "\"" and Temp != "app" and Temp != "location"):
201                                 Temp = GetToken(F)
202                         if Temp == "location":
203                                 # See if we have another case for location
204                                 Temp = GetToken(F) # Get ','
205                                 Temp = GetToken(F) # Get '"'
206                                 if Temp == "\"":
207                                         Temp = GetToken(F)                      
208                                 else:
209                                         # This is the special case where we will see, e.g., sunset, sunrise, etc.
210                                         Temp = "location"
211                         if Temp == "\"":
212                                 Temp = GetToken(F)
213
214                         #print "DEBUG: %s - %s" % (variable, Temp)
215                         #print capabilityMap
216                         #print "DEBUG: location variable: %s" % Temp    
217
218                         #This is a bogus event, just skip it...
219                         if Temp == "unlock":
220                                 continue                        
221                         #Translate and reduce through mapping
222                         if Temp in eventTypesMap.keys():                
223                                 Temp = eventTypesMap[Temp]
224                         if Temp == "app" or Temp == "location" or Temp == "mode" or Temp == "sunset" or Temp == "sunrise" or Temp == "sunsetTime" or Temp == "sunriseTime":
225                                 if Temp not in eventList:
226                                         eventList.append(Temp)
227                                         if (appName == "App1"):
228                                                 eventAppList.append("App1")
229                                         else:
230                                                 eventAppList.append("App2")
231                         elif Temp not in eventList or (variable in capabilityMap.keys() and capabilityMap[variable] not in capabilityList):
232                                 # We do not repeat the same capability twice
233                                 capability = capabilityMap[variable]
234                                 capabilityList.append(capability)
235                                 eventList.append(Temp)
236                                 if (appName == "App1"):
237                                         eventAppList.append("App1")
238                                 else:
239                                         eventAppList.append("App2")
240                                 if Temp in eventVarMap.keys():
241                                         listOfEvents = eventVarMap[Temp]
242                                         listOfEvents.append(variable)
243                                 else:
244                                         eventVarMap[Temp] = [variable]
245                         #print "DEBUG: %s - %s" % (variable, Temp)
246                         #print capabilityMap
247                         #print eventList
248
249                 #Check and analyze capabilities for physical interaction
250                 AnalyzeCapabilities(Temp, appName, F)
251
252                 Temp = GetToken(F)
253         
254         #Warn if there is a potential for physical interaction
255         AnalyzePhysicalInteraction(app1Capabilities, app2Capabilities)
256         AnalyzePhysicalInteraction(app2Capabilities, app1Capabilities)
257
258 def AnalyzeCapabilities(Temp, appName, F):
259                         #Illuminance related
260         if (Temp == "capability.switch" or
261                         Temp == "capability.switchLevel" or
262                         Temp == "capability.illuminanceMeasurement" or
263                         Temp == "capability.colorControl" or
264                         #Motion related
265                         Temp == "capability.motionSensor" or
266                         Temp == "capability.accelerationSensor" or
267                         #Water related
268                         Temp == "capability.valve" or
269                         Temp == "capability.waterSensor" or
270                         #Sound related
271                         Temp == "capability.musicPlayer" or
272                         Temp == "capability.alarm" or
273                         Temp == "capability.speechSynthesis" or
274                         Temp == "capability.soundSensor"):
275                 if (appName == "App1"):
276                         app1Capabilities.append(Temp)
277                 else:
278                         app2Capabilities.append(Temp)
279         if (Temp == "capability"):
280                 Temp = GetToken(F) #Get '"'
281                 Temp = GetToken(F) #Get 'Music'
282                 Temp = Temp + GetToken(F) #Get 'Player'
283                 if (Temp == "MusicPlayer"):
284                         if (appName == "App1"):
285                                 app1Capabilities.append("capability.musicPlayer")
286                         else:
287                                 app2Capabilities.append("capability.musicPlayer")
288                         
289 def AnalyzePhysicalInteraction(app1Capab, app2Capab):
290         #Light
291         if ("capability.illuminanceMeasurement" in app1Capab) and ("capability.switch" in app2Capab or 
292                         "capability.switchLevel" in app2Capab or "capability.colorControl" in app2Capab):
293                 print ("\nWARNING: Potential PHYSICAL CONFLICT (light) detected between App1 and App2!\n")
294         #Motion
295         # TODO: Technically this is not entirely precise since we need to be able to detect that the other app creates motion
296         if ("capability.motionSensor" in app1Capab) or ("capability.accelerationSensor" in app1Capab):
297                 print ("\nWARNING: Potential PHYSICAL CONFLICT (motion) detected between App1 and App2!\n")             
298         #Water
299         if ("capability.waterSensor" in app1Capab) and ("capability.valve" in app2Capab or 
300                         "capability.switch" in app2Capab):
301                 print ("\nWARNING: Potential PHYSICAL CONFLICT (water) detected between App1 and App2!\n")
302         #Sound
303         if ("capability.soundSensor" in app1Capab) and ("capability.musicPlayer" in app2Capab or 
304                         "capability.alarm" in app2Capab or "capability.speechSynthesis" in app2Capab):
305                 print ("\nWARNING: Potential PHYSICAL CONFLICT (sound) detected between App1 and App2!\n")
306
307 def CountEvents():
308         global eventList
309         numOfActualEvents = 0
310         #print "DEBUG: eventlist: %d" % len(eventList)
311         #print "DEBUG: eventlist: %s" % eventList
312         for event in eventList:
313                 #print "DEBUG: Event: %s %d" % (event, eventTypeCounterMap[event])
314                 numOfActualEvents = numOfActualEvents + eventTypeCounterMap[event]
315         return numOfActualEvents
316
317 def ExtractEvents(extractedEvents):
318         global eventList
319         global eventAppList
320         global eventVarMap
321         global capabilityMap
322         global eventVarCounterMap
323         # Count the number of events
324         numOfActualEvents = CountEvents()
325         extractedEvents.write("while(true) {\n")
326         extractedEvents.write("\tdef eventNumber = Verify.getInt(0,%d)\n" % (numOfActualEvents - 1))
327         extractedEvents.write("\tswitch(eventNumber) {\n")
328         counter = 0
329         indexApp1 = 0
330         if "App2" in eventAppList:
331                 indexApp2 = eventAppList.index("App2")
332         else:
333                 indexApp2 = len(eventList)
334         indexApp2Start = indexApp2
335         #print "DEBUG: App1: %d" % indexApp1
336         #print "DEBUG: App2: %d" % indexApp2
337         #print "DEBUG: eventList: %d" % len(eventList)
338         #print eventAppList
339         #print eventList
340         if indexApp2Start > 0:
341                 isApp1 = True
342         else:
343                 isApp1 = False
344         while counter < numOfActualEvents:
345                 # Interleave events from App1 and App2
346                 if isApp1 is True:
347                         i = indexApp1
348                         indexApp1 = indexApp1 + 1
349                         if indexApp2 < len(eventList):
350                                 isApp1 = False
351                 else:
352                         i = indexApp2
353                         indexApp2 = indexApp2 + 1
354                         if indexApp1 < indexApp2Start:
355                                 isApp1 = True
356                 #print "DEBUG: i: %d" % i
357                 extractedEvents.write("\t\tcase %d:\n" % counter)
358                 if eventList[i] == "lock":
359                         #Write two events subsequently
360                         event = open("eventSimulator/lockLockedEvent.groovy", "r")                      
361                         for line in event:
362                                 extractedEvents.write(line)
363                         event.close()
364                         extractedEvents.write("\n\t\t\tbreak\n")
365                         counter = counter + 1
366                         extractedEvents.write("\t\tcase %d:\n" % counter)
367                         event = open("eventSimulator/lockUnlockedEvent.groovy", "r")
368                         for line in event:
369                                 extractedEvents.write(line)
370                         event.close()
371                 elif eventList[i] == "nfcTouch":
372                         event = open("eventSimulator/nfcTouchEvent.groovy", "r")
373                         for line in event:
374                                 extractedEvents.write(line)
375                         event.close()
376                 elif eventList[i] == "app": #Case for Touched event
377                         event = open("eventSimulator/appTouchEvent.groovy", "r")
378                         for line in event:
379                                 extractedEvents.write(line)
380                         event.close()
381                 elif eventList[i] == "button":
382                         #Check which capability
383                         variable = eventVarMap[eventList[i]]
384                         if eventList[i] not in eventVarCounterMap.keys():
385                                 eventVarCounterMap[eventList[i]] = 1
386                                 eventVarCount = 0
387                         else:
388                                 eventVarCount = eventVarCounterMap[eventList[i]]
389                                 eventVarCounterMap[eventList[i]] = eventVarCount + 1
390                         capability = capabilityMap[variable[eventVarCount]]
391                         #Write two events subsequently
392                         if capability == "capability.button":
393                                 event = open("eventSimulator/buttonHeldEvent.groovy", "r")
394                         elif capability == "device.aeonKeyFob":
395                                 event = open("eventSimulator/aeonKeyFobHeldEvent.groovy", "r")
396                         for line in event:
397                                 extractedEvents.write(line)
398                         event.close()
399                         extractedEvents.write("\n\t\t\tbreak\n")
400                         counter = counter + 1
401                         extractedEvents.write("\t\tcase %d:\n" % counter)
402                         if capability == "capability.button":
403                                 event = open("eventSimulator/buttonPushedEvent.groovy", "r")
404                         elif capability == "device.aeonKeyFob":
405                                 event = open("eventSimulator/aeonKeyFobPushedEvent.groovy", "r")
406                         for line in event:
407                                 extractedEvents.write(line)
408                         event.close()
409                 elif eventList[i] == "water":
410                         #Write two events subsequently
411                         event = open("eventSimulator/waterDryEvent.groovy", "r")                        
412                         for line in event:
413                                 extractedEvents.write(line)
414                         event.close()
415                         extractedEvents.write("\n\t\t\tbreak\n")
416                         counter = counter + 1
417                         extractedEvents.write("\t\tcase %d:\n" % counter)
418                         event = open("eventSimulator/waterWetEvent.groovy", "r")
419                         for line in event:
420                                 extractedEvents.write(line)
421                         event.close()
422                 elif eventList[i] == "presence":
423                         #Check which capability
424                         variable = eventVarMap[eventList[i]]
425                         if eventList[i] not in eventVarCounterMap.keys():
426                                 eventVarCounterMap[eventList[i]] = 1
427                                 eventVarCount = 0
428                         else:
429                                 eventVarCount = eventVarCounterMap[eventList[i]]
430                                 eventVarCounterMap[eventList[i]] = eventVarCount + 1
431                         capability = capabilityMap[variable[eventVarCount]]
432                         #Write two events subsequently
433                         if capability == "capability.presenceSensor":
434                                 event = open("eventSimulator/presencePresentEvent.groovy", "r")
435                         elif capability == "capability.beacon":
436                                 event = open("eventSimulator/beaconPresentEvent.groovy", "r")
437                         for line in event:
438                                 extractedEvents.write(line)
439                         event.close()
440                         extractedEvents.write("\n\t\t\tbreak\n")
441                         counter = counter + 1
442                         extractedEvents.write("\t\tcase %d:\n" % counter)
443                         if capability == "capability.presenceSensor":
444                                 event = open("eventSimulator/presenceLeftEvent.groovy", "r")
445                         elif capability == "capability.beacon":
446                                 event = open("eventSimulator/beaconLeftEvent.groovy", "r")
447                         for line in event:
448                                 extractedEvents.write(line)
449                         event.close()
450                 elif eventList[i] == "doorState":
451                         #Write two events subsequently
452                         event = open("eventSimulator/doorOpenEvent.groovy", "r")                        
453                         for line in event:
454                                 extractedEvents.write(line)
455                         event.close()
456                         extractedEvents.write("\n\t\t\tbreak\n")
457                         counter = counter + 1
458                         extractedEvents.write("\t\tcase %d:\n" % counter)
459                         event = open("eventSimulator/doorClosedEvent.groovy", "r")
460                         for line in event:
461                                 extractedEvents.write(line)
462                         event.close()
463                 elif eventList[i] == "motion":
464                         print("HERE????")
465                         #Write two events subsequently
466                         event = open("eventSimulator/motionActiveEvent.groovy", "r")                    
467                         for line in event:
468                                 extractedEvents.write(line)
469                         event.close()
470                         extractedEvents.write("\n\t\t\tbreak\n")
471                         counter = counter + 1
472                         extractedEvents.write("\t\tcase %d:\n" % counter)
473                         event = open("eventSimulator/motionInactiveEvent.groovy", "r")
474                         for line in event:
475                                 extractedEvents.write(line)
476                         event.close()
477                 elif eventList[i] == "smoke":
478                         #Write three events subsequently
479                         event = open("eventSimulator/smokeClearEvent.groovy", "r")                      
480                         for line in event:
481                                 extractedEvents.write(line)
482                         event.close()
483                         extractedEvents.write("\n\t\t\tbreak\n")
484                         counter = counter + 1
485                         extractedEvents.write("\t\tcase %d:\n" % counter)
486                         event = open("eventSimulator/smokeDetectedEvent.groovy", "r")
487                         for line in event:
488                                 extractedEvents.write(line)
489                         event.close()
490                         extractedEvents.write("\n\t\t\tbreak\n")
491                         counter = counter + 1
492                         extractedEvents.write("\t\tcase %d:\n" % counter)
493                         event = open("eventSimulator/smokeTestedEvent.groovy", "r")
494                         for line in event:
495                                 extractedEvents.write(line)
496                         event.close()
497                 elif eventList[i] == "carbonMonoxide":
498                         #Check which capability
499                         variable = eventVarMap[eventList[i]]
500                         if eventList[i] not in eventVarCounterMap.keys():
501                                 eventVarCounterMap[eventList[i]] = 1
502                                 eventVarCount = 0
503                         else:
504                                 eventVarCount = eventVarCounterMap[eventList[i]]
505                                 eventVarCounterMap[eventList[i]] = eventVarCount + 1
506                         capability = capabilityMap[variable[eventVarCount]]
507                         #Write three events subsequently        
508                         if capability == "capability.smokeDetector":
509                                 event = open("eventSimulator/smokeCarbonMonoxideClearEvent.groovy", "r")
510                         elif capability == "capability.carbonMonoxideDetector":
511                                 event = open("eventSimulator/carbonMonoxideClearEvent.groovy", "r")
512                         for line in event:
513                                 extractedEvents.write(line)
514                         event.close()
515                         extractedEvents.write("\n\t\t\tbreak\n")
516                         counter = counter + 1
517                         extractedEvents.write("\t\tcase %d:\n" % counter)
518                         if capability == "capability.smokeDetector":
519                                 event = open("eventSimulator/smokeCarbonMonoxideDetectedEvent.groovy", "r")
520                         elif capability == "capability.carbonMonoxideDetector":
521                                 event = open("eventSimulator/carbonMonoxideDetectedEvent.groovy", "r")
522                         for line in event:
523                                 extractedEvents.write(line)
524                         event.close()
525                         extractedEvents.write("\n\t\t\tbreak\n")
526                         counter = counter + 1
527                         extractedEvents.write("\t\tcase %d:\n" % counter)
528                         if capability == "capability.smokeDetector":
529                                 event = open("eventSimulator/smokeCarbonMonoxideTestedEvent.groovy", "r")
530                         elif capability == "capability.carbonMonoxideDetector":
531                                 event = open("eventSimulator/carbonMonoxideTestedEvent.groovy", "r")
532                         for line in event:
533                                 extractedEvents.write(line)
534                         event.close()
535                 elif eventList[i] == "battery":
536                         #Check which capability
537                         variable = eventVarMap[eventList[i]]
538                         if eventList[i] not in eventVarCounterMap.keys():
539                                 eventVarCounterMap[eventList[i]] = 1
540                                 eventVarCount = 0
541                         else:
542                                 eventVarCount = eventVarCounterMap[eventList[i]]
543                                 eventVarCounterMap[eventList[i]] = eventVarCount + 1
544                         capability = capabilityMap[variable[eventVarCount]]
545                         if capability == "capability.smokeDetector":
546                                 event = open("eventSimulator/smokeDetectorBatteryEvent.groovy", "r")
547                         elif capability == "capability.battery":
548                                 event = open("eventSimulator/batteryBatteryEvent.groovy", "r")
549                         for line in event:
550                                 extractedEvents.write(line)
551                         event.close()
552                 elif eventList[i] == "thermostatMode":
553                         #Write five events subsequently
554                         event = open("eventSimulator/thermostatAutoModeEvent.groovy", "r")                      
555                         for line in event:
556                                 extractedEvents.write(line)
557                         event.close()
558                         extractedEvents.write("\n\t\t\tbreak\n")
559                         counter = counter + 1
560                         extractedEvents.write("\t\tcase %d:\n" % counter)
561                         event = open("eventSimulator/thermostatCoolModeEvent.groovy", "r")
562                         for line in event:
563                                 extractedEvents.write(line)
564                         event.close()
565                         extractedEvents.write("\n\t\t\tbreak\n")
566                         counter = counter + 1
567                         extractedEvents.write("\t\tcase %d:\n" % counter)
568                         event = open("eventSimulator/thermostatEmergencyHeatModeEvent.groovy", "r")
569                         for line in event:
570                                 extractedEvents.write(line)
571                         event.close()
572                         extractedEvents.write("\n\t\t\tbreak\n")
573                         counter = counter + 1
574                         extractedEvents.write("\t\tcase %d:\n" % counter)
575                         event = open("eventSimulator/thermostatHeatModeEvent.groovy", "r")
576                         for line in event:
577                                 extractedEvents.write(line)
578                         event.close()
579                         extractedEvents.write("\n\t\t\tbreak\n")
580                         counter = counter + 1
581                         extractedEvents.write("\t\tcase %d:\n" % counter)
582                         event = open("eventSimulator/thermostatOffModeEvent.groovy", "r")
583                         for line in event:
584                                 extractedEvents.write(line)
585                         event.close()
586                 elif eventList[i] == "thermostatFanMode":
587                         #Write five events subsequently
588                         event = open("eventSimulator/thermostatAutoFanModeEvent.groovy", "r")                   
589                         for line in event:
590                                 extractedEvents.write(line)
591                         event.close()
592                         extractedEvents.write("\n\t\t\tbreak\n")
593                         counter = counter + 1
594                         extractedEvents.write("\t\tcase %d:\n" % counter)
595                         event = open("eventSimulator/thermostatFanCirculateFanModeEvent.groovy", "r")
596                         for line in event:
597                                 extractedEvents.write(line)
598                         event.close()
599                         extractedEvents.write("\n\t\t\tbreak\n")
600                         counter = counter + 1
601                         extractedEvents.write("\t\tcase %d:\n" % counter)
602                         event = open("eventSimulator/thermostatCirculateFanModeEvent.groovy", "r")
603                         for line in event:
604                                 extractedEvents.write(line)
605                         event.close()
606                         extractedEvents.write("\n\t\t\tbreak\n")
607                         counter = counter + 1
608                         extractedEvents.write("\t\tcase %d:\n" % counter)
609                         event = open("eventSimulator/thermostatFanOnFanModeEvent.groovy", "r")
610                         for line in event:
611                                 extractedEvents.write(line)
612                         event.close()
613                         extractedEvents.write("\n\t\t\tbreak\n")
614                         counter = counter + 1
615                         extractedEvents.write("\t\tcase %d:\n" % counter)
616                         event = open("eventSimulator/thermostatOnFanModeEvent.groovy", "r")
617                         for line in event:
618                                 extractedEvents.write(line)
619                         event.close()
620                 elif eventList[i] == "thermostatOperatingState":
621                         #Write five events subsequently
622                         event = open("eventSimulator/thermostatOperatingStateAutoEvent.groovy", "r")                    
623                         for line in event:
624                                 extractedEvents.write(line)
625                         event.close()
626                         extractedEvents.write("\n\t\t\tbreak\n")
627                         counter = counter + 1
628                         extractedEvents.write("\t\tcase %d:\n" % counter)
629                         event = open("eventSimulator/thermostatOperatingStateCoolEvent.groovy", "r")
630                         for line in event:
631                                 extractedEvents.write(line)
632                         event.close()
633                         extractedEvents.write("\n\t\t\tbreak\n")
634                         counter = counter + 1
635                         extractedEvents.write("\t\tcase %d:\n" % counter)
636                         event = open("eventSimulator/thermostatOperatingStateOffEvent.groovy", "r")
637                         for line in event:
638                                 extractedEvents.write(line)
639                         event.close()
640                         extractedEvents.write("\n\t\t\tbreak\n")
641                         counter = counter + 1
642                         extractedEvents.write("\t\tcase %d:\n" % counter)
643                         event = open("eventSimulator/thermostatOperatingStateEmergencyHeatEvent.groovy", "r")
644                         for line in event:
645                                 extractedEvents.write(line)
646                         event.close()
647                         extractedEvents.write("\n\t\t\tbreak\n")
648                         counter = counter + 1
649                         extractedEvents.write("\t\tcase %d:\n" % counter)
650                         event = open("eventSimulator/thermostatOperatingStateHeatEvent.groovy", "r")
651                         for line in event:
652                                 extractedEvents.write(line)
653                         event.close()
654                 elif eventList[i] == "switch":
655                         #Check which capability
656                         variable = eventVarMap[eventList[i]]
657                         if eventList[i] not in eventVarCounterMap.keys():
658                                 eventVarCounterMap[eventList[i]] = 1
659                                 eventVarCount = 0
660                         else:
661                                 eventVarCount = eventVarCounterMap[eventList[i]]
662                                 eventVarCounterMap[eventList[i]] = eventVarCount + 1
663                         capability = capabilityMap[variable[eventVarCount]]
664                         #Write two events subsequently
665                         print("//////////////////////")
666                         print(capability)
667                         if capability == "capability.switch":
668                                 event = open("eventSimulator/switchOnEvent.groovy", "r")
669                         elif capability == "capability.switchLevel":
670                                 event = open("eventSimulator/switchLevelOnEvent.groovy", "r")
671                         elif capability == "capability.relaySwitch":
672                                 event = open("eventSimulator/relaySwitchOnEvent.groovy", "r")
673                         elif capability == "capability.colorControl":
674                                 event = open("eventSimulator/colorControlSwitchOnEvent.groovy", "r")
675                         elif capability == "capability.colorTemperature":
676                                 event = open("eventSimulator/colorTemperatureSwitchOnEvent.groovy", "r")
677                         for line in event:
678                                 extractedEvents.write(line)
679                         event.close()
680                         extractedEvents.write("\n\t\t\tbreak\n")
681                         counter = counter + 1
682                         extractedEvents.write("\t\tcase %d:\n" % counter)
683                         if capability == "capability.switch":
684                                 event = open("eventSimulator/switchOffEvent.groovy", "r")
685                         elif capability == "capability.switchLevel":
686                                 event = open("eventSimulator/switchLevelOffEvent.groovy", "r")
687                         elif capability == "capability.relaySwitch":
688                                 event = open("eventSimulator/relaySwitchOffEvent.groovy", "r")
689                         elif capability == "capability.colorControl":
690                                 event = open("eventSimulator/colorControlSwitchOffEvent.groovy", "r")
691                         elif capability == "capability.colorTemperature":
692                                 event = open("eventSimulator/colorTemperatureSwitchOffEvent.groovy", "r")
693                         for line in event:
694                                 extractedEvents.write(line)
695                         event.close()
696                 elif eventList[i] == "location": #Case for Location event
697                         #Write three events subsequently
698                         event = open("eventSimulator/locationHomeEvent.groovy", "r")                    
699                         for line in event:
700                                 extractedEvents.write(line)
701                         event.close()
702                         extractedEvents.write("\n\t\t\tbreak\n")
703                         counter = counter + 1
704                         extractedEvents.write("\t\tcase %d:\n" % counter)
705                         event = open("eventSimulator/locationAwayEvent.groovy", "r")
706                         for line in event:
707                                 extractedEvents.write(line)
708                         event.close()
709                         extractedEvents.write("\n\t\t\tbreak\n")
710                         counter = counter + 1
711                         extractedEvents.write("\t\tcase %d:\n" % counter)
712                         event = open("eventSimulator/locationNightEvent.groovy", "r")
713                         for line in event:
714                                 extractedEvents.write(line)
715                         event.close()
716                 elif eventList[i] == "sunrise":
717                         event = open("eventSimulator/locationSunriseEvent.groovy", "r")
718                         for line in event:
719                                 extractedEvents.write(line)
720                         event.close()
721                 elif eventList[i] == "sunset":
722                         event = open("eventSimulator/locationSunsetEvent.groovy", "r")
723                         for line in event:
724                                 extractedEvents.write(line)
725                         event.close()
726                 elif eventList[i] == "sunsetTime":
727                         event = open("eventSimulator/locationSunsetTimeEvent.groovy", "r")
728                         for line in event:
729                                 extractedEvents.write(line)
730                         event.close()
731                 elif eventList[i] == "sunriseTime":
732                         event = open("eventSimulator/locationSunriseTimeEvent.groovy", "r")
733                         for line in event:
734                                 extractedEvents.write(line)
735                         event.close()
736                 elif eventList[i] == "acceleration":
737                         #Write two events subsequently
738                         event = open("eventSimulator/accelerationActiveEvent.groovy", "r")                      
739                         for line in event:
740                                 extractedEvents.write(line)
741                         event.close()
742                         extractedEvents.write("\n\t\t\tbreak\n")
743                         counter = counter + 1
744                         extractedEvents.write("\t\tcase %d:\n" % counter)
745                         event = open("eventSimulator/accelerationInactiveEvent.groovy", "r")
746                         for line in event:
747                                 extractedEvents.write(line)
748                         event.close()
749                 elif eventList[i] == "sleeping":
750                         #Write two events subsequently
751                         event = open("eventSimulator/sleepSleepingEvent.groovy", "r")                   
752                         for line in event:
753                                 extractedEvents.write(line)
754                         event.close()
755                         extractedEvents.write("\n\t\t\tbreak\n")
756                         counter = counter + 1
757                         extractedEvents.write("\t\tcase %d:\n" % counter)
758                         event = open("eventSimulator/sleepNotSleepingEvent.groovy", "r")
759                         for line in event:
760                                 extractedEvents.write(line)
761                         event.close()
762                 elif eventList[i] == "goal":
763                         #Write two events subsequently
764                         event = open("eventSimulator/stepGoalHighEvent.groovy", "r")
765                         for line in event:
766                                 extractedEvents.write(line)
767                         event.close()
768                         extractedEvents.write("\n\t\t\tbreak\n")
769                         counter = counter + 1
770                         extractedEvents.write("\t\tcase %d:\n" % counter)
771                         event = open("eventSimulator/stepGoalLowEvent.groovy", "r")
772                         for line in event:
773                                 extractedEvents.write(line)
774                         event.close()
775                 elif eventList[i] == "steps":
776                         #Write two events subsequently
777                         event = open("eventSimulator/stepStepsHighEvent.groovy", "r")
778                         for line in event:
779                                 extractedEvents.write(line)
780                         event.close()
781                         extractedEvents.write("\n\t\t\tbreak\n")
782                         counter = counter + 1
783                         extractedEvents.write("\t\tcase %d:\n" % counter)
784                         event = open("eventSimulator/stepStepsLowEvent.groovy", "r")
785                         for line in event:
786                                 extractedEvents.write(line)
787                         event.close()
788                 elif eventList[i] == "color":
789                         event = open("eventSimulator/colorChangeEvent.groovy", "r")
790                         for line in event:
791                                 extractedEvents.write(line)
792                         event.close()
793                 elif eventList[i] == "colorTemperature":
794                         event = open("eventSimulator/colorTemperatureEvent.groovy", "r")
795                         for line in event:
796                                 extractedEvents.write(line)
797                         event.close()
798                 elif eventList[i] == "hue":
799                         event = open("eventSimulator/hueChangeEvent.groovy", "r")
800                         for line in event:
801                                 extractedEvents.write(line)
802                         event.close()
803                 elif eventList[i] == "saturation":
804                         event = open("eventSimulator/saturationChangeEvent.groovy", "r")
805                         for line in event:
806                                 extractedEvents.write(line)
807                         event.close()
808                 elif eventList[i] == "energy":
809                         event = open("eventSimulator/energyMeterEvent.groovy", "r")
810                         for line in event:
811                                 extractedEvents.write(line)
812                         event.close()
813                 elif eventList[i] == "power":
814                         #Write two events subsequently
815                         event = open("eventSimulator/powerMeterHighPowerEvent.groovy", "r")
816                         for line in event:
817                                 extractedEvents.write(line)
818                         event.close()
819                         extractedEvents.write("\n\t\t\tbreak\n")
820                         counter = counter + 1
821                         extractedEvents.write("\t\tcase %d:\n" % counter)
822                         event = open("eventSimulator/powerMeterLowPowerEvent.groovy", "r")
823                         for line in event:
824                                 extractedEvents.write(line)
825                         event.close()
826                 elif eventList[i] == "illuminance":
827                         event = open("eventSimulator/illuminanceMeasurementEvent.groovy", "r")
828                         for line in event:
829                                 extractedEvents.write(line)
830                         event.close()
831                 elif eventList[i] == "humidity":
832                         event = open("eventSimulator/humidityHighMeasurementEvent.groovy", "r")
833                         for line in event:
834                                 extractedEvents.write(line)
835                         event.close()
836                         extractedEvents.write("\n\t\t\tbreak\n")
837                         counter = counter + 1
838                         extractedEvents.write("\t\tcase %d:\n" % counter)
839                         event = open("eventSimulator/humidityLowMeasurementEvent.groovy", "r")
840                         for line in event:
841                                 extractedEvents.write(line)
842                         event.close()
843                 elif eventList[i] == "alarm":
844                         #Write four events subsequently
845                         event = open("eventSimulator/alarmBothEvent.groovy", "r")                       
846                         for line in event:
847                                 extractedEvents.write(line)
848                         event.close()
849                         extractedEvents.write("\n\t\t\tbreak\n")
850                         counter = counter + 1
851                         extractedEvents.write("\t\tcase %d:\n" % counter)
852                         event = open("eventSimulator/alarmSirenEvent.groovy", "r")
853                         for line in event:
854                                 extractedEvents.write(line)
855                         event.close()
856                         extractedEvents.write("\n\t\t\tbreak\n")
857                         counter = counter + 1
858                         extractedEvents.write("\t\tcase %d:\n" % counter)
859                         event = open("eventSimulator/alarmStrobeEvent.groovy", "r")
860                         for line in event:
861                                 extractedEvents.write(line)
862                         event.close()
863                         extractedEvents.write("\n\t\t\tbreak\n")
864                         counter = counter + 1
865                         extractedEvents.write("\t\tcase %d:\n" % counter)
866                         event = open("eventSimulator/alarmOffEvent.groovy", "r")
867                         for line in event:
868                                 extractedEvents.write(line)
869                         event.close()
870                 elif eventList[i] == "contact":
871                         #Check which capability
872                         variable = eventVarMap[eventList[i]]
873                         if eventList[i] not in eventVarCounterMap.keys():
874                                 eventVarCounterMap[eventList[i]] = 1
875                                 eventVarCount = 0
876                         else:
877                                 eventVarCount = eventVarCounterMap[eventList[i]]
878                                 eventVarCounterMap[eventList[i]] = eventVarCount + 1
879                         capability = capabilityMap[variable[eventVarCount]]
880                         #Write two events subsequently
881                         if capability == "capability.contactSensor":
882                                 event = open("eventSimulator/contactDefaultClosedEvent.groovy", "r")
883                         elif capability == "capability.valve":
884                                 event = open("eventSimulator/valveClosedEvent.groovy", "r")
885                         for line in event:
886                                 extractedEvents.write(line)
887                         event.close()
888                         extractedEvents.write("\n\t\t\tbreak\n")
889                         counter = counter + 1
890                         extractedEvents.write("\t\tcase %d:\n" % counter)
891                         if capability == "capability.contactSensor":
892                                 event = open("eventSimulator/contactDefaultOpenEvent.groovy", "r")
893                         elif capability == "capability.valve":
894                                 event = open("eventSimulator/valveOpenEvent.groovy", "r")
895                         for line in event:
896                                 extractedEvents.write(line)
897                         event.close()
898                 elif eventList[i] == "status":
899                         #Write three events subsequently
900                         event = open("eventSimulator/musicPlayerPlayingEvent.groovy", "r")                      
901                         for line in event:
902                                 extractedEvents.write(line)
903                         event.close()
904                         extractedEvents.write("\n\t\t\tbreak\n")
905                         counter = counter + 1
906                         extractedEvents.write("\t\tcase %d:\n" % counter)
907                         event = open("eventSimulator/musicPlayerStoppedEvent.groovy", "r")
908                         for line in event:
909                                 extractedEvents.write(line)
910                         event.close()
911                         extractedEvents.write("\n\t\t\tbreak\n")
912                         counter = counter + 1
913                         extractedEvents.write("\t\tcase %d:\n" % counter)
914                         event = open("eventSimulator/musicPlayerPausedEvent.groovy", "r")
915                         for line in event:
916                                 extractedEvents.write(line)
917                         event.close()
918                 elif eventList[i] == "level":
919                         #Check which capability
920                         variable = eventVarMap[eventList[i]]
921                         if eventList[i] not in eventVarCounterMap.keys():
922                                 eventVarCounterMap[eventList[i]] = 1
923                                 eventVarCount = 0
924                         else:
925                                 eventVarCount = eventVarCounterMap[eventList[i]]
926                                 eventVarCounterMap[eventList[i]] = eventVarCount + 1
927                         capability = capabilityMap[variable[eventVarCount]]
928                         if capability == "capability.musicPlayer":
929                                 event = open("eventSimulator/musicPlayerLevelEvent.groovy", "r")
930                         elif capability == "capability.switchLevel":
931                                 event = open("eventSimulator/switchLevelEvent.groovy", "r")
932                         elif capability == "capability.colorControl":
933                                 event = open("eventSimulator/colorLevelChangeEvent.groovy", "r")
934                         elif capability == "capability.switch":
935                                 event = open("eventSimulator/switchLevelChangeEvent.groovy", "r")
936                         for line in event:
937                                 extractedEvents.write(line)
938                         event.close()
939                 elif eventList[i] == "trackDescription":
940                         event = open("eventSimulator/musicPlayerTrackDescriptionEvent.groovy", "r")
941                         for line in event:
942                                 extractedEvents.write(line)
943                         event.close()
944                 elif eventList[i] == "trackData":
945                         event = open("eventSimulator/musicPlayerTrackDataEvent.groovy", "r")
946                         for line in event:
947                                 extractedEvents.write(line)
948                         event.close()
949                 elif eventList[i] == "mute":
950                         #Write two events subsequently
951                         event = open("eventSimulator/musicPlayerUnmutedEvent.groovy", "r")                      
952                         for line in event:
953                                 extractedEvents.write(line)
954                         event.close()
955                         extractedEvents.write("\n\t\t\tbreak\n")
956                         counter = counter + 1
957                         extractedEvents.write("\t\tcase %d:\n" % counter)
958                         event = open("eventSimulator/musicPlayerMutedEvent.groovy", "r")
959                         for line in event:
960                                 extractedEvents.write(line)
961                         event.close()
962                 elif eventList[i] == "temperature":
963                         #Check which capability
964                         variable = eventVarMap[eventList[i]]
965                         if eventList[i] not in eventVarCounterMap.keys():
966                                 eventVarCounterMap[eventList[i]] = 1
967                                 eventVarCount = 0
968                         else:
969                                 eventVarCount = eventVarCounterMap[eventList[i]]
970                                 eventVarCounterMap[eventList[i]] = eventVarCount + 1
971                         capability = capabilityMap[variable[eventVarCount]]
972                         #Write two events subsequently
973                         if capability == "capability.thermostat":
974                                 event = open("eventSimulator/temperatureHighEvent.groovy", "r")
975                         elif capability == "capability.temperatureMeasurement":
976                                 event = open("eventSimulator/temperatureHighMeasurementEvent.groovy", "r")
977                         for line in event:
978                                 extractedEvents.write(line)
979                         event.close()
980                         extractedEvents.write("\n\t\t\tbreak\n")
981                         counter = counter + 1
982                         extractedEvents.write("\t\tcase %d:\n" % counter)
983                         if capability == "capability.thermostat":
984                                 event = open("eventSimulator/temperatureLowEvent.groovy", "r")
985                         elif capability == "capability.temperatureMeasurement":
986                                 event = open("eventSimulator/temperatureLowMeasurementEvent.groovy", "r")
987                         for line in event:
988                                 extractedEvents.write(line)
989                         event.close()
990                 elif eventList[i] == "heatingSetpoint":
991                         event = open("eventSimulator/heatingSetpointEvent.groovy", "r")
992                         for line in event:
993                                 extractedEvents.write(line)
994                         event.close()
995                 elif eventList[i] == "coolingSetpoint":
996                         event = open("eventSimulator/coolingSetpointEvent.groovy", "r")
997                         for line in event:
998                                 extractedEvents.write(line)
999                         event.close()
1000                 elif eventList[i] == "thermostatSetpoint":
1001                         event = open("eventSimulator/thermostatSetpointEvent.groovy", "r")
1002                         for line in event:
1003                                 extractedEvents.write(line)
1004                         event.close()
1005                 elif eventList[i] == "threeAxis":
1006                         event = open("eventSimulator/threeAxisChangeEvent.groovy", "r")
1007                         for line in event:
1008                                 extractedEvents.write(line)
1009                         event.close()
1010                         
1011                 ###TODO: Add more events later
1012                 extractedEvents.write("\n\t\t\tbreak\n")
1013                 counter = counter + 1
1014         extractedEvents.write("\t}\n")
1015         extractedEvents.write("}\n")
1016         
1017 def CheckIfOnlyTouchEvents():
1018         #Check and throw an error if it is all touch events
1019         #This is called Direct-Direct interaction and we do not model-check for this case
1020         onlyTouchEvents = True
1021         for item in eventList:
1022                 if item != "nfcTouch" and item != "app":
1023                         onlyTouchEvents = False
1024         if onlyTouchEvents is True and app1Subscribe is True and app2Subscribe is True:
1025                 # Write error log file
1026                 extractError = open("appCreationError.log", "w+")
1027                 extractError.write("Direct-Direct Interaction detected: we are skipping this pair...\n")
1028                 extractError.close()
1029                 raise Exception("\n\nDirect-Direct Interaction detected: we are skipping this pair...\n\n")
1030
1031
1032 #Extract objects to call functions from App1
1033 F1 = open("Extractor/App1/App1.groovy", "r")
1034 extractedFunctionsApp1 = open("Extractor/App1/extractedFunctionsApp1.groovy", "w+")
1035 ExtractFunctions(F1, "App1")
1036 F1.close()
1037
1038 #Extract objects to call functions from App2
1039 F2 = open("Extractor/App2/App2.groovy", "r")
1040 extractedFunctionsApp2 = open("Extractor/App2/extractedFunctionsApp2.groovy", "w+")
1041 ExtractFunctions(F2, "App2")
1042 F2.close()
1043
1044 #Prepare eventSimulator file while parsing the App1 and App2 files
1045 extractedEvents = open("eventSimulator/eventSimulator.groovy", "w+")
1046 CheckIfOnlyTouchEvents()
1047 ExtractEvents(extractedEvents)
1048 extractedEvents.close()
1049
1050 #Save the extracted methods and app1 in a same file to extract information
1051 extractorFile = open("Extractor/extractorFile.groovy", "w+")
1052 Extractor = open("Extractor/Extractor.groovy", "r")
1053 F1 = open("Extractor/App1/App1.groovy", "r")
1054
1055 extractorFile.write("////////////////////\n")
1056 extractorFile.write("@Field App\n")
1057 extractorFile.write("App = \"App1\"")
1058 extractorFile.write("\n")
1059 for line in Extractor:
1060         extractorFile.write(line)
1061 extractorFile.write("\n\n")
1062 for line in F1:
1063         extractorFile.write(line)
1064 extractorFile.close()
1065 Extractor.close()
1066 F1.close()
1067 #Run the file to extract the objects
1068 os.system("groovy -classpath lib/jpf.jar Extractor/extractorFile.groovy")
1069
1070
1071 #Save the extracted methods and app2 in a same file to extract information
1072 extractorFile = open("Extractor/extractorFile.groovy", "w+")
1073 Extractor = open("Extractor/Extractor.groovy", "r")
1074 F2 = open("Extractor/App2/App2.groovy", "r")
1075
1076 extractorFile.write("////////////////////\n")
1077 extractorFile.write("@Field App\n")
1078 extractorFile.write("App = \"App2\"")
1079 extractorFile.write("\n")
1080 for line in Extractor:
1081         extractorFile.write(line)
1082 extractorFile.write("\n\n")
1083 for line in F2:
1084         extractorFile.write(line)
1085 #Run the file to extract the objects
1086 extractorFile.close()
1087 Extractor.close()
1088 F2.close()
1089 os.system("groovy -classpath lib/jpf.jar Extractor/extractorFile.groovy")
1090
1091