forked from cautonwong/GuruxDLMSLibServerExample
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGXDLMSBase.cpp
More file actions
395 lines (379 loc) · 14.7 KB
/
GXDLMSBase.cpp
File metadata and controls
395 lines (379 loc) · 14.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
//
// --------------------------------------------------------------------------
// Gurux Ltd
//
//
//
// Filename: $HeadURL$
//
// Version: $Revision$,
// $Date$
// $Author$
//
// Copyright (c) Gurux Ltd
//
//---------------------------------------------------------------------------
//
// DESCRIPTION
//
// This file is a part of Gurux Device Framework.
//
// Gurux Device Framework is Open Source software; you can redistribute it
// and/or modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; version 2 of the License.
// Gurux Device Framework is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU General Public License for more details.
//
// More information of Gurux products: http://www.gurux.org
//
// This code is licensed under the GNU General Public License v2.
// Full text may be retrieved at http://www.gnu.org/licenses/gpl-2.0.txt
//---------------------------------------------------------------------------
#include "stdafx.h"
#include "GXDLMSBase.h"
#include "GuruxDLMS/GXDateTime.h"
#include "GuruxDLMS/Objects/GXDLMSData.h"
#include "GuruxDLMS/Objects/GXDLMSRegister.h"
#include "GuruxDLMS/Objects/GXDLMSClock.h"
#include "GuruxDLMS/Objects/GXDLMSTcpUdpSetup.h"
#include "GuruxDLMS/Objects/GXDLMSProfileGeneric.h"
#include "GuruxDLMS/Objects/GXDLMSAutoConnect.h"
#include "GuruxDLMS/Objects/GXDLMSIECOpticalPortSetup.h"
#include "GuruxDLMS/Objects/GXDLMSActivityCalendar.h"
#include "GuruxDLMS/Objects/GXDLMSDemandRegister.h"
#include "GuruxDLMS/Objects/GXDLMSRegisterMonitor.h"
#include "GuruxDLMS/Objects/GXDLMSActionSchedule.h"
#include "GuruxDLMS/Objects/GXDLMSSapAssignment.h"
#include "GuruxDLMS/Objects/GXDLMSAutoAnswer.h"
#include "GuruxDLMS/Objects/GXDLMSModemConfiguration.h"
#include "GuruxDLMS/Objects/GXDLMSMacAddressSetup.h"
#include "GuruxDLMS/Objects/GXDLMSModemInitialisation.h"
#include "GuruxDLMS/Objects/GXDLMSActionSet.h"
/// <summary>
/// Generic initialize for all servers.
/// </summary>
/// <param name="server"></param>
int CGXDLMSBase::Init(int port)
{
m_Media.SetProtocol(GX_NW_TCPIP);
m_Media.SetPort(port);
m_Media.SetTrace(GX_TRACE_LEVEL_VERBOSE);
m_Media.IsServer(true);
m_Media.AddListener(this);
int ret;
if ((ret = m_Media.Open()) != 0)
{
printf("Media open failed %d", ret);
return ret;
}
///////////////////////////////////////////////////////////////////////
//Add Logical Device Name. 123456 is meter serial number.
///////////////////////////////////////////////////////////////////////
// COSEM Logical Device Name is defined as an octet-string of 16 octets.
// The first three octets uniquely identify the manufacturer of the device and it corresponds
// to the manufacturer's identification in IEC 62056-21.
// The following 13 octets are assigned by the manufacturer.
//The manufacturer is responsible for guaranteeing the uniqueness of these octets.
CGXDLMSVariant id2(serialNo.c_str());
id2.ChangeType(DLMS_DATA_TYPE_UINT32);
std::string tmp("GRX");
for(int a = serialNo.length(); a < 13; ++a)
{
tmp.append("0");
}
tmp.append(serialNo);
CGXDLMSVariant id(tmp.c_str());
id.ChangeType(DLMS_DATA_TYPE_OCTET_STRING);
CGXDLMSData* d = new CGXDLMSData("0.0.42.0.0.255", id);
//d->GetAttributes().push_back(CGXDLMSAttribute(2, DLMS_DATA_TYPE_OCTET_STRING));
GetItems().push_back(d);
// Electricity ID 1
d = new CGXDLMSData("1.1.0.0.0.255", tmp.c_str());
d->GetAttributes().push_back(CGXDLMSAttribute(2, DLMS_DATA_TYPE_STRING));
GetItems().push_back(d);
// Electricity ID 2
d = new CGXDLMSData("1.1.0.0.1.255", id2);
d->GetAttributes().push_back(CGXDLMSAttribute(2, DLMS_DATA_TYPE_UINT32));
GetItems().push_back(d);
//Add Last avarage.
CGXDLMSRegister* pRegister = new CGXDLMSRegister("1.1.21.25.0.255");
//Set access right. Client can't change Device name.
pRegister->SetAccess(2, ACCESSMODE_READ);
GetItems().push_back(pRegister);
//Add default clock. Clock's Logical Name is 0.0.1.0.0.255.
CGXDLMSClock* pClock = new CGXDLMSClock();
CGXDateTime begin(-1, 9, 1, -1, -1, -1, -1);
pClock->SetBegin(begin);
CGXDateTime end(-1, 3, 1, -1, -1, -1, -1);
pClock->SetEnd(end);
GetItems().push_back(pClock);
//Add Tcp/Udp setup. Default Logical Name is 0.0.25.0.0.255.
GetItems().push_back(new CGXDLMSTcpUdpSetup());
/*
///////////////////////////////////////////////////////////////////////
//Add Load profile.
CGXProfileGenericObject* pPG = new CGXProfileGenericObject("1.0.99.1.0.255");
//Set capture period to 60 second.
pPG->SetCapturePeriod(60);
//Maximum row count.
pPG->SetProfileEntries(100);
pPG->SetSortMethod(GX_SORT_METHOD_FIFO);
pPG->SetSortObject(pClock);
//Add colums.
//Set saved attribute index.
//pClock->SetSelectedAttributeIndex(2);
pPG->GetCaptureObjects().push_back(pClock);
//Set saved attribute index.
//pPG->SetSelectedAttributeIndex(2);
pPG->GetCaptureObjects().push_back(pRegister);
GetItems().push_back(pPG);
*/
///////////////////////////////////////////////////////////////////////
//Add Auto connect object.
CGXDLMSAutoConnect* pAC = new CGXDLMSAutoConnect();
pAC->SetMode(AUTO_CONNECT_MODE_AUTO_DIALLING_ALLOWED_ANYTIME);
pAC->SetRepetitions(10);
pAC->SetRepetitionDelay(60);
//Calling is allowed between 1am to 6am.
pAC->GetCallingWindow().push_back(std::make_pair(CGXDateTime(-1, -1, -1, 1, 0, 0, -1), CGXDateTime(-1, -1, -1, 6, 0, 0, -1)));
pAC->GetDestinations().push_back("www.gurux.org");
GetItems().push_back(pAC);
///////////////////////////////////////////////////////////////////////
//Add Activity Calendar object.
CGXDLMSActivityCalendar* pActivity = new CGXDLMSActivityCalendar();
pActivity->SetCalendarNameActive("Active");
pActivity->GetSeasonProfileActive().push_back(CGXDLMSSeasonProfile("Summer time", CGXDateTime(-1, 3, 31, -1, -1, -1, -1), ""));
pActivity->GetWeekProfileTableActive().push_back(CGXDLMSWeekProfile("Monday", 1, 1, 1, 1, 1, 1, 1));
CGXDLMSDayProfile aDp;
aDp.SetDayId(1);
aDp.GetDaySchedules().push_back(CGXDLMSDayProfileAction(CGXDateTime::Now(), "test", 1));
pActivity->GetDayProfileTableActive().push_back(aDp);
pActivity->SetCalendarNamePassive("Passive");
pActivity->GetSeasonProfilePassive().push_back(CGXDLMSSeasonProfile("Winter time", CGXDateTime(-1, 10, 30, -1, -1, -1, -1), ""));
pActivity->GetWeekProfileTablePassive().push_back(CGXDLMSWeekProfile("Tuesday", 1, 1, 1, 1, 1, 1, 1));
CGXDLMSDayProfile passive;
passive.SetDayId(1);
passive.GetDaySchedules().push_back(CGXDLMSDayProfileAction(CGXDateTime::Now(), "0.0.1.0.0.255", 1));
pActivity->GetDayProfileTablePassive().push_back(passive);
CGXDateTime dt(CGXDateTime::Now());
pActivity->SetTime(dt);
GetItems().push_back(pActivity);
///////////////////////////////////////////////////////////////////////
//Add Optical Port Setup object.
CGXDLMSIECOpticalPortSetup* pOptical = new CGXDLMSIECOpticalPortSetup();
pOptical->SetDefaultMode(OPTICAL_PROTOCOL_MODE_DEFAULT);
pOptical->SetProposedBaudrate(BAUDRATE_9600);
pOptical->SetDefaultBaudrate(BAUDRATE_300);
pOptical->SetResponseTime(LOCAL_PORT_RESPONSE_TIME_200_MS);
pOptical->SetDeviceAddress("Gurux");
pOptical->SetPassword1("Gurux1");
pOptical->SetPassword2("Gurux2");
pOptical->SetPassword5("Gurux5");
GetItems().push_back(pOptical);
///////////////////////////////////////////////////////////////////////
//Add Demand Register object.
CGXDLMSDemandRegister* pDr = new CGXDLMSDemandRegister();
pDr->SetLogicalName("0.0.1.0.0.255");
pDr->SetCurrentAvarageValue(10);
pDr->SetLastAvarageValue(20);
pDr->SetStatus(1);
pDr->SetStartTimeCurrent(CGXDateTime::Now());
pDr->SetCaptureTime(CGXDateTime::Now());
pDr->SetPeriod(10);
pDr->SetNumberOfPeriods(1);
GetItems().push_back(pDr);
///////////////////////////////////////////////////////////////////////
//Add Register Monitor object.
CGXDLMSRegisterMonitor* pRm = new CGXDLMSRegisterMonitor();
pRm->SetLogicalName("0.0.1.0.0.255");
CGXDLMSVariant threshold;
vector<CGXDLMSVariant> thresholds;
threshold.Add("Gurux1", 6);
thresholds.push_back(threshold);
threshold.Clear();
threshold.Add("Gurux2", 6);
thresholds.push_back(threshold);
pRm->SetThresholds(thresholds);
CGXDLMSMonitoredValue mv;
mv.Update(pRegister, 2);
pRm->SetMonitoredValue(mv);
CGXDLMSActionSet action;
string ln;
pRm->GetLogicalName(ln);
action.GetActionDown().SetLogicalName(ln);
action.GetActionDown().SetScriptSelector(1);
pRm->GetLogicalName(ln);
action.GetActionUp().SetLogicalName(ln);
action.GetActionUp().SetScriptSelector(1);
vector<CGXDLMSActionSet> actions;
actions.push_back(action);
pRm->SetActions(actions);
GetItems().push_back(pRm);
///////////////////////////////////////////////////////////////////////
//Add action schedule object.
CGXDLMSActionSchedule* pActionS = new CGXDLMSActionSchedule();
pActionS->SetLogicalName("0.0.1.0.0.255");
pActionS->SetExecutedScriptLogicalName("1.2.3.4.5.6");
pActionS->SetExecutedScriptSelector(1);
pActionS->SetType(SINGLE_ACTION_SCHEDULE_TYPE1);
pActionS->GetExecutionTime().push_back(CGXDateTime::Now());
GetItems().push_back(pActionS);
///////////////////////////////////////////////////////////////////////
//Add SAP Assignment object.
CGXDLMSSapAssignment* pSap = new CGXDLMSSapAssignment();
std::map<int, basic_string<char> > list;
list[1] = "Gurux";
list[16] = "Gurux-2";
pSap->SetSapAssignmentList(list);
GetItems().push_back(pSap);
///////////////////////////////////////////////////////////////////////
//Add Auto Answer object.
CGXDLMSAutoAnswer* pAa = new CGXDLMSAutoAnswer();
pAa->SetMode(AUTO_CONNECT_MODE_EMAIL_SENDING);
pAa->GetListeningWindow().push_back(std::pair<CGXDateTime, CGXDateTime>(CGXDateTime(-1, -1, -1, 6, -1, -1, -1), CGXDateTime(-1, -1, -1, 8, -1, -1, -1)));
pAa->SetStatus(AUTO_ANSWER_STATUS_INACTIVE);
pAa->SetNumberOfCalls(0);
pAa->SetNumberOfRingsInListeningWindow(1);
pAa->SetNumberOfRingsOutListeningWindow(2);
GetItems().push_back(pAa);
///////////////////////////////////////////////////////////////////////
//Add Modem Configuration object.
CGXDLMSModemConfiguration* pMc = new CGXDLMSModemConfiguration();
pMc->SetCommunicationSpeed(BAUDRATE_38400);
CGXDLMSModemInitialisation init;
vector<CGXDLMSModemInitialisation> initialisationStrings;
init.SetRequest("AT");
init.SetResponse("OK");
init.SetDelay(0);
initialisationStrings.push_back(init);
pMc->SetInitialisationStrings(initialisationStrings);
GetItems().push_back(pMc);
///////////////////////////////////////////////////////////////////////
//Add Mac Address Setup object.
CGXDLMSMacAddressSetup* pMac = new CGXDLMSMacAddressSetup();
pMac->SetMacAddress("00:11:22:33:44:55:66");
GetItems().push_back(pMac);
///////////////////////////////////////////////////////////////////////
//Server must initialize after all objects are added.
ret = CGXDLMSServerBase::Initialize();
if (ret != ERROR_CODES_OK)
{
return ret;
}
/*
//Add rows after Initialize.
Object[][] rows = new Object[][]{new Object[]{java.util.Calendar.getInstance().getTime(), 10}};
pg.setBuffer(rows);
*/
return ERROR_CODES_OK;
}
/////////////////////////////////////////////////////////////////////////////
//
/////////////////////////////////////////////////////////////////////////////
int CGXDLMSBase::OnRead(CGXDLMSObject* pItem, int index, CGXDLMSVariant& value, DLMS_DATA_TYPE& type)
{
//Let framework handle Logical Name read.
if (index == 1)
{
return ERROR_CODES_FALSE;
}
//Framework will handle Association objects automatically.
if (pItem->GetObjectType() == OBJECT_TYPE_ASSOCIATION_LOGICAL_NAME ||
pItem->GetObjectType() == OBJECT_TYPE_ASSOCIATION_SHORT_NAME ||
//Framework will handle profile generic automatically.
pItem->GetObjectType() == OBJECT_TYPE_PROFILE_GENERIC)
{
return ERROR_CODES_FALSE;
}
DLMS_DATA_TYPE ui, dt;
pItem->GetUIDataType(index, ui);
pItem->GetDataType(index, dt);
if ((ui == DLMS_DATA_TYPE_DATETIME ||
dt == DLMS_DATA_TYPE_DATETIME) &&
pItem->GetObjectType() != OBJECT_TYPE_CLOCK)
{
value = CGXDateTime::Now();
return ERROR_CODES_OK;
}
else if (pItem->GetObjectType() == OBJECT_TYPE_CLOCK)
{
if (index == 2)
{
value = CGXDateTime::Now();
return ERROR_CODES_OK;
}
}
else if (pItem->GetObjectType() == OBJECT_TYPE_REGISTER_MONITOR)
{
CGXDLMSRegisterMonitor* pRm = (CGXDLMSRegisterMonitor*) pItem;
if (index == 2)
{
//Initialize random seed.
srand ((unsigned int)time(NULL));
value.vt = DLMS_DATA_TYPE_ARRAY;
pRm->GetThresholds().clear();
pRm->GetThresholds().push_back(rand() % 100 + 1);
CGXDLMSVariant null;
return pRm->GetValue(index, 0, null, value);
}
}
else
{
CGXDLMSVariant null;
int ret = ((IGXDLMSBase*) pItem)->GetValue(index, 0, null, value);
if (ret != ERROR_CODES_OK)
{
return ret;
}
//If data is not assigned and value type is unknown return number.
DLMS_DATA_TYPE tp;
pItem->GetDataType(index, tp);
if (value.vt == DLMS_DATA_TYPE_NONE &&
(tp == DLMS_DATA_TYPE_NONE ||
tp == DLMS_DATA_TYPE_INT8 ||
tp == DLMS_DATA_TYPE_INT16 ||
tp == DLMS_DATA_TYPE_INT32 ||
tp == DLMS_DATA_TYPE_INT64 ||
tp == DLMS_DATA_TYPE_UINT8 ||
tp == DLMS_DATA_TYPE_UINT16 ||
tp == DLMS_DATA_TYPE_UINT32 ||
tp == DLMS_DATA_TYPE_UINT64))
{
//Initialize random seed.
srand ((unsigned int)time(NULL));
value = rand() % 100 + 1;
return ERROR_CODES_OK;
}
else if (value.vt == DLMS_DATA_TYPE_NONE && tp == DLMS_DATA_TYPE_STRING)
{
value = "Gurux";
return ERROR_CODES_OK;
}
}
//Use current value.
return ERROR_CODES_FALSE;
}
/////////////////////////////////////////////////////////////////////////////
//
/////////////////////////////////////////////////////////////////////////////
int CGXDLMSBase::OnWrite(CGXDLMSObject* pItem, int index, int selector, CGXDLMSVariant& value)
{
return ERROR_CODES_FALSE;
}
/////////////////////////////////////////////////////////////////////////////
//
/////////////////////////////////////////////////////////////////////////////
int CGXDLMSBase::OnAction(CGXDLMSObject* pItem, int index, CGXDLMSVariant& data)
{
return ERROR_CODES_FALSE;
}
/////////////////////////////////////////////////////////////////////////////
//
/////////////////////////////////////////////////////////////////////////////
int CGXDLMSBase::OnInvalidConnection()
{
printf("Invalid connection.\r\n");
return ERROR_CODES_OK;
}