make service openplotter managed

This commit is contained in:
sailoog 2022-04-24 14:21:51 +02:00
parent c9b3e94e29
commit 1aaffe8fda
6 changed files with 51 additions and 90 deletions

View File

@ -27,6 +27,12 @@ def main():
package = 'openplotter-maiana'
language.Language(currentdir, package, currentLanguage)
print(_('Installing python packages...'))
try:
subprocess.call(['pip3', 'install', 'websocket-client', '-U'])
print(_('DONE'))
except Exception as e: print(_('FAILED: ')+str(e))
print(_('Checking access to Signal K server...'))
try:
from openplotterSignalkInstaller import connections
@ -36,16 +42,6 @@ def main():
else: print(_('DONE'))
except Exception as e: print(_('FAILED: ')+str(e))
#TODO switch from service to startup
print(_('Adding openplotter-maiana-read service...'))
try:
fo = open('/etc/systemd/system/openplotter-maiana-read.service', "w")
fo.write( '[Service]\nExecStart=openplotter-maiana-read\nStandardOutput=syslog\nStandardError=syslog\nUser='+conf2.user+'\nRestart=always\nRestartSec=5\n\n[Install]\nWantedBy=multi-user.target')
fo.close()
subprocess.call(['systemctl', 'daemon-reload'])
print(_('DONE'))
except Exception as e: print(_('FAILED: ')+str(e))
print(_('Setting version...'))
try:
conf2.set('APPS', 'maiana', version)

View File

@ -26,13 +26,9 @@ def main():
package = 'openplotter-maiana'
language.Language(currentdir, package, currentLanguage)
#TODO switch from service to startup
print(_('Removing openplotter-maiana-read service...'))
print(_('Stopping OpenPlotter MAIANA service...'))
try:
subprocess.call(['systemctl', 'disable', 'openplotter-maiana-read'])
subprocess.call(['systemctl', 'stop', 'openplotter-maiana-read'])
subprocess.call(['rm', '-f', '/etc/systemd/system/openplotter-maiana-read.service'])
subprocess.call(['systemctl', 'daemon-reload'])
subprocess.call(['pkill','-f','openplotter-maiana-read'])
print(_('DONE'))
except Exception as e: print(_('FAILED: ')+str(e))

View File

@ -18,7 +18,7 @@
#TODO add tx action
#TODO parse bin file
import wx, os, webbrowser, subprocess, time, datetime, ujson, serial, requests, re
import wx, os, webbrowser, subprocess, time, datetime, ujson, serial, requests, re, sys
import wx.richtext as rt
from openplotterSettings import conf
from openplotterSettings import language
@ -137,6 +137,11 @@ class MyFrame(wx.Frame):
url = self.platform.http+'localhost:'+self.platform.skPort+'/admin/#/serverConfiguration/connections/-'
webbrowser.open(url, new=2)
def restartRead(self):
subprocess.call(['pkill','-f','openplotter-maiana-read'])
subprocess.Popen('openplotter-maiana-read')
time.sleep(1)
def onRead(self):
self.ShowStatusBarYELLOW(_('Reading MAIANA device settings...'))
self.mmsi.SetValue('')
@ -217,6 +222,15 @@ class MyFrame(wx.Frame):
self.SKconn.SetValue(self.connInit)
self.ShowStatusBarRED(_('Select the Signal K connection for the MAIANA device'))
if deviceOld != self.device:
if self.device: self.restartRead()
else: subprocess.call(['pkill','-f','openplotter-maiana-read'])
else:
if self.device:
test = subprocess.check_output(['ps','aux']).decode(sys.stdin.encoding)
if not 'openplotter-maiana-read' in test: self.restartRead()
else: subprocess.call(['pkill','-f','openplotter-maiana-read'])
if self.device:
ser = serial.Serial(self.device, 38400)
ser.write('sys?\r\n'.encode("utf-8"))
@ -235,6 +249,7 @@ class MyFrame(wx.Frame):
ts2 = time.mktime(datetime.datetime.strptime(timestamp, "%Y-%m-%dT%H:%M:%S.%fZ").timetuple())
if ts - ts2 > 3:
self.ShowStatusBarRED(_('Cannot connect with the device, try again'))
print('#############################')
return
hardwareRevision = data['hardwareRevision']['value']
hardwareRevision = hardwareRevision.split('.')
@ -391,17 +406,6 @@ class MyFrame(wx.Frame):
if 'bowOffset' in data['station']: self.bowOffset.SetValue(str(data['station']['bowOffset']['value']))
if 'portOffset' in data['station']: self.portOffset.SetValue(str(data['station']['portOffset']['value']))
if deviceOld != self.device:
if self.device:
subprocess.Popen([self.platform.admin, 'python3', self.currentdir+'/service.py', 'openplotter-maiana-read', 'restart'])
else:
subprocess.Popen([self.platform.admin, 'python3', self.currentdir+'/service.py', 'openplotter-maiana-read', 'stop'])
else:
if self.device:
try:
subprocess.check_output(['systemctl', 'is-active', 'openplotter-maiana-read']).decode(sys.stdin.encoding)
except:
subprocess.Popen([self.platform.admin, 'python3', self.currentdir+'/service.py', 'openplotter-maiana-read', 'restart'])
def onSKconn(self, event):
deviceOld = self.conf.get('MAIANA', 'device')
@ -426,18 +430,14 @@ class MyFrame(wx.Frame):
self.conf.set('MAIANA', 'device', self.device)
except: pass
if deviceOld != self.device:
if self.device:
subprocess.Popen([self.platform.admin, 'python3', self.currentdir+'/service.py', 'openplotter-maiana-read', 'restart'])
time.sleep(1)
else:
subprocess.Popen([self.platform.admin, 'python3', self.currentdir+'/service.py', 'openplotter-maiana-read', 'stop'])
if self.device: self.restartRead()
else: subprocess.call(['pkill','-f','openplotter-maiana-read'])
else:
if self.device:
try:
subprocess.check_output(['systemctl', 'is-active', 'openplotter-maiana-read']).decode(sys.stdin.encoding)
except:
subprocess.Popen([self.platform.admin, 'python3', self.currentdir+'/service.py', 'openplotter-maiana-read', 'restart'])
time.sleep(1)
test = subprocess.check_output(['ps','aux']).decode(sys.stdin.encoding)
if not 'openplotter-maiana-read' in test: self.restartRead()
else: subprocess.call(['pkill','-f','openplotter-maiana-read'])
self.onRead()
def pageSettings(self):

