redis 的key 過期通知設定

Redis - Wikipedia

redis有expire機制,且同時有redis keyspace notification。不過預設默認的通知是關閉的~需要透過redis.conf或config set啟用。

config set notify-keyspace-events KEA

接著在c# 使用StackExchange.Redis

然後訂閱__keyevent@0__:expired 這個頻道就可以了

var redisConnection = ConnectionMultiplexer.Connect(“localhost”);
var db = redisConnection.GetDatabase();

var subscriber = redisConnection.GetSubscriber();
// 訂閱過期事件通知頻道
subscriber.Subscribe(“__keyevent@0__:expired”, (channel, key1) =>
{
// 在這裡進行過期事件的處理邏輯
// key 是過期的 Redis 鍵名
Console.WriteLine($”Key {key1} has expired.”);
});

Console.WriteLine(“Press any key to exit.”);
Console.ReadKey();

// 斷開與 Redis 的連線
redisConnection.Close();

C# 微軟語音辨識/語音合成結合OpenAI ChatGPT 變成ai人工智慧管家

透過微軟azure上的語音服務,辨識麥克風傳入的聲音後將語音轉成文字
接著再把文字送到open ai api中的chatgpt 模型中取得回應內容
最後再將回應內容透過語音合成送出

參考:
Introducing ChatGPT and Whisper APIs
https://openai.com/blog/introducing-chatgpt-and-whisper-apis
openai api
https://platform.openai.com/docs/guides/chat

Betalgo.OpenAI.GPT3
https://www.nuget.org/packages/Betalgo.OpenAI.GPT3/6.7.0
https://github.com/betalgo/openai

使用.net 搖控 DJI TELLO 無人機

 

看到DJI TELLO有SDK,幾個重點

1 使用的是UDP

2   IP是: 192.168.10.1

3  使用的PORT有 8889(指令) 8890(狀態) 11111(取得畫面)

程式碼大概就是


// 定義 IP

string DJIIP = "192.168.10.1";

//定義埠號

int DJIPort = 8889;

// 開啟

UdpClient udpClient = new UdpClient();

udpClient.Connect(DJIIP,DJIPort);

//
Byte[] sendCmdBytes = null;

sendCmdBytes = Encoding.UTF8.GetBytes(“takeoff”);

udpClient.Send(sendCmdBytes, sendCmdBytes.Length);

 

 

https://www.ryzerobotics.com/zh-tw

https://www.ryzerobotics.com/zh-tw/tello/downloads

 

 

C#資料結構與演算法

使用對的演算法與資料結構,在程式效能上有很大的幫助。常用的資料結構有array、stack、 queue、 linked list、 tree、 graph、 heap、 hash。在c#上常見的是使用array 、arraylist、 list、 dictonary 以及queue可以滿足絕大部份的場景。

以下介紹github上二個高階資料結構與演算法的項目。

一、C Sharp Algorithms

一開始是學習資料結構,在不斷更新以及維護後,可以用在正式環境。

https://github.com/aalhour/C-Sharp-Algorithms

二、Advanced Algorithms

https://github.com/justcoding121/advanced-algorithms

.Net 7 開發增進效能的小撇步

預計在2022年11月發布的.net7目前進入RC2的版本。來談談.net7 做了那些效能上的改進~

LINQ部份

1針對max/min最佳化

2針對Average/sum

3新增order/orderDescending 取代orderby/orderbydescending

4 System.IO的改善 WriteAllText改ReadAllText

5 針對Jsonserializer NoCachedOptions提昇

6Guid的 GuidEquals提昇

7ParseBigInt提昇

8ParseBool

9 Stopwatch中的getprocessbyname與getcurrentprocessname

10 在stopwatch加上getelapsedtime取得時間戳

.net Multi-platform APP UI(MAUI)教學準備中

2022年11月微軟準備在.net 新版本中發表 Multi-platform APP UI(MAUI)新的跨平台開發架構,透過c#以及XAML就可以開發出同時在windows、android以及ios與mac os上的程式(對了!目前還沒有支援到linux)!

