Beispiel #1
0
 public UIState()
 {
     FactorySettings = new KomahubFactorySettings();
     Status          = new KomahubStatus();
     Outputs         = new KomahubOutput[6];
     for (int i = 0; i < 6; i++)
     {
         Outputs[i]      = new KomahubOutput();
         Outputs[i].type = KomahubOutput.OutputType.OFF;
     }
 }
Beispiel #2
0
        public KomahubStatus readStatus()
        {
            byte[] report = new byte[64];
            byte[] result = new byte[64];
            report[0] = KOMAHUB_MAGIC;
            report[1] = Commands.GetStatus;

            lock (hubLock)
            {
                send(report);
                recv(result);
            }
            KomahubStatus status = new KomahubStatus();

            status.relayIsOpen   = new bool[6];
            status.fuseIsBlown   = new bool[6];
            status.pwmDuty       = new byte[6];
            status.outputCurrent = new float[6];
            for (int output = 0; output < 6; output++)
            {
                status.relayIsOpen[output]   = (result[0] & (1 << output)) != 0;
                status.fuseIsBlown[output]   = (result[1] & (1 << output)) != 0;
                status.pwmDuty[output]       = result[2 + output];
                status.outputCurrent[output] = result[2 + 6 + 1 + output] / 10.0f;
            }
            status.inputVoltage = result[2 + 6] / 10.0f;

            status.numberOfExternalTemperatures = result[15];
            status.externalTemperatures[0]      = readInt16Float(result[16], result[17]);
            status.externalTemperatures[1]      = readInt16Float(result[18], result[19]);
            status.externalTemperatures[2]      = readInt16Float(result[20], result[21]);
            status.externalTemperatures[3]      = readInt16Float(result[22], result[23]);

            status.temperature = readInt16Float(result[24], result[25]);
            status.dewpoint    = readInt16Float(result[26], result[27]);
            status.humidity    = result[28];
            status.pressure    = readInt16Float(result[29], result[30]);
            status.skyQuality  = result[31] / 10.0f;

            status.skyTemperature        = readInt16Float(result[32], result[33]);
            status.skyTemperatureAmbient = readInt16Float(result[34], result[35]);
            status.pthPresent            = result[36] > 0;
            status.skyQualityPresent     = result[37] > 0;
            status.skyTemperaturePresent = result[38] > 0;
            status.skyQualityFreq        = readUInt32Float(result[39], result[40], result[41], result[42]);

            return(status);
        }
Beispiel #3
0
        private void reportSensors(HttpListenerContext context)
        {
            /*
             * {
             *   "pth" : { "temperature": 3.5, "humidity": 70.5, "pressure": 1001.5, "dewpoint": -3.2 },
             *   "skyquality" : { "magnitude": 21.3, "frequency": 1005 },
             *   "skytemperature" : { "sky": -15.3, "ambient": 0.2 },
             *   "power" : { "inputvoltage": 12.5, "outputcurrent": [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] },
             *   "temperatures": [ 10.5, 0, 0, 0 ]
             * }
             */

            KomahubStatus status = komaHubForm.getUIState().Status;

            Dictionary <string, object> data = new Dictionary <string, object>();

            if (status.pthPresent)
            {
                Dictionary <string, float> pthdata = new Dictionary <string, float>();
                pthdata.Add("temperature", status.temperature);
                pthdata.Add("humidity", status.humidity);
                pthdata.Add("pressure", status.pressure);
                pthdata.Add("dewpoint", status.dewpoint);
                data.Add("pth", pthdata);
            }
            if (status.skyQualityPresent)
            {
                Dictionary <string, float> skyqualitydata = new Dictionary <string, float>();
                skyqualitydata.Add("magnitude", status.skyQuality);
                skyqualitydata.Add("frequency", status.skyQualityFreq);
                data.Add("skyquality", skyqualitydata);
            }
            if (status.skyTemperaturePresent)
            {
                Dictionary <string, float> skydata = new Dictionary <string, float>();
                skydata.Add("sky", status.skyTemperature);
                skydata.Add("ambient", status.skyTemperatureAmbient);
                data.Add("skytemperature", skydata);
            }
            if (status.numberOfExternalTemperatures > 0)
            {
                Dictionary <string, float[]> temperaturedata = new Dictionary <string, float[]>();
                float[] temps = new float[status.numberOfExternalTemperatures];
                for (int i = 0; i < status.numberOfExternalTemperatures; i++)
                {
                    temps[i] = status.externalTemperatures[i];
                }
                data.Add("temperatures", temps);
            }

            Dictionary <string, object> powerdata = new Dictionary <string, object>();

            powerdata.Add("inputvoltage", status.inputVoltage);
            float[] outputcurrents = new float[6];
            for (int i = 0; i < 6; i++)
            {
                outputcurrents[i] = status.outputCurrent[i];
            }
            powerdata.Add("outputcurrents", outputcurrents);
            data.Add("power", powerdata);

            string json = Newtonsoft.Json.JsonConvert.SerializeObject(data);

            byte[] bytes = System.Text.Encoding.UTF8.GetBytes(json);
            context.Response.ContentLength64 = bytes.Length;
            context.Response.Headers.Add("Content-Type: application/json");
            context.Response.OutputStream.Write(bytes, 0, bytes.Length);
            context.Response.StatusCode = 200;
            context.Response.OutputStream.Close();
        }