def add_group(self): print "Adding group" self.group1 = Group(name="group") self.group1.save() self.group2 = Group(name="group2") self.group2.save() self.area = AreaInfo(areaid="TEST000000", areanm="TEST", groupid=self.group1) self.area.save() self.place = PlaceInfo(placeid="TEST000000", placenm="TEST", areaid=self.area, groupid=self.group2) self.place.save() self.devicetype = DeviceTypeInfo(typeid="0", devicetype=1, typenm="Controller") self.devicetype.save() print "Enter controller id from 0 to 19, please enter a single digit.\n" for ev in range(14): id1 = raw_input() test_ev = "0000-0000-0000" + id1 self.con = ControllerInfo(controllerid=test_ev, placeid=self.place, controllernm="TEST cont", type=self.devicetype) self.con.save()
class ResultTest(TestCase): def setUp(self): self.group = Group(name="group") self.group.save() self.area = AreaInfo(areaid="TEST000000",areanm="TEST",groupid = self.group) self.area.save() self.place = PlaceInfo(placeid="TEST000000",placenm="TEST",areaid=self.area,groupid = self.group) self.place.save() self.devicetype = DeviceTypeInfo(typeid="0",devicetype=1,typenm="Controller") self.devicetype.save() self.con = ControllerInfo(controllerid = "TEST-0000-000000",placeid=self.place,controllernm="TEST cont",type=self.devicetype) self.con.save() self.req = ControlResult(requestid=1,controllerid=self.con,controltype=80,devicetype=1,deviceindex=0) self.req.save() def test_normal(self): self.client = Client() csvline = "1,0,TEST" has = hashlib.md5(csvline+"gaui120gtahogsa").hexdigest() response = self.client.post('/emsserver/deviceif/controlresult/' + has,csvline,'application/x-www-form-urlencoded') self.assertEqual(response.content, 'OK', 'response dont match %s' % response.content) res = ControlResult.objects.filter(controllerid_id__exact=self.con.controllerid).order_by("updatedate")[0:1].get() self.assertEqual(res.result ,0, "controltype don't match {0}".format(res.result)) self.assertEqual(res.resultmsg ,"TEST", "devicetype don't match {0}".format(res.resultmsg)) def test_nothing(self): self.client = Client() csvline = "0,1,TEST" has = hashlib.md5(csvline+"gaui120gtahogsa").hexdigest() response = self.client.post('/emsserver/deviceif/controlresult/' + has,csvline,'application/x-www-form-urlencoded') self.assertEqual(response.content, "NG Data don't match", 'response dont match %s' % response.content) def test_getlog(self): self.req.controltype = 60 self.req.save() self.client = Client() csvline = "1,0,test.log" has = hashlib.md5(csvline+"gaui120gtahogsa").hexdigest() response = self.client.post('/emsserver/deviceif/controlresult/' + has,csvline,'application/x-www-form-urlencoded') self.assertEqual(response.content, 'OK') flist = LogList.objects.get(controllerid = self.con.controllerid) self.assertEqual(flist.filenm, 'test.log') self.assertEqual(flist.url, settings.MY_WEBDAV_URL + 'test.log')
def setUp(self): self.group1 = Group(name="group") self.group1.save() self.group2 = Group(name="group2") self.group2.save() self.area = AreaInfo(areaid="TEST000000",areanm="TEST",groupid = self.group1) self.area.save() self.place = PlaceInfo(placeid="TEST000000",placenm="TEST",areaid=self.area,groupid = self.group2) self.place.save() self.devicetype = DeviceTypeInfo(typeid="0",devicetype=1,typenm="Controller") self.devicetype.save() self.con = ControllerInfo(controllerid = "TEST-0000-000000",placeid=self.place,controllernm="TEST cont",type=self.devicetype) self.con.save()
class TestEventSave(TestCase): def setUp(self): self.group1 = Group(name="group") self.group1.save() self.group2 = Group(name="group2") self.group2.save() self.area = AreaInfo(areaid="TEST000000",areanm="TEST",groupid = self.group1) self.area.save() self.place = PlaceInfo(placeid="TEST000000",placenm="TEST",areaid=self.area,groupid = self.group2) self.place.save() self.devicetype = DeviceTypeInfo(typeid="0",devicetype=1,typenm="Controller") self.devicetype.save() self.con = ControllerInfo(controllerid = "TEST-0000-000000",placeid=self.place,controllernm="TEST cont",type=self.devicetype) self.con.save() def test_OneMailtoAdminHappen(self): #user = User.objects.create_user('tester', '*****@*****.**', 'password') user = User.objects.create_user('tester', '*****@*****.**', 'password') user.is_staff = True user.is_superuser = True user.save() #evmail = EventSms() #evmail.priorityno = 0 #evmail.sendrange = 0 #evmail.sendhappen = True #evmail.sendrestore= False #evmail.subject= 'Test Mail' #evmail.message = 'Happen Alarm $CONT-NM$' #evmail.targetevent = '1010000011' #evmail.save() evsms = EventSms() evsms.priorityno = 0 evsms.sendrange = 0 evsms.sendhappen = True evsms.sendrestore= False evsms.subject= 'Test Mail' evsms.message = 'Happen Alarm $CONT-NM$' evsms.targetevent = '1010000011' evsms.save() evn = Event(controllerid="TEST-0000-000000",deviceindex=0,alarmtype=1) evn.senddate = timezone.now() evn.measureddate = timezone.now() evn.id = createid(evn.controllerid,evn.measureddate,Event) evn.code = "1010000011" evn.level = 3 evn.message = "Changed Controller Setting by the Controller" evn.save()
def setUp(self): self.group = Group(name="group") self.group.save() self.area = AreaInfo(areaid="TEST000000",areanm="TEST",groupid = self.group) self.area.save() self.place = PlaceInfo(placeid="TEST000000",placenm="TEST",areaid=self.area,groupid = self.group) self.place.save() self.devicetype = DeviceTypeInfo(typeid="0",devicetype=1,typenm="Controller") self.devicetype.save() self.con = ControllerInfo(controllerid = "TEST-0000-000000",placeid=self.place,controllernm="TEST cont",type=self.devicetype) self.con.save() self.req = ControlResult(requestid=1,controllerid=self.con,controltype=80,devicetype=1,deviceindex=0) self.req.save()
def setUp(self): self.group = Group(name="group") self.group.save() self.area = AreaInfo(areaid="TEST000000", areanm="TEST", groupid=self.group) self.area.save() self.place = PlaceInfo(placeid="TEST000000", placenm="TEST", areaid=self.area, groupid=self.group) self.place.save() self.devicetype = DeviceTypeInfo(typeid="0", devicetype=1, typenm="Controller") self.devicetype.save() self.con = ControllerInfo(controllerid="0000-0000-000000", placeid=self.place, controllernm="TEST cont", type=self.devicetype) self.con.save() self.senddate = "2013-07-08 11:11:11" self.alermtype = "0" self.bttrtype = DeviceTypeInfo(typeid="1", devicetype=2, typenm="Battery") self.bttrtype.save() self.sb = SettingBattery(type=self.bttrtype) self.sb.controllerid = self.con self.sb.deviceindex = 0 self.sb.mode = 1 self.sb.soc_max = 80 self.sb.soc_min = 0 self.sb.energy = 1800 self.sb.shipping_energy = 1800 self.sb.chargepower_max = 2000 self.sb.dischargepower_max = 1000 self.sb.bms_cnt = 1 self.sb.voltage_max = 100 self.sb.voltage_min = 50 self.sb.peak_shaving_limit = 10000 self.sb.save()
def ControlResultSetup(obj): obj.group = Group(name="group") obj.group.save() obj.area = AreaInfo(areaid="TEST000000",areanm="TEST",groupid = obj.group) obj.area.save() obj.place = PlaceInfo(placeid="TEST000000",placenm="TEST",areaid=obj.area,groupid = obj.group) obj.place.save() obj.devicetype = DeviceTypeInfo(typeid="0",devicetype=1,typenm="Controller") obj.devicetype.save() obj.devicetypeb = DeviceTypeInfo(typeid="1",devicetype=2,typenm="Battery") obj.devicetypeb.save() obj.con = ControllerInfo(controllerid = "TEST-0000-000000",placeid=obj.place,controllernm="TEST cont",type=obj.devicetype) obj.con.save() for i in range(0,100): res = ControlResult(requestid=i+100, controllerid=obj.con, deviceindex=0, controltype=10, devicetype=1) if i < 60: res.result = 1 res.save() res.updatedate = (dt.utcnow() - timedelta(days=100-i,hours=-1)) cursor = connection.cursor() cursor.execute("update emscontrol_controlresult set updatedate=%s where requestid=%s",[res.updatedate.strftime('%Y-%m-%d %H:%M:%S'),str(res.requestid)]) transaction.commit_unless_managed()
def setUp(self): self.group = Group(name="group") self.group.save() self.area = AreaInfo(areaid="TEST000000", areanm="TEST", groupid=self.group) self.area.save() self.place = PlaceInfo(placeid="TEST000000", placenm="TEST", areaid=self.area, groupid=self.group) self.place.save() self.devicetype = DeviceTypeInfo(typeid="0", devicetype=1, typenm="Controller") self.devicetype.save() self.con = ControllerInfo(controllerid="0000-0000-000000", placeid=self.place, controllernm="TEST cont", type=self.devicetype) self.con.save() self.senddate = "2013-07-08 11:11:11" self.alermtype = "0"
class MakeDairyPowerTest(TestCase): def setUp(self): self.group = Group(name="group") self.group.save() self.area = AreaInfo(areaid="TEST000000", areanm="TEST", groupid=self.group) self.area.save() self.place = PlaceInfo(placeid="TEST000000", placenm="TEST", areaid=self.area, groupid=self.group) self.place.save() self.devicetype = DeviceTypeInfo(typeid="0", devicetype=1, typenm="Controller") self.devicetype.save() self.con = ControllerInfo(controllerid="0000-0000-000000", placeid=self.place, controllernm="TEST cont", type=self.devicetype) self.con.save() self.senddate = "2013-07-08 11:11:11" self.alermtype = "0" def test_load_pv_zero(self): ret = makedairypower(self.con.controllerid, "2013-02-01", 20) power = Target_daily_power.objects.filter( controllerid__exact=self.con.controllerid, target_day__exact=date(2013, 2, 1), devicetype=3).order_by('time') self.assertEqual(-1, ret, "ret={0}".format(ret)) self.assertEqual(0, len(power), "power insert {0}".format(len(power))) def test_load_zero(self): pv = MeasuredPV() measureddate = "2013-01-30 11:11:11" csvline = ",".join([ self.con.controllerid, "0", self.senddate, self.alermtype, "1", measureddate, "", "", "100" ]) pv.setcsvline(csvline) pv.save() ret = makedairypower(self.con.controllerid, "2013-02-01", 20) power = Target_daily_power.objects.filter( controllerid__exact=self.con.controllerid, target_day__exact=date(2013, 2, 1), devicetype=3).order_by('time') self.assertEqual(-1, ret, "ret={0}".format(ret)) self.assertEqual(0, len(power), "power insert {0}".format(len(power))) def test_pv_zero(self): load = MeasuredLoad() measureddate = "2013-01-30 11:11:11" csvline = ",".join([ self.con.controllerid, "0", self.senddate, self.alermtype, "1", measureddate, "", "", "100" ]) load.setcsvline(csvline) load.save() ret = makedairypower(self.con.controllerid, "2013-02-01", 20) power = Target_daily_power.objects.filter( controllerid__exact=self.con.controllerid, target_day__exact=date(2013, 2, 1), devicetype=4).order_by('time') self.assertEqual(0, ret, "ret={0}".format(ret)) self.assertEqual(24 * 3, len(power), "power insert {0}".format(len(power))) for test in power: if test.time >= time(11, 0): self.assertEqual( 10, test.power, "time={0} power={1}".format(test.time, test.power)) else: self.assertEqual( 0, test.power, "time={0} power={1}".format(test.time, test.power)) def test_load_full(self): for i in range(0, 24 * 60, 5): load = MeasuredLoad() measureddate = "2013-01-30 %d:%d:0" % (int( i / 60), int(i / 5) * 5 % 60) csvline = ",".join([ self.con.controllerid, "0", self.senddate, self.alermtype, "1", measureddate, "", "", str(i * 100) ]) load.setcsvline(csvline) load.save() ret = makedairypower(self.con.controllerid, "2013-02-01", 20) power = Target_daily_power.objects.filter( controllerid__exact=self.con.controllerid, target_day__exact=date(2013, 2, 1), devicetype=4).order_by('time') self.assertEqual(0, ret, "ret={0}".format(ret)) self.assertEqual(24 * 3, len(power), "power insert {0}".format(len(power))) for test in power: po = 75 + 10 * (test.time.hour * 60 + test.time.minute) self.assertEqual( po, test.power, "time={0} power={1}".format(test.time, test.power))
class makeperiodTest(TestCase): def setUp(self): self.group = Group(name="group") self.group.save() self.area = AreaInfo(areaid="TEST000000", areanm="TEST", groupid=self.group) self.area.save() self.place = PlaceInfo(placeid="TEST000000", placenm="TEST", areaid=self.area, groupid=self.group) self.place.save() self.devicetype = DeviceTypeInfo(typeid="0", devicetype=1, typenm="Controller") self.devicetype.save() self.con = ControllerInfo(controllerid="0000-0000-000000", placeid=self.place, controllernm="TEST cont", type=self.devicetype) self.con.save() self.senddate = "2013-07-08 11:11:11" self.alermtype = "0" self.bttrtype = DeviceTypeInfo(typeid="1", devicetype=2, typenm="Battery") self.bttrtype.save() self.sb = SettingBattery(type=self.bttrtype) self.sb.controllerid = self.con self.sb.deviceindex = 0 self.sb.mode = 1 self.sb.soc_max = 80 self.sb.soc_min = 0 self.sb.energy = 1800 self.sb.shipping_energy = 1800 self.sb.chargepower_max = 2000 self.sb.dischargepower_max = 1000 self.sb.bms_cnt = 1 self.sb.voltage_max = 100 self.sb.voltage_min = 50 self.sb.peak_shaving_limit = 10000 self.sb.save() def test_makeperiod_only_tariff(self): tari = SettingGridTariff(areaid=self.area, period_type=0, start_time=time(0, 0), end_time=time(12, 30), price=10) tari.save() tari = SettingGridTariff(areaid=self.area, period_type=1, start_time=time(12, 30), end_time=time(0, 0), price=20) tari.save() makeperiod(self.con.controllerid, 20, date(2013, 2, 1)) sdi = SettingDirective.objects.filter( controllerid__exact=self.con.controllerid, target_day__exact=date(2013, 2, 1)).order_by('start_time') self.assertEqual(2, len(sdi), "len={0}".format(len(sdi))) self.assertEqual(0, sdi[0].period_no, "period_no={0}".format(sdi[0].period_no)) self.assertEqual(1, sdi[1].period_no, "period_no={0}".format(sdi[1].period_no)) self.assertEqual(0, sdi[0].period_type, "period_type={0}".format(sdi[0].period_type)) self.assertEqual(1, sdi[1].period_type, "period_type={0}".format(sdi[1].period_type)) self.assertEqual(10, sdi[0].grid_cost, "grid_cost={0}".format(sdi[0].grid_cost)) self.assertEqual(20, sdi[1].grid_cost, "grid_cost={0}".format(sdi[1].grid_cost)) self.assertEqual( 0, sdi[0].outage_probability, "outage_probability={0}".format(sdi[0].outage_probability)) self.assertEqual(0, sdi[1].outage_probability, "grid_cost={0}".format(sdi[1].outage_probability)) self.assertEqual(time(0, 0), sdi[0].start_time, "start_time={0}".format(sdi[0].start_time)) self.assertEqual(time(12, 40), sdi[1].start_time, "start_time={0}".format(sdi[1].start_time)) self.assertEqual(time(12, 40), sdi[0].end_time, "end_time={0}".format(sdi[0].end_time)) self.assertEqual(time(0, 0), sdi[1].end_time, "end_time={0}".format(sdi[1].end_time)) self.assertEqual(0, sdi[0].soc_opt, "start_time={0}".format(sdi[0].soc_opt)) self.assertEqual(0, sdi[1].soc_opt, "start_time={0}".format(sdi[1].soc_opt)) self.assertEqual(0, sdi[0].outage_cnt_opt, "outage_cnt_opt={0}".format(sdi[0].outage_cnt_opt)) self.assertEqual(0, sdi[1].outage_cnt_opt, "outage_cnt_opt={0}".format(sdi[1].outage_cnt_opt)) self.assertEqual( 80, sdi[0].outage_soc_max_opt, "outage_soc_max_opt={0}".format(sdi[0].outage_soc_max_opt)) self.assertEqual( 80, sdi[1].outage_soc_max_opt, "outage_soc_max_opt={0}".format(sdi[1].outage_soc_max_opt)) self.assertEqual( 0, sdi[0].outage_soc_min_opt, "outage_soc_min_opt={0}".format(sdi[0].outage_soc_min_opt)) self.assertEqual( 0, sdi[1].outage_soc_min_opt, "outage_soc_min_opt={0}".format(sdi[1].outage_soc_min_opt)) def test_makeperiod_tariff_outage(self): tari = SettingGridTariff(areaid=self.area, period_type=0, start_time=time(0, 0), end_time=time(12, 30), price=10) tari.save() tari = SettingGridTariff(areaid=self.area, period_type=1, start_time=time(12, 30), end_time=time(0, 0), price=20) tari.save() out = Target_daily_outage_profile(controllerid=self.con, period_type=101, start_time=time(0, 0), end_time=time(0, 0), during_minute=100, probability=50) out.save() makeperiod(self.con.controllerid, 20, date(2013, 2, 1)) sdi = SettingDirective.objects.filter( controllerid__exact=self.con.controllerid, target_day__exact=date(2013, 2, 1)).order_by('start_time') self.assertEqual(2, len(sdi), "len={0}".format(len(sdi))) self.assertEqual(0, sdi[0].period_no, "period_no={0}".format(sdi[0].period_no)) self.assertEqual(1, sdi[1].period_no, "period_no={0}".format(sdi[1].period_no)) self.assertEqual(0, sdi[0].period_type, "period_type={0}".format(sdi[0].period_type)) self.assertEqual(1, sdi[1].period_type, "period_type={0}".format(sdi[1].period_type)) self.assertEqual(10, sdi[0].grid_cost, "grid_cost={0}".format(sdi[0].grid_cost)) self.assertEqual(20, sdi[1].grid_cost, "grid_cost={0}".format(sdi[1].grid_cost)) self.assertEqual( 50, sdi[0].outage_probability, "outage_probability={0}".format(sdi[0].outage_probability)) self.assertEqual( 50, sdi[1].outage_probability, "outage_probability={0}".format(sdi[1].outage_probability)) self.assertEqual(time(0, 0), sdi[0].start_time, "start_time={0}".format(sdi[0].start_time)) self.assertEqual(time(12, 40), sdi[1].start_time, "start_time={0}".format(sdi[1].start_time)) self.assertEqual(time(12, 40), sdi[0].end_time, "end_time={0}".format(sdi[0].end_time)) self.assertEqual(time(0, 0), sdi[1].end_time, "end_time={0}".format(sdi[1].end_time)) self.assertEqual(0, sdi[0].soc_opt, "start_time={0}".format(sdi[0].soc_opt)) self.assertEqual(0, sdi[1].soc_opt, "start_time={0}".format(sdi[1].soc_opt)) self.assertEqual(0, sdi[0].outage_cnt_opt, "outage_cnt_opt={0}".format(sdi[0].outage_cnt_opt)) self.assertEqual(0, sdi[1].outage_cnt_opt, "outage_cnt_opt={0}".format(sdi[1].outage_cnt_opt)) self.assertEqual( 80, sdi[0].outage_soc_max_opt, "outage_soc_max_opt={0}".format(sdi[0].outage_soc_max_opt)) self.assertEqual( 80, sdi[1].outage_soc_max_opt, "outage_soc_max_opt={0}".format(sdi[1].outage_soc_max_opt)) self.assertEqual( 0, sdi[0].outage_soc_min_opt, "outage_soc_min_opt={0}".format(sdi[0].outage_soc_min_opt)) self.assertEqual( 0, sdi[1].outage_soc_min_opt, "outage_soc_min_opt={0}".format(sdi[1].outage_soc_min_opt)) def test_makeperiod_tariff_outage_startsift(self): tari = SettingGridTariff(areaid=self.area, period_type=0, start_time=time(0, 0), end_time=time(12, 30), price=10) tari.save() tari = SettingGridTariff(areaid=self.area, period_type=1, start_time=time(12, 30), end_time=time(0, 0), price=20) tari.save() out = Target_daily_outage_profile(controllerid=self.con, period_type=101, start_time=time(8, 30), end_time=time(0, 0), during_minute=100, probability=50) out.save() makeperiod(self.con.controllerid, 20, date(2013, 2, 1)) sdi = SettingDirective.objects.filter( controllerid__exact=self.con.controllerid, target_day__exact=date(2013, 2, 1)).order_by('start_time') self.assertEqual(3, len(sdi), "len={0}".format(len(sdi))) self.assertEqual(0, sdi[0].period_no, "period_no={0}".format(sdi[0].period_no)) self.assertEqual(1, sdi[1].period_no, "period_no={0}".format(sdi[1].period_no)) self.assertEqual(2, sdi[2].period_no, "period_no={0}".format(sdi[2].period_no)) self.assertEqual(0, sdi[0].period_type, "period_type={0}".format(sdi[0].period_type)) self.assertEqual(0, sdi[1].period_type, "period_type={0}".format(sdi[1].period_type)) self.assertEqual(1, sdi[2].period_type, "period_type={0}".format(sdi[2].period_type)) self.assertEqual(10, sdi[0].grid_cost, "grid_cost={0}".format(sdi[0].grid_cost)) self.assertEqual(10, sdi[1].grid_cost, "grid_cost={0}".format(sdi[1].grid_cost)) self.assertEqual(20, sdi[2].grid_cost, "grid_cost={0}".format(sdi[2].grid_cost)) self.assertEqual( 0, sdi[0].outage_probability, "outage_probability={0}".format(sdi[0].outage_probability)) self.assertEqual( 50, sdi[1].outage_probability, "outage_probability={0}".format(sdi[1].outage_probability)) self.assertEqual( 50, sdi[2].outage_probability, "outage_probability={0}".format(sdi[2].outage_probability)) self.assertEqual(time(0, 0), sdi[0].start_time, "start_time={0}".format(sdi[0].start_time)) self.assertEqual(time(8, 40), sdi[1].start_time, "start_time={0}".format(sdi[1].start_time)) self.assertEqual(time(12, 40), sdi[2].start_time, "start_time={0}".format(sdi[2].start_time)) self.assertEqual(time(8, 40), sdi[0].end_time, "end_time={0}".format(sdi[0].end_time)) self.assertEqual(time(12, 40), sdi[1].end_time, "end_time={0}".format(sdi[1].end_time)) self.assertEqual(time(0, 0), sdi[2].end_time, "end_time={0}".format(sdi[2].end_time)) self.assertEqual(0, sdi[0].soc_opt, "start_time={0}".format(sdi[0].soc_opt)) self.assertEqual(0, sdi[1].soc_opt, "start_time={0}".format(sdi[1].soc_opt)) self.assertEqual(0, sdi[2].soc_opt, "start_time={0}".format(sdi[2].soc_opt)) self.assertEqual(0, sdi[0].outage_cnt_opt, "outage_cnt_opt={0}".format(sdi[0].outage_cnt_opt)) self.assertEqual(0, sdi[1].outage_cnt_opt, "outage_cnt_opt={0}".format(sdi[1].outage_cnt_opt)) self.assertEqual(0, sdi[2].outage_cnt_opt, "outage_cnt_opt={0}".format(sdi[2].outage_cnt_opt)) self.assertEqual( 80, sdi[0].outage_soc_max_opt, "outage_soc_max_opt={0}".format(sdi[0].outage_soc_max_opt)) self.assertEqual( 80, sdi[1].outage_soc_max_opt, "outage_soc_max_opt={0}".format(sdi[1].outage_soc_max_opt)) self.assertEqual( 80, sdi[2].outage_soc_max_opt, "outage_soc_max_opt={0}".format(sdi[2].outage_soc_max_opt)) self.assertEqual( 0, sdi[0].outage_soc_min_opt, "outage_soc_min_opt={0}".format(sdi[0].outage_soc_min_opt)) self.assertEqual( 0, sdi[1].outage_soc_min_opt, "outage_soc_min_opt={0}".format(sdi[1].outage_soc_min_opt)) self.assertEqual( 0, sdi[2].outage_soc_min_opt, "outage_soc_min_opt={0}".format(sdi[2].outage_soc_min_opt)) def test_makeperiod_tariff_outage_endsift(self): tari = SettingGridTariff(areaid=self.area, period_type=0, start_time=time(0, 0), end_time=time(12, 30), price=10) tari.save() tari = SettingGridTariff(areaid=self.area, period_type=1, start_time=time(12, 30), end_time=time(0, 0), price=20) tari.save() out = Target_daily_outage_profile(controllerid=self.con, period_type=101, start_time=time(0, 0), end_time=time(13, 30), during_minute=100, probability=50) out.save() makeperiod(self.con.controllerid, 20, date(2013, 2, 1)) sdi = SettingDirective.objects.filter( controllerid__exact=self.con.controllerid, target_day__exact=date(2013, 2, 1)).order_by('start_time') self.assertEqual(3, len(sdi), "len={0}".format(len(sdi))) self.assertEqual(0, sdi[0].period_no, "period_no={0}".format(sdi[0].period_no)) self.assertEqual(1, sdi[1].period_no, "period_no={0}".format(sdi[1].period_no)) self.assertEqual(2, sdi[2].period_no, "period_no={0}".format(sdi[2].period_no)) self.assertEqual(0, sdi[0].period_type, "period_type={0}".format(sdi[0].period_type)) self.assertEqual(1, sdi[1].period_type, "period_type={0}".format(sdi[1].period_type)) self.assertEqual(1, sdi[2].period_type, "period_type={0}".format(sdi[2].period_type)) self.assertEqual(10, sdi[0].grid_cost, "grid_cost={0}".format(sdi[0].grid_cost)) self.assertEqual(20, sdi[1].grid_cost, "grid_cost={0}".format(sdi[1].grid_cost)) self.assertEqual(20, sdi[2].grid_cost, "grid_cost={0}".format(sdi[2].grid_cost)) self.assertEqual( 50, sdi[0].outage_probability, "outage_probability={0}".format(sdi[0].outage_probability)) self.assertEqual( 50, sdi[1].outage_probability, "outage_probability={0}".format(sdi[1].outage_probability)) self.assertEqual( 0, sdi[2].outage_probability, "outage_probability={0}".format(sdi[2].outage_probability)) self.assertEqual(time(0, 0), sdi[0].start_time, "start_time={0}".format(sdi[0].start_time)) self.assertEqual(time(12, 40), sdi[1].start_time, "start_time={0}".format(sdi[1].start_time)) self.assertEqual(time(13, 40), sdi[2].start_time, "start_time={0}".format(sdi[2].start_time)) self.assertEqual(time(12, 40), sdi[0].end_time, "end_time={0}".format(sdi[0].end_time)) self.assertEqual(time(13, 40), sdi[1].end_time, "end_time={0}".format(sdi[1].end_time)) self.assertEqual(time(0, 0), sdi[2].end_time, "end_time={0}".format(sdi[2].end_time)) self.assertEqual(0, sdi[0].soc_opt, "start_time={0}".format(sdi[0].soc_opt)) self.assertEqual(0, sdi[1].soc_opt, "start_time={0}".format(sdi[1].soc_opt)) self.assertEqual(0, sdi[2].soc_opt, "start_time={0}".format(sdi[2].soc_opt)) self.assertEqual(0, sdi[0].outage_cnt_opt, "outage_cnt_opt={0}".format(sdi[0].outage_cnt_opt)) self.assertEqual(0, sdi[1].outage_cnt_opt, "outage_cnt_opt={0}".format(sdi[1].outage_cnt_opt)) self.assertEqual(0, sdi[2].outage_cnt_opt, "outage_cnt_opt={0}".format(sdi[2].outage_cnt_opt)) self.assertEqual( 80, sdi[0].outage_soc_max_opt, "outage_soc_max_opt={0}".format(sdi[0].outage_soc_max_opt)) self.assertEqual( 80, sdi[1].outage_soc_max_opt, "outage_soc_max_opt={0}".format(sdi[1].outage_soc_max_opt)) self.assertEqual( 80, sdi[2].outage_soc_max_opt, "outage_soc_max_opt={0}".format(sdi[2].outage_soc_max_opt)) self.assertEqual( 0, sdi[0].outage_soc_min_opt, "outage_soc_min_opt={0}".format(sdi[0].outage_soc_min_opt)) self.assertEqual( 0, sdi[1].outage_soc_min_opt, "outage_soc_min_opt={0}".format(sdi[1].outage_soc_min_opt)) self.assertEqual( 0, sdi[2].outage_soc_min_opt, "outage_soc_min_opt={0}".format(sdi[2].outage_soc_min_opt)) def test_makeperiod_tariff_outage_sift(self): tari = SettingGridTariff(areaid=self.area, period_type=0, start_time=time(0, 0), end_time=time(12, 30), price=10) tari.save() tari = SettingGridTariff(areaid=self.area, period_type=1, start_time=time(12, 30), end_time=time(0, 0), price=20) tari.save() out = Target_daily_outage_profile(controllerid=self.con, period_type=101, start_time=time(8, 30), end_time=time(13, 30), during_minute=100, probability=50) out.save() makeperiod(self.con.controllerid, 20, date(2013, 2, 1)) sdi = SettingDirective.objects.filter( controllerid__exact=self.con.controllerid, target_day__exact=date(2013, 2, 1)).order_by('start_time') self.assertEqual(4, len(sdi), "len={0}".format(len(sdi))) self.assertEqual(0, sdi[0].period_no, "period_no={0}".format(sdi[0].period_no)) self.assertEqual(1, sdi[1].period_no, "period_no={0}".format(sdi[1].period_no)) self.assertEqual(2, sdi[2].period_no, "period_no={0}".format(sdi[2].period_no)) self.assertEqual(3, sdi[3].period_no, "period_no={0}".format(sdi[3].period_no)) self.assertEqual(0, sdi[0].period_type, "period_type={0}".format(sdi[0].period_type)) self.assertEqual(0, sdi[1].period_type, "period_type={0}".format(sdi[1].period_type)) self.assertEqual(1, sdi[2].period_type, "period_type={0}".format(sdi[2].period_type)) self.assertEqual(1, sdi[3].period_type, "period_type={0}".format(sdi[3].period_type)) self.assertEqual(10, sdi[0].grid_cost, "grid_cost={0}".format(sdi[0].grid_cost)) self.assertEqual(10, sdi[1].grid_cost, "grid_cost={0}".format(sdi[1].grid_cost)) self.assertEqual(20, sdi[2].grid_cost, "grid_cost={0}".format(sdi[2].grid_cost)) self.assertEqual(20, sdi[3].grid_cost, "grid_cost={0}".format(sdi[3].grid_cost)) self.assertEqual( 0, sdi[0].outage_probability, "outage_probability={0}".format(sdi[0].outage_probability)) self.assertEqual( 50, sdi[1].outage_probability, "outage_probability={0}".format(sdi[1].outage_probability)) self.assertEqual( 50, sdi[2].outage_probability, "outage_probability={0}".format(sdi[2].outage_probability)) self.assertEqual( 0, sdi[3].outage_probability, "outage_probability={0}".format(sdi[3].outage_probability)) self.assertEqual(time(0, 0), sdi[0].start_time, "start_time={0}".format(sdi[0].start_time)) self.assertEqual(time(8, 40), sdi[1].start_time, "start_time={0}".format(sdi[1].start_time)) self.assertEqual(time(12, 40), sdi[2].start_time, "start_time={0}".format(sdi[2].start_time)) self.assertEqual(time(13, 40), sdi[3].start_time, "start_time={0}".format(sdi[3].start_time)) self.assertEqual(time(8, 40), sdi[0].end_time, "end_time={0}".format(sdi[0].end_time)) self.assertEqual(time(12, 40), sdi[1].end_time, "end_time={0}".format(sdi[1].end_time)) self.assertEqual(time(13, 40), sdi[2].end_time, "end_time={0}".format(sdi[2].end_time)) self.assertEqual(time(0, 0), sdi[3].end_time, "end_time={0}".format(sdi[3].end_time)) self.assertEqual(0, sdi[0].soc_opt, "start_time={0}".format(sdi[0].soc_opt)) self.assertEqual(0, sdi[1].soc_opt, "start_time={0}".format(sdi[1].soc_opt)) self.assertEqual(0, sdi[2].soc_opt, "start_time={0}".format(sdi[2].soc_opt)) self.assertEqual(0, sdi[3].soc_opt, "start_time={0}".format(sdi[3].soc_opt)) self.assertEqual(0, sdi[0].outage_cnt_opt, "outage_cnt_opt={0}".format(sdi[0].outage_cnt_opt)) self.assertEqual(0, sdi[1].outage_cnt_opt, "outage_cnt_opt={0}".format(sdi[1].outage_cnt_opt)) self.assertEqual(0, sdi[2].outage_cnt_opt, "outage_cnt_opt={0}".format(sdi[2].outage_cnt_opt)) self.assertEqual(0, sdi[3].outage_cnt_opt, "outage_cnt_opt={0}".format(sdi[3].outage_cnt_opt)) self.assertEqual( 80, sdi[0].outage_soc_max_opt, "outage_soc_max_opt={0}".format(sdi[0].outage_soc_max_opt)) self.assertEqual( 80, sdi[1].outage_soc_max_opt, "outage_soc_max_opt={0}".format(sdi[1].outage_soc_max_opt)) self.assertEqual( 80, sdi[2].outage_soc_max_opt, "outage_soc_max_opt={0}".format(sdi[2].outage_soc_max_opt)) self.assertEqual( 80, sdi[3].outage_soc_max_opt, "outage_soc_max_opt={0}".format(sdi[3].outage_soc_max_opt)) self.assertEqual( 0, sdi[0].outage_soc_min_opt, "outage_soc_min_opt={0}".format(sdi[0].outage_soc_min_opt)) self.assertEqual( 0, sdi[1].outage_soc_min_opt, "outage_soc_min_opt={0}".format(sdi[1].outage_soc_min_opt)) self.assertEqual( 0, sdi[2].outage_soc_min_opt, "outage_soc_min_opt={0}".format(sdi[2].outage_soc_min_opt)) self.assertEqual( 0, sdi[3].outage_soc_min_opt, "outage_soc_min_opt={0}".format(sdi[3].outage_soc_min_opt))
class PollingTest(TestCase): def setUp(self): self.group = Group(name="group") self.group.save() self.area = AreaInfo(areaid="TEST000000",areanm="TEST",groupid = self.group) self.area.save() self.place = PlaceInfo(placeid="TEST000000",placenm="TEST",areaid=self.area,groupid = self.group) self.place.save() self.devicetype = DeviceTypeInfo(typeid="0",devicetype=1,typenm="Controller") self.devicetype.save() self.devicetypeb = DeviceTypeInfo(typeid="1",devicetype=2,typenm="Battery") self.devicetypeb.save() self.con = ControllerInfo(controllerid = "TEST-0000-000000",placeid=self.place,controllernm="TEST cont",type=self.devicetype) self.con.save() def test_nothing(self): self.client = Client() deviceindex = "10" alarmtype = "0" senddate = "2020-07-08 11:11:11" csvline = ','.join([self.con.controllerid,deviceindex,senddate,alarmtype,]) has = hashlib.md5(csvline+"gaui120gtahogsa").hexdigest() response = self.client.post('/emsserver/deviceif/polling/' + has,csvline,'application/x-www-form-urlencoded') self.assertEqual(response.content,"Nothing",) def test_resetController(self): req = ControlRequest(controllerid=self.con,deviceindex=1,controltype=10,devicetype=1) req.save() self.client = Client() deviceindex = "10" alarmtype = "0" senddate = "2020-07-08 11:11:11" csvline = ','.join([self.con.controllerid,deviceindex,senddate,alarmtype,]) has = hashlib.md5(csvline+"gaui120gtahogsa").hexdigest() response = self.client.post('/emsserver/deviceif/polling/' + has,csvline,'application/x-www-form-urlencoded') result = response.content.split(':') self.assertEqual(result[0],"RESET",) self.assertEqual(result[2] ,"CONTROLLER", ) self.assertEqual(result[3] ,"1", ) self.assertEqual(result[4],"",) conr=ControlRequest.objects.filter(controllerid=self.con,deviceindex=1,controltype=10,devicetype=1).get() self.assertEqual(conr.deleteflg,True) reqreslt = ControlResult.objects.get(controllerid=self.con,deviceindex=1,controltype=10,devicetype=1) self.assertIsNone(reqreslt.result,) self.assertEqual(reqreslt.resultmsg,"") def test_settingController(self): scon = SettingController(controllerid=self.con,mode="01") scon.save() req = ControlRequest(controllerid=self.con,deviceindex=1,controltype=30,devicetype=1) req.save() self.client = Client() deviceindex = "10" alarmtype = "0" senddate = "2020-07-08 11:11:11" csvline = ','.join([self.con.controllerid,deviceindex,senddate,alarmtype,]) has = hashlib.md5(csvline+"gaui120gtahogsa").hexdigest() response = self.client.post('/emsserver/deviceif/polling/' + has,csvline,'application/x-www-form-urlencoded') result = response.content.split(':') self.assertEqual(result[0],"SETTING",'controltype does not match %s' % result[0]) self.assertRegexpMatches(result[1],"[0-9]+", "id don't match %s" % result[1]) self.assertEqual(result[2] ,"CONTROLLER", "controltype don't match %s" % result[2]) self.assertEqual(result[3] ,"1", "deviceid don't match %s" % result[3]) self.assertEqual(result[4],"01","csvdata don't match %s " % result[4]) res = ControlResult.objects.filter(controllerid_id__exact=self.con.controllerid).order_by("updatedate")[0:1].get() self.assertEqual(res.requestid ,int(result[1]), "requestid don't match {0}".format(res.requestid)) self.assertEqual(res.controllerid ,req.controllerid, "controllerid don't match {0}".format(res.controllerid)) self.assertEqual(res.deviceindex ,req.deviceindex, "deviceindex don't match {0}".format(res.deviceindex)) self.assertEqual(res.controltype ,req.controltype, "controltype don't match {0}".format(res.controltype)) self.assertEqual(res.devicetype ,req.devicetype, "devicetype don't match {0}".format(res.devicetype)) req =ControlRequest.objects.filter(controllerid=self.con,deviceindex=1,controltype=30,devicetype=1).get() self.assertEqual(req.deleteflg,True) reqreslt = ControlResult.objects.get(controllerid=self.con,deviceindex=1,controltype=30,devicetype=1) self.assertIsNone(reqreslt.result,) self.assertEqual(res.resultmsg,"") def test_settingBattery(self): sbttr = SettingBattery(controllerid=self.con) sbttr.deviceindex = 1 sbttr.mode = 00 sbttr.soc_max = 100 sbttr.soc_min = 100 sbttr.energy = 99999.9 sbttr.shipping_energy = 99999.9 sbttr.chargepower_max =99999.9 sbttr.dischargepower_max = 99999.9 sbttr.bms_cnt = 99 sbttr.voltage_max = 99999.9 sbttr.voltage_min =99999.9 sbttr.peak_shaving_limit = 99999.9 sbttr.type = self.devicetypeb sbttr.save() req = ControlRequest(controllerid=self.con,deviceindex=1,controltype=30,devicetype=2) req.save() self.client = Client() deviceindex = "10" alarmtype = "0" senddate = "2020-07-08 11:11:11" csvline = ','.join([self.con.controllerid,deviceindex,senddate,alarmtype,]) has = hashlib.md5(csvline+"gaui120gtahogsa").hexdigest() response = self.client.post('/emsserver/deviceif/polling/' + has,csvline,'application/x-www-form-urlencoded') result = response.content.split(':') self.assertEqual(result[0],"SETTING",'controltype does not match %s' % result[0]) self.assertRegexpMatches(result[1],"[0-9]+", "id don't match %s" % result[1]) self.assertEqual(result[2] ,"BATTERY", "controltype don't match %s" % result[2]) self.assertEqual(result[3] ,"1", "deviceid don't match %s" % result[3]) ressettting = result[4].split(',') self.assertEqual(ressettting[0], str(sbttr.mode),'mode does not match{0}!={1}'.format(int(ressettting[0]), sbttr.mode)) self.assertEqual(ressettting[1], str(sbttr.soc_max),'soc_max does not match{0}!={1}'.format(int(ressettting[1]), sbttr.soc_max)) self.assertEqual(ressettting[2], str(sbttr.soc_min),'soc_min does not match{0}!={1}'.format(int(ressettting[2]), sbttr.soc_min)) self.assertEqual(ressettting[3], str(int(sbttr.energy*10)),'energy does not match{0}!={1}'.format(Decimal(ressettting[3]) / Decimal(10), sbttr.energy)) self.assertEqual(ressettting[4], str(int(sbttr.shipping_energy*10)),'shipping_energy does not match{0}!={1}'.format(Decimal(ressettting[4]) / Decimal(10), sbttr.shipping_energy)) self.assertEqual(ressettting[5], str(int(sbttr.chargepower_max*10)),'chargepower_max does not match{0}!={1}'.format(Decimal(ressettting[5]) / Decimal(10), sbttr.chargepower_max)) self.assertEqual(ressettting[6], str(int(sbttr.dischargepower_max*10)),'dischargepower_max does not match{0}!={1}'.format(Decimal(ressettting[6]) / Decimal(10), sbttr.dischargepower_max)) self.assertEqual(ressettting[7], str(sbttr.bms_cnt), 'bms_cnt does not match{0}!={1}'.format(int(ressettting[7]), sbttr.bms_cnt)) self.assertEqual(ressettting[8], str(int(sbttr.voltage_max*10)),'voltage_max does not match{0}!={1}'.format(Decimal(ressettting[8]) / Decimal(10), sbttr.voltage_max)) self.assertEqual(ressettting[9], str(int(sbttr.voltage_min*10)),'voltage_min does not match{0}!={1}'.format(Decimal(ressettting[9]) / Decimal(10), sbttr.voltage_min)) self.assertEqual(ressettting[10], str(int(sbttr.peak_shaving_limit*10)),'peak_shaving_limit does not match{0}!={1}'.format(Decimal(ressettting[10]) / Decimal(10), sbttr.peak_shaving_limit)) req = ControlRequest.objects.filter(controllerid_id__exact=self.con.controllerid).order_by("updatedate")[0:1].get() self.assertEqual(req.deleteflg,True) res = ControlResult.objects.filter(controllerid_id__exact=self.con.controllerid).order_by("updatedate")[0:1].get() self.assertEqual(res.requestid ,int(result[1]), "requestid don't match {0}".format(res.requestid)) self.assertEqual(res.controllerid ,req.controllerid, "controllerid don't match {0}".format(res.controllerid)) self.assertEqual(res.deviceindex ,req.deviceindex, "deviceindex don't match {0}".format(res.deviceindex)) self.assertEqual(res.controltype ,req.controltype, "controltype don't match {0}".format(res.controltype)) self.assertEqual(res.devicetype ,req.devicetype, "devicetype don't match {0}".format(res.devicetype)) self.assertIsNone(res.result,) self.assertEqual(res.resultmsg,"") def test_settingDEG(self): sdeg = SettingDEG(controllerid=self.con) sdeg.deviceindex = 1 sdeg.max_power = 99999.9 sdeg.min_sec_switch_onoff = 99 sdeg.max_cnt_switch_onoff = 99 sdeg.mode = 1 sdeg.save() req = ControlRequest(controllerid=self.con,deviceindex=1,controltype=30,devicetype=6) req.save() self.client = Client() deviceindex = "1" alarmtype = "0" senddate = "2020-07-08 11:11:11" csvline = ','.join([self.con.controllerid,deviceindex,senddate,alarmtype,]) has = hashlib.md5(csvline+"gaui120gtahogsa").hexdigest() response = self.client.post('/emsserver/deviceif/polling/' + has,csvline,'application/x-www-form-urlencoded') result = response.content.split(':') self.assertEqual(result[0],"SETTING",'controltype does not match %s' % result[0]) self.assertRegexpMatches(result[1],"[0-9]+", "id don't match %s" % result[1]) self.assertEqual(result[2] ,"DEG", "controltype don't match %s" % result[2]) self.assertEqual(result[3] ,"1", "deviceid don't match %s" % result[3]) ressettting = result[4].split(',') self.assertEqual(ressettting[0], str(int(sdeg.max_power*10)),) self.assertEqual(ressettting[1], str(sdeg.min_sec_switch_onoff),) self.assertEqual(ressettting[2], str(sdeg.max_cnt_switch_onoff),) self.assertEqual(ressettting[3], str(sdeg.mode),) req = ControlRequest.objects.filter(controllerid_id__exact=self.con.controllerid).order_by("updatedate")[0:1].get() self.assertEqual(req.deleteflg,True) res = ControlResult.objects.filter(controllerid_id__exact=self.con.controllerid).order_by("updatedate")[0:1].get() self.assertEqual(res.requestid ,int(result[1]), "requestid don't match {0}".format(res.requestid)) self.assertEqual(res.controllerid ,req.controllerid, "controllerid don't match {0}".format(res.controllerid)) self.assertEqual(res.deviceindex ,req.deviceindex, "deviceindex don't match {0}".format(res.deviceindex)) self.assertEqual(res.controltype ,req.controltype, "controltype don't match {0}".format(res.controltype)) self.assertEqual(res.devicetype ,req.devicetype, "devicetype don't match {0}".format(res.devicetype)) self.assertIsNone(res.result,) self.assertEqual(res.resultmsg,"") def test_GET_LOG(self): slog = SettingGetLogInfo(controllerid=self.con) slog.deviceindex = 1 slog.logfile = "AAAa" slog.save() req = ControlRequest(controllerid=self.con,deviceindex=1,controltype=60,devicetype=1) req.save() self.client = Client() deviceindex = "1" alarmtype = "0" senddate = "2020-07-08 11:11:11" csvline = ','.join([self.con.controllerid,deviceindex,senddate,alarmtype,]) has = hashlib.md5(csvline+"gaui120gtahogsa").hexdigest() response = self.client.post('/emsserver/deviceif/polling/' + has,csvline,'application/x-www-form-urlencoded') result = response.content.split(':') print response.content self.assertEqual(result[0],"GET_LOG",'controltype does not match %s' % result[0]) self.assertRegexpMatches(result[1],"[0-9]+", "id don't match %s" % result[1]) self.assertEqual(result[2] ,"CONTROLLER", "controltype don't match %s" % result[2]) self.assertEqual(result[3] ,"1", "deviceid don't match %s" % result[3]) self.assertEqual(result[4] ,"AAAa", "deviceid don't match %s" % result[3]) req = ControlRequest.objects.filter(controllerid_id__exact=self.con.controllerid).order_by("updatedate")[0:1].get() self.assertEqual(req.deleteflg,True) res = ControlResult.objects.filter(controllerid_id__exact=self.con.controllerid).order_by("updatedate")[0:1].get() self.assertEqual(res.requestid ,int(result[1]), "requestid don't match {0}".format(res.requestid)) self.assertEqual(res.controllerid ,req.controllerid, "controllerid don't match {0}".format(res.controllerid)) self.assertEqual(res.deviceindex ,req.deviceindex, "deviceindex don't match {0}".format(res.deviceindex)) self.assertEqual(res.controltype ,req.controltype, "controltype don't match {0}".format(res.controltype)) self.assertEqual(res.devicetype ,req.devicetype, "devicetype don't match {0}".format(res.devicetype)) self.assertIsNone(res.result,) self.assertEqual(res.resultmsg,"") def test_FW_UPDATE(self): sfw = SettingFWUpdateInfo(controllerid=self.con) sfw.deviceindex = 1 sfw.url = "AAAa" fwt = FWType(typeid="0",typenm="test0",devicetype=self.devicetype) fwt.save() fwt2 = FWType(typeid="1",typenm="test1",devicetype=self.devicetype) fwt2.save() fwif = FWInfo(type=fwt2,version="X.X",url="http://aaa.aaa.aaa/romfs.img.gz",filehash="aaaa") fwif.save() sfw.fw = fwif sfw.save() req = ControlRequest(controllerid=self.con,deviceindex=1,controltype=50,devicetype=1) req.save() self.client = Client() deviceindex = "1" alarmtype = "0" senddate = "2020-07-08 11:11:11" csvline = ','.join([self.con.controllerid,deviceindex,senddate,alarmtype,]) has = hashlib.md5(csvline+"gaui120gtahogsa").hexdigest() response = self.client.post('/emsserver/deviceif/polling/' + has,csvline,'application/x-www-form-urlencoded') result = response.content.split(':') print response.content self.assertEqual(result[0],"FW_UPDATE",'controltype does not match %s' % result[0]) self.assertRegexpMatches(result[1],"[0-9]+", "id don't match %s" % result[1]) self.assertEqual(result[2] ,"CONTROLLER", "controltype don't match %s" % result[2]) self.assertEqual(result[3] ,"1", "deviceid don't match %s" % result[3]) self.assertEqual(result[4] ,"2", "deviceid don't match %s" % result[4]) self.assertEqual(result[5] ,";http", "deviceid don't match %s" % result[5]) req = ControlRequest.objects.filter(controllerid_id__exact=self.con.controllerid).order_by("updatedate")[0:1].get() self.assertEqual(req.deleteflg,True) res = ControlResult.objects.filter(controllerid_id__exact=self.con.controllerid).order_by("updatedate")[0:1].get() self.assertEqual(res.requestid ,int(result[1]), "requestid don't match {0}".format(res.requestid)) self.assertEqual(res.controllerid ,req.controllerid, "controllerid don't match {0}".format(res.controllerid)) self.assertEqual(res.deviceindex ,req.deviceindex, "deviceindex don't match {0}".format(res.deviceindex)) self.assertEqual(res.controltype ,req.controltype, "controltype don't match {0}".format(res.controltype)) self.assertEqual(res.devicetype ,req.devicetype, "devicetype don't match {0}".format(res.devicetype)) self.assertIsNone(res.result,) self.assertEqual(res.resultmsg,"") def test_Directive(self): req = ControlRequest(controllerid=self.con,deviceindex=1,controltype=80,devicetype=1) req.save() direct = SettingDirective() direct.controllerid = self.con direct.target_day = datetime.date(2014,1,2) direct.period_no = 0 direct.period_type = 1 direct.grid_cost = 1 direct.outage_probability = 0 direct.start_time = datetime.time(0,0,0) direct.end_time =datetime.time(12,0,0) direct.soc_opt =50 direct.outage_cnt_opt = 0 direct.outage_soc_max_opt = 50 direct.outage_soc_min_opt = 50 direct.save() objs = SettingDirective.objects.filter(controllerid = self.con).order_by("period_no") print len(objs) direct2 = SettingDirective() direct2.controllerid = self.con direct2.target_day = datetime.date(2014,1,2) direct2.period_no = 1 direct2.period_type = 2 direct2.grid_cost =2 direct2.outage_probability = 0 direct2.start_time = datetime.time(12,0,0) direct2.end_time =datetime.time(0,0,0) direct2.soc_opt =20 direct2.outage_cnt_opt = 0 direct2.outage_soc_max_opt = 50 direct2.outage_soc_min_opt = 50 direct2.save() objs = SettingDirective.objects.filter(controllerid = self.con).order_by("period_no") print len(objs) self.client = Client() deviceindex = "1" alarmtype = "0" senddate = "2020-07-08 11:11:11" csvline = ','.join([self.con.controllerid,deviceindex,senddate,alarmtype,]) has = hashlib.md5(csvline+"gaui120gtahogsa").hexdigest() response = self.client.post('/emsserver/deviceif/polling/' + has,csvline,'application/x-www-form-urlencoded') print response.content result = response.content.split(":") self.assertEqual(result[0],"DIRECTIVE",'controltype does not match %s' % result[0]) self.assertRegexpMatches(result[1],"[0-9]+", "id don't match %s" % result[1]) self.assertEqual(result[2] ,"CONTROLLER", "controltype don't match %s" % result[2]) self.assertEqual(result[3] ,"1", "deviceid don't match %s" % result[3]) self.assertEqual(result[4],"2","csvdata don't match %s = %s " % (result[4],direct.getline())) self.assertEqual(result[5],direct.getline(),"csvdata don't match %s = %s " % (result[5],direct.getline())) self.assertEqual(result[6],direct2.getline(),"csvdata don't match %s = %s " % (result[6],direct2.getline())) req = ControlRequest.objects.filter(controllerid_id__exact=self.con.controllerid).order_by("updatedate")[0:1].get() self.assertEqual(req.deleteflg,True) res = ControlResult.objects.filter(controllerid_id__exact=self.con.controllerid).order_by("updatedate")[0:1].get() self.assertEqual(res.requestid ,int(result[1]), "requestid don't match {0}".format(res.requestid)) self.assertEqual(res.controllerid ,req.controllerid, "controllerid don't match {0}".format(res.controllerid)) self.assertEqual(res.deviceindex ,req.deviceindex, "deviceindex don't match {0}".format(res.deviceindex)) self.assertEqual(res.controltype ,req.controltype, "controltype don't match {0}".format(res.controltype)) self.assertEqual(res.devicetype ,req.devicetype, "devicetype don't match {0}".format(res.devicetype)) self.assertIsNone(res.result,) self.assertEqual(res.resultmsg,"") def test_multirequest(self): req1 = ControlRequest(controllerid=self.con,deviceindex=1,controltype=10,devicetype=1) req1.save() req2 = ControlRequest(controllerid=self.con,deviceindex=1,controltype=40,devicetype=1) req2.save() self.client = Client() deviceindex = "1" alarmtype = "0" senddate = "2020-07-08 11:11:11" csvline = ','.join([self.con.controllerid,deviceindex,senddate,alarmtype,]) has = hashlib.md5(csvline+"gaui120gtahogsa").hexdigest() response = self.client.post('/emsserver/deviceif/polling/' + has,csvline,'application/x-www-form-urlencoded') result = response.content.split(':') self.assertEqual(result[0],"RESET",) self.assertEqual(result[2] ,"CONTROLLER", ) self.assertEqual(result[3] ,"1", ) self.assertEqual(result[4],"",) response = self.client.post('/emsserver/deviceif/polling/' + has,csvline,'application/x-www-form-urlencoded') result = response.content.split(':') self.assertEqual(result[0],"GET_SETTING",) self.assertEqual(result[2] ,"CONTROLLER", ) self.assertEqual(result[3] ,"1", ) self.assertEqual(result[4],"",)
class TestSql: def test_connSql(self): print "###################################" try: db = MySQLdb.connect(host="localhost", user="******", passwd="ems=2013", db="emsserver") cur = db.cursor() cur.execute("select * from receivedata_event") for row in cur.fetchall(): #print row #print "hello" pass db.close() except Exception as e: print e def test_email(self): a = DailyMail() a.getevent() def add_group(self): print "Adding group" self.group1 = Group(name="group") self.group1.save() self.group2 = Group(name="group2") self.group2.save() self.area = AreaInfo(areaid="TEST000000", areanm="TEST", groupid=self.group1) self.area.save() self.place = PlaceInfo(placeid="TEST000000", placenm="TEST", areaid=self.area, groupid=self.group2) self.place.save() self.devicetype = DeviceTypeInfo(typeid="0", devicetype=1, typenm="Controller") self.devicetype.save() print "Enter controller id from 0 to 19, please enter a single digit.\n" for ev in range(14): id1 = raw_input() test_ev = "0000-0000-0000" + id1 self.con = ControllerInfo(controllerid=test_ev, placeid=self.place, controllernm="TEST cont", type=self.devicetype) self.con.save() def test_sms(self): print "In test sms" evn = Event(controllerid="0000-0000-000000", deviceindex=0, alarmtype=1) #from datetime import datetime, time, timedelta #tz = pytz.timezone("Asia/Calcutta") #dt_naive = datetime.today() #dt = tz.localize(dt_naive, is_dst=None) evn.senddate = timezone.now() #evn.senddate = dt #evn.senddate = (datetime.date.today() - datetime.timedelta(1)) #print evn.senddate evn.measureddate = timezone.now() evn.id = createid(evn.controllerid, evn.measureddate, Event) evn.code = "1010000011" evn.level = 0 evn.message = "Changed Controller Setting by the Controller" #evn.save() evsms = EventSms() evsms.priorityno = 0 evsms.sendrange = 0 evsms.sendhappen = True evsms.sendrestore = False evsms.subject = 'Test Mail' evsms.message = 'Controller : $CONT-NM$ Area : $AREA-NM$ Place : $PLACE-NM$ Target Event Message : $MESSAGE$ Target Event Datetime: $DATETIME$ Target Event Code : $CODE$ Target Event Level : $LEVEL$ ' evsms.targetevent = '1010000011' #evsms.phone_no = '8800968857' #evsms.save() evn.save() def create_event(self, controID): #print "Enter controller id from 0 to 19, please enter a single digit.\n" #id1 = raw_input() #test_ev = "0000-0000-0000" + id1 test_ev = controID print "How many event you want to generate, please enter a digit \n" ev_gen = raw_input() evList = [] for ev in range(int(ev_gen)): evn = Event(controllerid=test_ev, deviceindex=0, alarmtype=3) #from datetime import datetime, time, timedelta #tz = pytz.timezone("Asia/Calcutta") #dt_naive = datetime.today() #dt = tz.localize(dt_naive, is_dst=None) #evn.senddate = dt evn.senddate = timezone.now() evn.updatedate = timezone.now() #evn.senddate = (dt.date() + timedelta(days=1)) #evn.senddate = "2014-09-25 02:14:17" #evn.senddate = (datetime.date.today() - datetime.timedelta(1)) #print evn.senddate evn.measureddate = timezone.now() #evn.measureddate = "2014-11-27 11:17:37.475515+00:00" #evn.measureddate = "2015-03-07 20:38:00" #evn.measureddate = (datetime.date.today() - datetime.timedelta(1)) #evn.measureddate = (timezone.now() - timedelta(1000)) print evn.measureddate #evn.measureddate = datetime.today() evn.id = createid(evn.controllerid, evn.measureddate, Event) evn.code = "2100000003" #evn.code = "2020000002" #evn.code = "3090000014" #evn.code = "8080808081" #evn.code = "2060000007" #evn.code = "2090000002" #if ev % 2 == 0: # evn.level = 3 #else : # evn.level = 2 evn.level = 3 #evn.message = "EMS Controller SNMP NO Response" evn.message = "Event Message 1" print "Saving event########" #evList.append(evn) #for l in evList: evn.save() #print evn.senddate def updateBattData(self, controID): battery = MeasuredBattery(controllerid=controID, deviceindex="0", alarmtype=3) battery.measureddate = timezone.now() #battery.measureddate = (datetime.date.today() - datetime.timedelta(1000)) battery.senddate = timezone.now() #battery.senddate = (datetime.date.today() - datetime.timedelta(1000)) status = raw_input( "Enter 01 to CHARGE, 02 to DISCHARGE, 00 to IDLE and 10 to FAILURE \n" ) if status == "01": battery.status = status #charge elif status == "02": battery.status = status #discharge elif status == "00": battery.status = status #idle elif status == "10": battery.status = status #failure else: print "Wrong choice" #battery.status = "02" #discharge #battery.status = "00" #idle #battery.status = "10" #failure battery.soc = "30" battery.energy = "16342.9" battery.id = createid(battery.controllerid, battery.measureddate, MeasuredBattery) battery.soh = "100" battery.power = "30.6" battery.celltem = "30" battery.uniquekey = "123456" battery.save() def updateDegData(self, controID): deg = MeasuredDEG(controllerid=controID, deviceindex="0", alarmtype=3) #deg.measureddate = (datetime.date.today() - datetime.timedelta(1000)) #deg.senddate = (datetime.date.today() - datetime.timedelta(1000)) deg.measureddate = timezone.now() deg.senddate = timezone.now() status = raw_input("Enter 0 to ON DEG and 1 to OFF DEG\n") if status == "1": deg.status = status #off elif status == "0": deg.status = status #on else: print "Wrong choice" deg.power = "0.0" deg.id = createid(deg.controllerid, deg.measureddate, MeasuredDEG) deg.uniquekey = "123457" deg.save() def updateGridData(self, controID): grid = MeasuredGrid(controllerid=controID, deviceindex="0", alarmtype=3) #grid.measureddate = (datetime.date.today() - datetime.timedelta(1000)) #grid.senddate = (datetime.date.today() - datetime.timedelta(1000)) grid.measureddate = timezone.now() grid.senddate = timezone.now() status = raw_input("Enter 0 to OUTAGE and 1 to NO OUTAGE\n") if status == "1": grid.status = status #no outage elif status == "0": grid.status = status #outage else: print "Wrong choice" grid.power = "100" grid.id = createid(grid.controllerid, grid.measureddate, MeasuredGrid) grid.uniquekey = "123458" grid.save() def updatePvData(self, controID): pv = MeasuredPV(controllerid=controID, deviceindex="0", alarmtype=3) #pv.measureddate = (datetime.date.today() - datetime.timedelta(1000)) #pv.senddate = (datetime.date.today() - datetime.timedelta(1000)) pv.measureddate = timezone.now() pv.senddate = timezone.now() pv.id = createid(pv.controllerid, pv.measureddate, MeasuredPV) power = raw_input("Enter 0 to give pv power and 1 to 0.0 pv power\n") if power == "0": pvPow = raw_input("Enter a pv power\n") pv.power = pvPow elif power == "1": pv.power = "0.0" else: print "Wrong choice" #pv.power = "30.6" pv.uniquekey = "123459" pv.save() def updateLoadData(self, controID): load = MeasuredLoad(controllerid=controID, deviceindex="0", alarmtype=3) #load.measureddate = (datetime.date.today() - datetime.timedelta(1000)) #load.senddate = (datetime.date.today() - datetime.timedelta(1000)) load.measureddate = timezone.now() load.senddate = timezone.now() load.id = createid(load.controllerid, load.measureddate, MeasuredLoad) loadPower = raw_input("Enter power\n") load.power = loadPower load.uniquekey = "123459" load.save() def updatePowerData(self, controID): power = MeasuredPower(controllerid=controID, deviceindex="0", alarmtype=3) #power.measureddate = (datetime.date.today() - datetime.timedelta(1000)) #power.senddate = (datetime.date.today() - datetime.timedelta(1000)) power.measureddate = timezone.now() power.senddate = timezone.now() power.id = createid(power.controllerid, power.measureddate, MeasuredPower) powerChoice = raw_input( "Enter 0 for power_a, power_b, power_c and 1 for default values \n" ) if powerChoice == "0": voltage_a = raw_input("Enter voltage_a in decimal\n") power.voltage_a = voltage_a current_a = raw_input("Enter current_a in decimal\n") power.current_a = current_a power_a = raw_input("Enter power_a in decimal\n") power.power_a = power_a app_power_a = raw_input("Enter apparent power_a in decimal\n") power.app_power_a = app_power_a voltage_b = raw_input("Enter voltage_b in decimal\n") power.voltage_b = voltage_b current_b = raw_input("Enter current_b in decimal\n") power.current_b = current_b power_b = raw_input("Enter power_b in decimal\n") power.power_b = power_b app_power_b = raw_input("Enter apparent power_b in decimal\n") power.app_power_b = app_power_b voltage_c = raw_input("Enter voltage_c in decimal\n") power.voltage_c = voltage_c current_c = raw_input("Enter current_c in decimal\n") power.current_c = current_c power_c = raw_input("Enter power_c in decimal\n") power.power_c = power_c app_power_c = raw_input("Enter apparent power_c in decimal\n") power.app_power_c = app_power_c elif powerChoice == "1": power.voltage_a = "0.0" power.current_a = "0.0" power.power_a = "0.0" power.app_power_a = "0.0" power.voltage_b = "0.0" power.current_b = "0.0" power.power_b = "0.0" power.app_power_b = "0.0" power.voltage_c = "0.0" power.current_c = "0.0" power.power_c = "0.0" power.app_power_c = "0.0" else: print "Wrong choice" ##pv.power = "30.6" power.uniquekey = "123459" power.save()