-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplugin.py
More file actions
184 lines (174 loc) · 6.65 KB
/
plugin.py
File metadata and controls
184 lines (174 loc) · 6.65 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
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
# -*- coding: utf-8 -*-
# vim: ts=2 sw=2 et ai
###############################################################################
# Copyright (c) 2021 Andreas Vogel andreas@wellenvogel.net
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:self.SERVICE_CFG
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
#
###############################################################################
import json
import os
import re
import time
import urllib.parse
from http import HTTPStatus
from urllib.request import urlopen
import dbus
class Plugin(object):
CONFIG = [
]
@classmethod
def pluginInfo(cls):
"""
the description for the module
@return: a dict with the content described below
parts:
* description (mandatory)
* data: list of keys to be stored (optional)
* path - the key - see AVNApi.addData, all pathes starting with "gps." will be sent to the GUI
* description
"""
return {
'description': 'avnav update plugin',
'version': '1.0'
}
def __init__(self,api):
"""
initialize a plugins
do any checks here and throw an exception on error
do not yet start any threads!
@param api: the api to communicate with avnav
@type api: AVNApi
"""
self.api = api # type: AVNApi
self.api.registerRequestHandler(self.requestHandler)
self.updaterPort=None
self.findError=None
self.isConnected=False
self.startSequence=0
if hasattr(self.api,'registerRestart'):
self.api.registerRestart(self._apiRestart)
def _apiRestart(self):
self.startSequence+=1
def _findServicePort(self):
port=None
try:
BUSNAME = 'org.freedesktop.systemd1'
UNITNAME = 'avnavupdater.service'
bus = dbus.SystemBus()
systemd = bus.get_object(
BUSNAME,
'/org/freedesktop/systemd1'
)
manager = dbus.Interface(systemd,'org.freedesktop.systemd1.Manager')
unit = manager.GetUnit(UNITNAME)
extended = bus.get_object(BUSNAME, unit)
properties=dbus.Interface(extended,dbus_interface="org.freedesktop.DBus.Properties")
env=properties.Get("org.freedesktop.systemd1.Service","Environment")
for e in env:
nv=e.split("=",2)
if len(nv) > 1 and nv[0] == 'PORT':
port=int(nv[1])
break
if port is None:
self.findError="no port found for updater"
except Exception as e:
self.findError=str(e)
if port is None:
self.api.setStatus("ERROR", "unable to get updater port: %s" % self.findError)
self.updaterPort = port
def run(self):
"""
the run method
this will be called after successfully instantiating an instance
this method will be called in a separate Thread
The example simply counts the number of NMEA records that are flowing through avnav
and writes them to the store every 10 records
@return:
"""
startSequence=self.startSequence
seq=0
self.api.log("started")
self.api.setStatus("INACTIVE","starting")
self.isConnected=False
self.api.setStatus("STARTING", "started")
avnavversion=0
try:
avnavversion=self.api.getAvNavVersion()
avnavversion=re.sub('[^0-9]','',avnavversion)
except:
pass
try:
if avnavversion >= 20220410:
self.api.registerUserApp(self.api.getBaseUrl()+"/api/index",os.path.join('gui','icons','system_update.svg'),title="AvNav Updater",preventConnectionLost=True)
else:
self.api.registerUserApp(self.api.getBaseUrl()+"/api/index",os.path.join('gui','icons','system_update.svg'),title="AvNav Updater")
except Exception as e:
self.api.setStatus("ERROR","unable to register user app: %s"%str(e))
return
while startSequence == self.startSequence:
self._findServicePort()
if self.updaterPort:
url = "http://localhost:%d" % self.updaterPort
pingUlr = url + "/api/ping"
try:
r=urlopen(pingUlr,timeout=5)
res=json.loads(r.read())
if res.get('status') != 'OK':
self.api.setStatue("ERROR","invalid response from service: %s",res.get('STATUS'))
else:
self.isConnected=True
self.api.setStatus("NMEA", "service running at port %d" % self.updaterPort)
except Exception as e:
self.api.setStatus("ERROR","error connecting to %s: %s"%(pingUlr,str(e)))
self.isConnected=False
else:
self.api.setStatus("ERROR","unable to find updater service port")
time.sleep(2)
def requestHandler(self, url, handler, args):
'''
handle api requests
@param url:
@param handler:
@param args:
@return:
'''
if url == 'index':
if self.updaterPort is None or not self.isConnected:
error = self.findError if self.updaterPort is None else "updater not available at port %s"%self.updaterPort
r=("<h1> unable to find updater service</h1><p>%s</p>"%error or '').encode('utf-8')
handler.send_response(200)
handler.send_header("Content-Type", "text/html")
handler.send_header("Content-Length", str(len(r)))
handler.send_header("Last-Modified", handler.date_time_string())
handler.end_headers()
handler.wfile.write(r)
else:
parts = urllib.parse.urlsplit(handler.path)
handler.send_response(HTTPStatus.MOVED_PERMANENTLY)
hostport=handler.headers.get('host')
hostport=re.sub(':[^:\]]*$','',hostport)
hostport+=':'+str(self.updaterPort)
if parts[0] == '':
proto="http"
else:
proto=parts[0]
new_parts = (proto, hostport, '/index.html',"title=no",None)
new_url = urllib.parse.urlunsplit(new_parts)
handler.send_header("Location", new_url)