簡單的說它算是Xamarin.Forms後續的版本,近期開始著手撰寫以及錄製MAUI的教學以及學習文件。

.net MAUI 新增程式
預設.net MAUI 範例教學程式

C# RabbitMQ模擬Producer與Consumer

最近系統考量可靠性問題,在中間加上一層 Message Queue(MQ)的架構,採用的是目前最多人使用的RabbitMQ當作server服務。順便直接以百萬筆資料為單位塞入MQ來測試一下效能。

Producer程式

System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();//引用stopwatch物件
            sw.Reset();//碼表歸零
            sw.Start();//碼表開始計時
            //
            string queue = "info";
            try
            {
                var factory = new RabbitMQ.Client.ConnectionFactory();
                factory.HostName = "localhost";
                factory.UserName = "guest";
                factory.Password = "guest";

                using (var connection = factory.CreateConnection())
                {
                    using (var channel = connection.CreateModel())
                    {
                        bool durable = true;
                        channel.QueueDeclare(queue, durable, false, false, null);
                        RabbitMQ.Client.IBasicProperties properties = channel.CreateBasicProperties();
                        properties.DeliveryMode = 2;
                        for (int i = 1; i <= 100; i++)
                        {
                            LogInfo info = new LogInfo();
                            info.SYS_ID = "系統";
                            info.COMPANY_ID = "公司";
                            info.STORE_ID = "店別";
                            info.Content = "訊息";
                            string m1 = Newtonsoft.Json.JsonConvert.SerializeObject(info);
                            var body = Encoding.UTF8.GetBytes(m1);
                            channel.BasicPublish("", queue, properties, body);
                        }

                    }
                }
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            sw.Stop();
            string result1 = sw.Elapsed.TotalMilliseconds.ToString();
            Console.WriteLine("共花費:" + result1 + "毫秒");
            Console.ReadLine();

Consumer端程式

    string queue = "info";
            try
            {
                var factory = new ConnectionFactory();
                factory.HostName = "localhost";
                factory.UserName = "guest";
                factory.Password = "guest";
                var connection = factory.CreateConnection();
                var channel = connection.CreateModel();
                channel.QueueDeclare(queue, true, false, false, null); // 定義處理那一個queue
                channel.BasicQos(0, 1, false);  // 每次處理1則

                var consumer = new RabbitMQ.Client.Events.EventingBasicConsumer(channel); 
                // 定義收到queue的內容處理方式
                consumer.Received += (sender, e) =>
                {
                    byte[] body = e.Body.ToArray();
                    string message1 = Encoding.UTF8.GetString(body); 
                    LogInfo log = JsonConvert.DeserializeObject<LogInfo>(message1);  // 將queue中的json轉回物件
                    // 以下可以更改為自己要處理的事項
                    Console.WriteLine(log.DateTime.ToString("yyyy/MM/dd HH:mm:ss:FFF")+" "+log.Content );  // 先顯示畫面上
                    //
                    channel.BasicAck(e.DeliveryTag, false); // 處理完手動回應    

                };
                channel.BasicConsume(queue, false, consumer);  // 開始處理
                Console.ReadLine();
                connection.Close();
                channel.Close();
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }

自然人憑證作為公文系統、簽核系統簽章用憑證

最近要用.net c#去開發憑證簽證、加解密的功能,於是下午跑去申請了自然人憑證。然後瞭解一下卡片中的資料~繼續努力中,似乎自然人憑證與未來的新式晶片身份證都會當做很重要的憑證以及加解密的依據。

內政部憑證 網址: https://moica.nat.gov.tw/index.html

電子簽章法 https://law.moj.gov.tw/LawClass/LawAll.aspx?pcode=J0080037

跨平台網頁元件下載 網址: https://moica.nat.gov.tw/rac_plugin.html

HiPKI Local Server範例 網址: https://gpkiapi.nat.gov.tw/PKCS7Verify/

http://localhost:61161

HiPKI Local Server (version:1.3.4.103339) at 127.0.0.1:61161

http://127.0.0.1:61161/pkcs11info


http://127.0.0.1:61161/sign

http://127.0.0.1:61161/decrypt