a5d46c899934a97e650c73d5a28ecd2f692dfdd3
[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                         #Write two events subsequently
465                         event = open("eventSimulator/motionActiveEvent.groovy", "r")                    
466                         for line in event:
467                                 extractedEvents.write(line)
468                         event.close()
469                         extractedEvents.write("\n\t\t\tbreak\n")
470                         counter = counter + 1
471                         extractedEvents.write("\t\tcase %d:\n" % counter)
472                         event = open("eventSimulator/motionInactiveEvent.groovy", "r")
473                         for line in event:
474                                 extractedEvents.write(line)
475                         event.close()
476                 elif eventList[i] == "smoke":
477                         #Write three events subsequently
478                         event = open("eventSimulator/smokeClearEvent.groovy", "r")                      
479                         for line in event:
480                                 extractedEvents.write(line)
481                         event.close()
482                         extractedEvents.write("\n\t\t\tbreak\n")
483                         counter = counter + 1
484                         extractedEvents.write("\t\tcase %d:\n" % counter)
485                         event = open("eventSimulator/smokeDetectedEvent.groovy", "r")
486                         for line in event:
487                                 extractedEvents.write(line)
488                         event.close()
489                         extractedEvents.write("\n\t\t\tbreak\n")
490                         counter = counter + 1
491                         extractedEvents.write("\t\tcase %d:\n" % counter)
492                         event = open("eventSimulator/smokeTestedEvent.groovy", "r")
493                         for line in event:
494                                 extractedEvents.write(line)
495                         event.close()
496                 elif eventList[i] == "carbonMonoxide":
497                         #Check which capability
498                         variable = eventVarMap[eventList[i]]
499                         if eventList[i] not in eventVarCounterMap.keys():
500                                 eventVarCounterMap[eventList[i]] = 1
501                                 eventVarCount = 0
502                         else:
503                                 eventVarCount = eventVarCounterMap[eventList[i]]
504                                 eventVarCounterMap[eventList[i]] = eventVarCount + 1
505                         capability = capabilityMap[variable[eventVarCount]]
506                         #Write three events subsequently        
507                         if capability == "capability.smokeDetector":
508                                 event = open("eventSimulator/smokeCarbonMonoxideClearEvent.groovy", "r")
509                         elif capability == "capability.carbonMonoxideDetector":
510                                 event = open("eventSimulator/carbonMonoxideClearEvent.groovy", "r")
511                         for line in event:
512                                 extractedEvents.write(line)
513                         event.close()
514                         extractedEvents.write("\n\t\t\tbreak\n")
515                         counter = counter + 1
516                         extractedEvents.write("\t\tcase %d:\n" % counter)
517                         if capability == "capability.smokeDetector":
518                                 event = open("eventSimulator/smokeCarbonMonoxideDetectedEvent.groovy", "r")
519                         elif capability == "capability.carbonMonoxideDetector":
520                                 event = open("eventSimulator/carbonMonoxideDetectedEvent.groovy", "r")
521                         for line in event:
522                                 extractedEvents.write(line)
523                         event.close()
524                         extractedEvents.write("\n\t\t\tbreak\n")
525                         counter = counter + 1
526                         extractedEvents.write("\t\tcase %d:\n" % counter)
527                         if capability == "capability.smokeDetector":
528                                 event = open("eventSimulator/smokeCarbonMonoxideTestedEvent.groovy", "r")
529                         elif capability == "capability.carbonMonoxideDetector":
530                                 event = open("eventSimulator/carbonMonoxideTestedEvent.groovy", "r")
531                         for line in event:
532                                 extractedEvents.write(line)
533                         event.close()
534                 elif eventList[i] == "battery":
535                         #Check which capability
536                         variable = eventVarMap[eventList[i]]
537                         if eventList[i] not in eventVarCounterMap.keys():
538                                 eventVarCounterMap[eventList[i]] = 1
539                                 eventVarCount = 0
540                         else:
541                                 eventVarCount = eventVarCounterMap[eventList[i]]
542                                 eventVarCounterMap[eventList[i]] = eventVarCount + 1
543                         capability = capabilityMap[variable[eventVarCount]]
544                         if capability == "capability.smokeDetector":
545                                 event = open("eventSimulator/smokeDetectorBatteryEvent.groovy", "r")
546                         elif capability == "capability.battery":
547                                 event = open("eventSimulator/batteryBatteryEvent.groovy", "r")
548                         for line in event:
549                                 extractedEvents.write(line)
550                         event.close()
551                 elif eventList[i] == "thermostatMode":
552                         #Write five events subsequently
553                         event = open("eventSimulator/thermostatAutoModeEvent.groovy", "r")                      
554                         for line in event:
555                                 extractedEvents.write(line)
556                         event.close()
557                         extractedEvents.write("\n\t\t\tbreak\n")
558                         counter = counter + 1
559                         extractedEvents.write("\t\tcase %d:\n" % counter)
560                         event = open("eventSimulator/thermostatCoolModeEvent.groovy", "r")
561                         for line in event:
562                                 extractedEvents.write(line)
563                         event.close()
564                         extractedEvents.write("\n\t\t\tbreak\n")
565                         counter = counter + 1
566                         extractedEvents.write("\t\tcase %d:\n" % counter)
567                         event = open("eventSimulator/thermostatEmergencyHeatModeEvent.groovy", "r")
568                         for line in event:
569                                 extractedEvents.write(line)
570                         event.close()
571                         extractedEvents.write("\n\t\t\tbreak\n")
572                         counter = counter + 1
573                         extractedEvents.write("\t\tcase %d:\n" % counter)
574                         event = open("eventSimulator/thermostatHeatModeEvent.groovy", "r")
575                         for line in event:
576                                 extractedEvents.write(line)
577                         event.close()
578                         extractedEvents.write("\n\t\t\tbreak\n")
579                         counter = counter + 1
580                         extractedEvents.write("\t\tcase %d:\n" % counter)
581                         event = open("eventSimulator/thermostatOffModeEvent.groovy", "r")
582                         for line in event:
583                                 extractedEvents.write(line)
584                         event.close()
585                 elif eventList[i] == "thermostatFanMode":
586                         #Write five events subsequently
587                         event = open("eventSimulator/thermostatAutoFanModeEvent.groovy", "r")                   
588                         for line in event:
589                                 extractedEvents.write(line)
590                         event.close()
591                         extractedEvents.write("\n\t\t\tbreak\n")
592                         counter = counter + 1
593                         extractedEvents.write("\t\tcase %d:\n" % counter)
594                         event = open("eventSimulator/thermostatFanCirculateFanModeEvent.groovy", "r")
595                         for line in event:
596                                 extractedEvents.write(line)
597                         event.close()
598                         extractedEvents.write("\n\t\t\tbreak\n")
599                         counter = counter + 1
600                         extractedEvents.write("\t\tcase %d:\n" % counter)
601                         event = open("eventSimulator/thermostatCirculateFanModeEvent.groovy", "r")
602                         for line in event:
603                                 extractedEvents.write(line)
604                         event.close()
605                         extractedEvents.write("\n\t\t\tbreak\n")
606                         counter = counter + 1
607                         extractedEvents.write("\t\tcase %d:\n" % counter)
608                         event = open("eventSimulator/thermostatFanOnFanModeEvent.groovy", "r")
609                         for line in event:
610                                 extractedEvents.write(line)
611                         event.close()
612                         extractedEvents.write("\n\t\t\tbreak\n")
613                         counter = counter + 1
614                         extractedEvents.write("\t\tcase %d:\n" % counter)
615                         event = open("eventSimulator/thermostatOnFanModeEvent.groovy", "r")
616                         for line in event:
617                                 extractedEvents.write(line)
618                         event.close()
619                 elif eventList[i] == "thermostatOperatingState":
620                         #Write five events subsequently
621                         event = open("eventSimulator/thermostatOperatingStateAutoEvent.groovy", "r")                    
622                         for line in event:
623                                 extractedEvents.write(line)
624                         event.close()
625                         extractedEvents.write("\n\t\t\tbreak\n")
626                         counter = counter + 1
627                         extractedEvents.write("\t\tcase %d:\n" % counter)
628                         event = open("eventSimulator/thermostatOperatingStateCoolEvent.groovy", "r")
629                         for line in event:
630                                 extractedEvents.write(line)
631                         event.close()
632                         extractedEvents.write("\n\t\t\tbreak\n")
633                         counter = counter + 1
634                         extractedEvents.write("\t\tcase %d:\n" % counter)
635                         event = open("eventSimulator/thermostatOperatingStateOffEvent.groovy", "r")
636                         for line in event:
637                                 extractedEvents.write(line)
638                         event.close()
639                         extractedEvents.write("\n\t\t\tbreak\n")
640                         counter = counter + 1
641                         extractedEvents.write("\t\tcase %d:\n" % counter)
642                         event = open("eventSimulator/thermostatOperatingStateEmergencyHeatEvent.groovy", "r")
643                         for line in event:
644                                 extractedEvents.write(line)
645                         event.close()
646                         extractedEvents.write("\n\t\t\tbreak\n")
647                         counter = counter + 1
648                         extractedEvents.write("\t\tcase %d:\n" % counter)
649                         event = open("eventSimulator/thermostatOperatingStateHeatEvent.groovy", "r")
650                         for line in event:
651                                 extractedEvents.write(line)
652                         event.close()
653                 elif eventList[i] == "switch":
654                         #Check which capability
655                         variable = eventVarMap[eventList[i]]
656                         if eventList[i] not in eventVarCounterMap.keys():
657                                 eventVarCounterMap[eventList[i]] = 1
658                                 eventVarCount = 0
659                         else:
660                                 eventVarCount = eventVarCounterMap[eventList[i]]
661                                 eventVarCounterMap[eventList[i]] = eventVarCount + 1
662                         capability = capabilityMap[variable[eventVarCount]]
663                         #Write two events subsequently
664                         print("//////////////////////")
665                         print(capability)
666                         if capability == "capability.switch":
667                                 event = open("eventSimulator/switchOnEvent.groovy", "r")
668                         elif capability == "capability.switchLevel":
669                                 event = open("eventSimulator/switchLevelOnEvent.groovy", "r")
670                         elif capability == "capability.relaySwitch":
671                                 event = open("eventSimulator/relaySwitchOnEvent.groovy", "r")
672                         elif capability == "capability.colorControl":
673                                 event = open("eventSimulator/colorControlSwitchOnEvent.groovy", "r")
674                         elif capability == "capability.colorTemperature":
675                                 event = open("eventSimulator/colorTemperatureSwitchOnEvent.groovy", "r")
676                         for line in event:
677                                 extractedEvents.write(line)
678                         event.close()
679                         extractedEvents.write("\n\t\t\tbreak\n")
680                         counter = counter + 1
681                         extractedEvents.write("\t\tcase %d:\n" % counter)
682                         if capability == "capability.switch":
683                                 event = open("eventSimulator/switchOffEvent.groovy", "r")
684                         elif capability == "capability.switchLevel":
685                                 event = open("eventSimulator/switchLevelOffEvent.groovy", "r")
686                         elif capability == "capability.relaySwitch":
687                                 event = open("eventSimulator/relaySwitchOffEvent.groovy", "r")
688                         elif capability == "capability.colorControl":
689                                 event = open("eventSimulator/colorControlSwitchOffEvent.groovy", "r")
690                         elif capability == "capability.colorTemperature":
691                                 event = open("eventSimulator/colorTemperatureSwitchOffEvent.groovy", "r")
692                         for line in event:
693                                 extractedEvents.write(line)
694                         event.close()
695                 elif eventList[i] == "location": #Case for Location event
696                         #Write three events subsequently
697                         event = open("eventSimulator/locationHomeEvent.groovy", "r")                    
698                         for line in event:
699                                 extractedEvents.write(line)
700                         event.close()
701                         extractedEvents.write("\n\t\t\tbreak\n")
702                         counter = counter + 1
703                         extractedEvents.write("\t\tcase %d:\n" % counter)
704                         event = open("eventSimulator/locationAwayEvent.groovy", "r")
705                         for line in event:
706                                 extractedEvents.write(line)
707                         event.close()
708                         extractedEvents.write("\n\t\t\tbreak\n")
709                         counter = counter + 1
710                         extractedEvents.write("\t\tcase %d:\n" % counter)
711                         event = open("eventSimulator/locationNightEvent.groovy", "r")
712                         for line in event:
713                                 extractedEvents.write(line)
714                         event.close()
715                 elif eventList[i] == "sunrise":
716                         event = open("eventSimulator/locationSunriseEvent.groovy", "r")
717                         for line in event:
718                                 extractedEvents.write(line)
719                         event.close()
720                 elif eventList[i] == "sunset":
721                         event = open("eventSimulator/locationSunsetEvent.groovy", "r")
722                         for line in event:
723                                 extractedEvents.write(line)
724                         event.close()
725                 elif eventList[i] == "sunsetTime":
726                         event = open("eventSimulator/locationSunsetTimeEvent.groovy", "r")
727                         for line in event:
728                                 extractedEvents.write(line)
729                         event.close()
730                 elif eventList[i] == "sunriseTime":
731                         event = open("eventSimulator/locationSunriseTimeEvent.groovy", "r")
732                         for line in event:
733                                 extractedEvents.write(line)
734                         event.close()
735                 elif eventList[i] == "acceleration":
736                         #Write two events subsequently
737                         event = open("eventSimulator/accelerationActiveEvent.groovy", "r")                      
738                         for line in event:
739                                 extractedEvents.write(line)
740                         event.close()
741                         extractedEvents.write("\n\t\t\tbreak\n")
742                         counter = counter + 1
743                         extractedEvents.write("\t\tcase %d:\n" % counter)
744                         event = open("eventSimulator/accelerationInactiveEvent.groovy", "r")
745                         for line in event:
746                                 extractedEvents.write(line)
747                         event.close()
748                 elif eventList[i] == "sleeping":
749                         #Write two events subsequently
750                         event = open("eventSimulator/sleepSleepingEvent.groovy", "r")                   
751                         for line in event:
752                                 extractedEvents.write(line)
753                         event.close()
754                         extractedEvents.write("\n\t\t\tbreak\n")
755                         counter = counter + 1
756                         extractedEvents.write("\t\tcase %d:\n" % counter)
757                         event = open("eventSimulator/sleepNotSleepingEvent.groovy", "r")
758                         for line in event:
759                                 extractedEvents.write(line)
760                         event.close()
761                 elif eventList[i] == "goal":
762                         #Write two events subsequently
763                         event = open("eventSimulator/stepGoalHighEvent.groovy", "r")
764                         for line in event:
765                                 extractedEvents.write(line)
766                         event.close()
767                         extractedEvents.write("\n\t\t\tbreak\n")
768                         counter = counter + 1
769                         extractedEvents.write("\t\tcase %d:\n" % counter)
770                         event = open("eventSimulator/stepGoalLowEvent.groovy", "r")
771                         for line in event:
772                                 extractedEvents.write(line)
773                         event.close()
774                 elif eventList[i] == "steps":
775                         #Write two events subsequently
776                         event = open("eventSimulator/stepStepsHighEvent.groovy", "r")
777                         for line in event:
778                                 extractedEvents.write(line)
779                         event.close()
780                         extractedEvents.write("\n\t\t\tbreak\n")
781                         counter = counter + 1
782                         extractedEvents.write("\t\tcase %d:\n" % counter)
783                         event = open("eventSimulator/stepStepsLowEvent.groovy", "r")
784                         for line in event:
785                                 extractedEvents.write(line)
786                         event.close()
787                 elif eventList[i] == "color":
788                         event = open("eventSimulator/colorChangeEvent.groovy", "r")
789                         for line in event:
790                                 extractedEvents.write(line)
791                         event.close()
792                 elif eventList[i] == "colorTemperature":
793                         event = open("eventSimulator/colorTemperatureEvent.groovy", "r")
794                         for line in event:
795                                 extractedEvents.write(line)
796                         event.close()
797                 elif eventList[i] == "hue":
798                         event = open("eventSimulator/hueChangeEvent.groovy", "r")
799                         for line in event:
800                                 extractedEvents.write(line)
801                         event.close()
802                 elif eventList[i] == "saturation":
803                         event = open("eventSimulator/saturationChangeEvent.groovy", "r")
804                         for line in event:
805                                 extractedEvents.write(line)
806                         event.close()
807                 elif eventList[i] == "energy":
808                         event = open("eventSimulator/energyMeterEvent.groovy", "r")
809                         for line in event:
810                                 extractedEvents.write(line)
811                         event.close()
812                 elif eventList[i] == "power":
813                         #Write two events subsequently
814                         event = open("eventSimulator/powerMeterHighPowerEvent.groovy", "r")
815                         for line in event:
816                                 extractedEvents.write(line)
817                         event.close()
818                         extractedEvents.write("\n\t\t\tbreak\n")
819                         counter = counter + 1
820                         extractedEvents.write("\t\tcase %d:\n" % counter)
821                         event = open("eventSimulator/powerMeterLowPowerEvent.groovy", "r")
822                         for line in event:
823                                 extractedEvents.write(line)
824                         event.close()
825                 elif eventList[i] == "illuminance":
826                         event = open("eventSimulator/illuminanceMeasurementEvent.groovy", "r")
827                         for line in event:
828                                 extractedEvents.write(line)
829                         event.close()
830                 elif eventList[i] == "humidity":
831                         event = open("eventSimulator/humidityHighMeasurementEvent.groovy", "r")
832                         for line in event:
833                                 extractedEvents.write(line)
834                         event.close()
835                         extractedEvents.write("\n\t\t\tbreak\n")
836                         counter = counter + 1
837                         extractedEvents.write("\t\tcase %d:\n" % counter)
838                         event = open("eventSimulator/humidityLowMeasurementEvent.groovy", "r")
839                         for line in event:
840                                 extractedEvents.write(line)
841                         event.close()
842                 elif eventList[i] == "alarm":
843                         #Write four events subsequently
844                         event = open("eventSimulator/alarmBothEvent.groovy", "r")                       
845                         for line in event:
846                                 extractedEvents.write(line)
847                         event.close()
848                         extractedEvents.write("\n\t\t\tbreak\n")
849                         counter = counter + 1
850                         extractedEvents.write("\t\tcase %d:\n" % counter)
851                         event = open("eventSimulator/alarmSirenEvent.groovy", "r")
852                         for line in event:
853                                 extractedEvents.write(line)
854                         event.close()
855                         extractedEvents.write("\n\t\t\tbreak\n")
856                         counter = counter + 1
857                         extractedEvents.write("\t\tcase %d:\n" % counter)
858                         event = open("eventSimulator/alarmStrobeEvent.groovy", "r")
859                         for line in event:
860                                 extractedEvents.write(line)
861                         event.close()
862                         extractedEvents.write("\n\t\t\tbreak\n")
863                         counter = counter + 1
864                         extractedEvents.write("\t\tcase %d:\n" % counter)
865                         event = open("eventSimulator/alarmOffEvent.groovy", "r")
866                         for line in event:
867                                 extractedEvents.write(line)
868                         event.close()
869                 elif eventList[i] == "contact":
870                         #Check which capability
871                         variable = eventVarMap[eventList[i]]
872                         if eventList[i] not in eventVarCounterMap.keys():
873                                 eventVarCounterMap[eventList[i]] = 1
874                                 eventVarCount = 0
875                         else:
876                                 eventVarCount = eventVarCounterMap[eventList[i]]
877                                 eventVarCounterMap[eventList[i]] = eventVarCount + 1
878                         capability = capabilityMap[variable[eventVarCount]]
879                         #Write two events subsequently
880                         if capability == "capability.contactSensor":
881                                 event = open("eventSimulator/contactDefaultClosedEvent.groovy", "r")
882                         elif capability == "capability.valve":
883                                 event = open("eventSimulator/valveClosedEvent.groovy", "r")
884                         for line in event:
885                                 extractedEvents.write(line)
886                         event.close()
887                         extractedEvents.write("\n\t\t\tbreak\n")
888                         counter = counter + 1
889                         extractedEvents.write("\t\tcase %d:\n" % counter)
890                         if capability == "capability.contactSensor":
891                                 event = open("eventSimulator/contactDefaultOpenEvent.groovy", "r")
892                         elif capability == "capability.valve":
893                                 event = open("eventSimulator/valveOpenEvent.groovy", "r")
894                         for line in event:
895                                 extractedEvents.write(line)
896                         event.close()
897                 elif eventList[i] == "status":
898                         #Write three events subsequently
899                         event = open("eventSimulator/musicPlayerPlayingEvent.groovy", "r")                      
900                         for line in event:
901                                 extractedEvents.write(line)
902                         event.close()
903                         extractedEvents.write("\n\t\t\tbreak\n")
904                         counter = counter + 1
905                         extractedEvents.write("\t\tcase %d:\n" % counter)
906                         event = open("eventSimulator/musicPlayerStoppedEvent.groovy", "r")
907                         for line in event:
908                                 extractedEvents.write(line)
909                         event.close()
910                         extractedEvents.write("\n\t\t\tbreak\n")
911                         counter = counter + 1
912                         extractedEvents.write("\t\tcase %d:\n" % counter)
913                         event = open("eventSimulator/musicPlayerPausedEvent.groovy", "r")
914                         for line in event:
915                                 extractedEvents.write(line)
916                         event.close()
917                 elif eventList[i] == "level":
918                         #Check which capability
919                         variable = eventVarMap[eventList[i]]
920                         if eventList[i] not in eventVarCounterMap.keys():
921                                 eventVarCounterMap[eventList[i]] = 1
922                                 eventVarCount = 0
923                         else:
924                                 eventVarCount = eventVarCounterMap[eventList[i]]
925                                 eventVarCounterMap[eventList[i]] = eventVarCount + 1
926                         capability = capabilityMap[variable[eventVarCount]]
927                         if capability == "capability.musicPlayer":
928                                 event = open("eventSimulator/musicPlayerLevelEvent.groovy", "r")
929                         elif capability == "capability.switchLevel":
930                                 event = open("eventSimulator/switchLevelEvent.groovy", "r")
931                         elif capability == "capability.colorControl":
932                                 event = open("eventSimulator/colorLevelChangeEvent.groovy", "r")
933                         elif capability == "capability.switch":
934                                 event = open("eventSimulator/switchLevelChangeEvent.groovy", "r")
935                         for line in event:
936                                 extractedEvents.write(line)
937                         event.close()
938                 elif eventList[i] == "trackDescription":
939                         event = open("eventSimulator/musicPlayerTrackDescriptionEvent.groovy", "r")
940                         for line in event:
941                                 extractedEvents.write(line)
942                         event.close()
943                 elif eventList[i] == "trackData":
944                         event = open("eventSimulator/musicPlayerTrackDataEvent.groovy", "r")
945                         for line in event:
946                                 extractedEvents.write(line)
947                         event.close()
948                 elif eventList[i] == "mute":
949                         #Write two events subsequently
950                         event = open("eventSimulator/musicPlayerUnmutedEvent.groovy", "r")                      
951                         for line in event:
952                                 extractedEvents.write(line)
953                         event.close()
954                         extractedEvents.write("\n\t\t\tbreak\n")
955                         counter = counter + 1
956                         extractedEvents.write("\t\tcase %d:\n" % counter)
957                         event = open("eventSimulator/musicPlayerMutedEvent.groovy", "r")
958                         for line in event:
959                                 extractedEvents.write(line)
960                         event.close()
961                 elif eventList[i] == "temperature":
962                         #Check which capability
963                         variable = eventVarMap[eventList[i]]
964                         if eventList[i] not in eventVarCounterMap.keys():
965                                 eventVarCounterMap[eventList[i]] = 1
966                                 eventVarCount = 0
967                         else:
968                                 eventVarCount = eventVarCounterMap[eventList[i]]
969                                 eventVarCounterMap[eventList[i]] = eventVarCount + 1
970                         capability = capabilityMap[variable[eventVarCount]]
971                         #Write two events subsequently
972                         if capability == "capability.thermostat":
973                                 event = open("eventSimulator/temperatureHighEvent.groovy", "r")
974                         elif capability == "capability.temperatureMeasurement":
975                                 event = open("eventSimulator/temperatureHighMeasurementEvent.groovy", "r")
976                         for line in event:
977                                 extractedEvents.write(line)
978                         event.close()
979                         extractedEvents.write("\n\t\t\tbreak\n")
980                         counter = counter + 1
981                         extractedEvents.write("\t\tcase %d:\n" % counter)
982                         if capability == "capability.thermostat":
983                                 event = open("eventSimulator/temperatureLowEvent.groovy", "r")
984                         elif capability == "capability.temperatureMeasurement":
985                                 event = open("eventSimulator/temperatureLowMeasurementEvent.groovy", "r")
986                         for line in event:
987                                 extractedEvents.write(line)
988                         event.close()
989                 elif eventList[i] == "heatingSetpoint":
990                         event = open("eventSimulator/heatingSetpointEvent.groovy", "r")
991                         for line in event:
992                                 extractedEvents.write(line)
993                         event.close()
994                 elif eventList[i] == "coolingSetpoint":
995                         event = open("eventSimulator/coolingSetpointEvent.groovy", "r")
996                         for line in event:
997                                 extractedEvents.write(line)
998                         event.close()
999                 elif eventList[i] == "thermostatSetpoint":
1000                         event = open("eventSimulator/thermostatSetpointEvent.groovy", "r")
1001                         for line in event:
1002                                 extractedEvents.write(line)
1003                         event.close()
1004                 elif eventList[i] == "threeAxis":
1005                         event = open("eventSimulator/threeAxisChangeEvent.groovy", "r")
1006                         for line in event:
1007                                 extractedEvents.write(line)
1008                         event.close()
1009                         
1010                 ###TODO: Add more events later
1011                 extractedEvents.write("\n\t\t\tbreak\n")
1012                 counter = counter + 1
1013         extractedEvents.write("\t}\n")
1014         extractedEvents.write("}\n")
1015         
1016 def CheckIfOnlyTouchEvents():
1017         #Check and throw an error if it is all touch events
1018         #This is called Direct-Direct interaction and we do not model-check for this case
1019         onlyTouchEvents = True
1020         for item in eventList:
1021                 if item != "nfcTouch" and item != "app":
1022                         onlyTouchEvents = False
1023         if onlyTouchEvents is True and app1Subscribe is True and app2Subscribe is True:
1024                 # Write error log file
1025                 extractError = open("appCreationError.log", "w+")
1026                 extractError.write("Direct-Direct Interaction detected: we are skipping this pair...\n")
1027                 extractError.close()
1028                 raise Exception("\n\nDirect-Direct Interaction detected: we are skipping this pair...\n\n")
1029
1030
1031 #Extract objects to call functions from App1
1032 F1 = open("Extractor/App1/App1.groovy", "r")
1033 extractedFunctionsApp1 = open("Extractor/App1/extractedFunctionsApp1.groovy", "w+")
1034 ExtractFunctions(F1, "App1")
1035 F1.close()
1036
1037 #Extract objects to call functions from App2
1038 F2 = open("Extractor/App2/App2.groovy", "r")
1039 extractedFunctionsApp2 = open("Extractor/App2/extractedFunctionsApp2.groovy", "w+")
1040 ExtractFunctions(F2, "App2")
1041 F2.close()
1042
1043 #Prepare eventSimulator file while parsing the App1 and App2 files
1044 extractedEvents = open("eventSimulator/eventSimulator.groovy", "w+")
1045 CheckIfOnlyTouchEvents()
1046 ExtractEvents(extractedEvents)
1047 extractedEvents.close()
1048
1049 #Save the extracted methods and app1 in a same file to extract information
1050 extractorFile = open("Extractor/extractorFile.groovy", "w+")
1051 Extractor = open("Extractor/Extractor.groovy", "r")
1052 F1 = open("Extractor/App1/App1.groovy", "r")
1053
1054 extractorFile.write("////////////////////\n")
1055 extractorFile.write("@Field App\n")
1056 extractorFile.write("App = \"App1\"")
1057 extractorFile.write("\n")
1058 for line in Extractor:
1059         extractorFile.write(line)
1060 extractorFile.write("\n\n")
1061 for line in F1:
1062         extractorFile.write(line)
1063 extractorFile.close()
1064 Extractor.close()
1065 F1.close()
1066 #Run the file to extract the objects
1067 os.system("groovy -classpath lib/jpf.jar Extractor/extractorFile.groovy")
1068
1069
1070 #Save the extracted methods and app2 in a same file to extract information
1071 extractorFile = open("Extractor/extractorFile.groovy", "w+")
1072 Extractor = open("Extractor/Extractor.groovy", "r")
1073 F2 = open("Extractor/App2/App2.groovy", "r")
1074
1075 extractorFile.write("////////////////////\n")
1076 extractorFile.write("@Field App\n")
1077 extractorFile.write("App = \"App2\"")
1078 extractorFile.write("\n")
1079 for line in Extractor:
1080         extractorFile.write(line)
1081 extractorFile.write("\n\n")
1082 for line in F2:
1083         extractorFile.write(line)
1084 #Run the file to extract the objects
1085 extractorFile.close()
1086 Extractor.close()
1087 F2.close()
1088 os.system("groovy -classpath lib/jpf.jar Extractor/extractorFile.groovy")
1089
1090