mirror of
https://github.com/zhaopeiym/IoTClient
synced 2025-10-26 22:15:44 +08:00
init
This commit is contained in:
45
IoTServer/Common/ConfigurationManager.cs
Normal file
45
IoTServer/Common/ConfigurationManager.cs
Normal file
@@ -0,0 +1,45 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using System.IO;
|
||||
|
||||
namespace IoTServer.Common
|
||||
{
|
||||
public static class ConfigurationManager
|
||||
{
|
||||
public readonly static IConfiguration Configuration;
|
||||
|
||||
static ConfigurationManager() =>
|
||||
Configuration = new ConfigurationBuilder()
|
||||
.SetBasePath(Directory.GetCurrentDirectory())
|
||||
.AddJsonFile("appsettings.json", optional: true)
|
||||
.Build();
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="key"></param>
|
||||
/// <returns></returns>
|
||||
public static T GetSection<T>(this string key) where T : class, new()
|
||||
{
|
||||
return Configuration.GetSection(key).Get<T>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取配置文件
|
||||
/// </summary>
|
||||
/// <param name="key"></param>
|
||||
/// <param name="defaultValue"></param>
|
||||
/// <returns></returns>
|
||||
public static string GetConfig(this string key, string defaultValue = "")
|
||||
{
|
||||
var value = Configuration.GetValue(key, defaultValue);
|
||||
if (string.IsNullOrWhiteSpace(value))
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(defaultValue))
|
||||
return defaultValue?.Trim();
|
||||
throw new System.Exception($"获取配置{key}异常");
|
||||
}
|
||||
return value?.Trim();
|
||||
}
|
||||
}
|
||||
}
|
||||
75
IoTServer/Common/DataPersist.cs
Normal file
75
IoTServer/Common/DataPersist.cs
Normal file
@@ -0,0 +1,75 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Talk.Redis;
|
||||
|
||||
namespace IoTServer.Common
|
||||
{
|
||||
public class DataPersist
|
||||
{
|
||||
/// <summary>
|
||||
/// 是否使用redis对数据持久化,否则存内存
|
||||
/// </summary>
|
||||
private bool RedisPersist = true;
|
||||
RedisManager redisManager;
|
||||
static Dictionary<string, string> data = new Dictionary<string, string>();
|
||||
|
||||
public DataPersist(string redisConfig)
|
||||
{
|
||||
RedisPersist = !string.IsNullOrWhiteSpace(redisConfig);
|
||||
if (RedisPersist)
|
||||
redisManager = new RedisManager(1, redisConfig);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 读
|
||||
/// </summary>
|
||||
/// <param name="key"></param>
|
||||
/// <returns></returns>
|
||||
public string Read(string key)
|
||||
{
|
||||
key = "iot_" + key;
|
||||
if (RedisPersist)
|
||||
{
|
||||
return redisManager.GetString(key);
|
||||
}
|
||||
else if (data.Keys.Contains(key))
|
||||
{
|
||||
return data[key];
|
||||
}
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
public string Read(int key)
|
||||
{
|
||||
return Read(key.ToString());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 写
|
||||
/// </summary>
|
||||
/// <param name="key"></param>
|
||||
/// <param name="value"></param>
|
||||
public void Write(string key, string value)
|
||||
{
|
||||
key = "iot_" + key;
|
||||
if (RedisPersist)
|
||||
{
|
||||
redisManager.Set(key, value);
|
||||
}
|
||||
else if (data.Keys.Contains(key))
|
||||
{
|
||||
data[key] = value;
|
||||
}
|
||||
else
|
||||
{
|
||||
data.Add(key, value);
|
||||
}
|
||||
}
|
||||
|
||||
public void Write(int key, string value)
|
||||
{
|
||||
Write(key.ToString(), value);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user