View File

@ -37,7 +37,6 @@ def main():
sock = False
while True:
try:
#TODO switch from service to startup
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_address = ('localhost', 10110)
sock.connect(server_address)

View File

@ -1,31 +0,0 @@
#!/usr/bin/env python3
# This file is part of Openplotter.
# Copyright (C) 2021 by Sailoog <https://github.com/openplotter/openplotter-maiana>
#
# Openplotter is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# any later version.
# Openplotter is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Openplotter. If not, see <http://www.gnu.org/licenses/>.
import sys, subprocess
#TODO remove
if sys.argv[1]=='openplotter-maiana-read':
if sys.argv[2]=='start':
subprocess.call(['systemctl', 'enable', 'openplotter-maiana-read'])
subprocess.call(['systemctl', 'start', 'openplotter-maiana-read'])
if sys.argv[2]=='stop':
subprocess.call(['systemctl', 'disable', 'openplotter-maiana-read'])
subprocess.call(['systemctl', 'stop', 'openplotter-maiana-read'])
if sys.argv[2]=='restart':
subprocess.call(['systemctl', 'enable', 'openplotter-maiana-read'])
subprocess.call(['systemctl', 'restart', 'openplotter-maiana-read'])

View File

@ -25,15 +25,17 @@ class Start():
currentdir = os.path.dirname(os.path.abspath(__file__))
language.Language(currentdir,'openplotter-maiana',currentLanguage)
self.initialMessage = ''
#TODO run read from here
self.initialMessage = _('Starting MAIANA transponder...')
def start(self):
green = ''
black = ''
red = ''
subprocess.call(['pkill', '-f', 'openplotter-maiana-read'])
subprocess.Popen('openplotter-maiana-read')
time.sleep(1)
return {'green': green,'black': black,'red': red}
class Check():
@ -73,27 +75,26 @@ class Check():
if not green: green = msg
else: green+= ' | '+msg
#service
# check service
test = subprocess.check_output(['ps','aux']).decode(sys.stdin.encoding)
if device and (result[0] == 'approved' or result[0] == 'validated'):
try:
subprocess.check_output(['systemctl', 'is-active', 'openplotter-maiana-read']).decode(sys.stdin.encoding)
msg = _('OpenPlotter MAIANA service is running')
if 'openplotter-maiana-read' in test:
msg = _('running')
if not green: green = msg
else: green+= ' | '+msg
except:
msg = _('OpenPlotter MAIANA service is not running')
if not red: red = msg
else: red+= '\n '+msg
else:
try:
subprocess.check_output(['systemctl', 'is-active', 'openplotter-maiana-read']).decode(sys.stdin.encoding)
msg = _('OpenPlotter MAIANA service is running')
if not red: red = msg
else: red+= '\n '+msg
except:
msg = _('OpenPlotter MAIANA service is not running')
if not green: green = msg
else: green+= ' | '+msg
msg = _('not running')
if red: red += '\n '+msg
else: red = msg
else:
if 'openplotter-maiana-read' in test:
msg = _('running')
if red: red += '\n '+msg
else: red = msg
else:
msg = _('not running')
if not black: black = msg
else: black+= ' | '+msg
return {'green': green,'black': black,'red': red}