예제 #1
0
 public Message(object obj, string topic, string controller, JsonSerializer serializer)
 {
     this._serializer = serializer;
     this.D = _serializer.Serialize(obj);
     this.T = topic.ToLower();
     this.C = controller;
     this.MessageType = MessageType.Text;
 }
예제 #2
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="server"></param>
 /// <param name="port"></param>        
 /// <param name="protocolName">The protocol to use</param>
 /// <param name="protocolResponse">Expected response from the protocol</param>
 public XSocketClient(string server, int port, string protocolName = "JsonProtocol", string protocolResponse = "CONNECTED") 
 {
     this.Serializer = new JsonSerializer();            
     this.Server = server;
     this.Port = port;
     this.ProtocolName = protocolName;
     this.ProtocolResponse = protocolResponse;
 }        
예제 #3
0
        public void correct_time_stamp(SDCard sdCard)
        {
            Measurement mymeasure     = new Measurement();
            string      rootDirectory = sdCard.StorageDevice.RootDirectory;
            FileStream  FileHandle    = new FileStream(rootDirectory + @"\SetUp.txt", FileMode.Open, FileAccess.Read);

            byte[] data       = new byte[100];
            int    read_count = FileHandle.Read(data, 0, data.Length);
            string str        = new string(Encoding.UTF8.GetChars(data), 0, read_count);

            Debug.Print(str);
            string[] parts = str.Split(' ');
            lastSent   = Int32.Parse(parts[0]);
            lastTosend = Int32.Parse(parts[1]);
            FileHandle.Close();
            //FileStream
            //public static TimeSpan operator -(DateTime d1, DateTime d2);

            int j;

            if (lastSent == 0)
            {
                j = lastSent;
            }
            else
            {
                j = lastSent + 1;
            }
            for (int i = j; i <= lastTosend; i++)
            {
                FileStream fs = new FileStream(rootDirectory + @"\wrong_toSend" + i + ".JSON", FileMode.Open, FileAccess.Read);
                try
                {
                    byte[]         data1        = new byte[100];
                    int            read_count1  = fs.Read(data1, 0, data1.Length);
                    string         str1         = new string(Encoding.UTF8.GetChars(data), 0, read_count1);
                    JsonSerializer myserializer = new Json.NETMF.JsonSerializer(DateTimeFormat.Default);
                    mymeasure = (Measurement)myserializer.Deserialize(str1);
                    Debug.Print(i + " " + mymeasure.iso_timestamp.ToString());
                    DateTime d2 = mymeasure.iso_timestamp;
                    DateTime d1 = DateTime.Now; //starting local time
                    TimeSpan T  = d2 - d1;
                    DateTime d3 = DateTime.Now - T;
                    mymeasure.iso_timestamp = d3;
                    JSON.printString(sdCard, mymeasure, true);
                    // d1 =


                    //File.Delete(rootDirectory + @"\wrong_tosend" + i + ".JSON");

                    fs.Close();
                }
                catch (System.IO.IOException)
                {
                    Debug.Print("File " + i + "not found.\n");
                }
            }
        }
예제 #4
0
        internal override EventData PrepareEventData(IDictionary bag)
        {
            EventData data = new EventData();

            // Create hashtable for data
            Hashtable hashtable = new Hashtable();
            hashtable.Add("Subject", "wthr");
            hashtable.Add("time", DateTime.UtcNow);
            hashtable.Add("from", this.DeviceId);
            hashtable.Add("dspl", this.DeviceName);

            foreach (SensorType type in bag.Keys)
            {
                if (type == SensorType.Temperature)
                {
                    double temperature = (double)bag[type];
                    data.Properties["temp"] = temperature;
                    Debug.Print("temp: " + temperature);
                }
                else if (type == SensorType.Humidity)
                {
                    double humidity = (double)bag[type];
                    data.Properties["hmdt"] = humidity;
                    Debug.Print("hmdt: " + humidity);
                }
                else if (type == SensorType.Accelerometer)
                {
                    double[] acceleration = (double[])bag[type];
                    data.Properties["accx"] = acceleration[0];
                    data.Properties["accy"] = acceleration[1];
                    data.Properties["accz"] = acceleration[2];
                    Debug.Print("acceleration: " + acceleration[0] + "," + acceleration[1] + "," + acceleration[2]);
                }
            }

            // Serialize hashtable into JSON
            JsonSerializer serializer = new JsonSerializer(DateTimeFormat.Default);
            string payload = serializer.Serialize(hashtable);

            data.PartitionKey = this.DeviceId;

            return data;
        }
