-
Notifications
You must be signed in to change notification settings - Fork 4
/
buttonShutdown.py
96 lines (72 loc) · 2.08 KB
/
buttonShutdown.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
import configparser
import datetime
import json
import os
import sys
import time
from subprocess import call
from Device import Device
# only if using Googe Voice
sys.path.append('/home/pi/AIY-voice-kit-python/src')
import aiy.voicehat
# globals
TOPIC_ALERTS_NAME = "caralerts"
sleepTime = 5
# used for STOP_TRIP msg
STFORMAT3 = "%d-%m-%Y %H"
#
#
# createEventMsg
#
def createEventMsg(event_type):
msg = {}
# in the main code it is defined if Simulator or not !
msg['EVENT_TYPE'] = event_type
msg['CARID'] = carID
msg['DAYHOUR'] = datetime.datetime.now().strftime(STFORMAT3)
# format in JSON
msgJson = json.dumps(msg)
return msgJson
def on_button_press():
button.on_press(None)
print('The button is pressed!')
time.sleep(3)
print('Shutting down...')
# here I should add the code to send STOP_TRIP alert
# in this point I must insert the SEND for START_TRIP msg
try:
msgJson = createEventMsg("STOP_TRIP")
gateway.publish(TOPIC_ALERTS_NAME, msgJson)
except:
print('Error in sending STOP_TRIP...')
call("sudo shutdown -h now", shell=True)
#
# main
#
# read configuration from gateway.ini file
# read OBD2_HOME env variable
OBD2HOME = os.getenv('OBD2_HOME')
config = configparser.ConfigParser()
config.read(OBD2HOME + '/gateway.ini')
carID = config['DEFAULT']['carID']
# should be read from config file
HOST = "ubuntucontainers-dashboariotnew-zekci6cs.srv.ravcloud.com"
PORT = 8883
# clientID is passed to make possible different clientID
# MQTT doesn't allow different clients with same ID (second is disconnected)
gateway = Device("googshut1")
# try connecting in loop
# to handle case in which initially no network connection
while gateway.isConnected() != True:
try:
gateway.connect(HOST, PORT, "YES")
except:
print('*** Error in MQTT connection !')
print('\n')
print('*** Error info: ', sys.exc_info()[0], sys.exc_info()[1])
time.sleep(sleepTime)
button = aiy.voicehat.get_button()
button.on_press(on_button_press)
while True:
print('Another loop...')
time.sleep(5)