using SensorHub.Servers; using SensorHub.Servers.Commands.CASICCommands; using SensorHub.Servers.JsonFormat; using SuperSocket.SocketBase.Command; using SuperSocket.SocketBase.Protocol; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Newtonsoft.Json; namespace SensorHub.Lamp { public class Lamp : CommandBase<CasicSession, StringRequestInfo> { public override void ExecuteCommand(CasicSession session, StringRequestInfo requestInfo) { //TODO: construct the receving casic data String preamble = requestInfo.Parameters[0]; String version = requestInfo.Parameters[1]; String leng = requestInfo.Parameters[2]; String devCode = requestInfo.Parameters[3]; String routeFlag = requestInfo.Parameters[4]; String dstNodeAddr = requestInfo.Parameters[5]; String pduType = requestInfo.Parameters[6]; String seq = requestInfo.Parameters[7]; String settings = requestInfo.Parameters[8]; String source = requestInfo.Parameters[9]; if (source.Contains("-")) { session.Send("HTTP/1.1 200 OK\r\n\r\n\r\n"); session.Close(); } //print the receving data String devType = "单灯控制器"; String operType = Common.getOpeTypeByPdu(pduType); session.Logger.Info("AD接收数据:" + requestInfo.Body); session.Logger.Info("设备类型:" + devType); session.Logger.Info("操作类型:" + operType); session.Logger.Info("会话:" + session.HubAddr + "," + session.SessionID); //判断是返回的设置确认数据帧, 回复第三方 if (operType == "SetResponse") { Common.sendSetResponse(session, devCode, "Lamp"); return; } //if (operType == "TrapResponse" || operType == "SetRequest" || operType == "GetRequest") //{ // return; //} List<Tag> tags = Common.getTags(settings, session); //具体业务处理 String collectDate = ""; List<String> eventList = new List<String>(); List<DatasJson> datasList = new List<DatasJson>(); String uptime = null;//数据采集时间 float electricity_1 = -1.0f; float voltage_1 = -1.0f; float electricity_2 = -1.0f; float voltage_2 = -1.0f; try { foreach (Tag tag in tags) { if (!(tag is UploadTag)) { //非业务处理 if (tag != null && tag is SystemDateTag) { SystemDateTag systemDateTag = tag as SystemDateTag; collectDate = systemDateTag.CollectDate; continue; } if (tag != null && tag is SensorException0Tag) { SensorException0Tag sensorException0 = tag as SensorException0Tag; int state = sensorException0.state; if (state == 0) continue; eventList.Add(getLampAlarm(state)); session.Logger.Info("通道一发送容错信息:oid:" + tag.Oid + ";value:" + state); continue; } if (tag != null && tag is SensorException1Tag) { SensorException1Tag sensorException1 = tag as SensorException1Tag; int state = sensorException1.state; if (state == 0) continue; eventList.Add(getLampAlarm(state + 4)); session.Logger.Info("通道二发送容错信息:oid:" + tag.Oid + ";value:" + state); continue; } } else { //业务处理 UploadTag uploadTag = tag as UploadTag; switch (uploadTag.BizType) { case 14: //电流 TagHandler eleHandler = new ElectricityTagHandler(); eleHandler.resolve(tag, session); if (eleHandler.DataList.Count > 0) { if (electricity_1 == -1.0f) { DateTime upTime = Convert.ToDateTime(collectDate + " " + eleHandler.CollecTime); uptime = upTime.ToString("yyyy") + upTime.ToString("MM") + upTime.ToString("dd") + upTime.ToString("HH") + upTime.ToString("mm") + upTime.ToString("ss"); electricity_1 = (float)eleHandler.DataList[0]; } else { electricity_2 = (float)eleHandler.DataList[0]; } } break; case 15: //电压 TagHandler volHandler = new VoltageTagHandler(); volHandler.resolve(tag, session); if (volHandler.DataList.Count > 0) { if (voltage_1 == -1.0f) { voltage_1 = (float)volHandler.DataList[0]; } else { voltage_2 = (float)volHandler.DataList[0]; } } break; default: session.Logger.Info("未知业务类型!"); break; } } } if (uptime != null) { datasList.Add(new LampDatasJson(uptime, electricity_1, voltage_1, electricity_2, voltage_2)); } sendMessage(session, "Lamp", devCode, -1, eventList, datasList, null); byte[] btPdu = new byte[2]; //2个字节 btPdu[0] = Common.getRespOperType(operType, true); btPdu[1] = 0x90; Common.sender433Config(session, devCode, btPdu, 0x05); } catch (Exception e) { session.Logger.Error(e.ToString()); } } private void sendMessage(CasicSession session, String devName, String devCode, int cell, List<String> eventList, List<DatasJson> datasList, List<String> startupList) { DateTime now = DateTime.Now; String logtime = now.ToString("yyyy") + now.ToString("MM") + now.ToString("dd") + now.ToString("HH") + now.ToString("mm") + now.ToString("ss"); DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); //当地时区 long timeStamp = (long)(now - startTime).TotalMilliseconds; //相差毫秒数 try { if (eventList != null && eventList.Count > 0) { String message = JsonConvert.SerializeObject(new Json("Event", devName, devCode, new LampEventJson(devName + "Event", eventList, datasList, logtime), timeStamp)); if (Common.SendMessage(message)) { session.Logger.Info("往第三方发送数据:" + message); } else { session.Logger.Info("未连接上第三方服务器"); } return; } if (datasList != null && datasList.Count > 0) { String message = JsonConvert.SerializeObject(new Json("Data", devName, devCode, new DataJson(devName + "Data", cell, datasList, logtime), timeStamp)); if (Common.SendMessage(message)) { session.Logger.Info("往第三方发送数据:" + message); } else { session.Logger.Info("未连接上第三方服务器"); } } } catch (Exception ex) { session.Logger.Error("往第三方发送数据出错:" + ex.Message); } } private String getLampAlarm(int state) { switch (state) { case 0: return "LampChannel1Normal";//通道一正常 case 1: return "LampChannel1TurnOnFail";//通道一开灯异常 case 2: return "LampChannel1TurnOffFail"; //通道一关灯异常 case 3: return "LampChannel1LowElectricityAlarm"; //通道一电流低于阈值告警 case 4: return "LampChannel2Normal"; //通道二正常 case 5: return "LampChannel2TurnOnFail"; //通道二开灯异常 case 6: return "LampChannel2TurnOffFail"; //通道二关灯异常 case 7: return "LampChannel2LowElectricityAlarm"; //通道二电流低于阈值告警 case 15: return "LampChannel1Unknown"; //通道一未知异常 case 19: return "LampChannel2Unknown"; //通道二未知异常 default: return "LampChannelUnknown"; //未知异常 } } } }