SMF.cs
4.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using static System.Net.WebRequestMethods;
namespace OnlineStore.Common
{
public class SMF
{
public static string DeviceType = "MIMO_G2";
static string _server = Setting_Init.App_http_server;
static string server
{
get
{
//_server = "http://192.168.1.243/smf-core";
if (_server.EndsWith("/"))
{
return _server.Remove(_server.Length - 1, 1);
}
else
return _server;
}
}
public static bool UploadLangJsonData(string json, out SmfResult smfResult)
{
var insertindex = json.IndexOf("{");
json = json.Insert(insertindex + 1, "\"type\": \"" + DeviceType + "\",");
return UploadJsonData("/api/translation/resource", json, out smfResult);
}
public static bool UploadJsonData(string api, string json, out SmfResult smfResult)
{
smfResult = new SmfResult();
if (string.IsNullOrEmpty(server))
return false;
try
{
using (MyWebClient wc = new MyWebClient(10 * 1000))
{
wc.Headers.Add("Content-Type", "application/json;charset=UTF-8");
var resp = wc.UploadData(server + api, Encoding.UTF8.GetBytes(json));
smfResult = JsonHelper.DeserializeJsonToObject<SmfResult>(Encoding.UTF8.GetString(resp));
}
}
catch (Exception e)
{
LogUtil.info($"UploadJsonData:{api}" + e.ToString());
}
return smfResult.okResult;
}
public static List<SmfLangData> DownloadLngData()
{
string api = "/api/translation/resource?type=" + DeviceType;
try
{
using (MyWebClient wc = new MyWebClient(10 * 1000))
{
wc.Headers.Add("Content-Type", "application/json;charset=UTF-8");
var resp = wc.DownloadData(server + api);
var jsondata = Encoding.UTF8.GetString(resp);
return JsonConvert.DeserializeObject<List<SmfLangData>>(jsondata);
}
}
catch (Exception e)
{
LogUtil.info($"DownloadLngData:{api}" + e.ToString());
}
return new List<SmfLangData>();
}
/// <summary>
/// 上传监控
/// </summary>
static string Addr_uploadVideo = "/service/video/upload";
public static bool UploadVideo(string cid, string img)
{
try
{
ServicePointManager.Expect100Continue = false;
Dictionary<string, string> paramMap = new Dictionary<string, string>
{
{ "cid", cid },
{ "img", ParseBase64Str(img) }
};
var resultStr = HttpHelper.Post(server+Addr_uploadVideo,JsonHelper.SerializeObject(paramMap), 300);
SmfResult smfResult = JsonHelper.DeserializeJsonToObject<SmfResult>(resultStr);
if (smfResult != null)
{
return smfResult.code == 0;
}
}
catch (Exception ex)
{
LogUtil.error($"{cid} UploadVideo error ", ex);
}
return false;
}
static string ParseBase64Str(string base64)
{
if (base64 == null) return "";
string dump = base64.Trim('"', '"').Replace("%", "").Replace(",", "").Replace(" ", "+").Replace("\\", "");
if (dump.Length % 4 > 0)
{
dump = dump.PadRight(dump.Length + 4 - dump.Length % 4, '=');
}
return dump;
}
}
//{"code":0,"data":"ok","msg":"ok","msgKey":"smfcore.ok","okResult":true}
public class SmfResult
{
public int code;
public string data;
public string msg;
public string msgKey;
public bool okResult;
}
public class SmfLangData
{
public string lanCode;
public Dictionary<string, string> resourceMap;
}
}