-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathRemoteService.cs
192 lines (179 loc) · 7.98 KB
/
RemoteService.cs
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
using dotnetCampus.Configurations;
using dotnetCampus.Configurations.Core;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Quartz;
using Quartz.Impl;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Text.Json.Nodes;
using System.Threading.Tasks;
namespace PayListener
{
internal class RemoteService
{
/// <summary>
/// 启动心跳上报
/// true: 启动
/// false: 停止
/// </summary>
public static async Task<bool> HeartBeatManagerAsync(bool type)
{
try
{
StdSchedulerFactory factory = new StdSchedulerFactory();
IScheduler scheduler = await factory.GetScheduler();
var gropName = "Tasks";
var jobName = "HeartBeat_job";
var tiggerName = "HeartBeat_tigger";
if (type)
{
IJobDetail job_heartbeat = JobBuilder.Create<HeartBeatJob>()
.WithIdentity(jobName, gropName)
//.UsingJobData("key", "value")//为任务的具体任务传递参数,键值对(非必须)
.Build();//创建
ITrigger trigger_heartbeat = TriggerBuilder.Create()
.WithIdentity(tiggerName, gropName) //为触发器的tiggerName和gropName赋值,相当与给予一个身份
.StartNow() //现在开始
.WithSimpleSchedule(x => x
.WithIntervalInSeconds(30) //触发时间,30秒一次。
.RepeatForever()) //不间断重复执行
.Build(); //最终创建
await scheduler.ScheduleJob(job_heartbeat, trigger_heartbeat); //把任务,触发器加入调度器。
await scheduler.Start();
}
else
{
TriggerKey triggerKey = new TriggerKey(tiggerName, gropName);
JobKey jobKey = new JobKey(jobName, gropName);
if (scheduler != null)
{
await scheduler.PauseTrigger(triggerKey);
await scheduler.UnscheduleJob(triggerKey);
await scheduler.DeleteJob(jobKey);
await scheduler.Shutdown();// 关闭
}
}
return true;
}
catch (Exception)
{
return false;
}
}
public class List
{
public int code { get; set; }
public string msg { get; set; }
}
public static string AppPush(string t, string type, string price)
{
var config = Program.config;
string timestamp = new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds().ToString();
string sign = MD5Encrypt32( type + price + t + config.CallbackKey);
var data = new JsonObject();
data.Add("t", t);
data.Add("type", type);
data.Add("price", price);
data.Add("sign", sign.ToLower());
try
{
string res = Post((config.Callbackssl ? "https://" : "http://") + config.CallbackHost + "/appPush", data);
JObject? PostInfoList = (JObject?)JsonConvert.DeserializeObject(res);
if (PostInfoList?["code"]?.ToString() == "1")
{
Shell.WriteLine("{0}|上报:{1}", "信息", "收款上报: "+ data.ToJsonString());
return "上报成功";
}
else
{
Shell.WriteLine("{0}|上报:{1}", "警告", "收款上报失败: " + PostInfoList?["msg"]?.ToString() ?? "无法获取到错误信息");
return PostInfoList?["msg"]?.ToString() ?? "无法获取到错误信息";
}
}
catch (Exception e)
{
Shell.WriteLine("{0}|上报:{1}", "错误", "收款上报发生错误: " + e.Message);
return e.Message;
}
}
public static string MD5Encrypt32(string txt)
{
byte[] sor = Encoding.UTF8.GetBytes(txt);
MD5 md5 = MD5.Create();
byte[] result = md5.ComputeHash(sor);
StringBuilder strbul = new StringBuilder(40);
for (int i = 0; i < result.Length; i++)
{
//加密结果"x2"结果为32位,"x3"结果为48位,"x4"结果为64位
strbul.Append(result[i].ToString("x2"));
}
return strbul.ToString();
}
public static string Post(string url, JsonObject data)
{
HttpClient httpClient = new HttpClient();
var httpContent = new StringContent(data.ToString(), Encoding.UTF8, "application/json");
var response = httpClient.PostAsync(url, httpContent).Result;
var result = response.Content.ReadAsStringAsync().Result;
httpContent.Dispose();
return result;
}
}
public class HeartBeatJob : IJob
{
public static string LastStatus = "暂未上报";
Task IJob.Execute(IJobExecutionContext context)
{
var config = Program.config;
string timestamp = new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds().ToString();
string sign = MD5Encrypt32(timestamp + config.CallbackKey);
var data = new JsonObject();
data.Add("t", timestamp);
data.Add("sign", sign.ToLower());
try
{
string res = Post((config.Callbackssl ? "https://" : "http://") + config.CallbackHost + "/appHeart", data).Result;
//MessageBox.Show("上报成功:\n" + res);
JObject? resInfo = (JObject?)JsonConvert.DeserializeObject(res);
var rowdata = new object[] { new object[] { $"{DateTime.Now.ToLocalTime():yyyy-MM-dd HH:mm:ss}", resInfo?["code"]?.ToString() == "1" ? "成功" : "失败", res } };
Shell.WriteLine("{0}|上报:{1}", resInfo?["code"]?.ToString() == "1" ? "信息" : "错误", "心跳上报返回: " + res);
Form1.form1.listView1.Invoke(Form1.updateHbList, rowdata);
//Form1.form1.Invoke(Form1.updateHbList, rowdata);
LastStatus = res;
}
catch (Exception a)
{
Shell.WriteLine("{0}|上报:{1}", "错误", "上报任务发生错误: " + a.Message);
//MessageBox.Show("发生未预料的错误:\n" + a.Message);
//throw;
}
return Task.CompletedTask;
}
public static string MD5Encrypt32(string txt)
{
byte[] sor = Encoding.UTF8.GetBytes(txt);
MD5 md5 = MD5.Create();
byte[] result = md5.ComputeHash(sor);
StringBuilder strbul = new StringBuilder(40);
for (int i = 0; i < result.Length; i++)
{
//加密结果"x2"结果为32位,"x3"结果为48位,"x4"结果为64位
strbul.Append(result[i].ToString("x2"));
}
return strbul.ToString();
}
public async Task<string> Post(string url, JsonObject data)
{
HttpClient httpClient = new HttpClient();
var httpContent = new StringContent(data.ToString(), Encoding.UTF8, "application/json");
var response = await httpClient.PostAsync(url, httpContent);
var result = await response.Content.ReadAsStringAsync();
httpContent.Dispose();
return result;
}
}
}