Friday, 30 November 2018

IOT Based Smart Parking System -- Integration between ServiceNow and Live Sensors Part 1


Things to do in ServiceNow and mid server:


1.    MID Server name, Instance name, User and password in mid server file
2.    IP address of mid server in workflow
3.    Raspberry PI and Laptop should be in same network  
4.    Credentials in credential table
5.    property for mid server name in payload
a.    CreateAThon_MidServer_Name
6.    property for mid server IP in payload
a.    CreateAThon.source.ipaddress
7.    Change field style in “sys_ui_style.LIST” for all the fields of table
a.    background-color:#B3BBC1;
font-size:14px;
color:black;




Scheduled Script: Fetch Parking Status

var midServerToUse = '',pay = "";

var instance = gs.getProperty('instance_name');

midServerToUse = "mid.server." + gs.getProperty('CreateAThon_MidServer_Name'); //Mid Server Used

var params = '<?xml version="1.0" encoding="UTF-8"?><parameters><parameter name="used_by_runbook" value="true"/><parameter name="glide.xmlhelper.trim.enable" value="true"/><parameter name="debug" value="true"/><parameter name="workflow" value="9a44ec644f915300869e4a318110c700"/><parameter name="skip_simple_parameter_substitution" value="true"/><parameter name="debug_ssh" value="true"/><parameter name="execution_sys_id" value="d644ec644f915300869e4a318110c702"/><parameter name="probe_name" value="SSHCommand"/><parameter name="skip_sensor" value="true"/><parameter name="probe" value="e07f03e8c0a800670196a29829b0e452"/></parameters>';
//Input of the curl command to be executed in ECC Queue
var flag = "false";
var outq = new GlideRecord('ecc_queue');
outq.initialize();
outq.agent = midServerToUse;
outq.topic = "SSHCommand";
outq.name = "cd /servicenow/distance_sensor/;python distance.py";
outq.queue = "output";
outq.payload = params;
outq.source = gs.getProperty('CreateAThon.source.ipaddress');
var s = outq.insert();
gs.sleep(6000);

//                           // Output we Get After Execution is processed
var inqu = new GlideRecord('ecc_queue');
inqu.addEncodedQuery("topicLIKESSHCommand");
inqu.addQuery("response_to", s);
inqu.addQuery("agent", midServerToUse);
inqu.query();
while (inqu.next()) {
          var xmlDoc = new XMLDocument2();
          xmlDoc.parseXML(inqu.payload);
          pay = xmlDoc.getNodeText("//output");
}

var gTrack = new GlideRecord('u_light_and_parking_space_track');
//gTrack.initialize();
gTrack.addQuery("sys_id","feaa832d4f511300784e045e9310c72a");
gTrack.query();
if(gTrack.next()){
          var res = pay.split(" ");
          if(res[1] < 100)
                   {
                   gTrack.u_status = 'false';
                   gTrack.update();
          }
          if(res[1]>= 100)
                   {
                  
                   gTrack.u_status = 'true';
                   gTrack.update();
          }
         
}

Scheduled Script: Fetch Light Status IOT

var midServerToUse = '',pay = "";
                   var instance = gs.getProperty('instance_name');
                   gs.info(instance);
                   midServerToUse = "mid.server." + gs.getProperty('x_112172_createath.CreateAThon_MidServer_Name'); //Mid Server Used

                             var params = '<?xml version="1.0" encoding="UTF-8"?><parameters><parameter name="used_by_runbook" value="true"/><parameter name="glide.xmlhelper.trim.enable" value="true"/><parameter name="debug" value="true"/><parameter name="workflow" value="a3b5e4a44f915300869e4a318110c721"/><parameter name="skip_simple_parameter_substitution" value="true"/><parameter name="debug_ssh" value="true"/><parameter name="execution_sys_id" value="efb5e4a44f915300869e4a318110c722"/><parameter name="probe_name" value="SSHCommand"/><parameter name="skip_sensor" value="true"/><parameter name="probe" value="e07f03e8c0a800670196a29829b0e452"/><parameter name="credential_id" value="597324644f915300869e4a318110c73e"/></parameters>';
                             //Input of the curl command to be executed in ECC Queue
                             var flag = "false";
                             var outq = new GlideRecord('ecc_queue');
                             outq.initialize();
                             outq.agent = midServerToUse;
                             outq.topic = "SSHCommand";
                             outq.name = "cd /servicenow/Light_Sensor/;python light_sensor.py";
                             outq.queue = "output";
                             outq.payload = params;
                             outq.source = gs.getProperty('x_112172_createath.CreateAThon.source.ipaddress');
                             var s = outq.insert();
                             gs.sleep(6000);
                            
                             // Output we Get After Execution is processed
                             var inqu = new GlideRecord('ecc_queue');
                             inqu.addEncodedQuery("topicLIKESSHCommand");
                             inqu.addQuery("response_to", s);
                             inqu.addQuery("agent", midServerToUse);
                             inqu.query();
                             while (inqu.next()) {
                                      var xmlDoc = new XMLDocument2();
                                      xmlDoc.parseXML(inqu.payload);
                                      pay = xmlDoc.getNodeText("//output");
                             }
                  
var gTrack = new GlideRecord('u_light_and_parking_space_track');
gTrack.initialize();
gTrack.u_sensor = "Light";
gTrack.u_status = pay;
gTrack.insert();



No comments:

Post a comment