-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathlistenerNoGui.py
executable file
·183 lines (157 loc) · 5.36 KB
/
listenerNoGui.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
#!/usr/bin/python3
# The non-GUI variant of the listener
# Functionality:
# - gets the GPS location from a GPS daemon (gpsd on linux)
# - gets the chargers MAC address from the address manager
# - sends the location and the chargers MAC to a server via http request
import time
import pyPlcWorker
from pyPlcModes import *
import sys # for argv
from configmodule import getConfigValue, getConfigValueBool
import urllib.request
import gps # the gpsd interface module
import RPi.GPIO as GPIO # for controlling hardware pins of the raspberry
startTime_ms = round(time.time()*1000)
GPSsession = gps.gps(mode=gps.WATCH_ENABLE)
strGpsPos = "no_pos"
strLoggingUrl = getConfigValue("logging_url")
def cbAddToTrace(s):
currentTime_ms = round(time.time()*1000)
dT_ms = currentTime_ms - startTime_ms
print("[" + str(dT_ms) + "ms] " + s)
def cbShowStatus(s, selection=""):
pass
def testBlockingBeep(patternselection):
if (patternselection==1):
# The "MAC Found" beep
p.ChangeDutyCycle(30)
p.ChangeFrequency(1100)
time.sleep(0.3)
p.ChangeFrequency(1300)
time.sleep(0.3)
p.ChangeFrequency(1500)
time.sleep(0.3)
p.ChangeDutyCycle(0)
if (patternselection==2):
# The short "ok" beep
p.ChangeDutyCycle(10)
p.ChangeFrequency(1600)
time.sleep(0.07)
p.ChangeDutyCycle(0)
if (patternselection==3):
# The "no GPS position" beep
print("no pos beep 4")
p.ChangeDutyCycle(10)
p.ChangeFrequency(1700)
time.sleep(0.04)
p.ChangeDutyCycle(0)
time.sleep(0.04)
p.ChangeDutyCycle(10)
p.ChangeFrequency(1700)
time.sleep(0.04)
p.ChangeDutyCycle(0)
time.sleep(0.04)
p.ChangeDutyCycle(10)
p.ChangeFrequency(1700)
time.sleep(0.04)
p.ChangeDutyCycle(0)
time.sleep(0.04)
p.ChangeDutyCycle(10)
p.ChangeFrequency(1700)
time.sleep(0.04)
p.ChangeDutyCycle(0)
def trySomeHttp():
print("***************trying some http********************")
strChargerMac = "chargerMac=na"
if (worker.addressManager.isEvseMacNew()):
testBlockingBeep(1)
strChargerMac = "chargerMac=" + worker.addressManager.getEvseMacAsStringAndClearUpdateFlag().replace(":", "")
strParams = strChargerMac
strParams = strParams + "&loops="+str(nMainloops)+"&pos="+strGpsPos
print(strParams)
try:
contents = urllib.request.urlopen(strLoggingUrl + "?" + strParams).read()
print(len(strGpsPos))
if (len(strGpsPos)>7):
print("Valid GPS coordinates, and http logging worked")
#testBlockingBeep(2)
else:
print("http was ok, but no GPS position")
#testBlockingBeep(3)
except Exception as err:
contents = "(no contents received) " + str(err)
print(contents)
def GpsMainfunction():
global strGpsPos
GPSsession.read()
if (gps.MODE_SET & GPSsession.valid):
print('Mode: %s(%d) Time: ' %
(("Invalid", "NO_FIX", "2D", "3D")[GPSsession.fix.mode],
GPSsession.fix.mode), end="")
# print time, if we have it
if gps.TIME_SET & GPSsession.valid:
print(GPSsession.fix.time, end="")
else:
print('n/a', end="")
if ((gps.isfinite(GPSsession.fix.latitude) and
gps.isfinite(GPSsession.fix.longitude))):
print(" Lat %.6f Lon %.6f" %
(GPSsession.fix.latitude, GPSsession.fix.longitude))
strGpsPos = str(GPSsession.fix.latitude)+"_"+str(GPSsession.fix.longitude)
else:
print(" Lat n/a Lon n/a")
strGpsPos = str(0.0)+"_"+str(0.0)
print(strGpsPos)
def mytestfunction():
global soundstate
#testBlockingBeep(2)
#if (soundstate==0):
# p.ChangeDutyCycle(10)
# p.ChangeFrequency(1000)
#if (soundstate==1):
# p.ChangeDutyCycle(10)
# p.ChangeFrequency(1200)
#if (soundstate==2):
# p.ChangeDutyCycle(0)
#soundstate+=1
#if (soundstate>=3):
# soundstate=0
myMode = C_LISTEN_MODE
isSimulationMode=0
if (len(sys.argv) > 1):
if (sys.argv[1] == "S"):
isSimulationMode=1
print("starting in LISTEN_MODE")
print("press Ctrl-C to exit")
worker=pyPlcWorker.pyPlcWorker(cbAddToTrace, cbShowStatus, myMode, isSimulationMode)
nMainloops=0
httpTime_ms = round(time.time()*1000)
testTime_ms = httpTime_ms
#intervalForHttpLogging_ms = 120*1000 # each 2 minutes send the logging data to the server
intervalForHttpLogging_ms = 30*1000 # each half minute send the logging data to the server
soundstate = 0
GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIO.OUT)
p=GPIO.PWM(23, 500) # 500 Hz
p.start(0)
p.ChangeDutyCycle(50)
time.sleep(0.1)
p.ChangeDutyCycle(0)
time.sleep(0.1)
p.ChangeDutyCycle(50)
time.sleep(0.1)
p.ChangeDutyCycle(0)
while (1):
time.sleep(.03) # 'do some calculation'
nMainloops+=1
currentTime_ms = round(time.time()*1000)
if ((currentTime_ms-httpTime_ms)>intervalForHttpLogging_ms) or (worker.addressManager.isEvseMacNew()):
trySomeHttp()
httpTime_ms = currentTime_ms
if ((currentTime_ms-testTime_ms)>2000):
mytestfunction()
testTime_ms = currentTime_ms
worker.mainfunction()
GpsMainfunction()
#---------------------------------------------------------------