forked from victronenergy/localsettings
-
Notifications
You must be signed in to change notification settings - Fork 0
/
localsettings.py
executable file
·949 lines (811 loc) · 29.6 KB
/
localsettings.py
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
#!/usr/bin/python -u
## @package localsettings
#
#Gregory Burte - 3 MArch 2019
#Update of logging system
#
#Requires Logging
#
# Previous comments
#---------------------
# Dbus-service for local settings.
#
# Below code needs a major check and cleanup. A not complete list would be:
# - get rid of the tracing, just use the standard logging modul, as also done in dbus_conversions for example
# - use argparse.ArgumentParser, so get rid of usage()
# - probably remove a lot of dbus code and replace by items from vedbus.py from velib_python
# The local-settings-dbus-service provides the local-settings storage in non-volatile-memory.
# The settings are stored in the settings.xml file. At startup the xml file is parsed and
# the dbus-service with his paths are created from the content of the xml file.
# <Settings></Settings> is the root of the xml file. A group of settings is a child of
# the root <Settings>. A setting is a child of a group and contains text.
# (e.g. <LogInterval>900</LogInterval>.
# Example 1:
# <Settings>
# <Logging>
# <LogInterval>900</LogInterval>
# </Logging>
# </Settings>
# This will be parsed as an dbus-object-path /Settings/Logging/LogInterval.
#
# These are set as an attribute of a (setting) element.
# Example 2: <Brigthness type="i" min="0" max="100" default="100">100</Brigthness>
# Example 3: <LogInterval type="i" min="5" default="900">900</LogInterval>
# Example 4: <LogPath type="s" default=".">.</LogPath>
# Settings or a group of settings can be set to default. A setting (and group) can be
# added by means of dbus. And of course a setting can be changed by means of dbus.
# By means of the file settingchanges.xml settings can be added or deleted. This file
# is processed at startup and then deleted.
#
##
# Python imports
from dbus.mainloop.glib import DBusGMainLoop
import dbus
import dbus.service
from gobject import timeout_add, source_remove, MainLoop
from os import path, getpid, remove, rename, _exit, environ
import sys
import signal
from lxml import etree
import getopt
import errno
import platform
import os
import re
# Victron imports
sys.path.insert(1, os.path.join(os.path.dirname(__file__), './ext/velib_python'))
#import tracing
import logging
## Major version.
FIRMWARE_VERSION_MAJOR = 0x01
## Minor version.
FIRMWARE_VERSION_MINOR = 0x0c
## Localsettings version.
version = (FIRMWARE_VERSION_MAJOR << 8) | FIRMWARE_VERSION_MINOR
## Traces (info / debug) setup
pathTraces = '/log/'
traceFileName = 'localsettingstraces'
tracingEnabled = False
traceToConsole = False
traceToFile = False
traceDebugOn = False
## The dbus bus and bus-name.
bus = None
busName = None
## Dbus service name and interface name(s).
dbusName = 'com.victronenergy.settings'
InterfaceBusItem = 'com.victronenergy.BusItem'
InterfaceSettings = 'com.victronenergy.Settings'
## The dictonary's containing the settings and the groups.
settings = {}
groups = []
## Index values for settings.
VALUE = 0
ATTRIB = 1
## ATTRIB keywords.
TYPE='type'
MIN='min'
MAX='max'
DEFAULT='default'
SILENT='silent'
## Supported types for convert xml-text to value.
supportedTypes = {
'i':int,
's':str,
'f':float,
}
## The list of MyDbusService(s).
myDbusServices = []
myDbusGroupServices = []
## The main group dbus-service.
myDbusMainGroupService = None
## Save settings timeout.
timeoutSaveSettingsEventId = None
timeoutSaveSettingsTime = 2 # Timeout value in seconds.
## File names.
fileSettings = 'settings.xml'
fileSettingChanges = 'settingchanges.xml'
newFileExtension = '.new'
newFileSettings = fileSettings + newFileExtension
## Path(s) definitions.
pathSettings = '/data/conf/'
sysSettingsDir = '/etc/venus/settings.d'
## Settings file version tag, encoding and root-element.
settingsTag = 'version'
settingsVersion = '1.0'
settingsEncoding = 'UTF-8'
settingsRootName = 'Settings'
## Indicates if settings are added
settingsAdded = False
class MyDbusObject(dbus.service.Object):
global InterfaceBusItem
## Constructor of MyDbusObject
#
# Creates the dbus-object under the given bus-name (dbus-service-name).
# @param busName Return value from dbus.service.BusName, see run()).
# @param objectPath The dbus-object-path (e.g. '/Settings/Logging/LogInterval').
def __init__(self, busName, objectPath):
dbus.service.Object.__init__(self, busName, objectPath)
## Dbus method GetValue
# Returns the value of the dbus-object-path (the settings).
# When the object-path is a group a -1 is returned.
# @return setting A setting value or -1 (error)
@dbus.service.method(InterfaceBusItem, out_signature = 'v')
def GetValue(self):
global settings
global groups
logger.debug('GetValue %s' % self._object_path)
if self._object_path in groups:
prefix = self._object_path + '/'
return dbus.Dictionary({ k[len(prefix):]: v[VALUE] \
for k, v in settings.iteritems() \
if k.startswith(prefix) and len(k)>len(prefix) },
signature=dbus.Signature('sv'), variant_level=1)
return settings[self._object_path][VALUE]
## Dbus method GetText
# Returns the value as string of the dbus-object-path (the settings).
# When the object-path is a group a '' is returned.
# @return setting A setting value or '' (error)
@dbus.service.method(InterfaceBusItem, out_signature = 's')
def GetText(self):
global settings
if self._object_path in groups:
return ''
return str(settings[self._object_path][VALUE])
## Dbus method SetValue
# Sets the value of a setting. When the type of the setting is a integer or float,
# the new value is checked according to minimum and maximum.
# @param value The new value for the setting.
# @return completion-code When successful a 0 is return, and when not a -1 is returned.
@dbus.service.method(InterfaceBusItem, in_signature = 'v', out_signature = 'i')
def SetValue(self, value):
global settings
global supportedTypes
logger.debug('SetValue %s' % self._object_path)
if self._object_path in groups:
return -1
okToSave = True
v = value
path = self._object_path
if TYPE in settings[path][ATTRIB]:
itemType = settings[path][ATTRIB][TYPE]
if itemType in supportedTypes:
try:
v = convertToType(itemType, value)
if MIN in settings[path][ATTRIB]:
if v < convertToType(itemType, settings[path][ATTRIB][MIN]):
okToSave = False
if MAX in settings[path][ATTRIB]:
if v > convertToType(itemType, settings[path][ATTRIB][MAX]):
okToSave = False
except:
okToSave = False
if okToSave == True:
if v != settings[path][VALUE]:
self._setValue(v)
return 0
else:
return -1
@dbus.service.method(InterfaceBusItem, out_signature = 'v')
def GetMin(self):
if MIN in settings[self._object_path][ATTRIB]:
return settings[self._object_path][ATTRIB][MIN]
else:
return 0
@dbus.service.method(InterfaceBusItem, out_signature = 'v')
def GetMax(self):
if MAX in settings[self._object_path][ATTRIB]:
return settings[self._object_path][ATTRIB][MAX]
else:
return 0
## Sets the value and starts the time-out for saving to the settings-xml-file.
# @param value The new value for the setting.
def _setValue(self, value, printLog=True, sendAttributes=False):
global settings
if printLog and settings[self._object_path][ATTRIB].get(SILENT) != 'True':
logger.info('Setting %s changed. Old: %s, New: %s' % (self._object_path, settings[self._object_path][VALUE], value))
settings[self._object_path][VALUE] = value
self._startTimeoutSaveSettings()
text = self.GetText()
change = {'Value':value, 'Text':text}
if sendAttributes:
change.update({'Min': self.GetMin(), 'Max': self.GetMax(), 'Default': self.GetDefault()})
self.PropertiesChanged(change)
@dbus.service.signal(InterfaceBusItem, signature = 'a{sv}')
def PropertiesChanged(self, changes):
logger.debug('signal PropertiesChanged')
@dbus.service.signal(InterfaceSettings, signature = '')
def ObjectPathsChanged(self):
logger.debug('signal ObjectPathsChanged')
## Method for starting the time-out for saving to the settings-xml-file.
# (Re)Starts the time-out. When after x time no settings are changed,
# the settings-xml-file is saved.
def _startTimeoutSaveSettings(self):
global timeoutSaveSettingsEventId
global timeoutSaveSettingsTime
if timeoutSaveSettingsEventId is not None:
source_remove(timeoutSaveSettingsEventId)
timeoutSaveSettingsEventId = None
timeoutSaveSettingsEventId = timeout_add(timeoutSaveSettingsTime*1000, saveSettingsCallback)
## Dbus method GetDefault.
# Returns the default value of a setting.
# @return value The default value or -1 (error or no default set)
@dbus.service.method(InterfaceBusItem, out_signature = 'v')
def GetDefault(self):
global settings
logger.info('GetDefault %s' % self._object_path)
path = self._object_path
if path in groups:
return -1
try:
type = settings[path][ATTRIB][TYPE]
value = convertToType(type, settings[path][ATTRIB][DEFAULT])
return value
except:
logger.error('Could not get default for %s %s' % (path, settings[path][ATTRIB].items()))
return -1
## Dbus method SetDefault.
# Sets the value of the setting to default. When the object-path is a group,
# it sets the default for all the settings in that group.
# @return completion-code When successful a 0 is return, and when not a -1 is returned.
@dbus.service.method(InterfaceBusItem, out_signature = 'i')
def SetDefault(self):
global myDbusServices
global settings
logger.info('SetDefault %s' % self._object_path)
try:
path = self._object_path
if path in groups:
for service in myDbusServices:
servicePath = service._object_path
if path in servicePath:
service.SetValue(settings[servicePath][ATTRIB][DEFAULT])
else:
self.SetValue(settings[path][ATTRIB][DEFAULT])
return 0
except:
logger.error('Could not set default for %s %s' % (path, settings[path][ATTRIB].items()))
return -1
## Dbus method GetSilent.
# @return 1 if the setting is silent, 0 is it is not. Returns -1 if an error occurred.
@dbus.service.method(InterfaceBusItem, out_signature = 'i')
def GetSilent(self):
global settings
logger.debug('GetSilent %s' % self._object_path)
path = self._object_path
if path in groups:
return -1
try:
value = settings[path][ATTRIB].get(SILENT) == 'True'
return 1 if value else 0
except:
logger.error('Could not get silent property for %s %s' % (path, settings[path][ATTRIB].items()))
return -1
## Dbus method AddSetting.
# Add a new setting by the given parameters. The object-path must be a group.
# Example 1: dbus /Settings AddSetting Groupname Settingname 100 i 0 100
# Example 2: dbus /Settings AddSetting Groupname Settingname '/home/root' s 0 0
# When the new setting is of type string the minimum and maximum will be ignored.
# @param group The group-name.
# @param name The setting-name.
# @param defaultValue The default value (and initial value) of the setting.
# @param itemType Types 's' string, 'i' integer or 'f' float.
# @param minimum The minimum value.
# @param maximum The maximum value.
# @return completion-code When successful a 0 is return, and when not a -1 is returned.
@dbus.service.method(InterfaceSettings, in_signature = 'ssvsvv', out_signature = 'i')
def AddSetting(self, group, name, defaultValue, itemType, minimum, maximum):
return self.addSetting(group, name, defaultValue, itemType, minimum, maximum, silent=False)
@dbus.service.method(InterfaceSettings, in_signature = 'ssvsvv', out_signature = 'i')
def AddSilentSetting(self, group, name, defaultValue, itemType, minimum, maximum):
return self.addSetting(group, name, defaultValue, itemType, minimum, maximum, silent=True)
def addSetting(self, group, name, defaultValue, itemType, minimum, maximum, silent):
global groups
global settings
global defaults
global busName
global myDbusGroupServices
global myDbusServices
global settingsAdded
logger.debug('AddSetting %s %s %s' % (self._object_path, group, name))
if self._object_path not in groups:
return -1
if group.startswith('/') or group == '':
groupPath = self._object_path + str(group)
else:
groupPath = self._object_path + '/' + str(group)
if name.startswith('/'):
itemPath = groupPath + str(name)
else:
itemPath = groupPath + '/' + str(name)
# A prefixing underscore is an escape char: don't allow it in a normal path
if "/_" in itemPath:
return -2
if itemType not in supportedTypes:
return -3
try:
value = convertToType(itemType, defaultValue)
if type(value) != str:
min = convertToType(itemType, minimum)
max = convertToType(itemType, maximum)
if min is 0 and max is 0:
attributes = {TYPE:str(itemType), DEFAULT:str(value)}
elif value >= min and value <= max:
attributes = {TYPE:str(itemType), DEFAULT:str(value), MIN:str(min), MAX:str(max)}
else:
attributes = {TYPE:str(itemType), DEFAULT:str(value)}
attributes[SILENT] = str(silent)
except:
return -4
if not groupPath in groups:
groups.append(groupPath)
groupObject = MyDbusObject(busName, groupPath)
myDbusGroupServices.append(groupObject)
if not itemPath in settings:
# New setting
myDbusObject = MyDbusObject(busName, itemPath)
myDbusServices.append(myDbusObject)
else:
# Existing setting
if settings[itemPath][ATTRIB][TYPE] != attributes[TYPE]:
return -5
for service in myDbusServices:
if service._object_path == itemPath:
myDbusObject = service
break
unmatched = set(settings[itemPath][ATTRIB].items()) ^ set(attributes.items())
if len(unmatched) == 0:
# There are no changes
return 0
# There are changes, save them while keeping the current value.
value = settings[itemPath][VALUE]
settings[itemPath] = [0, {}]
settings[itemPath][ATTRIB] = attributes
logger.info('Added new setting %s. default:%s, type:%s, min:%s, max: %s, silent: %s' % (itemPath, defaultValue, itemType, minimum, maximum, silent))
myDbusObject._setValue(value, printLog=False, sendAttributes=True)
settingsAdded = True
self._startTimeoutSaveSettings()
return 0
# This object will send an overview of all available D-Bus paths and their values in the settings service if
# a GetValue or GetText is issued on the root of the service.
class RootObject(dbus.service.Object):
def __init__(self, busname):
dbus.service.Object.__init__(self, busname, '/')
@dbus.service.method(InterfaceBusItem, out_signature='v')
def GetValue(self):
values = dict((k[1:], v[VALUE]) for k, v in settings.iteritems())
return dbus.Dictionary(values, signature=dbus.Signature('sv'), variant_level=1)
@dbus.service.method(InterfaceBusItem, out_signature='v')
def GetText(self):
values = dict((k[1:], str(v[VALUE])) for k, v in settings.iteritems())
return values
## The callback method for saving the settings-xml-file.
# Calls the parseDictonaryToXmlFile with the dictonary settings and settings-xml-filename.
def saveSettingsCallback():
global timeoutSaveSettingsEventId
global settings
global fileSettings
global myDbusMainGroupService
global settingsAdded
logger.debug('Saving settings to file')
source_remove(timeoutSaveSettingsEventId)
timeoutSaveSettingsEventId = None
parseDictonaryToXmlFile(settings, fileSettings)
if settingsAdded is True:
settingsAdded = False
myDbusMainGroupService.ObjectPathsChanged()
## Method for converting a value the the given type.
# When the type is not supported it simply returns the value as is.
# @param type The type to convert to.
# @param value The value to convert.
# @return value The converted value (if type is supported).
def convertToType(type, value):
if type in supportedTypes:
return supportedTypes[type](value)
else:
return value
## Method for parsing the file to the given dictonary.
# The dictonary will be in following format {dbus-object-path, [value, {attributes}]}.
# When a array is given for the groups, the found groups are appended.
# @param file The filename (path can be included, e.g. /home/root/localsettings/settings.xml).
# @param dictonaryItems The dictonary for the settings.
# @param arrayGroups The array for the groups.
# @param filter A filter used for filtering in settingchanges.xml for example "Add".
def parseXmlFileToDictonary(file, dictonaryItems, arrayGroups, filter):
parser = etree.XMLParser(remove_blank_text=True)
tree = etree.parse(file, parser)
root = tree.getroot()
logger.debug('parseXmlFileToDictonary %s:' % file)
docinfo = tree.docinfo
logger.debug("docinfo version %s" % docinfo.xml_version)
logger.debug("docinfo encoding %s" % docinfo.encoding)
logger.debug("settings version %s" % root.attrib)
logger.debug(etree.tostring(root))
parseXmlToDictonary(root, '/', dictonaryItems, arrayGroups, filter)
## Method for parsing a xml-element to a dbus-object-path.
# The dbus-object-path can be a setting (contains a text-value) or
# a group. Only for a setting attributes are added.
# @param element The lxml-Element from the ElementTree API.
# @param path The path of the element.
# @param dictonaryItems The dictonary for the settings.
# @param arrayGroups The array for the groups.
# @param filter A filter used for filtering in settingchanges.xml for example "Add".
def parseXmlToDictonary(element, path, dictonaryItems, arrayGroups, filter):
if path != '/':
path += '/'
path += element.tag
for child in element:
parseXmlToDictonary(child, path, dictonaryItems, arrayGroups, filter)
if filter == None or path.startswith(filter) == True:
if filter != None:
objectPath = path.replace(filter, '')
if objectPath == '':
return
else:
objectPath = path
# Remove possible underscore prefix
objectPath = objectPath.replace("/_", "/")
if element.get('type') != None:
elementType = element.attrib[TYPE]
text = element.text
if not element.text:
text = ''
value = convertToType(elementType, text)
dictonaryItems[objectPath] = [value, element.attrib]
elif arrayGroups != None:
if not objectPath in arrayGroups:
arrayGroups.append(objectPath)
## Method for parsing a dictonary to a giving xml-file.
# The dictonary must be in following format {dbus-object-path, [value, {attributes}]}.
# @param dictonary The dictonary with the settings.
# @param file The filename.
def parseDictonaryToXmlFile(dictonary, file):
logger.debug('parseDictonaryToXmlFile %s' % file)
root = None
for key in list(dictonary):
items = key.split('/')
items.remove('')
if root == None:
root = etree.Element(items[0])
root.set(settingsTag, settingsVersion)
tree = etree.ElementTree(root)
items.remove(root.tag)
elem = root
for item in items:
# Prefix items starting with a digit, because an XML element cannot start with a digit.
if item[0].isdigit():
item = "_" + item
foundElem = elem.find(item)
if foundElem == None:
elem = etree.SubElement(elem, item)
else:
elem = foundElem
elem.text = str(dictonary[key][VALUE])
attributes = dictonary[key][ATTRIB]
for attribute, value in attributes.iteritems():
elem.set(attribute, str(value))
newFile = file + newFileExtension
with open(newFile, 'wb') as fp:
tree.write(fp, encoding = settingsEncoding, pretty_print = True, xml_declaration = True)
fp.flush()
os.fsync(fp.fileno())
try:
rename(newFile, file)
except:
logger.error('renaming new file to settings file failed')
def to_bool(val):
if type(val) != str:
return bool(val)
try:
return bool(int(val))
except:
pass
return val.lower() == 'true'
## Validate and convert to value + attributes
def makeDictEntry(val, itemtype, minval, maxval, silent):
if itemtype not in supportedTypes:
raise Exception('invalid type')
val = convertToType(itemtype, val)
attrs = { TYPE: itemtype, DEFAULT: str(val) }
if type(val) != str:
minval = convertToType(itemtype, minval)
maxval = convertToType(itemtype, maxval)
if minval or maxval:
if val < minval or val > maxval:
raise Exception('default value out of range')
attrs[MIN] = str(minval)
attrs[MAX] = str(maxval)
silent = to_bool(silent)
attrs[SILENT] = str(silent)
return val, attrs
## Load settings from text file
def loadSettingsFile(name, dictionary):
with open(name, 'r') as f:
for line in f:
v = re.sub('#.*', '', line).strip().split()
if not v:
continue
try:
path = v[0]
defval = v[1]
itemtype = v[2]
except:
raise Exception('syntax error: ' + line)
minval = v[3] if len(v) > 3 else 0
maxval = v[4] if len(v) > 4 else 0
silent = v[5] if len(v) > 5 else 0
val, attrs = makeDictEntry(defval, itemtype,
minval, maxval, silent)
path = '/Settings/' + path.lstrip('/')
dictionary[path] = [val, attrs]
## Load settings from each file in dir
def loadSettingsDir(path, dictionary):
try:
names = os.listdir(path)
except:
return
for name in names:
filename = os.path.join(path, name)
try:
loadSettingsFile(filename, dictionary)
except Exception as ex:
logger.error('error loading %s: %s' %
(filename, str(ex)))
## Handles the system (Linux / Windows) signals such as SIGTERM.
#
# Stops the logscript with an exit-code.
# @param signum the signal-number.
# @param stack the call-stack.
def handlerSignals(signum, stack):
logger.warning('handlerSignals received: %d' % signum)
exitCode = 0
if signum == signal.SIGHUP:
exitCode = 1
exit(exitCode)
# Migration code.
# TODO ideally this should be elsewhere.
def delete_from_tree(tree, path):
obj = tree.xpath(path)
if not obj:
return
obj[0].getparent().remove(obj[0])
## Migrate old canbus settings
def migrate_can_profile(tree):
global fileSettings
global newFileSettings
if not os.path.isfile("/etc/venus/canbus_ports"):
return
with open('/etc/venus/canbus_ports', 'r') as f:
iflist = f.readline().split(None, 1)
if not iflist:
return
interface = iflist[0]
path = "/Settings/Canbus/" + interface + "/Profile"
if tree.xpath(path):
return
# default to Ve.Can
profile = 1
if tree.xpath("/Settings/Services/LgResu/text()") == ["1"]:
profile = 3
elif tree.xpath("/Settings/Services/OceanvoltMotorDrive/text()") == ["1"] or \
tree.xpath("/Settings/Services/OceanvoltValence/text()") == ["1"]:
profile = 4
elif tree.xpath("/Settings/Services/VeCan/text()") == ["0"]:
profile = 0
print("Setting " + path + " to " + str(profile))
settings = tree.getroot()
canbus = settings.find("Canbus")
if canbus == None:
canbus = etree.SubElement(settings, "Canbus")
inter = canbus.find(interface)
if inter == None:
inter = etree.SubElement(canbus, interface)
prof = etree.SubElement(inter, "Profile")
prof.text = str(profile)
prof.set('type', 'i')
delete_from_tree(tree, "/Settings/Services/LgResu")
delete_from_tree(tree, "/Settings/Services/OceanvoltMotorDrive")
delete_from_tree(tree, "/Settings/Services/OceanvoltValence")
delete_from_tree(tree, "/Settings/Services/VeCan")
tree.write(newFileSettings, pretty_print = True, xml_declaration = True, encoding = settingsEncoding)
rename(newFileSettings, fileSettings)
## The main function.
def run():
global bus
global dbusName
global myDbusServices
global myDbusGroupServices
global settings
global pathSettings
global fileSettings
global fileSettingChanges
global newFileSettings
global sysSettingsDir
global groups
global busName
global tracingEnabled
global pathTraces
global traceToConsole
global traceToFile
global traceFileName
global traceDebugOn
global myDbusMainGroupService
global logger
DBusGMainLoop(set_as_default=True)
# set the settings path
fileSettings = pathSettings + fileSettings
fileSettingChanges = pathSettings + fileSettingChanges
newFileSettings = pathSettings + newFileSettings
# setup debug traces.
logger = logging.getLogger('localsettings victron applicaiton')
#formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
formatter = logging.Formatter('%(levelname)s - %(message)s')
logger.setLevel(logging.INFO)
if traceDebugOn == 1:
logger.setLevel(logging.DEBUG)
if traceToConsole == 1:
print ("log console activated")
ch = logging.StreamHandler()
ch.setFormatter(formatter)
ch.setLevel(logging.DEBUG)
logger.addHandler(ch)
if traceToFile == 1 :
fh = logging.FileHandler('trace.log')
fh.setFormatter(formatter)
fh.setLevel(logging.DEBUG)
print ("tracefilename:")
print (traceFileName + traceFileName)
logger.addHandler(fh)
#tracing.setupTraces(tracingEnabled, pathTraces, traceFileName, traceToConsole, traceToFile, traceDebugOn)
logger.debug('tracingPath = %s' % pathTraces)
# Print the logscript version
logger.info('Localsettings version is: 0x%04x' % version)
logger.info('Localsettings PID is: %d' % getpid())
# Trace the python version.
pythonVersion = platform.python_version()
logger.debug('Current python version: %s' % pythonVersion)
# setup signal handling.
signal.signal(signal.SIGHUP, handlerSignals) # 1: Hangup detected
signal.signal(signal.SIGINT, handlerSignals) # 2: Ctrl-C
signal.signal(signal.SIGUSR1, handlerSignals) # 10: kill -USR1 <logscript-pid>
signal.signal(signal.SIGTERM, handlerSignals) # 15: Terminate
# load system default settings
loadSettingsDir(sysSettingsDir, settings)
if not path.isdir(pathSettings):
print('Error path %s does not exist!' % pathSettings)
sys.exit(errno.ENOENT)
if path.isfile(newFileSettings):
logger.info('New settings file exist')
try:
tree = etree.parse(newFileSettings)
root = tree.getroot()
logger.info('New settings file %s validated' % newFileSettings)
rename(newFileSettings, fileSettings)
logger.info('renamed new settings file to settings file')
except:
logger.error('New settings file %s invalid' % newFileSettings)
remove(newFileSettings)
logger.error('%s removed' % newFileSettings)
if path.isfile(fileSettings):
# Try to validate the settings file.
try:
tree = etree.parse(fileSettings)
root = tree.getroot()
migrate_can_profile(tree)
logger.info('Settings file %s validated' % fileSettings)
except:
logger.error('Settings file %s invalid' % fileSettings)
remove(fileSettings)
logger.error('%s removed' % fileSettings)
# check if settings file is present, if not exit create a "empty" settings file.
if not path.isfile(fileSettings):
logger.warning('Settings file %s not found' % fileSettings)
root = etree.Element(settingsRootName)
root.set(settingsTag, settingsVersion)
tree = etree.ElementTree(root)
tree.write(fileSettings, encoding = settingsEncoding, pretty_print = True, xml_declaration = True)
logger.warning('Created settings file %s' % fileSettings)
# read the settings.xml
parseXmlFileToDictonary(fileSettings, settings, groups, None)
logger.debug('settings:')
logger.debug(settings.items())
logger.debug('groups:')
logger.debug(groups)
# check if new settings must be changed
if path.isfile(fileSettingChanges):
# process the settings which must be deleted.
delSettings = {}
parseXmlFileToDictonary(fileSettingChanges, delSettings, None, "/Change/Delete")
logger.debug('setting to delete:')
logger.debug(delSettings.items())
for item in delSettings:
if item in settings:
logger.debug('delete item %s' % item)
del settings[item]
saveChanges = True
# process the settings which must be added.
addSettings = {}
parseXmlFileToDictonary(fileSettingChanges, addSettings, None, "/Change/Add")
logger.debug('setting to add:')
logger.debug(addSettings.items())
saveChanges = False
for item in addSettings:
if not item in settings:
logger.debug('add item %s' % item)
settings[item] = addSettings[item]
saveChanges = True
if saveChanges == True:
logger.warning('Change settings according to %s' % fileSettingChanges)
parseDictonaryToXmlFile(settings, fileSettings)
# update settings and groups from file.
settings = {}
groups = []
parseXmlFileToDictonary(fileSettings, settings, groups, None)
logger.debug('settings:')
logger.debug(settings.items())
logger.debug('groups:')
logger.debug(groups)
remove(fileSettingChanges)
# For a PC, connect to the SessionBus
# For a CCGX, connect to the SystemBus
bus = dbus.SessionBus() if 'DBUS_SESSION_BUS_ADDRESS' in environ else dbus.SystemBus()
busName = dbus.service.BusName(dbusName, bus)
root = RootObject(busName)
for setting in settings:
myDbusObject = MyDbusObject(busName, setting)
myDbusServices.append(myDbusObject)
for group in groups:
myDbusObject = MyDbusObject(busName, group)
myDbusGroupServices.append(myDbusObject)
myDbusMainGroupService = myDbusGroupServices[0]
MainLoop().run()
def usage():
print("Usage: ./localsettings [OPTION]")
print("-h, --help\tdisplay this help and exit")
print("-t\t\tenable tracing to file (standard off)")
print("-d\t\tset tracing level to debug (standard info)")
print("-v, --version\treturns the program version")
print("--banner\tshows program-name and version at startup")
print ("--verbose\tSpeak like my mother")
print("--path=dir\tuse given dir as data directory instead of /data")
print("")
print("NOTE FOR DEBUGGING ON DESKTOP")
print("This code expects a path /data/conf or --path to be set, and")
print("permissions in that path to write/read.")
def main(argv):
global tracingEnabled
global traceToConsole
global traceToFile
global traceDebugOn
global pathSettings
global timeoutSaveSettingsTime
try:
opts, args = getopt.getopt(argv, "vhctd", ["help", "version", "banner", "path=", "no-delay","verbose"])
except getopt.GetoptError:
usage()
sys.exit(errno.EINVAL)
for opt, arg in opts:
if opt == '-h' or opt == '--help':
usage()
sys.exit()
elif opt == '-t':
tracingEnabled = True
traceToFile = True
elif opt == '-d':
traceDebugOn = True
elif opt == '--verbose':
tracingEnabled = True
traceToConsole = True
elif opt == '--path':
pathSettings = arg
if pathSettings[-1] != '/':
pathSettings += "/"
elif opt == '--no-delay':
print("no delay")
timeoutSaveSettingsTime = 0
elif (opt == '-v' or opt == '--version'):
print(version)
sys.exit()
print("localsettings v%01x.%02x starting up " % (FIRMWARE_VERSION_MAJOR, FIRMWARE_VERSION_MINOR))
run()
main(sys.argv[1:])