예제 #5
0
        public static void printConfiguration(SDCard sdCard, object o)
        {
            string     rootDirectory = sdCard.StorageDevice.RootDirectory;
            string     json;
            FileStream fs;

            fs = new FileStream(rootDirectory + @"\Configuration.JSON", FileMode.Create, FileAccess.ReadWrite);
            JsonSerializer mySerializer = new Json.NETMF.JsonSerializer(DateTimeFormat.Default);

            json = mySerializer.Serialize(o);

            byte[] data = Encoding.UTF8.GetBytes(json);
            fs.Write(data, 0, data.Length);
            fs.Close();
            //write immediately on the sdCard
            VolumeInfo[] vi = VolumeInfo.GetVolumes();
            for (int i = 0; i < vi.Length; i++)
            {
                vi[i].FlushAll();
            }
        }
예제 #6
0
        private void publishMqtt(Measurement mymeasure)
        {
            try
            {
                if (!_mqttclient.IsConnected)
                {
                    byte b = _mqttclient.Connect(Guid.NewGuid().ToString());
                    Debug.Print(b.ToString());
                }

                JsonSerializer mySerializer = new Json.NETMF.JsonSerializer(DateTimeFormat.ISO8601);
                jsonString = mySerializer.Serialize(mymeasure);
                _mqttclient.Publish(topic, Encoding.UTF8.GetBytes(jsonString), 0, false);
                Debug.Print("publishing\n");
                //_mqttclient.Disconnect();
                Debug.Print(jsonString);
            }
            catch (Exception ex)
            {
                //Debug.Print(ex.ToString());
            }
        }
예제 #7
0
        public static void printString(SDCard sdCard, object o, bool connection)
        {
            string     rootDirectory = sdCard.StorageDevice.RootDirectory;
            string     json;
            FileStream fs;

            //Debug.Print("I am here");
            //timestamp applied is corretc no need to change it
            if (connection)
            {
                fs = new FileStream(rootDirectory + @"\correct_toSend" + numbOnline + ".JSON", FileMode.Create, FileAccess.ReadWrite);
                increseOnline();
                //SetUp.setLastToSend(numbOnline);
            }
            //timestamp applied is not correct change it, before sending
            else
            {
                fs = new FileStream(rootDirectory + @"\wrong_toSend" + numbOffline + ".JSON", FileMode.Create, FileAccess.ReadWrite);
                increaseOffline();
                //SetUp.setWrong(numbOffline);
            }
            JsonSerializer mySerializer = new Json.NETMF.JsonSerializer(DateTimeFormat.Default);

            json = mySerializer.Serialize(o);

            byte[] data = Encoding.UTF8.GetBytes(json);
            fs.Write(data, 0, data.Length);
            fs.Close();

            //write immediately on the sdCard
            VolumeInfo[] vi = VolumeInfo.GetVolumes();
            for (int i = 0; i < vi.Length; i++)
            {
                vi[i].FlushAll();
            }

            //Debug.Print("pringt data to SD!"+properties.Humidity+properties.Temperature+properties.iso_timestamp);
        }
예제 #8
0
        private string FormatMessage(string measureName, string unitOfMeasure, double value)
        {
            // Create hashtable for data
            var hashtable = new Hashtable();
            hashtable.Add("organization", Organization);
            hashtable.Add("location", Location);
            hashtable.Add("guid", SensorGUID);
            hashtable.Add("displayname", SensorName);
            hashtable.Add("unitofmeasure", unitOfMeasure);
            hashtable.Add("measurename", measureName);
            hashtable.Add("value", value);
            hashtable.Add("timecreated", DateTime.UtcNow);

            // Serialize hashtable into JSON
            var serializer = new JsonSerializer(DateTimeFormat.Default);
            var payload = serializer.Serialize(hashtable);

            return payload;
        }
