Set time and time zone

This commit is contained in:
David Carley
2022-07-21 20:53:57 -07:00
parent 46d26deb8e
commit 3f3b609de6
16 changed files with 1303 additions and 141 deletions

View File

@@ -1,12 +1,24 @@
import traceback
import copy
import json
import uuid
import os
import socket
import bbctrl
import iw_parse
from tornado import gen
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
def call_get_output(cmd):
p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
s = p.communicate()[0].decode('utf-8')
if p.returncode:
raise HTTPError(400, 'Command failed')
return s
class UploadChangeHandler(FileSystemEventHandler):
def __init__(self, state):
self.state = state
@@ -64,6 +76,36 @@ class State(object):
self), self.ctrl.get_upload(), recursive=True)
observer.start()
self._updateNetworkInfo()
@gen.coroutine
def _updateNetworkInfo(self):
try:
ipAddresses = call_get_output(['hostname', '-I']).split()
except:
ipAddresses = ""
hostname = socket.gethostname()
try:
wifi = json.loads(call_get_output(['config-wifi', '-j']))
except:
wifi = {'enabled': False}
try:
lines = iw_parse.call_iwlist().decode("utf-8").split("\n")
wifi['networks'] = iw_parse.get_parsed_cells(lines)
except:
wifi['networks'] = []
self.set('networkInfo', {
'ipAddresses': ipAddresses,
'hostname': hostname,
'wifi': wifi
})
self.timeout = self.ctrl.ioloop.call_later(5, self._updateNetworkInfo)
def reset(self):
# Unhome all motors
for i in range(4):
@@ -170,8 +212,11 @@ class State(object):
return name
def has(self, name): return self.resolve(name) in self.vars
def set_callback(self, name, cb): self.callbacks[self.resolve(name)] = cb
def has(self, name):
return self.resolve(name) in self.vars
def set_callback(self, name, cb):
self.callbacks[self.resolve(name)] = cb
def set(self, name, value):
name = self.resolve(name)
@@ -233,7 +278,8 @@ class State(object):
self.listeners.append(listener)
listener(self.vars)
def remove_listener(self, listener): self.listeners.remove(listener)
def remove_listener(self, listener):
self.listeners.remove(listener)
def set_machine_vars(self, vars):
# Record all machine vars, indexed or otherwise
@@ -284,7 +330,8 @@ class State(object):
if motor_axis == axis.lower() and self.vars.get('%dme' % motor, 0):
return motor
def is_axis_homed(self, axis): return self.get('%s_homed' % axis, False)
def is_axis_homed(self, axis):
return self.get('%s_homed' % axis, False)
def is_axis_enabled(self, axis):
motor = self.find_motor(axis)

View File

@@ -1,6 +1,5 @@
import os
import re
import json
import tornado
import sockjs.tornado
import datetime
@@ -10,7 +9,6 @@ from tornado.web import HTTPError
from tornado import gen
import bbctrl
import iw_parse
def call_get_output(cmd):
@@ -101,31 +99,6 @@ class HostnameHandler(bbctrl.APIHandler):
class NetworkHandler(bbctrl.APIHandler):
def get(self):
try:
ipAddresses = call_get_output(['hostname', '-I']).split()
except:
ipAddresses = ""
hostname = socket.gethostname()
try:
wifi = json.loads(call_get_output(['config-wifi', '-j']))
except:
wifi = {'enabled': False}
try:
lines = iw_parse.call_iwlist().decode("utf-8").split("\n")
wifi['networks'] = iw_parse.get_parsed_cells(lines)
except:
wifi['networks'] = []
self.write_json({
'ipAddresses': ipAddresses,
'hostname': hostname,
'wifi': wifi
})
def put(self):
if self.get_ctrl().args.demo:
raise HTTPError(400, 'Cannot configure WiFi in demo mode')
@@ -383,13 +356,34 @@ class ScreenRotationHandler(bbctrl.APIHandler):
text = config.read()
text = transformationMatrixPattern.sub(r'\1\2\3\5', text)
if rotated:
text = matchIsTouchscreenPattern.sub(r'\1\2\3\2Option "TransformationMatrix" "-1 0 1 0 -1 1 0 0 1"\1\4', text)
text = matchIsTouchscreenPattern.sub(
r'\1\2\3\2Option "TransformationMatrix" "-1 0 1 0 -1 1 0 0 1"\1\4', text)
with open("/usr/share/X11/xorg.conf.d/40-libinput.conf", 'wt') as config:
config.write(text)
subprocess.run('reboot')
class TimeHandler(bbctrl.APIHandler):
def get(self):
timeinfo = call_get_output(['timedatectl'])
timezones = call_get_output(
['timedatectl', 'list-timezones', '--no-pager'])
self.get_log('TimeHandler').info(
'Time stuff: {}, {}'.format(timeinfo, timezones))
self.write_json({
'timeinfo': timeinfo,
'timezones': timezones
})
def put_ok(self):
datetime = self.json['datetime']
timezone = self.json['timezone']
subprocess.Popen(['timedatectl', 'set-time', datetime])
subprocess.Popen(['timedatectl', 'set-timezone', timezone])
# Base class for Web Socket connections
class ClientConnection(object):
def __init__(self, app):
@@ -526,6 +520,7 @@ class Web(tornado.web.Application):
(r'/api/jog', JogHandler),
(r'/api/video', bbctrl.VideoHandler),
(r'/api/screen-rotation', ScreenRotationHandler),
(r'/api/time', TimeHandler),
(r'/(.*)', StaticFileHandler,
{'path': bbctrl.get_resource('http/'),
'default_filename': 'index.html'}),
@@ -545,7 +540,8 @@ class Web(tornado.web.Application):
print('Listening on http://%s:%d/' % (args.addr, args.port))
def opened(self, ctrl): ctrl.clear_timeout()
def opened(self, ctrl):
ctrl.clear_timeout()
def closed(self, ctrl):
# Time out clients in demo mode