1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- using System;
- using System.Threading;
- using System.Linq;
- using System.Data;
- using Library;
- using MySystem.Models;
- using LitJson;
- using System.Collections.Generic;
- using System.Security.Cryptography;
- namespace MySystem
- {
- public class TianYuVoiceHelper
- {
- public readonly static TianYuVoiceHelper Instance = new TianYuVoiceHelper();
- private TianYuVoiceHelper()
- {
- }
- string accesskeyld = "IOT_ACCOUNT_TEST_CSYH";
- string accesskeySecret = "AtR4g5sa24fdy6GII3hts75_CSYH";
- string reqUrl = "https://voicetest.tysmartpos.com/api/bank/pushmsg";
- public void Start()
- {
- Thread thread = new Thread(listen);
- thread.IsBackground = true;
- thread.Start();
- }
- public void listen()
- {
- while (true)
- {
- string content = RedisDbconn.Instance.RPop<string>("TianYuVoiceQueue");
- if (!string.IsNullOrEmpty(content))
- {
- try
- {
- doSomething(content);
- Thread.Sleep(10);
- }
- catch (Exception ex)
- {
- LogHelper.Instance.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "天喻云喇叭异常");
- }
- }
- else
- {
- Thread.Sleep(1000);
- }
- }
- }
- public string doSomething(string content)
- {
- JsonData jsonObj = JsonMapper.ToObject(content);
- SortedList<string, string> data = new SortedList<string, string>();
- data.Add("accessKeyId", accesskeyld);
- data.Add("requestId", jsonObj["OrderNo"].ToString());
- data.Add("timestamp", function.ConvertDateTimeInt(DateTime.Now).ToString());
- data.Add("deviceId", jsonObj["DeviceId"].ToString());
- data.Add("content", "来客吧收款" + jsonObj["Amount"].ToString() + "元");
-
-
- string result = post(data);
- return result;
- }
- private string post(SortedList<string, string> data)
- {
- string signString = function.BuildQueryString(data);
- data.Add("sign", hmac_sha256(signString));
- string req = Newtonsoft.Json.JsonConvert.SerializeObject(data);
- string result = function.PostWebRequest(reqUrl, req, "application/json");
- return result;
- }
- #region hmac_sha256算法
- public string hmac_sha256(string message)
- {
- var encoding = new System.Text.UTF8Encoding();
- byte[] keyByte = encoding.GetBytes(accesskeySecret);
- byte[] messageBytes = encoding.GetBytes(message);
- using (var hmacsha256 = new HMACSHA256(keyByte))
- {
- byte[] hashmessage = hmacsha256.ComputeHash(messageBytes);
- return Convert.ToBase64String(hashmessage);
- }
- }
- #endregion
- }
- }
|