天津投入产出系统后端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
2.5 KiB

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using QMAPP.FJC.Entity.EnergyManage;
namespace MeterReadingService
{
public partial class MeterReadingService : ServiceBase
{
System.Threading.Timer readtimer;
int _Period = 60000;
List<Meter> _MeterList = new List<Meter>();
MeterReader.ElectricMeterReader reader = new MeterReader.ElectricMeterReader();
public MeterReadingService()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
QMAPP.FJC.BLL.EnergyManage.MeterBLL meterbll = new QMAPP.FJC.BLL.EnergyManage.MeterBLL();
_MeterList = meterbll.GetList(new Meter { });
string setperiod = System.Configuration.ConfigurationManager.AppSettings["ReadPeriod"];
if (!int.TryParse(setperiod, out _Period))
{
_Period = 60000;
}
if (readtimer == null)
{
readtimer = new System.Threading.Timer(new System.Threading.TimerCallback(readertimer_tick));
}
readtimer.Change(0, _Period);
}
protected override void OnStop()
{
if (readtimer != null)
{
readtimer.Change(System.Threading.Timeout.Infinite, System.Threading.Timeout.Infinite);
readtimer.Dispose();
}
_MeterList.Clear();
}
private void readertimer_tick(object obj)
{
QMAPP.FJC.BLL.EnergyManage.MeterReadingsBLL mrbll = new QMAPP.FJC.BLL.EnergyManage.MeterReadingsBLL();
foreach (var meter in _MeterList)
{
var lastreading=mrbll.GetLastReading(meter.METER_CODE);
var read = reader.Read(meter);
MeterReadings newreading = new MeterReadings
{
DIFF_WITH_LAST = lastreading == null ? 0 : (decimal)read - lastreading.READING,
ERROR = "",
METER_PID = meter.PID,
READING = (decimal)read,
PID = Guid.NewGuid().ToString(),
READ_RESULT = read > 0 ? "1" : "0",
READ_TIME = DateTime.Now
};
mrbll.Insert(newreading);
}
}
}
}