Preface:
Even though I do have background in software development, I have very limited experience with web technologies. This is why I am using Drupal in the first place. Please be gentle as php and js are not my domain.
Project:
I am working on a project where I have a large number of (very) light weight sensor nodes which have connection to internet and capable of sending small amount of information. For example, each node sends out its location. I want to use Drupal website which takes the location data from each node together with node's id and stores it all in DB for farther visualization on a map for example.
Configuration:
Drupal 6-16
CCK 6.x-2.6
Services 6.x-2.2
JSON server 6.x-2.0-alpha2
Location 6.x-3.1-rc1
Problem:
I need to set lat and lon of each node using Services (after initially creating nodes and setting lat/lon on the website). I have spend days and cannot find a way to do it. I need to use JSON server. I have been so far only experimenting calling methods node.get and node.save directly from Site Building/Services pages.
I have tried two approaches:
1) Using CCK madule and creating location fields lon and lat
2) Use location module
Output of node.get with nid=16 and fields = ["title","field_lat", "location"] (in JSON format) is shown bellow. How do I change either field_lat or latitude of the location? I think I have tried all the combination on node.save Services page. E.g.
{"nid":"16","field_lat": ["{"value":"120"}"]}
{"nid":"16","field_lat": ["{\"value\":\"120\"}"]} --- this one seems to be doing something but changes lat to 'f'
{"nid":"16","location": "{\"latitude\":\"120\"}"}
...
Nothing works. Please help. Also, if this is a wrong message board, please recommend a better one. Are there other options I should explore?
Output of node.get with nid=16 and fields = ["title","field_lat", "location"] (in JSON format)
stdClass Object
(
[title] => asset 05
[field_lat] => Array
(
[0] => Array
(
[value] => 10
)
)
[location] => Array
(
[lid] => 1
[name] =>
[street] =>
[additional] =>
[city] =>
[province] =>
[postal_code] =>
[country] => us
[latitude] => 10.000000
[longitude] => 20.000000
[source] => 1
[is_primary] => 0
[locpick] => Array
(
[user_latitude] => 10.000000
[user_longitude] => 20.000000
)
[province_name] =>
[country_name] => United States
)
)
Comments
JavaScript, JSON Server and Services Resources
Hey artemtk,
I have absolutely no experience with the Location module, but I've got some info on working with CCK, as well as a number of JS resources for Services and JSON Server.
JavaScript - CCK Fields in the Node Object // Cross-method Insights
http://drupal.org/node/776122
JavaScript - Node.Save & Edit // Cross-method Insights
http://drupal.org/node/774116
(There are other JS nodes linked at the bottom of each page.)
Quite honestly working through a lot of this stuff is trial and error right now, but this basic info might help. My focus is strictly JavaScript and in the coming months I hope to help the Services team with their documentation. This is my humble start.
Good luck!
still does not work, plus an additional Internal Server Error
Hi Ninjay,
I came across your posts (776122 and 774116) when I was researching this problem. Unfortunetly, I still have no luck. For example, if you try something like this:
curl -i -d 'method=node.save&node={"nid":"16","body":"hellow world again"}' http://localhost/drupaltest/services/json
i get this back from the server and the body values is set properly:
HTTP/1.1 200 OK
Date: Thu, 20 May 2010 05:09:15 GMT
Server: Apache/2.0.59 (Unix) PHP/5.2.6 DAV/2
X-Powered-By: PHP/5.2.6
Set-Cookie: SESS1302d7bca5f141e1d50092de0e9048cb=2259d5bf6c889072109d6d88eca98061; expires=Sat, 12 Jun 2010 08:42:35 GMT; path=/
Expires: Sun, 19 Nov 1978 05:00:00 GMT
Last-Modified: Thu, 20 May 2010 05:09:15 GMT
Cache-Control: store, no-cache, must-revalidate
Cache-Control: post-check=0, pre-check=0
Content-Length: 34
Content-Type: text/html; charset=utf-8
However if try
curl -i -d 'method=node.save&node={"nid":"16","field_lat": [{"0":"{\"value\":\"120\"}"}]}' http://localhost/drupaltest/services/json
or
curl -i -d 'method=node.save&node={"nid":"16","field_lat": [{"0":{"value":"120"}}]}' http://localhost/drupaltest/services/json
I get error
HTTP/1.0 500 Internal Server Error
Date: Thu, 20 May 2010 05:14:07 GMT
Server: Apache/2.0.59 (Unix) PHP/5.2.6 DAV/2
X-Powered-By: PHP/5.2.6
Set-Cookie: SESS1302d7bca5f141e1d50092de0e9048cb=1c19756dabac836dca76284910904ea3; expires=Sat, 12 Jun 2010 08:47:27 GMT; path=/
Expires: Sun, 19 Nov 1978 05:00:00 GMT
Last-Modified: Thu, 20 May 2010 05:14:07 GMT
Cache-Control: store, no-cache, must-revalidate
Cache-Control: post-check=0, pre-check=0
Content-Length: 0
Connection: close
Content-Type: text/html; charset=utf-8
Can you point me in the direction how I can debug this problem? If I knew php and js I think this would all be much easier :)
On my iPhone so I can't
On my iPhone so I can't really do much, but first glance it appears that you haven't followed the format in the examples.
You have:
[{"0":{"value":"120"}}]Assuming this is a cck text field the examples would lead you make it as such:
[{"value":"120"}]I've never attempted to connect from the command line so I can't offer much help there. But hopefully this is all you need.
Forgot to list
Hi Ninjay,
Thank you for your help. I have tried [{"value":"120"}] as well per your example, just forgot to list it as something I have tried. It has the same issue as [{"0":{"value":"120"}}]. I believe they are equivalent statements.
I think I will stop fighting a long this direction. I think Node service is not the right best thing in the first place. What I really need is Location service where I can specify LID (location ID) together with Lat and Log values. I have found some instructions on how to create services and will attempt to reverse engineer Node (or User, Comment...) services to create Location Service. Oh, it will take me a while but at least I might learn something...
Again, thank you for your help. Cheers,
-A
P.S. if you have some good reference on how to create services modules, please let me know. Thanks.
They are def not
They are def not equivalent.
Yours breaks down like this:
var field = []; // arrayfield[0] = {}; // object as the first value of the array
field[0]['0'] = {}; // second object, with a string of 0 as the key
field[0]['0'].value = '120'; // "value" as the key, with the string of 120 as the value
Which is not equiv to:
var field = []; // arrayfield[0] = {}; // object as the first value of the array
field[0].value = '120'; // "value" as the key, with the value of '120' as a string
You have an extra object. I assume you are working in the '0' incorrectly. The 0 you are seeing in references represents the first value of the array.
myArray = ['etc'];myArray[0] = 'etc';
I'm not 100% clear about what
I'm not 100% clear about what you are trying to do. But if the location module has it's own keys in the node object, then you probably do need a new service module. The instructions provided assume you're simply working with CCK text fields.
As far as writing a service module I've done it a number of times, but the only reference I had were the existing modules. If there are resources you can likely find them in the Services Handbook.
http://drupal.org/handbook/modules/services
A quick check provides a couple examples, but I haven't looked closely at them.
http://drupal.org/node/113699
In terms of help with
In terms of help with debugging... The most frustrating problem is the lack of meaningful error messages. As in, there are none. Thus it's really trial and error, and stepping carefully through the module code to understand what is happening.
/// Key Advice
Most of the problems I experienced were in understanding the node object and how data is structured.
For example, I've noticed that CCK fields return differently via Node.Get than Views.Get. I didn't bother to investigate why. But it seems clear that it's just how Views works. Probably in order to not conflict with the native CCK output.
Regardless of why, things like this are tricky at times if you're not paying attention. Easy to do after banging your head on the desk a while at 3 in the morning ;)
The only way to deal is to setup solid test nodes Drupal-Side, call them down via Services and inspect how they return. (With a magnifying lens.)
/// Tools to help with JS and Services
A: Plenty of caffeine.
B: Firefox's Firebug extension.
This is invaluable. Perhaps the most important tool you have for whipping through test cases right in the browser. Once you have it sorted, it's a simple copy/paste into your project and away you go.
C: JavaScript Framework
I'd recommend using frameworks like MooTools and jQuery as they have an extra layer of error trapping that might help you navigate through some of the greyer areas. Not to mention they make everything faster and simpler.
I personally feel MooTools is by far the superior product. It's very elegant. And the project authors are some of the best in the industry, imho. Having said that jQuery is more used and is officially supported by Drupal.
If you work alongside the native Drupal output jQuery might be better otherwise you have conflicts that you need to work through. (Both frameworks have native settings / methodology that allow you to mix-in with their competitor's product -- but this is not enabled by default.) My project is completely divorced from Drupal's native output so this is a non-issue. If this is the same for you I'd recommend MooTools most definitely.
I am not sure if I help at
I am not sure if I help at all cause no time to read in depth. Google brought me here and after that I found that my node_save was not working cause the node vid (revision id) was 0 for some reason...
I will either try to see why this is so with my already existing node or I will try to feed in a vid=highest vid + 1 ...
HTH.