Esempio n. 1
0
        static async void SendToPowerBI(PowerBIData data)
        {
            var url     = "https://api.powerbi.com/beta/e4a5cd36-e58f-4f98-8a1a-7a8e545fc65a/datasets/c3152879-fd74-4ba6-94aa-2b9b7111005f/rows?key=AUL%2FVTsGwsmJGzP28v7ah5EInDrjg7rTXx4b1IarBiTTcuB62zXzkG8QGoCZuJwyICzydqAT6ieTzGxsMXMETQ%3D%3D";
            var content = new StringContent(JsonConvert.SerializeObject(data), Encoding.UTF8, "application/json");
            var res     = await client.PostAsync(url, content, CancellationToken.None);

            if (res.IsSuccessStatusCode)
            {
                Console.WriteLine("data sent to power bi - " + DateTime.Now);
            }
        }
Esempio n. 2
0
        static void Loop()
        {
            UdpClient udpServer = new UdpClient(8888);

            while (true)
            {
                var remoteEP = new IPEndPoint(IPAddress.Any, 8888);
                var data     = udpServer.Receive(ref remoteEP); // listen on port 8888

                var datastr = System.Text.Encoding.Default.GetString(data);
                Console.WriteLine("receive data from " + remoteEP.ToString());
                Console.WriteLine("data: " + datastr);
                var obj = JsonConvert.DeserializeObject <RootObject>(datastr);
                if (obj != null)
                {
                    byte[] databyte      = Convert.FromBase64String(obj.rx.userdata.payload);
                    string decodedString = Encoding.UTF8.GetString(databyte);
                    var    originalValue = Unpack(decodedString);
                    Console.WriteLine("unpack :" + originalValue);
                    var deviceData  = JsonConvert.DeserializeObject <DeviceData>(originalValue);
                    var sensorValue = new PowerBIData()
                    {
                        LimitSwitch1 = deviceData.LimitSwitch1?1:0, LimitSwitch2 = deviceData.LimitSwitch2?1:0, Relay1 = deviceData.Relay1?1:0, Relay2 = deviceData.Relay2?1:0,
                        TDS1         = deviceData.TDS1, TDS2 = deviceData.TDS2, Temp = deviceData.Temp, Tanggal = DateTime.Now
                    };

                    //call power bi api
                    SendToPowerBI(sensorValue);
                    //send data to gateway

                    /*
                     * {
                     *  Transmitter.ObjMoteTx objtx = new Transmitter.ObjMoteTx();
                     *  objtx.tx = new Transmitter.Tx();
                     *  objtx.tx.moteeui = "00000000AAABBBEE";
                     *  objtx.tx.txmsgid = "000000000001";
                     *  objtx.tx.trycount = 5;
                     *  objtx.tx.txsynch = false;
                     *  objtx.tx.ackreq = false;
                     *  //string to hex str, hex str to base64 string
                     *  objtx.tx.userdata = new Transmitter.Userdata() { payload = "Njg2NTZjNmM2ZjIwNjM2ZjZkNzA3NTc0NjU3Mg==", port = 5 };
                     *  var jsonStr = JsonConvert.SerializeObject(objtx);
                     *  byte[] bytes = Encoding.ASCII.GetBytes(jsonStr);
                     *  udpServer.Send(bytes, bytes.Length, remoteEP);
                     *
                     * }*/
                    Thread.Sleep(5000);
                }
            }
        }