-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServiceHandler.py
More file actions
131 lines (106 loc) · 4 KB
/
ServiceHandler.py
File metadata and controls
131 lines (106 loc) · 4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import VikaSyntax
import socket
import sys
from typing import cast
import requests
import json
from zeroconf import ServiceBrowser, Zeroconf
'''Represent a configured action on a device'''
class VikaAction:
def __init__(self, url: str, verbs : list(), localisation: str, objects: list()):
self.Url = url
self.Verbs = verbs
self.Localisation = localisation
self.Objects = objects
def PrintAction(self):
print()
print("url:", self.Url)
print("verbs")
print(*self.Verbs)
print("loc")
print(*self.Localisation)
print("obj")
print(*self.Objects)
print()
'''returns the matching percentage between a syntax and this action'''
def Match(self, syntax: VikaSyntax) -> int:
print("passed syntax")
syntax.PrintSyntax()
score = 0
maxScore = len(syntax.verbs) + len(syntax.localisation) + len(syntax.objects)
print("maxScore:", maxScore)
self.PrintAction()
for verb in syntax.verbs:
for actionVerb in self.Verbs:
if(verb["r"] in actionVerb):
score += 1
for loc in syntax.localisation:
for actionLoc in self.Localisation:
if(loc["n"] in actionLoc):
score += 1
for obj in syntax.objects:
for actionObj in self.Objects:
if(obj["n"] in actionObj):
score += 1
print("match score:", score)
return (score*100)/maxScore
'''represent a device that has actions and a config'''
class VikaDevice:
def __init__(self, addr):
self.address = addr
self.actions = list()
self.HasBeenInit = False
'''Copies a devices config'''
def GetConfig(self):
ipstr = str(self.address[0]) + "." + str(self.address[1]) + "." + str(self.address[2]) + "." + str(self.address[3])
self.address = ipstr
configUrl = "http://" + ipstr + "/getConfig"
config = requests.get(configUrl)
strconfig = json.dumps(config.json())
jsonConfig = json.loads(strconfig)
for action in jsonConfig["a"]:
self.actions.append(VikaAction(action["url"],
action["verbs"],
action["loc"],
action["obj"]))
self.HasBeenInit = True
print("config has been read")
'''Handles the devices on the network'''
class DeviceHandler:
def __init__(self):
self.devices = list()
self.zeroconf = Zeroconf()
self.serviceBrowser = ServiceBrowser(self.zeroconf, "_vika._tcp.local.", self)
def InitDevices(self):
for d in self.devices:
if(not d.HasBeenInit):
d.GetConfig()
'''Tries to match the given syntax to a devices config'''
def MatchSyntax(self, syntax: VikaSyntax) -> str:
for d in self.devices:
for action in d.actions:
if(action.Match(syntax) >= 50):
deviceUrl = "http://" + d.address + action.Url
print("match found with url :", deviceUrl)
return deviceUrl
else:
print("no match found")
'''called when a device disconnects'''
def remove_service(self, zeroconf, type, name):
print("service removed")
for d in self.devices:
if(d.name == name):
self.devices.remove(d)
'''called when a device connects, adds the device to the deviceHandler'''
def add_service(self, zeroconf, type, name):
info = zeroconf.get_service_info(type,name)
addr = info.address
intaddr = list()
for i in addr:
intaddr.append(i)
addresses = [device for device in self.devices]
if(intaddr not in addresses):
self.devices.append(VikaDevice(intaddr))
else:
print("duplicate device")
self.InitDevices()