-
Notifications
You must be signed in to change notification settings - Fork 1
/
co12m.py
173 lines (155 loc) · 5.44 KB
/
co12m.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
# Copyright 2020 Dr. Domenico Suriano (domenico.suriano@enea.it)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
### WARNING: CO12M_ADDRESS, CO12M_PORT, CO12M_ID_STRING and CO12M_UNITS
### depend on the settings of your Environnement device CO12M,
### therefore check on them and update or modify them if necessary.
import socket
import time
CONNECTION_TYPE = "eth"
ETH_ADDRESSES = ["192.168.20.41"]
DEVICE_IDENTITY = "CO12"
DEVICE_SENSORS = "co[ppm]"
CO12M_PORT = 8000
SOCKET_TIMEOUT = 2
MAX_NUM_ATTEMPT = 4
ACK = '\x06'
ERR_VAL = "-100"
class Co12m:
def __init__(self):
self.identity = DEVICE_IDENTITY
self.connection_type = CONNECTION_TYPE
self.sensors = DEVICE_SENSORS
self.addresses = ETH_ADDRESSES
self.address = None
self.port = CO12M_PORT
self.sk = None
self.status = 'U'
self.alarm = None
## the function "connect" check if the device is plugged into Ethernet port,
## then returns 1 if the device is found
def connect(self,address):
found = 0
try:
self.sk = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.sk.settimeout(SOCKET_TIMEOUT)
except Exception as e:
return found
command = chr(2) + "CO12" + "16"
tocalc = command.encode()
c1,c2 = self.BCCcalc(tocalc)
command = command + chr(c1) + chr(c2) + chr(3)
tent = 0
while tent < MAX_NUM_ATTEMPT:
try:
sent = self.sk.sendto(command.encode(), (address,self.port))
data1, server = self.sk.recvfrom(1024)
data = data1.decode()
except socket.timeout:
if tent >= MAX_NUM_ATTEMPT:
return found
else:
tent = tent + 1
time.sleep(0.2)
continue
except Exception as e:
if tent >= MAX_NUM_ATTEMPT:
return found
else:
tent = tent + 1
time.sleep(0.2)
continue
if (data[0]!= ACK) and (tent>=MAX_NUM_ATTEMPT):
return found
if (data[0]!= ACK) and (tent<MAX_NUM_ATTEMPT):
tent = tent + 1
time.sleep(0.2)
continue
if (data[0]== ACK):
self.identity = str(data[1:5])
self.status = data[13]
self.alarm = str(data[14]) + str(data[15])
self.address = address
found = 1
return found
def getConnectionParams(self):
return self.addresses
def getConnectionType(self):
return self.connection_type
def getIdentity(self):
return self.identity
def setIdentity(self,idstring):
self.identity = idstring
def getSensors(self):
return self.sensors
def terminate(self):
try:
self.sk.close()
except:
return
def __del__(self):
try:
self.sk.close()
except:
return
def BCCcalc(self,mb):
BCC = 0
for i in range(1,len(mb)):
BCC = BCC ^ mb[i]
BCC1 = BCC>>4|48
if BCC1 > 57:
BCC1+=7
BCC2 = (BCC&15)|48
if BCC2 > 57:
BCC2+=7
return(BCC1,BCC2)
## the function "sample" reads the sensor output,
## then returns a string separate by semicolon containing data
## if an error happens, it returns the string with err value: "-100.0"
def sample(self):
command = chr(2) + "CO12" + "16"
tocalc = command.encode()
c1,c2 = self.BCCcalc(tocalc)
command = command + chr(c1) + chr(c2) + chr(3)
tent = 0
while tent < MAX_NUM_ATTEMPT:
try:
sent = self.sk.sendto(command.encode(), (self.address,self.port))
data1, server = self.sk.recvfrom(1024)
data = data1.decode()
except socket.timeout:
tent = tent + 1
time.sleep(0.2)
continue
except Exception as e:
tent = tent + 1
time.sleep(0.2)
continue
if data[0]!= ACK:
tent = tent + 1
time.sleep(0.2)
continue
else:
self.status = data[13]
self.alarm = str(data[14]) + str(data[15])
str2 = data[17:]
strm = str2.split(' ')
try:
val = float(strm[0])
except:
return ERR_VAL
if val < 0:
return "0.0"
else:
return strm[0]
return ERR_VAL