@@ -4,7 +4,6 @@ import com.sun.net.httpserver.HttpServer
44import mu.KotlinLogging
55import org.strykeforce.healthcheck.HealthCheckCommand
66import java.io.OutputStream
7- import java.io.Writer
87import java.net.InetSocketAddress
98
109private val logger = KotlinLogging .logger {}
@@ -45,21 +44,26 @@ class ReportServer(private val healthCheck: RobotHealthCheck) {
4544
4645class JsonVisitor : HealthCheckVisitor {
4746
48- private val metaCase = StringBuilder ()
49- private val metaName = StringBuilder ()
50- private val metaTalon = StringBuilder ()
51- private val metaType = StringBuilder ()
52- private val metaOutput = StringBuilder ()
53- private val metaDuration = StringBuilder ()
54-
55- private val timestamp = StringBuilder ()
56- private val talon = StringBuilder ()
57- private val case = StringBuilder ()
58- private val voltage = StringBuilder ()
59- private val position = StringBuilder ()
60- private val speed = StringBuilder ()
61- private val supplyCurrent = StringBuilder ()
62- private val statorCurrent = StringBuilder ()
47+ private val meta = mapOf (
48+ " case" to StringBuilder (),
49+ " case_uuid" to StringBuilder (),
50+ " name" to StringBuilder (),
51+ " talon" to StringBuilder (),
52+ " type" to StringBuilder (),
53+ " output" to StringBuilder (),
54+ " duration" to StringBuilder (),
55+ )
56+
57+ private val data = mutableMapOf (
58+ " msec_elapsed" to StringBuilder (),
59+ " talon" to StringBuilder (),
60+ " case" to StringBuilder (),
61+ " voltage" to StringBuilder (),
62+ " position" to StringBuilder (),
63+ " speed" to StringBuilder (),
64+ " supplyCurrent" to StringBuilder (),
65+ " statorCurrent" to StringBuilder (),
66+ )
6367
6468 private var name = " "
6569
@@ -69,21 +73,8 @@ class JsonVisitor : HealthCheckVisitor {
6973 healthCheck.healthChecks.forEach { it.accept(this ) }
7074
7175 // remove trailing commas, grr json
72- metaCase.deleteCharAt(metaCase.lastIndex)
73- metaName.deleteCharAt(metaName.lastIndex)
74- metaTalon.deleteCharAt(metaTalon.lastIndex)
75- metaType.deleteCharAt(metaType.lastIndex)
76- metaOutput.deleteCharAt(metaOutput.lastIndex)
77- metaDuration.deleteCharAt(metaDuration.lastIndex)
78-
79- timestamp.deleteCharAt(timestamp.lastIndex)
80- talon.deleteCharAt(talon.lastIndex)
81- case.deleteCharAt(case.lastIndex)
82- voltage.deleteCharAt(voltage.lastIndex)
83- position.deleteCharAt(position.lastIndex)
84- speed.deleteCharAt(speed.lastIndex)
85- supplyCurrent.deleteCharAt(supplyCurrent.lastIndex)
86- statorCurrent.deleteCharAt(statorCurrent.lastIndex)
76+ meta.values.forEach { it.deleteCharAt(it.lastIndex) }
77+ data.values.forEach { it.deleteCharAt(it.lastIndex) }
8778 }
8879
8980 override fun visit (healthCheck : SubsystemHealthCheck ) {
@@ -96,64 +87,46 @@ class JsonVisitor : HealthCheckVisitor {
9687 }
9788
9889 override fun visit (healthCheck : TalonHealthCheckCase ) {
99- metaCase.append(" \" ${metaIndex} \" :${healthCheck.case} ," )
100- metaName.append(" \" ${metaIndex} \" :\" $name \" ," )
101- metaTalon.append(" \" ${metaIndex} \" :${healthCheck.talon.deviceID} ," )
102- metaType.append(" \" ${metaIndex} \" :\" ${healthCheck.type} \" ," )
103- metaOutput.append(" \" ${metaIndex} \" :${healthCheck.output} ," )
104- metaDuration.append(" \" ${metaIndex} \" :${healthCheck.duration} ," )
90+ meta.getValue(" case" ).append(" \" ${metaIndex} \" :${healthCheck.case} ," )
91+ meta.getValue(" case_uuid" ).append(" \" ${metaIndex} \" :\" ${healthCheck.uuid} \" ," )
92+ meta.getValue(" name" ).append(" \" ${metaIndex} \" :\" $name \" ," )
93+ meta.getValue(" talon" ).append(" \" ${metaIndex} \" :${healthCheck.talon.deviceID} ," )
94+ meta.getValue(" type" ).append(" \" ${metaIndex} \" :\" ${healthCheck.type} \" ," )
95+ meta.getValue(" output" ).append(" \" ${metaIndex} \" :${healthCheck.output} ," )
96+ meta.getValue(" duration" ).append(" \" ${metaIndex} \" :${healthCheck.duration} ," )
10597 metaIndex++
10698
107- healthCheck.data.forEach { data ->
108- data .timestamp.forEachIndexed { i, v ->
109- timestamp .append(" \" ${index + i} \" :$v ," )
110- talon.append(" \" ${index + i} \" :${data .deviceId} ," )
111- case.append(" \" ${index + i} \" :${data .case} ," )
112- voltage.append(" \" ${index + i} \" :${data .voltage[i]} ," )
113- position.append(" \" ${index + i} \" :${data .position[i]} ," )
114- speed.append(" \" ${index + i} \" :${data .speed[i]} ," )
115- supplyCurrent.append(" \" ${index + i} \" :${data .supplyCurrent[i]} ," )
116- statorCurrent.append(" \" ${index + i} \" :${data .statorCurrent[i]} ," )
99+ healthCheck.data.forEach { healthCheckData ->
100+ healthCheckData .timestamp.forEachIndexed { i, v ->
101+ data.getValue( " msec_elapsed " ) .append(" \" ${index + i} \" :$v ," )
102+ data.getValue( " talon" ) .append(" \" ${index + i} \" :${healthCheckData .deviceId} ," )
103+ data.getValue( " case" ) .append(" \" ${index + i} \" :${healthCheckData .case} ," )
104+ data.getValue( " voltage" ) .append(" \" ${index + i} \" :${healthCheckData .voltage[i]} ," )
105+ data.getValue( " position" ) .append(" \" ${index + i} \" :${healthCheckData .position[i]} ," )
106+ data.getValue( " speed" ) .append(" \" ${index + i} \" :${healthCheckData .speed[i]} ," )
107+ data.getValue( " supplyCurrent" ) .append(" \" ${index + i} \" :${healthCheckData .supplyCurrent[i]} ," )
108+ data.getValue( " statorCurrent" ) .append(" \" ${index + i} \" :${healthCheckData .statorCurrent[i]} ," )
117109 }
118- index + = data .timestamp.size
110+ index + = healthCheckData .timestamp.size
119111 }
120112 }
121113
122- private fun sendData (writer : Writer ) {
123- writer.write(
124- " {" +
125- " \" case\" :{$case }," +
126- " \" msec_elapsed\" :{$timestamp }," +
127- " \" talon\" :{$talon }," +
128- " \" voltage\" :{$voltage }," +
129- " \" position\" :{$position }," +
130- " \" speed\" :{$speed }," +
131- " \" supply_current\" :{$supplyCurrent }," +
132- " \" stator_current\" :{$statorCurrent }" +
133- " }"
134- )
135- }
136-
137- private fun sendMeta (writer : Writer ) {
138- writer.write(
139- " {" +
140- " \" case\" :{$metaCase }," +
141- " \" name\" :{$metaName }," +
142- " \" talon\" :{$metaTalon }," +
143- " \" type\" :{$metaType }," +
144- " \" output\" :{$metaOutput }," +
145- " \" duration\" :{$metaDuration }" +
146- " }"
147- )
148- }
149-
150114 fun sendHealthCheck (os : OutputStream ) {
151115 val writer = os.writer()
152- writer.write(" {\" meta\" :" )
153- sendMeta(writer)
154- writer.write(" ,\" data\" :" )
155- sendData(writer)
156- writer.write(" }" )
116+
117+ writer.write(" {\" meta\" :{" )
118+ for ((i, key) in meta.keys.withIndex()) {
119+ writer.write(" \" $key \" :{${meta[key]} }" )
120+ if (i < meta.size - 1 ) writer.write(" ," )
121+ }
122+
123+ writer.write(" },\" data\" :{" )
124+ for ((i, key) in data.keys.withIndex()) {
125+ writer.write(" \" $key \" :{${data[key]} }" )
126+ if (i < data.size - 1 ) writer.write(" ," )
127+ }
128+
129+ writer.write(" }}" )
157130 writer.flush()
158131 }
159132}
0 commit comments