public static void Parse(List <byte> recvDatas, uint siteNum, Sites sites) { byte b = 0; STATUS s = STATUS.R_UNKOWN; int nEB = 0; int nParsed = 0; for (int i = 0; i < recvDatas.Count; ++i) { string sss = ByteToHexString(recvDatas.ToArray(), recvDatas.ToArray().Length); //System.IO.File.AppendAllText("c:/data.txt", sss + "\r\n"); //Console.WriteLine(sss); b = recvDatas[i]; switch (s) { case STATUS.R_UNKOWN: { if (b == 0xEB) { s = STATUS.R_EB; nEB = i; } } break; case STATUS.R_EB: { if (b == 0x90) { s = STATUS.R_90; } } break; case STATUS.R_90: { if (i - nEB == 9) { if (b == 0xAA) { siteNum = recvDatas[nEB + 2]; uint sensorNum = recvDatas[nEB + 3]; //recvDatas[nEB+8],recvDatas[nEB+7],recvDatas[nEB+6],recvDatas[nEB+5] //double d = (double)BitConverter.ToUInt16(new byte[]{recvDatas[nEB+6],recvDatas[nEB+5]},0); double?[] data = new double?[5]; //用来存放数据,长度可能还会更改 //double d = (double)BitConverter.ToUInt16(new byte[] { recvDatas[nEB + 8], recvDatas[nEB + 7], recvDatas[nEB + 6], recvDatas[nEB + 5] }, 0); SENSOR_TYPE type = sites.GetType(siteNum, sensorNum); switch (type) { case SENSOR_TYPE.SHUIWEI: case SENSOR_TYPE.YALI: case SENSOR_TYPE.WENDU: case SENSOR_TYPE.DIANYA: data[0] = (double)BitConverter.ToUInt16(new byte[] { recvDatas[nEB + 6], recvDatas[nEB + 5] }, 0); break; case SENSOR_TYPE.MINGQU: break; case SENSOR_TYPE.GUANDAO: data[0] = (double)BitConverter.ToUInt16(new byte[] { recvDatas[nEB + 6], recvDatas[nEB + 5] }, 0); data[1] = (double)BitConverter.ToUInt16(new byte[] { recvDatas[nEB + 8], recvDatas[nEB + 7] }, 0); break; default: break; } Sensor ss; Site site; do { sites.Get(siteNum, out site); site.Get(sensorNum, out ss); if (site == null || ss == null) { break; } //if (ss.formulaDelegate == null) //{ // Log.Write("传感器没有编译好的公式"); // break; //} //double saveData = ss.formulaDelegate.Invoke(d); double dataOrigion = data[0].Value; double saveData = 0; double sumData = 0; double liangcheng = 0; bool flag = double.TryParse(ss.formula, out liangcheng); //此处的"公式"废弃不用,使用"量程"替代 liangcheng = flag ? liangcheng : 1; switch (type) { case SENSOR_TYPE.YALI: case SENSOR_TYPE.WENDU: saveData = (data[0].Value - 819) / (4096 - 819) * liangcheng; break; case SENSOR_TYPE.SHUIWEI: double tempData = (data[0].Value - 819) / (4096 - 819) * liangcheng; saveData = ss.wellDepth - ss.ropeDepth + tempData; break; case SENSOR_TYPE.MINGQU: break; case SENSOR_TYPE.GUANDAO: saveData = data[0].Value / 100 * liangcheng; //DateTime? lastTime = DbServer.GetLastCreateTime(siteNum, sensorNum); //if (lastTime == null) //{ // sumData = 0; // File.AppendAllText("d:/log.txt","if lasttime=null sumData=" + sumData) ; //} //else //{ // double substractHours = DateTime.Now.Subtract(lastTime.Value).Seconds / 3600.0; // sumData = saveData * substractHours; // File.AppendAllText("d:/log.txt", "if lasttime=null else sumData=" + sumData); //} break; case SENSOR_TYPE.DIANYA: saveData = data[0].Value / 100; break; default: break; } DateTime?lastTime = DbServer.GetLastCreateTime(siteNum, sensorNum); if (lastTime == null) { sumData = 0; File.AppendAllText("d:/log.txt", "if lasttime=null sumData=" + sumData); } else { double substractHours = DateTime.Now.Subtract(lastTime.Value).Seconds / 3600.0; sumData = saveData * substractHours; File.AppendAllText("d:/log.txt", "if lasttime=null else sumData=" + sumData); } //if (type == SENSOR_TYPE.GUANDAO || type == SENSOR_TYPE.MINGQU) //{ // if (saveData <= 0.00005) // { // saveData = 0.0; // } //} saveData *= Balance.GetParam((int)siteNum, (int)sensorNum); File.AppendAllText("d:/log.txt", "sumData=" + sumData.ToString()); //double weight = GetWeight(siteNum, sensorNum); double weight = 1; if (weight <= 0.0) { break; } //weight = (type == SENSOR_TYPE.GUANDAO || type == SENSOR_TYPE.MINGQU) ? weight:1; saveData = Math.Round(saveData, 3); saveData = saveData <= 0 ? 0 : saveData; sumData = Math.Round(sumData, 3); DbServer.SaveAsync(siteNum, sensorNum, dataOrigion, saveData, sumData, weight, DateTime.Now); // if (siteNum == 1 && sensorNum == 2) // { // DbServer.SaveAsync(1, 3, saveData / 100, 1, DateTime.Now); // } } while (false); } else { //数据格式错误 } nParsed = i; s = STATUS.R_UNKOWN; nEB = 0; } } break; } } recvDatas.RemoveRange(0, nParsed); }
public static void Proc() { Sites sites; Sites.Load(out sites, new DbServer()); DbServer.MarkNotNew(); //编译公式 Site st; Sensor ss; int i = 0; while (sites.GetAt(i++, out st)) { int j = 0; while (st.GetAt(j++, out ss)) { string err = ""; ss.formulaDelegate = FormulaBuilder.TryCompile(ss.formula, out err); if (err != "") { Log.Write("编译公式出错:" + err); continue; } } } //获取本机所有IP IPAddress[] ips = Dns.GetHostAddresses(Dns.GetHostName()); //获取服务器里所有的服务器IP Server server = DbServer.ReadServerConfig(); bool exists = ips.Any(s => s.ToString() == server.IP); if (exists) { //说明是本机 if (server.IsEnable) { serverThread = new Thread(delegate() { TcpServerComm tsc = new TcpServerComm(server, sites); tsc.Start(); }); serverThread.Start(); } } SitesComm sc = new SitesComm(sites); sc.Start(); while (bWork) { //Thread.Sleep(10000); Thread.Sleep(1000); if (DbServer.IsNewConfig()) { sc.Stop(); Sites.Load(out sites, new DbServer()); sc.sites = sites; DbServer.MarkNotNew(); sc.Start(); } } sc.Stop(); }
//以左边为准,校正右边 private void CalcBalanceParams() { foreach (var kv in balanceFormulas) { double sumLeft = 0; double sumRight = 0; string[] strSiteSensors = kv.Key.Split(','); foreach (var sss in strSiteSensors) { string[] ss = sss.Split(':'); if (ss.Length == 2) { string strSite = ss[0]; string strSensor = ss[1]; int site = Convert.ToInt32(strSite); int sensor = Convert.ToInt32(strSensor); sumLeft += DbServer.Sum(site, sensor, DateTime.Now.AddDays(-10), DateTime.Now, 3); } } strSiteSensors = kv.Value.Split(','); foreach (var sss in strSiteSensors) { string[] ss = sss.Split(':'); if (ss.Length == 2) { string strSite = ss[0]; string strSensor = ss[1]; int site = Convert.ToInt32(strSite); int sensor = Convert.ToInt32(strSensor); sumRight += DbServer.Sum(site, sensor, DateTime.Now.AddDays(-10), DateTime.Now, 3); } } if (sumLeft > 0 && sumRight > 0) { double muti = (sumLeft / sumRight) * (sumLeft / sumRight); if (muti > 10 && muti < 0.1) { muti = 1; } foreach (var sss in strSiteSensors) { string[] ss = sss.Split(':'); if (ss.Length == 2) { string strSite = ss[0]; string strSensor = ss[1]; int site = Convert.ToInt32(strSite); int sensor = Convert.ToInt32(strSensor); lock (balanceParams) { if (!balanceParams.ContainsKey(site)) { balanceParams.Add(site, new Dictionary <int, double>()); } if (!balanceParams[site].ContainsKey(sensor)) { balanceParams[site].Add(sensor, 1.0); } balanceParams[site][sensor] = muti; Log.Write(string.Format("MUTI:{0}:{1}=={2}", site, sensor, muti)); } } } } } }
bool IDatabase.SaveConfig(byte[] config) { return(DbServer.SaveConfig(config)); }
//private static MySqlConnection connection=new MySqlConnection(); //private static ORDesignerDataContext ShuiwenDbContext = null; #region IDatabase 成员 byte[] IDatabase.ReadConfig() { return(DbServer.ReadConfig()); }
private void RecvProc() { while (bWork) { Thread.Sleep(10 * 1000); //Thread.Sleep(1000); try { smsRecvProc = new Process(); smsRecvProc.StartInfo.FileName = AppDomain.CurrentDomain.BaseDirectory + "\\shuiwenSMS.exe"; smsRecvProc.StartInfo.UseShellExecute = false; // 是否使用外壳程序 smsRecvProc.StartInfo.CreateNoWindow = true; //是否在新窗口中启动该进程的值 smsRecvProc.StartInfo.RedirectStandardInput = true; // 重定向输入流 smsRecvProc.StartInfo.RedirectStandardOutput = true; //重定向输出流 smsRecvProc.StartInfo.RedirectStandardError = true; //重定向错误流 smsRecvProc.OutputDataReceived += new DataReceivedEventHandler(OutputDataReceived); smsRecvProc.EnableRaisingEvents = true; smsRecvProc.Start(); smsRecvProc.BeginOutputReadLine(); smsRecvProc.WaitForExit(); if (smsData.Length > 0) { string[] msgs = smsData.Split('|'); foreach (string msg in msgs) { msg.Trim(); msg.Trim('\r', '\n'); string[] contex = msg.Split(new String[] { "----" }, StringSplitOptions.None); if (contex.Length == 3 && contex[0] == "MSG") { string[] datas = contex[1].Split(';'); foreach (var data in datas) { Sensor ss; Site site; string[] d = data.Split(','); if (d.Length == 3) { uint siteNum = Convert.ToUInt32(d[0]); uint sensorNum = Convert.ToUInt32(d[1]); SENSOR_TYPE type = sites.GetType(siteNum, sensorNum); if (type == SENSOR_TYPE.UNKNOWN) { Log.Write("短信收到没有设置的分站和传感器号" + data); } sites.Get(siteNum, out site); site.Get(sensorNum, out ss); double rawData = Convert.ToDouble(d[2]); IFormatProvider ifp = new CultureInfo("zh-CN", true); DateTime time = DateTime.ParseExact(contex[2], "yyyyMMddHHmmss", ifp); int weight = (type == SENSOR_TYPE.GUANDAO || type == SENSOR_TYPE.MINGQU) ? ss.interval : 1; if (ss.formulaDelegate == null) { Log.Write("传感器没有编译好的公式"); continue; } double saveData = ss.formulaDelegate.Invoke(rawData); if (type == SENSOR_TYPE.GUANDAO || type == SENSOR_TYPE.MINGQU) { if (saveData <= 0.01) { saveData = 0.0; } } DbServer.SaveAsync(siteNum, sensorNum, saveData, saveData, saveData, weight, time); } } } else { if (msg.Length > 0) { Log.Write("无法解析的短信" + msg); } } } } } catch (System.Exception ex) { Log.Write(ex.Message); } finally { smsData = ""; } } }
protected override void OnStart(string[] args) { System.Diagnostics.Debugger.Launch(); DbServer.ReadConnString(); WorkProc.Work(); }