using CommLayerFactory; using ICommLayer; using Nancy; using Nancy.Bootstrapper; using Nancy.Hosting.Self; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; namespace WcfControlMonitorLib { public class SampleModule : NancyModule { private static NancyHost _server; private Object thisLock = new Object(); public static void Start() { _server = new NancyHost(new Bootstrapper(), new Uri("http://192.168.0.30:8088")); //_server = new NancyHost(new Bootstrapper(), new Uri("http://192.168.0.30:8686")); _server.Start(); } public class Bootstrapper : DefaultNancyBootstrapper { /// /// Register only NancyModules found in this assembly /// protected override IEnumerable Modules { get { return GetType().Assembly.GetTypes().Where(type => type.BaseType == typeof(NancyModule)).Select(type => new ModuleRegistration(type)); } } } public SampleModule() { // 设置库存上下限 Post["/API/SetInventory"] = _ => { /* { "storage": [{ // 类型 1.2.3.4.5 "type": "1", // 上下限 0.1.2 "inventory": "0" }, { "type": "2", "inventory": "" }, { "type": "3", "inventory": "2" }] } */ var body = this.Request.Body; int length = (int)body.Length; byte[] data = new byte[length]; body.Read(data, 0, length); string jsonStr = Encoding.UTF8.GetString(data); //JObject json = JsonConvert.DeserializeObject(jsonStr); //Dictionary json = JsonConvert.DeserializeObject>(jsonStr); WCS_STORAGE jsonObject = JsonConvert.DeserializeObject(jsonStr); // 定义一个 JSON 键值对对象 //Dictionary jsonObject1 = new Dictionary(); //// 添加键值对,品种1.2.3.4.5 db区及设备号 //jsonObject1["1"] = 7060; //jsonObject1["2"] = 7061; //jsonObject1["3"] = 7062; //jsonObject1["4"] = 7063; //jsonObject1["5"] = 7064; //for (int i = 0; i < jsonObject.storage; i++) //type 1--对应电气db区7060 //type 2--对应电气db区7061 。。。 foreach (STORAGE storage in jsonObject.storage) { string deviceIndexii = storage.type; int deviceinedx = 0; switch (deviceIndexii) { case "1": deviceinedx = 7060; break; case "2": deviceinedx = 7061; break; case "3": deviceinedx = 7062; break; case "4": deviceinedx = 7063; break; case "5": deviceinedx = 7064; break; } //var deviceinedx = int.Parse(Convert.ToString(jsonObject[i]["type"])); ISendDeviceOrder sdo; //Model.MDevice devinfo; //devinfo = Model.CGetInfo.GetDeviceInfo(deviceinedx); StringBuilder[] witemnames = { new StringBuilder("") }; StringBuilder[] wv = { new StringBuilder("2") }; witemnames[0].Clear(); witemnames[0].Append(Model.CGeneralFunction.DBGet).Append(".").Append(Convert.ToString(deviceinedx + 0)).Append(",b");//20130510 //witemnames[0].Append(Model.CGeneralFunction.DBGet).Append(".").Append(Convert.ToString(devinfo.Dbw2Address + 0)).Append(",b");//20130510 wv[0].Clear(); wv[0].Append(storage.inventory); //wv[0].Append(jsonObject[i]["inventory"]); sdo = CommModeCreate.CreateSendDeviceOrder(deviceinedx); //7060-7064 db区写入上下限值 sdo.WriteDBData(witemnames, wv, "S7 connection_2"); //sdo.WriteDBData(witemnames, wv, devinfo.S7Connection); } byte[] responseData = Encoding.UTF8.GetBytes(jsonStr); return new Response() { Contents = stream => { stream.Write(responseData, 0, responseData.Length); }, StatusCode = HttpStatusCode.OK }; }; } } public class WCS_STORAGE { public List storage { get; set; } } public class STORAGE { public string type { get; set; } public string inventory { get; set; } } }