예제 #9
0
        public static bool SerializeDeserializeDateTest()
        {
            try
            {
                DateTime testTime = new DateTime(2015, 04, 22, 11, 56, 39, 456);
                JsonSerializer dataSerializer = new JsonSerializer(DateTimeFormat.ISO8601);
                string jsonString = dataSerializer.Serialize(testTime);
                string deserializedJsonString = (string)dataSerializer.Deserialize(jsonString);
                DateTime convertTime = DateTimeExtensions.FromIso8601(deserializedJsonString);

                if (testTime != convertTime)
                {
                    Debug.Print("Fail: SerializeDeserializeDateTest - Values did not match");
                    return false;
                }

                Debug.Print("Success: SerializeDeserializeDateTest");
                return true;
            }
            catch (Exception ex)
            {
                Debug.Print("Fail: SerializeDeserializeDateTest - " + ex.Message);
                return false;
            }
        }
예제 #10
0
파일: Program.cs 프로젝트: leestott/Beats
        //void initWiFi(String ssid, String passphrase)
        //{
        //    bool foundSSID = false;
        //    if (!wifiRS21.NetworkInterface.Opened)
        //        wifiRS21.NetworkInterface.Open();

        //    wifiRS21.NetworkInterface.EnableDhcp();
        //    wifiRS21.UseDHCP();
        //    //wifi.UseStaticIP("192.168.1.225", "255.255.255.0", "192.168.1.1", new string[] { "10.1.10.1" });

        //    wifiRS21.NetworkDown += new GTM.Module.NetworkModule.NetworkEventHandler(wifiRS21_NetworkDown);
        //    wifiRS21.NetworkUp += new GTM.Module.NetworkModule.NetworkEventHandler(wifiRS21_NetworkUp);

        //    Debug.Print("Scan for wireless networks");
        //    WiFiRS9110.NetworkParameters[] scanResult = wifiRS21.NetworkInterface.Scan();
        //    if (scanResult != null)
        //    {
        //        foreach (WiFiRS9110.NetworkParameters x in scanResult)
        //        {
        //            if (x.Ssid == ssid) foundSSID = true;
        //            Debug.Print(x.Ssid.ToString());
        //        }
        //    }
        //    else
        //    {
        //        Debug.Print("No wireless networks were found.");
        //    }
        //    Debug.Print("------------------------------------");
        //    if (foundSSID)
        //    {
        //        Debug.Print("Connecting to " + ssid);
        //        wifiRS21.NetworkInterface.Join(ssid, passphrase); // Network with WPA or WPA2 security.
        //        Thread.Sleep(1000);
        //        Debug.Print("Connected");
        //        Debug.Print("IP Address: " + wifiRS21.NetworkSettings.IPAddress);
        //    }
        //    else
        //    {
        //        Debug.Print(ssid + " Wireless network was not found");

        //    }
        //    Debug.Print("------------------------------------");
        //}

        //void wifiRS21_NetworkDown(GTM.Module.NetworkModule sender, GTM.Module.NetworkModule.NetworkState state)
        //{
        //    Debug.Print("Network down");
        //}

        //void wifiRS21_NetworkUp(GTM.Module.NetworkModule sender, GTM.Module.NetworkModule.NetworkState state)
        //{
        //    Debug.Print("Network Up");
        //    Debug.Print("IP Address: " + wifiRS21.NetworkSettings.IPAddress);
        //}

        // Callback function for measurement of temp and humidity. called every SendFrequency milliseconds
        void tempHumidity_MeasurementComplete(TempHumidity sender, TempHumidity.MeasurementCompleteEventArgs e)
        {
            // Convert temperature into Fahrenheit
            var FahrenheitTemp = e.Temperature * 9 / 5 + 32;

            // Display data and time in debug console
            Debug.Print(DateTime.Now.ToString());
            Debug.Print("Temperature=" + FahrenheitTemp.ToString());
            Debug.Print("Humidity=" + e.RelativeHumidity.ToString());

            // Create hashtable for data
            Hashtable hashtable = new Hashtable();
            hashtable.Add("hmdt", e.RelativeHumidity);
            hashtable.Add("temp", FahrenheitTemp);
            hashtable.Add("Subject", "wthr");
            hashtable.Add("time", DateTime.UtcNow);
            hashtable.Add("from", DeviceID);
            hashtable.Add("dspl", DeviceName);

            // Serialize hashtable into JSON
            JsonSerializer serializer = new JsonSerializer(DateTimeFormat.Default);
            string payload = serializer.Serialize(hashtable);

            // send to Event Hub
            SendAMQPMessage(payload);
        }