Skip to content

Commit 979c742

Browse files
committed
Use mutex when updating device info.
1 parent 6d12b0d commit 979c742

File tree

1 file changed

+86
-79
lines changed

1 file changed

+86
-79
lines changed

myDevices/devices/manager.py

Lines changed: 86 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import os.path
33
import json as JSON
44
from time import sleep, time
5+
from threading import RLock
56
from myDevices.utils import logger
67
from myDevices.utils import types
78
from myDevices.utils.config import Config
@@ -12,6 +13,9 @@
1213
PACKAGES = [serial, digital, analog, sensor, shield]
1314
DYNAMIC_DEVICES = {}
1415
DEVICES_JSON_FILE = "/etc/myDevices/devices.json"
16+
17+
mutex = RLock()
18+
1519
def deviceDetector():
1620
logger.debug('deviceDetector')
1721
try:
@@ -28,7 +32,6 @@ def deviceDetector():
2832
saveDevice(dev['name'], int(time()))
2933
except Exception as e:
3034
logger.error("Device detector: %s" % e)
31-
# sleep(5)
3235

3336
def findDeviceClass(name):
3437
for package in PACKAGES:
@@ -47,34 +50,36 @@ def findDeviceClass(name):
4750
return None
4851

4952
def saveDevice(name, install_date):
50-
logger.debug('saveDevice: ' + str(name))
51-
if name not in DEVICES:
52-
return
53-
#never save to json devices that are manually added
54-
if DEVICES[name]['origin'] == 'manual':
55-
return
56-
DYNAMIC_DEVICES[name] = DEVICES[name]
57-
DEVICES[name]['install_date'] = install_date
58-
json_devices = getJSON(DYNAMIC_DEVICES)
59-
with open(DEVICES_JSON_FILE, 'w') as outfile:
60-
outfile.write(json_devices)
53+
with mutex:
54+
logger.debug('saveDevice: ' + str(name))
55+
if name not in DEVICES:
56+
return
57+
#never save to json devices that are manually added
58+
if DEVICES[name]['origin'] == 'manual':
59+
return
60+
DYNAMIC_DEVICES[name] = DEVICES[name]
61+
DEVICES[name]['install_date'] = install_date
62+
json_devices = getJSON(DYNAMIC_DEVICES)
63+
with open(DEVICES_JSON_FILE, 'w') as outfile:
64+
outfile.write(json_devices)
6165

6266
def removeDevice(name):
63-
if name in DEVICES:
64-
if name in DYNAMIC_DEVICES:
65-
if hasattr(DEVICES[name]["device"], 'close'):
66-
DEVICES[name]["device"].close()
67-
del DEVICES[name]
68-
del DYNAMIC_DEVICES[name]
69-
json_devices = getJSON(DYNAMIC_DEVICES)
70-
with open(DEVICES_JSON_FILE, 'w') as outfile:
71-
outfile.write(json_devices)
72-
logger.debug("Deleted device %s" % name)
73-
return (200, None, None)
74-
logger.error("Cannot delete %s, found but not added via REST" % name)
75-
return (403, None, None)
76-
logger.error("Cannot delete %s, not found" % name)
77-
return (404, None, None)
67+
with mutex:
68+
if name in DEVICES:
69+
if name in DYNAMIC_DEVICES:
70+
if hasattr(DEVICES[name]["device"], 'close'):
71+
DEVICES[name]["device"].close()
72+
del DEVICES[name]
73+
del DYNAMIC_DEVICES[name]
74+
json_devices = getJSON(DYNAMIC_DEVICES)
75+
with open(DEVICES_JSON_FILE, 'w') as outfile:
76+
outfile.write(json_devices)
77+
logger.debug("Deleted device %s" % name)
78+
return (200, None, None)
79+
logger.error("Cannot delete %s, found but not added via REST" % name)
80+
return (403, None, None)
81+
logger.error("Cannot delete %s, not found" % name)
82+
return (404, None, None)
7883

7984

8085
def addDeviceJSON(json):
@@ -105,57 +110,58 @@ def addDeviceJSON(json):
105110
return (500, "ERROR", "text/plain")
106111

107112
def updateDevice(name, json):
108-
if not name in DEVICES:
109-
return (404, None, None)
113+
with mutex:
114+
if not name in DEVICES:
115+
return (404, None, None)
110116

