From 3755eda05a20766d847250f4d45d7e2f9cdd4be0 Mon Sep 17 00:00:00 2001
From: David Trattnig <david.trattnig@o94.at>
Date: Thu, 7 May 2020 22:44:35 +0200
Subject: [PATCH] Get Engine Status via API.

---
 engine-api.py                          | 21 ++++++++++++---------
 modules/communication/redis/adapter.py |  5 ++++-
 2 files changed, 16 insertions(+), 10 deletions(-)

diff --git a/engine-api.py b/engine-api.py
index 0ee13358..34e83e36 100644
--- a/engine-api.py
+++ b/engine-api.py
@@ -21,6 +21,8 @@
 
 import logging
 import os, os.path
+import subprocess
+import json
 
 from datetime                       import datetime, date, timedelta
 
@@ -109,6 +111,7 @@ class EngineApi:
         spec.components.schema("Report",        schema=ReportSchema)
         spec.components.schema("Schedule",      schema=ScheduleSchema)
         spec.components.schema("Clock",         schema=ClockDataSchema)
+        spec.components.schema("Status",        schema=StatusSchema)
 
         # TODO Generates HTML for specification
         #self.logger.info(spec.to_yaml())
@@ -129,7 +132,7 @@ class EngineApi:
         self.api.add_resource(ReportResource,               config.api_prefix + "/report/<string:year_month>")
         self.api.add_resource(UpcomingSchedulesResource,    config.api_prefix + "/schedule/upcoming")
         self.api.add_resource(ClockDataResource,            config.api_prefix + "/clock")
-        self.api.add_resource(ClockDataResource,            config.api_prefix + "/status")
+        self.api.add_resource(StatusResource,               config.api_prefix + "/status")
 
         self.logger.info("Engine API routes successfully set!")
 
@@ -283,8 +286,11 @@ class ReportSchema(ma.Schema):
 class StatusSchema(ma.Schema):
     class Meta:
         fields = (
-                "is_liquidsoap_running",
-                "is_core_running"
+                "engine",
+                "soundsystem",
+                "api",
+                "redis_ready",
+                "audio_store"
             )
 
 
@@ -420,12 +426,9 @@ class StatusResource(Resource):
         self.logger = logging.getLogger("engine-api")
 
     def get(self):
-        
-        # FIXME Not yet implemented!
-
-        # #ss = SoundSystem(cr)
-        # return "check removed!" #ss.auraengine_state()
-        status = None
+        status = subprocess.check_output(["python3", "guru.py", "-s", "-q"])
+        status = status.decode("utf-8").replace("'", '"')
+        status = json.loads(status, strict=False)
 
         if not status:
             return abort(204) # No content available
diff --git a/modules/communication/redis/adapter.py b/modules/communication/redis/adapter.py
index 06eefd54..07fbde3b 100644
--- a/modules/communication/redis/adapter.py
+++ b/modules/communication/redis/adapter.py
@@ -24,6 +24,7 @@
 
 import sys
 import time
+import json
 import redis
 import logging
 import threading
@@ -159,7 +160,7 @@ class ServerRedisAdapter(threading.Thread, RedisMessenger):
         elif item["data"] == "get_status":
             def get_status_string():
                 status = self.soundsystem.monitoring.get_status()
-                return str(status)
+                return json.dumps(status)
 
             self.execute(RedisChannel.GS_REPLY.value, get_status_string)
 
@@ -186,6 +187,8 @@ class ServerRedisAdapter(threading.Thread, RedisMessenger):
         elif item["data"] == "recreate_db":
             self.execute(RedisChannel.RDB_REPLY.value, self.scheduler.recreate_database)
 
+        elif item["data"] == "status":
+            return True
 
         else:
             raise RedisConnectionException("ServerRedisAdapter Cannot understand command: " + item["data"])
-- 
GitLab