forked from MarcDorval/TeleinfoLinky
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathteleinfo_ram.py
More file actions
81 lines (72 loc) · 2.44 KB
/
teleinfo_ram.py
File metadata and controls
81 lines (72 loc) · 2.44 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
#!/usr/bin/python
# use case: python teleinfo_ram.py IRMS1 IRMS2 IRMS3 URMS1 URMS2 URMS3 loop
import serial
import time
import sys
baudrate=9600
ser = serial.Serial('/dev/ttyAMA0', baudrate, bytesize=7, timeout=1)
ser.isOpen()
count=0
looping = 1
teleinfo_items=[]
results_items=[]
results_dict={}
return_string=""
results_folder="/var/tmp_ram/"
sys.argv.remove(sys.argv[0])
expected_items = len(sys.argv)
for item in sys.argv:
# print "item " + item
item_split = item.split()
# print item_split
for sub_item in item_split:
# print " sub_item " + sub_item
teleinfo_items.append(sub_item)
results_items.append(sub_item)
# print "teleinfo_items:"
# print teleinfo_items
try:
while looping:
response = ser.readline()
localtime = time.asctime( time.localtime(time.time()) )
count = count + 1
# print count
# If one of the arguments is not found, we must exit after a while
if count >= 100:
print " At least 1 or more argument missing after " + str(count) + " lines : exiting!\n Remaining (probably mis-spelled or unknown) arguments:"
for a in teleinfo_items:
print " ~ " + a
looping = 0
break;
if response != "":
# print "# The line is not empty, let's go on..."
items = response.split()
splitLen = len(items)
if splitLen >= 2:
# There are at leaast 3 items in the line, as expected, let's go on...
# The name is the first item
# The value is the one-before-last item (in most cases). This is the only case we handle.
item = items[0]
value = items[splitLen-2]
if item in teleinfo_items:
# print item + " == " + str(value)
if value.isdigit():
# Remove leading zeros from numerical values
value_int = int(value)
value = str(value_int)
results_dict[item]=value
filename = results_folder+item
file_object = open(filename, 'w')
file_object.write(results_dict[item])
file_object.close( )
# Once an argument is retrieved, it's removed from the list
teleinfo_items.remove(item)
# Once the list is empty, fill the return string and exit
if len(teleinfo_items) == 0:
for item in results_items:
return_string = return_string + results_dict[item] + " "
print return_string
sys.exit(0)
break;
except KeyboardInterrupt:
ser.close()