111-
if "name" in json:
112-
# forbid name changed
113-
# if json["name"] != name:
114-
# return (403, "FORBIDDEN", "text/plain")
115-
116-
if json["name"] != name and json["name"] in DEVICES:
117-
return (403, "ALREADY_EXISTS", "text/plain")
117+
if "name" in json:
118+
# forbid name changed
119+
# if json["name"] != name:
120+
# return (403, "FORBIDDEN", "text/plain")
118121

119-
logger.info("Edit %s" % name)
120-
(c, d, t) = removeDevice(name)
121-
if c == 200:
122-
(c, d, t) = addDeviceJSON(json)
123-
124-
return (c, d, t)
122+
if json["name"] != name and json["name"] in DEVICES:
123+
return (403, "ALREADY_EXISTS", "text/plain")
125124

125+
logger.info("Edit %s" % name)
126+
(c, d, t) = removeDevice(name)
127+
if c == 200:
128+
(c, d, t) = addDeviceJSON(json)
129+
130+
return (c, d, t)
126131

127132
def addDevice(name, device, description, args, origin):
128-
if name in DEVICES:
129-
logger.error("Device <%s> already exists" % name)
130-
return -1
131-
logger.debug('addDevice: ' + str(name) + ' ' + str(device))
132-
# if '/' in device:
133-
# deviceClass = device.split('/')[0]
134-
# else:
135-
# deviceClass = device
136-
try:
137-
constructor = findDeviceClass(device)
138-
except Exception as ex:
139-
logger.debug('findDeviceClass failure:' + str(ex))
140-
return 0
141-
logger.debug('constructor class found ' + str(constructor))
142-
if constructor == None:
143-
raise Exception("Device driver not found for %s" % device)
133+
with mutex:
134+
if name in DEVICES:
135+
logger.error("Device <%s> already exists" % name)
136+
return -1
137+
logger.debug('addDevice: ' + str(name) + ' ' + str(device))
138+
# if '/' in device:
139+
# deviceClass = device.split('/')[0]
140+
# else:
141+
# deviceClass = device
142+
try:
143+
constructor = findDeviceClass(device)
144+
except Exception as ex:
145+
logger.debug('findDeviceClass failure:' + str(ex))
146+
return 0
147+
logger.debug('constructor class found ' + str(constructor))
148+
if constructor == None:
149+
raise Exception("Device driver not found for %s" % device)
144150

145-
instance = None
146-
try:
147-
if len(args) > 0:
148-
instance = constructor(**args)
149-
else:
150-
instance = constructor()
151-
logger.debug('Adding instance ' + str(instance))
152-
addDeviceInstance(name, device, description, instance, args, origin)
153-
return 1
154-
except Exception as e:
155-
logger.error("Error while adding device %s(%s) : %s" % (name, device, e))
156-
# addDeviceInstance(name, device, description, None, args, origin)
157-
removeDevice(name)
158-
return 0
151+
instance = None
152+
try:
153+
if len(args) > 0:
154+
instance = constructor(**args)
155+
else:
156+
instance = constructor()
157+
logger.debug('Adding instance ' + str(instance))
158+
addDeviceInstance(name, device, description, instance, args, origin)
159+
return 1
160+
except Exception as e:
161+
logger.error("Error while adding device %s(%s) : %s" % (name, device, e))
162+
# addDeviceInstance(name, device, description, None, args, origin)
163+
removeDevice(name)
164+
return 0
159165

160166
def addDeviceConf(devices, origin):
161167
for (name, params) in devices:
@@ -222,13 +228,14 @@ def addDeviceInstance(name, device, description, instance, args, origin):
222228
}
223229

224230
def closeDevices():
225-
devices = [k for k in DEVICES.keys()]
226-
for name in devices:
227-
device = DEVICES[name]["device"]
228-
logger.debug("Closing device %s - %s" % (name, device))
229-
del DEVICES[name]
230-
if hasattr(device, 'close'):
231-
device.close()
231+
with mutex:
232+
devices = [k for k in DEVICES.keys()]
233+
for name in devices:
234+
device = DEVICES[name]["device"]
235+
logger.debug("Closing device %s - %s" % (name, device))
236+
del DEVICES[name]
237+
if hasattr(device, 'close'):
238+
device.close()
232239

233240
def getJSON(devices_list):
234241
return types.jsonDumps(getDeviceList(devices_list))

0 commit comments

Comments
 (0)