feat: make plugin integration opt-in

This commit is contained in:
way
2026-09-27 22:28:05 +08:00
parent 4367d0b9d0
commit 50a0f5690f
7 changed files with 58 additions and 10 deletions
+11 -1
View File
@@ -42,7 +42,7 @@ class Handler(BaseHTTPRequestHandler):
if self.path=='/v1/descriptor':self.send(200,descriptor())
elif self.path=='/v1/status':self.send(200,c.status())
elif self.path=='/v1/config':
with c.lock:self.send(200,{'config':c.config,'schema':descriptor()['schema'],'notes':descriptor()['notes']})
with c.lock:self.send(200,{'config':{k:v for k,v in c.config.items() if k!='integration'},'schema':{k:v for k,v in descriptor()['schema'].items() if k!='integration'},'notes':descriptor()['notes']})
else:self.send(404,{'error':'not found'})
def do_PUT(self):
@@ -53,6 +53,7 @@ class Handler(BaseHTTPRequestHandler):
elif self.path=='/v1/config':
updates=validated(body.get('updates',{}))
if not updates:raise ValueError('no configuration updates')
if 'integration' in updates:raise ValueError('change integration locally and restart the plugin')
with c.lock:
value={s:dict(v) for s,v in c.config.items()}
for section,fields in updates.items():value[section].update(fields)
@@ -62,6 +63,7 @@ class Handler(BaseHTTPRequestHandler):
backup=path.with_name(path.name+'.bak.'+str(time.time_ns()))
backup.write_bytes(path.read_bytes());backup.chmod(0o600)
save(path,value);c.config=value
log('CONFIG_UPDATED',sections=','.join(updates));self.send(200,{'ok':True})
else:self.send(404,{'error':'not found'})
except (ValueError,TypeError,KeyError) as exc:self.send(400,{'error':str(exc)})
@@ -104,6 +106,14 @@ def main():
controller.stop()
controller.driver=None
path.unlink(missing_ok=True)
if config['integration']['enabled']!='true':
stopped=threading.Event()
signal.signal(signal.SIGTERM,lambda *_:stopped.set())
signal.signal(signal.SIGINT,lambda *_:stopped.set())
log('START',plugin=descriptor()['id'],integration=False,available=controller.driver is not None)
try:stopped.wait()
finally:controller.stop();log('STOP',i2c_errors=controller.i2c_errors)
return
try:
with Server(str(path),Handler) as server:
path.chmod(0o660);server.controller=controller;server.config_path=Path(args.config)